diff --git a/clang-tools-extra/clangd/clients/clangd-vscode/README.md b/clang-tools-extra/clangd/clients/clangd-vscode/README.md index f950cb6308c6..9babfce5f95e 100644 --- a/clang-tools-extra/clangd/clients/clangd-vscode/README.md +++ b/clang-tools-extra/clangd/clients/clangd-vscode/README.md @@ -50,6 +50,11 @@ point to the binary. # When VS Code starts, press . ``` +## 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 diff --git a/clang-tools-extra/clangd/clients/clangd-vscode/package.json b/clang-tools-extra/clangd/clients/clangd-vscode/package.json index cf91eda6a266..5ef8e92ae5ee 100644 --- a/clang-tools-extra/clangd/clients/clangd-vscode/package.json +++ b/clang-tools-extra/clangd/clients/clangd-vscode/package.json @@ -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" }, diff --git a/clang-tools-extra/clangd/clients/clangd-vscode/src/extension.ts b/clang-tools-extra/clangd/clients/clangd-vscode/src/extension.ts index 4202a6113129..458bda89c536 100644 --- a/clang-tools-extra/clangd/clients/clangd-vscode/src/extension.ts +++ b/clang-tools-extra/clangd/clients/clangd-vscode/src/extension.ts @@ -7,8 +7,8 @@ import * as vscodelc from 'vscode-languageclient'; * @param defaultValue default value to return if option is not set */ function getConfig(option: string, defaultValue?: any): T { - const config = vscode.workspace.getConfiguration('clangd'); - return config.get(option, defaultValue); + const config = vscode.workspace.getConfiguration('clangd'); + return config.get(option, defaultValue); } namespace SwitchSourceHeaderRequest { @@ -18,35 +18,33 @@ export const type = } class FileStatus { - private statuses = new Map(); - private readonly statusBarItem = - vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, 10); + private statuses = new Map(); + 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(); - } + 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(); + 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(); - } + 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('syncFileEvents', true); + const syncFileEvents = getConfig('syncFileEvents', true); - const clangd: vscodelc.Executable = { - command: getConfig('path'), - args: getConfig('arguments') - }; - const traceFile = getConfig('trace'); - if (!!traceFile) { - const trace = { CLANGD_TRACE: traceFile }; - clangd.options = { env: { ...process.env, ...trace } }; - } - const serverOptions: vscodelc.ServerOptions = clangd; + const clangd: vscodelc.Executable = { + command : getConfig('path'), + args : getConfig('arguments') + }; + const traceFile = getConfig('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 = { + // 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.ExtensionContext) { 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 () => {})); } diff --git a/clang-tools-extra/clangd/clients/clangd-vscode/test/extension.test.ts b/clang-tools-extra/clangd/clients/clangd-vscode/test/extension.test.ts index de4e068d8ee5..19691d69e49e 100644 --- a/clang-tools-extra/clangd/clients/clangd-vscode/test/extension.test.ts +++ b/clang-tools-extra/clangd/clients/clangd-vscode/test/extension.test.ts @@ -6,10 +6,9 @@ import * as myExtension from '../src/extension'; // 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 diff --git a/clang-tools-extra/clangd/clients/clangd-vscode/test/index.ts b/clang-tools-extra/clangd/clients/clangd-vscode/test/index.ts index 50bae4567f87..1ffba03d9532 100644 --- a/clang-tools-extra/clangd/clients/clangd-vscode/test/index.ts +++ b/clang-tools-extra/clangd/clients/clangd-vscode/test/index.ts @@ -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