-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Server-side Semantic Tokens #3159
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,94 @@ | ||
//! Semantic Tokens helpers | ||
|
||
use lsp_types::{Range, SemanticToken, SemanticTokenModifier, SemanticTokenType}; | ||
|
||
const SUPPORTED_TYPES: &[SemanticTokenType] = &[ | ||
SemanticTokenType::COMMENT, | ||
SemanticTokenType::KEYWORD, | ||
SemanticTokenType::STRING, | ||
SemanticTokenType::NUMBER, | ||
SemanticTokenType::REGEXP, | ||
SemanticTokenType::OPERATOR, | ||
SemanticTokenType::NAMESPACE, | ||
SemanticTokenType::TYPE, | ||
SemanticTokenType::STRUCT, | ||
SemanticTokenType::CLASS, | ||
SemanticTokenType::INTERFACE, | ||
SemanticTokenType::ENUM, | ||
SemanticTokenType::TYPE_PARAMETER, | ||
SemanticTokenType::FUNCTION, | ||
SemanticTokenType::MEMBER, | ||
SemanticTokenType::PROPERTY, | ||
SemanticTokenType::MACRO, | ||
SemanticTokenType::VARIABLE, | ||
SemanticTokenType::PARAMETER, | ||
SemanticTokenType::LABEL, | ||
]; | ||
|
||
const SUPPORTED_MODIFIERS: &[SemanticTokenModifier] = &[ | ||
SemanticTokenModifier::DOCUMENTATION, | ||
SemanticTokenModifier::DECLARATION, | ||
SemanticTokenModifier::DEFINITION, | ||
SemanticTokenModifier::REFERENCE, | ||
SemanticTokenModifier::STATIC, | ||
SemanticTokenModifier::ABSTRACT, | ||
SemanticTokenModifier::DEPRECATED, | ||
SemanticTokenModifier::ASYNC, | ||
SemanticTokenModifier::VOLATILE, | ||
SemanticTokenModifier::READONLY, | ||
]; | ||
|
||
/// Token types that the server supports | ||
pub(crate) fn supported_token_types() -> &'static [SemanticTokenType] { | ||
SUPPORTED_TYPES | ||
} | ||
|
||
/// Token modifiers that the server supports | ||
pub(crate) fn supported_token_modifiers() -> &'static [SemanticTokenModifier] { | ||
SUPPORTED_MODIFIERS | ||
} | ||
|
||
/// Tokens are encoded relative to each other. | ||
/// | ||
/// This is a direct port of https://github.com/microsoft/vscode-languageserver-node/blob/f425af9de46a0187adb78ec8a46b9b2ce80c5412/server/src/sematicTokens.proposed.ts#L45 | ||
#[derive(Default)] | ||
pub(crate) struct SemanticTokensBuilder { | ||
prev_line: u32, | ||
prev_char: u32, | ||
data: Vec<SemanticToken>, | ||
} | ||
|
||
impl SemanticTokensBuilder { | ||
/// Push a new token onto the builder | ||
pub fn push(&mut self, range: Range, token_index: u32, modifier_bitset: u32) { | ||
let mut push_line = range.start.line as u32; | ||
let mut push_char = range.start.character as u32; | ||
|
||
if !self.data.is_empty() { | ||
push_line -= self.prev_line; | ||
if push_line == 0 { | ||
push_char -= self.prev_char; | ||
} | ||
} | ||
|
||
// A token cannot be multiline | ||
let token_len = range.end.character - range.start.character; | ||
|
||
let token = SemanticToken { | ||
delta_line: push_line, | ||
delta_start: push_char, | ||
length: token_len as u32, | ||
token_type: token_index, | ||
token_modifiers_bitset: modifier_bitset, | ||
}; | ||
|
||
self.data.push(token); | ||
|
||
self.prev_line = range.start.line as u32; | ||
self.prev_char = range.start.character as u32; | ||
} | ||
|
||
pub fn build(self) -> Vec<SemanticToken> { | ||
self.data | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could do a more direct conversion here, avoiding the
Vec
allocation, right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think
smallvec
could be more appropriate here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem is using
&'static str
type in the first place. It doesn't make sense to optimize impl details while the API itself is wrong. I think we should move to a model closer totag+modifier
of LSP.