Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to hook up Semantic Tokens from a server ? #796

Closed
AntoineBalaine opened this issue Dec 29, 2022 · 7 comments
Closed

How to hook up Semantic Tokens from a server ? #796

AntoineBalaine opened this issue Dec 29, 2022 · 7 comments

Comments

@AntoineBalaine
Copy link

Hello,

I’m trying to hook-up the semantic tokens sample into the lsp-sample’s server.

Following the semantic highlight guide doesn’t quite give me all the info I need. The guide points to vscode.languages.registerDocumentSemanticTokensProvider(selector, provider, legend);, but:

  • After passing {semanticTokensProvider: legend} in the server capabilities; in the server’s initialize, how is the DocumentSemanticTokensProvider supposed to be hooked-up to the connection?
  • is there also some setup to do on the client side ?
  • do the SemanticTokens get automatically handled at every update or must they be continuously updated in the documentsManager.onDidChangeContent ?

Referencing lsp-sample/server/src/server.ts:

import {
	DocumentSemanticTokensProvider,
	legend,
} from './semanticTokensProvider/tokensProvider';

const connection = createConnection(ProposedFeatures.all);
const SemanticProvider = new DocumentSemanticTokensProvider(); // where does this guy go?

const documentsManager: TextDocuments<TextDocument> = new TextDocuments(TextDocument);
//[...]
connection.onInitialize((params: InitializeParams) => {
	const capabilities = params.capabilities;

	const serverCapabilities: InitializeResult = {
		capabilities: {
                       //[...]
			semanticTokensProvider: {
				legend,
			},
		},
	};
	return serverCapabilities;
});
@AlecGhost
Copy link

Hello there,
unfortunatly I cannot really answer your first question.

However, I can confirm, that you do not need any additional setup on the client side. If the client starts the server and the server announces, that it has the capability to provide semantic tokens, you are fine.

Regarding your final question: VSCode as a client automatically requests semantic tokens, when it needs them. So you do not need to send them on your own. In fact, there is not even a method defined in the LSP, that lets you do this. The only thing, that you can do (if I read the protocol correctly) is to send a workspace/semanticTokens/refresh request, which recommends to the client to ask for semantic tokens again.

I hope this information can help you.

@AntoineBalaine
Copy link
Author

Got it, thank you very much!

@caryyu
Copy link

caryyu commented Jul 5, 2023

@AntoineBalaine Did you get it resolved? Any solutions for this, I got this from the Extension Development Host output:

[Error - 8:12:35 PM] Request textDocument/semanticTokens/full failed.
  Message: Unhandled method textDocument/semanticTokens/full
  Code: -32601 

Here's my config very similar to yours:

  const result: InitializeResult = {
    capabilities: {
      semanticTokensProvider: {
        legend: {
          tokenTypes: [
            'comment', 'string', 'keyword', 'number', 'regexp', 'operator', 'namespace',
            'type', 'struct', 'class', 'interface', 'enum', 'typeParameter', 'function',
            'method', 'decorator', 'macro', 'variable', 'parameter', 'property', 'label'
          ],
          tokenModifiers: [
            'declaration', 'documentation', 'readonly', 'static', 'abstract', 'deprecated',
            'modification', 'async'
          ],
        },
        full: true,
      },
....

@mtiller-jh
Copy link

I found this useful:

microsoft/vscode-discussions#819

@wakaztahir
Copy link

wakaztahir commented Feb 13, 2024

@AlecGhost Hi, you said client doesn't need additional setup, however VSCode is just not sending the request to server, so client requires additional setup through

vscode.languages.registerDocumentSemanticTokensProvider(selector, provider, legend);

As stated in :
microsoft/vscode#204906 (comment)

Even though, this should not be the case, The client should send the request automatically or at least when instructed by the programmer without having to reimplement it because the LSP Client API is not understandable for someone who didn't work on it.

@AlecGhost
Copy link

@wakaztahir I have no knowledge of the VSCode internals, I can only report the behaviour I experience. My extension works without additional client-side setup for the semantic tokens, see https://github.com/AlecGhost/LSP4SPL/blob/master/editors/code/src/extension.ts.

Besides, registerDocumentSemanticTokensProvider sounds very much like a server method, not like a client method.
Maybe your server does not announce a semanticTokenProvider in its capabilities during the initialization phase?

@wakaztahir
Copy link

wakaztahir commented Feb 26, 2024

Hi @AlecGhost , I have fixed everything, I've mentioned everything here

microsoft/vscode#204906

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants
@caryyu @wakaztahir @AntoineBalaine @AlecGhost @mtiller-jh and others