To gain access to semantic tokens in VSCode extension development using context key related to them, you can follow these steps:
package.json
file to match the semantic token type that you want to access. For example, if you want to access all variables in a document, you can define a context key like this:"contributes": {
"menus": {
"editor/context": [
{
"when": "variableScope",
"command": "myExtension.doSomething"
}
]
},
"contextMenus": [
{
"when": "editorTextFocus && variableScope",
"command": "myExtension.doSomething"
}
]
}
vscode.languages.registerDocumentSemanticTokensProvider
function to register a semantic tokens provider for your desired language. This will allow you to access the tokens in your extension code.const provider = vscode.languages.registerDocumentSemanticTokensProvider(
{ language: 'myLanguage' },
{
provideDocumentSemanticTokens(document: vscode.TextDocument): vscode.ProviderResult<vscode.SemanticTokens> {
// return semantic tokens for a document
}
}
);
vscode.commands.executeCommand
function to execute a command when the defined context key is matched. This will allow you to access the semantic tokens when the command is executed.vscode.commands.executeCommand('setContext', 'variableScope', true);
vscode.languages.getSemanticTokens
function.vscode.commands.registerCommand('myExtension.doSomething', () => {
const tokens = vscode.languages.getSemanticTokens(vscode.window.activeTextEditor.document);
// do something with the tokens
});
By following these steps, you can gain access to semantic tokens in your VSCode extension development by using context keys related to them.
Asked: 2021-09-16 11:00:00 +0000
Seen: 10 times
Last updated: Jan 27 '23