[clang-tools-extra] r367684 - [clangd][vscode] clang-format the the extension code.

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 2 07:24:02 PDT 2019


Author: hokein
Date: Fri Aug  2 07:24:02 2019
New Revision: 367684

URL: http://llvm.org/viewvc/llvm-project?rev=367684&view=rev
Log:
[clangd][vscode] clang-format the the extension code.

Summary:
As we are going to grow the extension in the near future, it is time to
formalize the TS code format/style of our extension (although we'd lose the
history).

We use default options of clang-format:
- 80 max line length
- 2 space indent

Reviewers: ilya-biryukov, sammccall, jvikstrom

Subscribers: MaskRay, jkorous, arphaman, kadircet, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D65657

Modified:
    clang-tools-extra/trunk/clangd/clients/clangd-vscode/README.md
    clang-tools-extra/trunk/clangd/clients/clangd-vscode/package.json
    clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/extension.ts
    clang-tools-extra/trunk/clangd/clients/clangd-vscode/test/extension.test.ts
    clang-tools-extra/trunk/clangd/clients/clangd-vscode/test/index.ts

Modified: clang-tools-extra/trunk/clangd/clients/clangd-vscode/README.md
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/clients/clangd-vscode/README.md?rev=367684&r1=367683&r2=367684&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/clients/clangd-vscode/README.md (original)
+++ clang-tools-extra/trunk/clangd/clients/clangd-vscode/README.md Fri Aug  2 07:24:02 2019
@@ -50,6 +50,11 @@ point to the binary.
    # When VS Code starts, press <F5>.
 ```
 
+## Contributing
+
+Please follow the exsiting code style when contributing to the extension, we
+recommend to run `npm run format` before sending a patch.
+
 ## Publish to VS Code Marketplace
 
 New changes to `clangd-vscode` are not released until a new version is published

Modified: clang-tools-extra/trunk/clangd/clients/clangd-vscode/package.json
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/clients/clangd-vscode/package.json?rev=367684&r1=367683&r2=367684&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/clients/clangd-vscode/package.json (original)
+++ clang-tools-extra/trunk/clangd/clients/clangd-vscode/package.json Fri Aug  2 07:24:02 2019
@@ -32,6 +32,7 @@
         "vscode:prepublish": "tsc -p ./",
         "compile": "tsc -watch -p ./",
         "postinstall": "node ./node_modules/vscode/bin/install",
+        "format": "clang-format --style=LLVM -i --glob=\"{src,test}/*.ts\"",
         "test": "node ./node_modules/vscode/bin/test"
     },
     "dependencies": {
@@ -42,6 +43,7 @@
         "@types/mocha": "^2.2.32",
         "@types/node": "^6.0.40",
         "mocha": "^5.2.0",
+        "clang-format": "1.2.4",
         "typescript": "^2.0.3",
         "vscode": "^1.1.0"
     },

Modified: clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/extension.ts
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/extension.ts?rev=367684&r1=367683&r2=367684&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/extension.ts (original)
+++ clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/extension.ts Fri Aug  2 07:24:02 2019
@@ -7,8 +7,8 @@ import * as vscodelc from 'vscode-langua
  * @param defaultValue default value to return if option is not set
  */
 function getConfig<T>(option: string, defaultValue?: any): T {
-    const config = vscode.workspace.getConfiguration('clangd');
-    return config.get<T>(option, defaultValue);
+  const config = vscode.workspace.getConfiguration('clangd');
+  return config.get<T>(option, defaultValue);
 }
 
 namespace SwitchSourceHeaderRequest {
@@ -18,35 +18,33 @@ export const type =
 }
 
 class FileStatus {
-    private statuses = new Map<string, any>();
-    private readonly statusBarItem =
-        vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, 10);
-
-    onFileUpdated(fileStatus: any) {
-        const filePath = vscode.Uri.parse(fileStatus.uri);
-        this.statuses.set(filePath.fsPath, fileStatus);
-        this.updateStatus();
+  private statuses = new Map<string, any>();
+  private readonly statusBarItem =
+      vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, 10);
+
+  onFileUpdated(fileStatus: any) {
+    const filePath = vscode.Uri.parse(fileStatus.uri);
+    this.statuses.set(filePath.fsPath, fileStatus);
+    this.updateStatus();
+  }
+
+  updateStatus() {
+    const path = vscode.window.activeTextEditor.document.fileName;
+    const status = this.statuses.get(path);
+    if (!status) {
+      this.statusBarItem.hide();
+      return;
     }
+    this.statusBarItem.text = `clangd: ` + status.state;
+    this.statusBarItem.show();
+  }
+
+  clear() {
+    this.statuses.clear();
+    this.statusBarItem.hide();
+  }
 
-    updateStatus() {
-        const path = vscode.window.activeTextEditor.document.fileName;
-        const status = this.statuses.get(path);
-        if (!status) {
-          this.statusBarItem.hide();
-          return;
-        }
-        this.statusBarItem.text = `clangd: ` + status.state;
-        this.statusBarItem.show();
-    }
-
-    clear() {
-        this.statuses.clear();
-        this.statusBarItem.hide();
-    }
-
-    dispose() {
-        this.statusBarItem.dispose();
-    }
+  dispose() { this.statusBarItem.dispose(); }
 }
 
 /**
@@ -54,27 +52,27 @@ class FileStatus {
  *  your extension is activated the very first time the command is executed
  */
 export function activate(context: vscode.ExtensionContext) {
-    const syncFileEvents = getConfig<boolean>('syncFileEvents', true);
+  const syncFileEvents = getConfig<boolean>('syncFileEvents', true);
 
-    const clangd: vscodelc.Executable = {
-        command: getConfig<string>('path'),
-        args: getConfig<string[]>('arguments')
-    };
-    const traceFile = getConfig<string>('trace');
-    if (!!traceFile) {
-        const trace = { CLANGD_TRACE: traceFile };
-        clangd.options = { env: { ...process.env, ...trace } };
-    }
-    const serverOptions: vscodelc.ServerOptions = clangd;
-
-    // Note that CUDA ('.cu') files are special. When opening files of all other
-    // extensions, VSCode would load clangd automatically. This is achieved by
-    // having a corresponding 'onLanguage:...' activation event in package.json.
-    // However, VSCode does not have CUDA as a supported language yet, so we
-    // cannot add a corresponding activationEvent for CUDA files and clangd will
-    // *not* load itself automatically on '.cu' files.
-    const cudaFilePattern: string = '**/*.{' +['cu'].join()+ '}';
-    const clientOptions: vscodelc.LanguageClientOptions = {
+  const clangd: vscodelc.Executable = {
+    command : getConfig<string>('path'),
+    args : getConfig<string[]>('arguments')
+  };
+  const traceFile = getConfig<string>('trace');
+  if (!!traceFile) {
+    const trace = {CLANGD_TRACE : traceFile};
+    clangd.options = {env : {...process.env, ...trace}};
+  }
+  const serverOptions: vscodelc.ServerOptions = clangd;
+
+  // Note that CUDA ('.cu') files are special. When opening files of all other
+  // extensions, VSCode would load clangd automatically. This is achieved by
+  // having a corresponding 'onLanguage:...' activation event in package.json.
+  // However, VSCode does not have CUDA as a supported language yet, so we
+  // cannot add a corresponding activationEvent for CUDA files and clangd will
+  // *not* load itself automatically on '.cu' files.
+  const cudaFilePattern: string = '**/*.{' + [ 'cu' ].join() + '}';
+  const clientOptions: vscodelc.LanguageClientOptions = {
         // Register the server for c-family and cuda files.
         documentSelector: [
             { scheme: 'file', language: 'c' },
@@ -91,45 +89,44 @@ export function activate(context: vscode
         revealOutputChannelOn: vscodelc.RevealOutputChannelOn.Never
     };
 
-    const clangdClient = new vscodelc.LanguageClient('Clang Language Server',serverOptions, clientOptions);
-    console.log('Clang Language Server is now active!');
-    context.subscriptions.push(clangdClient.start());
-    context.subscriptions.push(vscode.commands.registerCommand(
-        'clangd-vscode.switchheadersource', async () => {
-            const uri =
-                vscode.Uri.file(vscode.window.activeTextEditor.document.fileName);
-            if (!uri) {
-                return;
-            }
-            const docIdentifier =
-                vscodelc.TextDocumentIdentifier.create(uri.toString());
-            const sourceUri = await clangdClient.sendRequest(
-                SwitchSourceHeaderRequest.type, docIdentifier);
-            if (!sourceUri) {
-                return;
-            }
-            const doc = await vscode.workspace.openTextDocument(
-                vscode.Uri.parse(sourceUri));
-            vscode.window.showTextDocument(doc);
-        }));
-    const status = new FileStatus();
-    context.subscriptions.push(vscode.window.onDidChangeActiveTextEditor(() => {
-        status.updateStatus();
-    }));
-    clangdClient.onDidChangeState(
-        ({ newState }) => {
-            if (newState == vscodelc.State.Running) {
-                // clangd starts or restarts after crash.
-                clangdClient.onNotification(
-                    'textDocument/clangd.fileStatus',
-                    (fileStatus) => { status.onFileUpdated(fileStatus); });
-            } else if (newState == vscodelc.State.Stopped) {
-                // Clear all cached statuses when clangd crashes.
-                status.clear();
-            }
-        })
-    // An empty place holder for the activate command, otherwise we'll get an
-    // "command is not registered" error.
-    context.subscriptions.push(vscode.commands.registerCommand(
-            'clangd-vscode.activate', async () => {}));
+  const clangdClient = new vscodelc.LanguageClient(
+      'Clang Language Server', serverOptions, clientOptions);
+  console.log('Clang Language Server is now active!');
+  context.subscriptions.push(clangdClient.start());
+  context.subscriptions.push(vscode.commands.registerCommand(
+      'clangd-vscode.switchheadersource', async () => {
+        const uri =
+            vscode.Uri.file(vscode.window.activeTextEditor.document.fileName);
+        if (!uri) {
+          return;
+        }
+        const docIdentifier =
+            vscodelc.TextDocumentIdentifier.create(uri.toString());
+        const sourceUri = await clangdClient.sendRequest(
+            SwitchSourceHeaderRequest.type, docIdentifier);
+        if (!sourceUri) {
+          return;
+        }
+        const doc = await vscode.workspace.openTextDocument(
+            vscode.Uri.parse(sourceUri));
+        vscode.window.showTextDocument(doc);
+      }));
+  const status = new FileStatus();
+  context.subscriptions.push(vscode.window.onDidChangeActiveTextEditor(
+      () => { status.updateStatus(); }));
+  clangdClient.onDidChangeState(({newState}) => {
+    if (newState == vscodelc.State.Running) {
+      // clangd starts or restarts after crash.
+      clangdClient.onNotification(
+          'textDocument/clangd.fileStatus',
+          (fileStatus) => { status.onFileUpdated(fileStatus); });
+    } else if (newState == vscodelc.State.Stopped) {
+      // Clear all cached statuses when clangd crashes.
+      status.clear();
+    }
+  })
+  // An empty place holder for the activate command, otherwise we'll get an
+  // "command is not registered" error.
+  context.subscriptions.push(vscode.commands.registerCommand(
+      'clangd-vscode.activate', async () => {}));
 }

Modified: clang-tools-extra/trunk/clangd/clients/clangd-vscode/test/extension.test.ts
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/clients/clangd-vscode/test/extension.test.ts?rev=367684&r1=367683&r2=367684&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/clients/clangd-vscode/test/extension.test.ts (original)
+++ clang-tools-extra/trunk/clangd/clients/clangd-vscode/test/extension.test.ts Fri Aug  2 07:24:02 2019
@@ -6,10 +6,9 @@ import * as myExtension from '../src/ext
 
 // TODO: add tests
 suite("Extension Tests", () => {
-
-    // Defines a Mocha unit test
-    test("Something 1", () => {
-        assert.equal(-1, [1, 2, 3].indexOf(5));
-        assert.equal(-1, [1, 2, 3].indexOf(0));
-    });
+  // Defines a Mocha unit test
+  test("Something 1", () => {
+    assert.equal(-1, [ 1, 2, 3 ].indexOf(5));
+    assert.equal(-1, [ 1, 2, 3 ].indexOf(0));
+  });
 });
\ No newline at end of file

Modified: clang-tools-extra/trunk/clangd/clients/clangd-vscode/test/index.ts
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/clients/clangd-vscode/test/index.ts?rev=367684&r1=367683&r2=367684&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/clients/clangd-vscode/test/index.ts (original)
+++ clang-tools-extra/trunk/clangd/clients/clangd-vscode/test/index.ts Fri Aug  2 07:24:02 2019
@@ -5,18 +5,21 @@
 // By default the test runner in use is Mocha based.
 //
 // You can provide your own test runner if you want to override it by exporting
-// a function run(testRoot: string, clb: (error:Error) => void) that the extension
-// host can call to run the tests. The test runner is expected to use console.log
-// to report the results back to the caller. When the tests are finished, return
-// a possible error to the callback or null if none.
+// a function run(testRoot: string, clb: (error:Error) => void) that the
+// extension host can call to run the tests. The test runner is expected to use
+// console.log to report the results back to the caller. When the tests are
+// finished, return a possible error to the callback or null if none.
 
 var testRunner = require('vscode/lib/testrunner');
 
 // You can directly control Mocha options by uncommenting the following lines
-// See https://github.com/mochajs/mocha/wiki/Using-mocha-programmatically#set-options for more info
+// See
+// https://github.com/mochajs/mocha/wiki/Using-mocha-programmatically#set-options
+// for more info
 testRunner.configure({
-    ui: 'tdd', 		// the TDD UI is being used in extension.test.ts (suite, test, etc.)
-    useColors: true // colored output from test results
+  ui : 'tdd', // the TDD UI is being used in extension.test.ts (suite, test,
+              // etc.)
+  useColors : true // colored output from test results
 });
 
 module.exports = testRunner;
\ No newline at end of file




More information about the cfe-commits mailing list