vscode-languageserver-node: Is there some document about how language server receive semantic tokens request? #819
-
Hi there. I'm developing a language server. It provides semantic highlight. By I'm not sure how to receive something like My code like this: import { createConnection, ProposedFeatures } from "vscode-languageserver/node";
const connection = createConnection(ProposedFeatures.all);
connection.onInitialize(() => {
// ...
return {
semanticTokensProvider: {
legend: {
tokenTypes: ["function", "namespace"], // register legend like the same thing in VS Code example
tokenModifiers: [],
},
},
}
})
connection.languages.semanticTokens.on(() => {
// DOESN'T called
});
I have read this guide and I have successfully implemented semantic highlighting through the vs code API. But this is not the same in languageserver-server. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
Hi @imbant, The Here's how you can set up your language server to handle import { createConnection, ProposedFeatures } from "vscode-languageserver/node";
const connection = createConnection(ProposedFeatures.all);
connection.onInitialize(() => {
// ...
return {
semanticTokensProvider: {
legend: {
tokenTypes: ["function", "namespace"], // register legend like the same thing in VS Code example
tokenModifiers: [],
},
},
}
});
connection.onRequest("textDocument/semanticTokens/full", (params) => {
// Implement your logic to provide semantic tokens for the given document here.
// You should return the semantic tokens as a response.
const semanticTokens = computeSemanticTokens(params.textDocument.uri);
return semanticTokens;
});
// Start the language server
connection.listen(); In the code above:
Make sure that your handler function computes and returns the semantic tokens in the format expected by the client, following the Language Server Protocol (LSP) specification for semantic tokens. Regarding pushing semantic tokens to the client, in most cases, the client (e.g., Visual Studio Code) sends requests to the language server to obtain semantic tokens. However, you can't push semantic tokens to the client at will. The client initiates the requests when it needs the tokens, such as when opening a document or making changes to the text. The language server responds to these requests with the corresponding semantic tokens. You can't force updates; the client controls when it requests updates. Hope this helped. |
Beta Was this translation helpful? Give feedback.
-
hi @imbant , Could you share with me Client capabilities config so that it will send for semantictokens/full ? Thanks, |
Beta Was this translation helpful? Give feedback.
Hi @imbant,
The
connection.languages.semanticTokens.on
event is not typically used for providing semantic tokens to the client in response to a request liketextDocument/semanticTokens/full
. Instead, you should implement thetextDocument/semanticTokens
request handler in your language server.Here's how you can set up your language server to handle
textDocument/semanticTokens/full
requests: