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

Have SemanticTokensFull return nil when no symbols are available for the file #3531

Merged
merged 5 commits into from
Dec 12, 2024

Conversation

doriable
Copy link
Member

@doriable doriable commented Dec 11, 2024

When a file does not have parsed symbols, we are currently
sending a nil slice for the data in semantic tokens.

The specification for textDocument/semanticTokens/full outlines that
the expected response is SemanticTokens | null, where SemanticTokens
is defined as:

export interface [SemanticTokens](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#semanticTokens) {
	/**
	 * An optional result id. If provided and clients support delta updating
	 * the client will include the result id in the next semantic token request.
	 * A server can then instead of computing all semantic tokens again simply
	 * send a delta.
	 */
	resultId?: string;

	/**
	 * The actual tokens.
	 */
	data: [uinteger](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#uinteger)[];
}

https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_semanticTokens

data is not an optional field for SemanticTokens, so setting it to nil
causes failures on clients:

Screenshot 2024-12-11 at 18 23 04

From the LSP logs:

DEBUG\tresponding\t{"method": "textDocument/semanticTokens/full", "params": {"data":null}}\n'

We should instead be returning nil for SemanticTokensFull in this case, which
is expected by clients.

LSP logs after the change:

DEBUG\tresponding\t{"method": "textDocument/semanticTokens/full", "params": null}\n

@doriable doriable requested a review from mcy December 11, 2024 23:33
Copy link
Contributor

github-actions bot commented Dec 11, 2024

The latest Buf updates on your PR. Results from workflow Buf CI / buf (pull_request).

BuildFormatLintBreakingUpdated (UTC)
✅ passed✅ passed✅ passed✅ passedDec 12, 2024, 9:52 PM

prevLine, prevCol uint32
)
encoded := []uint32{}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you point to how this makes a difference? Downstream code should generally not care whether this is 0 or nil. If there's a specific reason, this should be documented here with a code comment so that we understand this choice in the future.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the PR, title, and description: I did some digging into the specification, and textDocument/semanticTokens/full expects SemanticTokens | null as the response, but data on SemanticTokens is not optional. So in this case, if we are not sending any data, we should instead be returning nil for semantic tokens. I added this explanation as in-line comments in the code change as well.

@doriable doriable changed the title Instantiate slice for semantic tokens data Have SemanticTokensFull return nil when no symbols are available for the file Dec 12, 2024
@doriable doriable merged commit 1539cac into main Dec 12, 2024
10 checks passed
@doriable doriable deleted the fix-semantic-tokens-nil branch December 12, 2024 22:11
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

Successfully merging this pull request may close these issues.

2 participants