Skip to content

Commit 4b42acf

Browse files
committed
Add basic support for augmentsSyntaxTokens
1 parent c26a43d commit 4b42acf

File tree

4 files changed

+70
-22
lines changed

4 files changed

+70
-22
lines changed

crates/ide/src/syntax_highlighting.rs

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -469,25 +469,8 @@ fn traverse(
469469
}
470470

471471
// apply config filtering
472-
match &mut highlight.tag {
473-
HlTag::StringLiteral if !config.strings => continue,
474-
// If punctuation is disabled, make the macro bang part of the macro call again.
475-
tag @ HlTag::Punctuation(HlPunct::MacroBang) => {
476-
if !config.macro_bang {
477-
*tag = HlTag::Symbol(SymbolKind::Macro);
478-
} else if !config.specialize_punctuation {
479-
*tag = HlTag::Punctuation(HlPunct::Other);
480-
}
481-
}
482-
HlTag::Punctuation(_) if !config.punctuation => continue,
483-
tag @ HlTag::Punctuation(_) if !config.specialize_punctuation => {
484-
*tag = HlTag::Punctuation(HlPunct::Other);
485-
}
486-
HlTag::Operator(_) if !config.operator && highlight.mods.is_empty() => continue,
487-
tag @ HlTag::Operator(_) if !config.specialize_operator => {
488-
*tag = HlTag::Operator(HlOperator::Other);
489-
}
490-
_ => (),
472+
if !filter_by_config(&mut highlight, config) {
473+
continue;
491474
}
492475

493476
if inside_attribute {
@@ -498,3 +481,27 @@ fn traverse(
498481
}
499482
}
500483
}
484+
485+
fn filter_by_config(highlight: &mut Highlight, config: HighlightConfig) -> bool {
486+
match &mut highlight.tag {
487+
HlTag::StringLiteral if !config.strings => return false,
488+
// If punctuation is disabled, make the macro bang part of the macro call again.
489+
tag @ HlTag::Punctuation(HlPunct::MacroBang) => {
490+
if !config.macro_bang {
491+
*tag = HlTag::Symbol(SymbolKind::Macro);
492+
} else if !config.specialize_punctuation {
493+
*tag = HlTag::Punctuation(HlPunct::Other);
494+
}
495+
}
496+
HlTag::Punctuation(_) if !config.punctuation => return false,
497+
tag @ HlTag::Punctuation(_) if !config.specialize_punctuation => {
498+
*tag = HlTag::Punctuation(HlPunct::Other);
499+
}
500+
HlTag::Operator(_) if !config.operator && highlight.mods.is_empty() => return false,
501+
tag @ HlTag::Operator(_) if !config.specialize_operator => {
502+
*tag = HlTag::Operator(HlOperator::Other);
503+
}
504+
_ => (),
505+
}
506+
true
507+
}

crates/rust-analyzer/src/config.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,6 +1028,11 @@ impl Config {
10281028
.is_some()
10291029
}
10301030

1031+
pub fn semantics_tokens_augments_syntax_tokens(&self) -> bool {
1032+
try_!(self.caps.text_document.as_ref()?.semantic_tokens.as_ref()?.augments_syntax_tokens?)
1033+
.unwrap_or(false)
1034+
}
1035+
10311036
pub fn position_encoding(&self) -> PositionEncoding {
10321037
negotiated_encoding(&self.caps)
10331038
}

crates/rust-analyzer/src/handlers/request.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1472,7 +1472,12 @@ pub(crate) fn handle_semantic_tokens_full(
14721472
snap.workspaces.is_empty() || !snap.proc_macros_loaded;
14731473

14741474
let highlights = snap.analysis.highlight(highlight_config, file_id)?;
1475-
let semantic_tokens = to_proto::semantic_tokens(&text, &line_index, highlights);
1475+
let semantic_tokens = to_proto::semantic_tokens(
1476+
&text,
1477+
&line_index,
1478+
highlights,
1479+
snap.config.semantics_tokens_augments_syntax_tokens(),
1480+
);
14761481

14771482
// Unconditionally cache the tokens
14781483
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(
14961501
snap.workspaces.is_empty() || !snap.proc_macros_loaded;
14971502

14981503
let highlights = snap.analysis.highlight(highlight_config, file_id)?;
1499-
let semantic_tokens = to_proto::semantic_tokens(&text, &line_index, highlights);
1504+
let semantic_tokens = to_proto::semantic_tokens(
1505+
&text,
1506+
&line_index,
1507+
highlights,
1508+
snap.config.semantics_tokens_augments_syntax_tokens(),
1509+
);
15001510

15011511
let mut cache = snap.semantic_tokens_cache.lock();
15021512
let cached_tokens = cache.entry(params.text_document.uri).or_default();
@@ -1530,7 +1540,12 @@ pub(crate) fn handle_semantic_tokens_range(
15301540
snap.workspaces.is_empty() || !snap.proc_macros_loaded;
15311541

15321542
let highlights = snap.analysis.highlight_range(highlight_config, frange)?;
1533-
let semantic_tokens = to_proto::semantic_tokens(&text, &line_index, highlights);
1543+
let semantic_tokens = to_proto::semantic_tokens(
1544+
&text,
1545+
&line_index,
1546+
highlights,
1547+
snap.config.semantics_tokens_augments_syntax_tokens(),
1548+
);
15341549
Ok(Some(semantic_tokens.into()))
15351550
}
15361551

crates/rust-analyzer/src/to_proto.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,7 @@ pub(crate) fn semantic_tokens(
586586
text: &str,
587587
line_index: &LineIndex,
588588
highlights: Vec<HlRange>,
589+
semantics_tokens_augments_syntax_tokens: bool,
589590
) -> lsp_types::SemanticTokens {
590591
let id = TOKEN_RESULT_COUNTER.fetch_add(1, Ordering::SeqCst).to_string();
591592
let mut builder = semantic_tokens::SemanticTokensBuilder::new(id);
@@ -595,6 +596,26 @@ pub(crate) fn semantic_tokens(
595596
continue;
596597
}
597598

599+
if semantics_tokens_augments_syntax_tokens {
600+
match highlight_range.highlight.tag {
601+
HlTag::BoolLiteral
602+
| HlTag::ByteLiteral
603+
| HlTag::CharLiteral
604+
| HlTag::Comment
605+
| HlTag::Keyword
606+
| HlTag::NumericLiteral
607+
| HlTag::Operator(_)
608+
| HlTag::Punctuation(_)
609+
| HlTag::StringLiteral
610+
| HlTag::None
611+
if highlight_range.highlight.mods.is_empty() =>
612+
{
613+
continue
614+
}
615+
_ => (),
616+
}
617+
}
618+
598619
let (ty, mods) = semantic_token_type_and_modifiers(highlight_range.highlight);
599620
let token_index = semantic_tokens::type_index(ty);
600621
let modifier_bitset = mods.0;

0 commit comments

Comments
 (0)