-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Export client from activation as API for use in other extensions (#575)
- Loading branch information
Showing
6 changed files
with
116 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# VS Code clangd Extension API | ||
|
||
The VS Code clangd extension exposes an API that other extensions can consume: | ||
|
||
```typescript | ||
import * as vscode from 'vscode'; | ||
import type { ClangdExtension, ASTParams, ASTNode } from '@clangd/vscode-clangd'; | ||
|
||
const CLANGD_EXTENSION = 'llvm-vs-code-extensions.vscode-clangd'; | ||
const CLANGD_API_VERSION = 1; | ||
|
||
const ASTRequestMethod = 'textDocument/ast'; | ||
|
||
const provideHover = async (document: vscode.TextDocument, position: vscode.Position, _token: vscode.CancellationToken): Promise<vscode.Hover | undefined> => { | ||
|
||
const clangdExtension = vscode.extensions.getExtension<ClangdExtension>(CLANGD_EXTENSION); | ||
|
||
if (clangdExtension) { | ||
const api = (await clangdExtension.activate()).getApi(CLANGD_API_VERSION); | ||
|
||
const textDocument = api.languageClient.code2ProtocolConverter.asTextDocumentIdentifier(document); | ||
const range = api.languageClient.code2ProtocolConverter.asRange(new vscode.Range(position, position)); | ||
const params: ASTParams = { textDocument, range }; | ||
|
||
const ast: ASTNode | undefined = await api.languageClient.sendRequest(ASTRequestMethod, params); | ||
|
||
if (!ast) { | ||
return undefined; | ||
} | ||
|
||
return { | ||
contents: [ast.kind] | ||
}; | ||
} | ||
}; | ||
|
||
vscode.languages.registerHoverProvider(['c', 'cpp'], { provideHover }); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "@clangd/vscode-clangd", | ||
"version": "0.0.0", | ||
"description": "API for the llvm-vs-code-extensions.vscode-clangd VS Code extension", | ||
"types": "vscode-clangd.d.ts", | ||
"publishConfig": { | ||
"registry": "https://npm.pkg.github.com" | ||
}, | ||
"repository": "https://github.com/clangd/vscode-clangd", | ||
"license": "MIT", | ||
"homepage": "https://github.com/clangd/vscode-clangd/blob/master/api/README.md", | ||
"dependencies": { | ||
"vscode-languageclient": "8.0.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import {BaseLanguageClient} from 'vscode-languageclient'; | ||
import * as vscodelc from 'vscode-languageclient/node'; | ||
|
||
export interface ClangdApiV1 { | ||
// vscode-clangd's language client which can be used to send requests to the | ||
// clangd language server | ||
// Standard requests: | ||
// https://microsoft.github.io/language-server-protocol/specifications/specification-current | ||
// clangd custom requests: | ||
// https://clangd.llvm.org/extensions | ||
languageClient: BaseLanguageClient | ||
} | ||
|
||
export interface ClangdExtension { | ||
getApi(version: 1): ClangdApiV1; | ||
} | ||
|
||
// clangd custom request types | ||
// (type declarations for other requests may be added later) | ||
|
||
// textDocument/ast wire format | ||
// Send: position | ||
export interface ASTParams { | ||
textDocument: vscodelc.TextDocumentIdentifier; | ||
range: vscodelc.Range; | ||
} | ||
|
||
// Receive: tree of ASTNode | ||
export interface ASTNode { | ||
role: string; // e.g. expression | ||
kind: string; // e.g. BinaryOperator | ||
detail?: string; // e.g. || | ||
arcana?: string; // e.g. BinaryOperator <0x12345> <col:12, col:1> 'bool' '||' | ||
children?: Array<ASTNode>; | ||
range?: vscodelc.Range; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import {BaseLanguageClient} from 'vscode-languageclient'; | ||
|
||
import {ClangdApiV1, ClangdExtension} from '../api/vscode-clangd'; | ||
|
||
export class ClangdExtensionImpl implements ClangdExtension { | ||
constructor(private readonly client: BaseLanguageClient) {} | ||
|
||
public getApi(version: 1): ClangdApiV1; | ||
public getApi(version: number): unknown { | ||
if (version === 1) { | ||
return {languageClient: this.client}; | ||
} | ||
|
||
throw new Error(`No API version ${version} found`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters