Skip to content

Commit 848aa56

Browse files
Merge #4397
4397: Textmate cooperation r=matklad a=georgewfraser This PR tweaks the fallback TextMate scopes to make them more consistent with the existing grammar and other languages, and edits the builtin TextMate grammar to align with semantic coloring. Before is on the left, after is on the right: <img width="855" alt="Screen Shot 2020-05-10 at 1 45 51 PM" src="https://user-images.githubusercontent.com/1369240/81512320-a8be7e80-92d4-11ea-8940-2c03f6769015.png"> **Use keyword.other for regular keywords instead of keyword**. This is a really peculiar quirk of TextMate conventions, but virtually *all* TextMate grammars use `keyword.other` (colored blue in VSCode Dark+) for regular keywords and `keyword.control` (colored purple in VSCode Dark+) for control keywords. The TextMate scope `keyword` is colored like control keywords, not regular keywords. It may seem strange that the `keyword` scope is not the right fallback for the `keyword` semantic token, but TextMate has a long and weird history. Note how keywords change from purple back to blue (what they were before semantic coloring was added): **(1) Use punctuation.section.embedded for format specifiers**. This aligns with how Typescript colors formatting directives: <img width="238" alt="Screen Shot 2020-05-09 at 10 54 01 AM" src="https://user-images.githubusercontent.com/1369240/81481258-93b5f280-91e3-11ea-99c2-c6d258c5bcad.png"> **(2) Consistently use `entity.name.type.*` scopes for type names**. Avoid using `entity.name.*` which gets colored like a keyword. **(3) Use Property instead of Member for fields**. Property and Member are very similar, but if you look at the TextMate fallback scopes, it's clear that Member is intended for function-like-things (methods?) and Property is intended for variable-like-things. **(4) Color `for` as a regular keyword when it's part of `impl Trait for Struct`**. **(5) Use `variable.other.constant` for constants instead of `entity.name.constant`**. In the latest VSCode insiders, variable.other.constant has a subtly different color that differentiates constants from ordinary variables. It looks close to the green of types but it's not the same---it's a new color recently added to take advantage of semantic coloring. I also made some minor changes that make the TextMate scopes better match the semantic scopes. The effect of this for the user is you observe less of a change when semantic coloring "activates". You can see the changes I made relative to the built-in TextMate grammar here: https://github.com/rust-analyzer/rust-analyzer/pull/4397/files/a91d15c80c337dd1afb0eddd5eb048010d098ac7..97428b6d52d25f810dbd7d7a8d787740c58bfbd2#diff-6966c729b862f79f79bf7258eb3e0885 Co-authored-by: George Fraser <george@fivetran.com>
2 parents de1fe23 + 97428b6 commit 848aa56

File tree

7 files changed

+734
-11
lines changed

7 files changed

+734
-11
lines changed

crates/ra_ide/src/snapshots/highlighting.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@
3333
<span class="keyword">pub</span> <span class="field declaration">y</span>: <span class="builtin_type">i32</span>,
3434
}
3535

36+
<span class="keyword">trait</span> <span class="trait declaration">Bar</span> {
37+
<span class="keyword">fn</span> <span class="function declaration">bar</span>(&<span class="keyword">self</span>) -&gt; <span class="builtin_type">i32</span>;
38+
}
39+
40+
<span class="keyword">impl</span> <span class="trait">Bar</span> <span class="keyword">for</span> <span class="struct">Foo</span> {
41+
<span class="keyword">fn</span> <span class="function declaration">bar</span>(&<span class="keyword">self</span>) -&gt; <span class="builtin_type">i32</span> {
42+
<span class="keyword">self</span>.<span class="field">x</span>
43+
}
44+
}
45+
3646
<span class="keyword">static</span> <span class="keyword">mut</span> <span class="static declaration mutable">STATIC_MUT</span>: <span class="builtin_type">i32</span> = <span class="numeric_literal">0</span>;
3747

3848
<span class="keyword">fn</span> <span class="function declaration">foo</span>&lt;<span class="lifetime declaration">'a</span>, <span class="type_param declaration">T</span>&gt;() -&gt; <span class="type_param">T</span> {
@@ -63,6 +73,10 @@
6373
<span class="static mutable">STATIC_MUT</span> = <span class="numeric_literal">1</span>;
6474
}
6575

76+
<span class="keyword control">for</span> <span class="variable declaration">e</span> <span class="keyword control">in</span> <span class="variable mutable">vec</span> {
77+
<span class="comment">// Do nothing</span>
78+
}
79+
6680
<span class="keyword">let</span> <span class="keyword">mut</span> <span class="variable declaration mutable">x</span> = <span class="numeric_literal">42</span>;
6781
<span class="keyword">let</span> <span class="variable declaration mutable">y</span> = &<span class="keyword">mut</span> <span class="variable mutable">x</span>;
6882
<span class="keyword">let</span> <span class="variable declaration">z</span> = &<span class="variable mutable">y</span>;

crates/ra_ide/src/syntax_highlighting.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,12 +403,13 @@ fn highlight_element(
403403
T![break]
404404
| T![continue]
405405
| T![else]
406-
| T![for]
407406
| T![if]
408407
| T![loop]
409408
| T![match]
410409
| T![return]
411-
| T![while] => h | HighlightModifier::ControlFlow,
410+
| T![while]
411+
| T![in] => h | HighlightModifier::ControlFlow,
412+
T![for] if !is_child_of_impl(element) => h | HighlightModifier::ControlFlow,
412413
T![unsafe] => h | HighlightModifier::Unsafe,
413414
_ => h,
414415
}
@@ -432,6 +433,13 @@ fn highlight_element(
432433
}
433434
}
434435

436+
fn is_child_of_impl(element: SyntaxElement) -> bool {
437+
match element.parent() {
438+
Some(e) => e.kind() == IMPL_DEF,
439+
_ => false,
440+
}
441+
}
442+
435443
fn highlight_name(db: &RootDatabase, def: Definition) -> Highlight {
436444
match def {
437445
Definition::Macro(_) => HighlightTag::Macro,

crates/ra_ide/src/syntax_highlighting/tests.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ struct Foo {
1717
pub y: i32,
1818
}
1919
20+
trait Bar {
21+
fn bar(&self) -> i32;
22+
}
23+
24+
impl Bar for Foo {
25+
fn bar(&self) -> i32 {
26+
self.x
27+
}
28+
}
29+
2030
static mut STATIC_MUT: i32 = 0;
2131
2232
fn foo<'a, T>() -> T {
@@ -47,6 +57,10 @@ fn main() {
4757
STATIC_MUT = 1;
4858
}
4959
60+
for e in vec {
61+
// Do nothing
62+
}
63+
5064
let mut x = 42;
5165
let y = &mut x;
5266
let z = &y;

crates/rust-analyzer/src/to_proto.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ fn semantic_token_type_and_modifiers(
264264
HighlightTag::Trait => lsp_types::SemanticTokenType::INTERFACE,
265265
HighlightTag::BuiltinType => semantic_tokens::BUILTIN_TYPE,
266266
HighlightTag::SelfType => lsp_types::SemanticTokenType::TYPE,
267-
HighlightTag::Field => lsp_types::SemanticTokenType::MEMBER,
267+
HighlightTag::Field => lsp_types::SemanticTokenType::PROPERTY,
268268
HighlightTag::Function => lsp_types::SemanticTokenType::FUNCTION,
269269
HighlightTag::Module => lsp_types::SemanticTokenType::NAMESPACE,
270270
HighlightTag::Constant => {

editors/code/.vscodeignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
!package.json
44
!package-lock.json
55
!ra_syntax_tree.tmGrammar.json
6+
!rust.tmGrammar.json
67
!icon.png
78
!README.md

editors/code/package.json

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,11 @@
478478
}
479479
],
480480
"grammars": [
481+
{
482+
"language": "rust",
483+
"scopeName": "source.rust",
484+
"path": "rust.tmGrammar.json"
485+
},
481486
{
482487
"language": "ra_syntax_tree",
483488
"scopeName": "source.ra_syntax_tree",
@@ -596,28 +601,28 @@
596601
"support.type.primitive"
597602
],
598603
"lifetime": [
599-
"entity.name.lifetime.rust"
604+
"storage.modifier.lifetime.rust"
600605
],
601606
"typeAlias": [
602-
"entity.name.typeAlias"
607+
"entity.name.type.typeAlias"
603608
],
604609
"union": [
605-
"entity.name.union"
610+
"entity.name.type.union"
606611
],
607612
"struct": [
608613
"entity.name.type.struct"
609614
],
610-
"keyword.unsafe": [
611-
"keyword.other.unsafe"
612-
],
613615
"keyword": [
614-
"keyword"
616+
"keyword.other"
615617
],
616618
"keyword.controlFlow": [
617619
"keyword.control"
618620
],
619621
"variable.constant": [
620-
"entity.name.constant"
622+
"variable.other.constant"
623+
],
624+
"formatSpecifier": [
625+
"punctuation.section.embedded"
621626
]
622627
}
623628
}

0 commit comments

Comments
 (0)