Add basic support for augmentsSyntaxTokens

This commit is contained in:
Lukas Wirth 2023-05-10 20:48:51 +02:00
parent c26a43d6bd
commit 4b42acf617
4 changed files with 70 additions and 22 deletions

View file

@ -469,25 +469,8 @@ fn traverse(
} }
// apply config filtering // apply config filtering
match &mut highlight.tag { if !filter_by_config(&mut highlight, config) {
HlTag::StringLiteral if !config.strings => continue, continue;
// If punctuation is disabled, make the macro bang part of the macro call again.
tag @ HlTag::Punctuation(HlPunct::MacroBang) => {
if !config.macro_bang {
*tag = HlTag::Symbol(SymbolKind::Macro);
} else if !config.specialize_punctuation {
*tag = HlTag::Punctuation(HlPunct::Other);
}
}
HlTag::Punctuation(_) if !config.punctuation => continue,
tag @ HlTag::Punctuation(_) if !config.specialize_punctuation => {
*tag = HlTag::Punctuation(HlPunct::Other);
}
HlTag::Operator(_) if !config.operator && highlight.mods.is_empty() => continue,
tag @ HlTag::Operator(_) if !config.specialize_operator => {
*tag = HlTag::Operator(HlOperator::Other);
}
_ => (),
} }
if inside_attribute { if inside_attribute {
@ -498,3 +481,27 @@ fn traverse(
} }
} }
} }
fn filter_by_config(highlight: &mut Highlight, config: HighlightConfig) -> bool {
match &mut highlight.tag {
HlTag::StringLiteral if !config.strings => return false,
// If punctuation is disabled, make the macro bang part of the macro call again.
tag @ HlTag::Punctuation(HlPunct::MacroBang) => {
if !config.macro_bang {
*tag = HlTag::Symbol(SymbolKind::Macro);
} else if !config.specialize_punctuation {
*tag = HlTag::Punctuation(HlPunct::Other);
}
}
HlTag::Punctuation(_) if !config.punctuation => return false,
tag @ HlTag::Punctuation(_) if !config.specialize_punctuation => {
*tag = HlTag::Punctuation(HlPunct::Other);
}
HlTag::Operator(_) if !config.operator && highlight.mods.is_empty() => return false,
tag @ HlTag::Operator(_) if !config.specialize_operator => {
*tag = HlTag::Operator(HlOperator::Other);
}
_ => (),
}
true
}

View file

@ -1028,6 +1028,11 @@ impl Config {
.is_some() .is_some()
} }
pub fn semantics_tokens_augments_syntax_tokens(&self) -> bool {
try_!(self.caps.text_document.as_ref()?.semantic_tokens.as_ref()?.augments_syntax_tokens?)
.unwrap_or(false)
}
pub fn position_encoding(&self) -> PositionEncoding { pub fn position_encoding(&self) -> PositionEncoding {
negotiated_encoding(&self.caps) negotiated_encoding(&self.caps)
} }

View file

@ -1472,7 +1472,12 @@ pub(crate) fn handle_semantic_tokens_full(
snap.workspaces.is_empty() || !snap.proc_macros_loaded; snap.workspaces.is_empty() || !snap.proc_macros_loaded;
let highlights = snap.analysis.highlight(highlight_config, file_id)?; let highlights = snap.analysis.highlight(highlight_config, file_id)?;
let semantic_tokens = to_proto::semantic_tokens(&text, &line_index, highlights); let semantic_tokens = to_proto::semantic_tokens(
&text,
&line_index,
highlights,
snap.config.semantics_tokens_augments_syntax_tokens(),
);
// Unconditionally cache the tokens // Unconditionally cache the tokens
snap.semantic_tokens_cache.lock().insert(params.text_document.uri, semantic_tokens.clone()); snap.semantic_tokens_cache.lock().insert(params.text_document.uri, semantic_tokens.clone());
@ -1496,7 +1501,12 @@ pub(crate) fn handle_semantic_tokens_full_delta(
snap.workspaces.is_empty() || !snap.proc_macros_loaded; snap.workspaces.is_empty() || !snap.proc_macros_loaded;
let highlights = snap.analysis.highlight(highlight_config, file_id)?; let highlights = snap.analysis.highlight(highlight_config, file_id)?;
let semantic_tokens = to_proto::semantic_tokens(&text, &line_index, highlights); let semantic_tokens = to_proto::semantic_tokens(
&text,
&line_index,
highlights,
snap.config.semantics_tokens_augments_syntax_tokens(),
);
let mut cache = snap.semantic_tokens_cache.lock(); let mut cache = snap.semantic_tokens_cache.lock();
let cached_tokens = cache.entry(params.text_document.uri).or_default(); let cached_tokens = cache.entry(params.text_document.uri).or_default();
@ -1530,7 +1540,12 @@ pub(crate) fn handle_semantic_tokens_range(
snap.workspaces.is_empty() || !snap.proc_macros_loaded; snap.workspaces.is_empty() || !snap.proc_macros_loaded;
let highlights = snap.analysis.highlight_range(highlight_config, frange)?; let highlights = snap.analysis.highlight_range(highlight_config, frange)?;
let semantic_tokens = to_proto::semantic_tokens(&text, &line_index, highlights); let semantic_tokens = to_proto::semantic_tokens(
&text,
&line_index,
highlights,
snap.config.semantics_tokens_augments_syntax_tokens(),
);
Ok(Some(semantic_tokens.into())) Ok(Some(semantic_tokens.into()))
} }

View file

@ -586,6 +586,7 @@ pub(crate) fn semantic_tokens(
text: &str, text: &str,
line_index: &LineIndex, line_index: &LineIndex,
highlights: Vec<HlRange>, highlights: Vec<HlRange>,
semantics_tokens_augments_syntax_tokens: bool,
) -> lsp_types::SemanticTokens { ) -> lsp_types::SemanticTokens {
let id = TOKEN_RESULT_COUNTER.fetch_add(1, Ordering::SeqCst).to_string(); let id = TOKEN_RESULT_COUNTER.fetch_add(1, Ordering::SeqCst).to_string();
let mut builder = semantic_tokens::SemanticTokensBuilder::new(id); let mut builder = semantic_tokens::SemanticTokensBuilder::new(id);
@ -595,6 +596,26 @@ pub(crate) fn semantic_tokens(
continue; continue;
} }
if semantics_tokens_augments_syntax_tokens {
match highlight_range.highlight.tag {
HlTag::BoolLiteral
| HlTag::ByteLiteral
| HlTag::CharLiteral
| HlTag::Comment
| HlTag::Keyword
| HlTag::NumericLiteral
| HlTag::Operator(_)
| HlTag::Punctuation(_)
| HlTag::StringLiteral
| HlTag::None
if highlight_range.highlight.mods.is_empty() =>
{
continue
}
_ => (),
}
}
let (ty, mods) = semantic_token_type_and_modifiers(highlight_range.highlight); let (ty, mods) = semantic_token_type_and_modifiers(highlight_range.highlight);
let token_index = semantic_tokens::type_index(ty); let token_index = semantic_tokens::type_index(ty);
let modifier_bitset = mods.0; let modifier_bitset = mods.0;