diff options
Diffstat (limited to 'clangd/clients/clangd-vscode/src/extension.ts')
-rw-r--r-- | clangd/clients/clangd-vscode/src/extension.ts | 80 |
1 files changed, 53 insertions, 27 deletions
diff --git a/clangd/clients/clangd-vscode/src/extension.ts b/clangd/clients/clangd-vscode/src/extension.ts index cad6a3a1..2cb97d97 100644 --- a/clangd/clients/clangd-vscode/src/extension.ts +++ b/clangd/clients/clangd-vscode/src/extension.ts @@ -40,6 +40,11 @@ class FileStatus { this.statusBarItem.show(); } + clear() { + this.statuses.clear(); + this.statusBarItem.hide(); + } + dispose() { this.statusBarItem.dispose(); } @@ -63,8 +68,18 @@ export function activate(context: vscode.ExtensionContext) { } 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. When any of the files + // with other extensions are open, clangd will load itself and will also + // work on '.cu' files. const filePattern: string = '**/*.{' + - ['cpp', 'c', 'cc', 'cxx', 'c++', 'm', 'mm', 'h', 'hh', 'hpp', 'hxx', 'inc'].join() + '}'; + ['cpp', 'c', 'cc', 'cu', 'cxx', 'c++', 'm', 'mm', + 'h', 'hh', 'hpp', 'hxx', 'inc'].join() + + '}'; const clientOptions: vscodelc.LanguageClientOptions = { // Register the server for C/C++ files documentSelector: [{ scheme: 'file', pattern: filePattern }], @@ -87,34 +102,45 @@ 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 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.onReady().then(() => { - clangdClient.onNotification( - 'textDocument/clangd.fileStatus', - (fileStatus) => { status.onFileUpdated(fileStatus); }); - }) + 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 () => {})); } |