Skip to content

Commit 56f6245

Browse files
8024: Updated the implementation for trait modifier.
Fixed the test cases.
1 parent f269fe7 commit 56f6245

File tree

6 files changed

+30
-20
lines changed

6 files changed

+30
-20
lines changed

crates/ide/src/syntax_highlighting/highlight.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,11 @@ fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight {
276276
hir::ModuleDef::Function(func) => {
277277
let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Function));
278278
if let Some(item) = func.as_assoc_item(db) {
279+
h |= HlMod::Associated;
280+
if func.self_param(db).is_none() {
281+
h |= HlMod::Static
282+
}
283+
279284
match item.container(db) {
280285
AssocItemContainer::Impl(i) => {
281286
if i.target_trait(db).is_some() {
@@ -288,12 +293,6 @@ fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight {
288293
}
289294
}
290295

291-
if func.as_assoc_item(db).is_some() {
292-
h |= HlMod::Associated;
293-
if func.self_param(db).is_none() {
294-
h |= HlMod::Static
295-
}
296-
}
297296
if func.is_unsafe(db) {
298297
h |= HlMod::Unsafe;
299298
}
@@ -305,9 +304,20 @@ fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight {
305304
hir::ModuleDef::Variant(_) => HlTag::Symbol(SymbolKind::Variant),
306305
hir::ModuleDef::Const(konst) => {
307306
let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Const));
308-
if konst.as_assoc_item(db).is_some() {
309-
h |= HlMod::Associated
307+
if let Some(item) = konst.as_assoc_item(db) {
308+
h |= HlMod::Associated;
309+
match item.container(db) {
310+
AssocItemContainer::Impl(i) => {
311+
if i.target_trait(db).is_some() {
312+
h |= HlMod::Trait;
313+
}
314+
}
315+
AssocItemContainer::Trait(_t) => {
316+
h |= HlMod::Trait;
317+
}
318+
}
310319
}
320+
311321
return h;
312322
}
313323
hir::ModuleDef::Trait(_) => HlTag::Symbol(SymbolKind::Trait),
@@ -375,7 +385,7 @@ fn highlight_method_call(
375385
if func.is_unsafe(sema.db) || sema.is_unsafe_method_call(&method_call) {
376386
h |= HlMod::Unsafe;
377387
}
378-
if let Some(_t) = func.as_assoc_item(sema.db)?.containing_trait(sema.db) {
388+
if func.as_assoc_item(sema.db).and_then(|it| it.containing_trait(sema.db)).is_some() {
379389
h |= HlMod::Trait
380390
}
381391

crates/ide/src/syntax_highlighting/tags.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub enum HlMod {
5858
Associated,
5959
/// Used for intra doc links in doc injection.
6060
IntraDocLink,
61-
/// Used for trait items in impls.
61+
/// Used for items in traits and trait impls.
6262
Trait,
6363

6464
/// Keep this last!

crates/ide/src/syntax_highlighting/test_data/highlight_assoc_functions.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@
4747
<span class="brace">}</span>
4848

4949
<span class="keyword">trait</span> <span class="trait declaration">t</span> <span class="brace">{</span>
50-
<span class="keyword">fn</span> <span class="function declaration static associated">t_is_static</span><span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="brace">{</span><span class="brace">}</span>
51-
<span class="keyword">fn</span> <span class="function declaration associated">t_is_not_static</span><span class="parenthesis">(</span><span class="operator">&</span><span class="self_keyword declaration">self</span><span class="parenthesis">)</span> <span class="brace">{</span><span class="brace">}</span>
50+
<span class="keyword">fn</span> <span class="function declaration static associated trait">t_is_static</span><span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="brace">{</span><span class="brace">}</span>
51+
<span class="keyword">fn</span> <span class="function declaration associated trait">t_is_not_static</span><span class="parenthesis">(</span><span class="operator">&</span><span class="self_keyword declaration">self</span><span class="parenthesis">)</span> <span class="brace">{</span><span class="brace">}</span>
5252
<span class="brace">}</span>
5353

5454
<span class="keyword">impl</span> <span class="trait">t</span> <span class="keyword">for</span> <span class="struct">foo</span> <span class="brace">{</span>
55-
<span class="keyword">pub</span> <span class="keyword">fn</span> <span class="function declaration static associated">is_static</span><span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="brace">{</span><span class="brace">}</span>
56-
<span class="keyword">pub</span> <span class="keyword">fn</span> <span class="function declaration associated">is_not_static</span><span class="parenthesis">(</span><span class="operator">&</span><span class="self_keyword declaration">self</span><span class="parenthesis">)</span> <span class="brace">{</span><span class="brace">}</span>
55+
<span class="keyword">pub</span> <span class="keyword">fn</span> <span class="function declaration static associated trait">is_static</span><span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="brace">{</span><span class="brace">}</span>
56+
<span class="keyword">pub</span> <span class="keyword">fn</span> <span class="function declaration associated trait">is_not_static</span><span class="parenthesis">(</span><span class="operator">&</span><span class="self_keyword declaration">self</span><span class="parenthesis">)</span> <span class="brace">{</span><span class="brace">}</span>
5757
<span class="brace">}</span>
5858
</code></pre>

crates/ide/src/syntax_highlighting/test_data/highlight_injection.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
<span class="keyword">fn</span> <span class="function declaration">main</span><span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="brace">{</span>
4343
<span class="function">fixture</span><span class="parenthesis">(</span><span class="string_literal">r#"</span>
4444
<span class="keyword">trait</span> <span class="trait declaration">Foo</span> <span class="brace">{</span>
45-
<span class="keyword">fn</span> <span class="function declaration static associated">foo</span><span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="brace">{</span>
45+
<span class="keyword">fn</span> <span class="function declaration static associated trait">foo</span><span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="brace">{</span>
4646
<span class="macro">println!</span><span class="parenthesis">(</span><span class="string_literal">"2 + 2 = {}"</span><span class="comma">,</span> <span class="numeric_literal">4</span><span class="parenthesis">)</span><span class="semicolon">;</span>
4747
<span class="brace">}</span>
4848
<span class="brace">}</span><span class="string_literal">"#</span>

crates/ide/src/syntax_highlighting/test_data/highlight_unsafe.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@
6262
<span class="brace">}</span>
6363

6464
<span class="keyword">trait</span> <span class="trait declaration">DoTheAutoref</span> <span class="brace">{</span>
65-
<span class="keyword">fn</span> <span class="function declaration associated">calls_autoref</span><span class="parenthesis">(</span><span class="operator">&</span><span class="self_keyword declaration">self</span><span class="parenthesis">)</span><span class="semicolon">;</span>
65+
<span class="keyword">fn</span> <span class="function declaration associated trait">calls_autoref</span><span class="parenthesis">(</span><span class="operator">&</span><span class="self_keyword declaration">self</span><span class="parenthesis">)</span><span class="semicolon">;</span>
6666
<span class="brace">}</span>
6767

6868
<span class="keyword">impl</span> <span class="trait">DoTheAutoref</span> <span class="keyword">for</span> <span class="builtin_type">u16</span> <span class="brace">{</span>
69-
<span class="keyword">fn</span> <span class="function declaration associated">calls_autoref</span><span class="parenthesis">(</span><span class="operator">&</span><span class="self_keyword declaration">self</span><span class="parenthesis">)</span> <span class="brace">{</span><span class="brace">}</span>
69+
<span class="keyword">fn</span> <span class="function declaration associated trait">calls_autoref</span><span class="parenthesis">(</span><span class="operator">&</span><span class="self_keyword declaration">self</span><span class="parenthesis">)</span> <span class="brace">{</span><span class="brace">}</span>
7070
<span class="brace">}</span>
7171

7272
<span class="keyword">fn</span> <span class="function declaration">main</span><span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="brace">{</span>
@@ -96,6 +96,6 @@
9696
<span class="keyword">let</span> <span class="struct">Packed</span> <span class="brace">{</span> <span class="field">a</span><span class="colon">:</span> <span class="keyword unsafe">ref</span> <span class="variable declaration">_a</span> <span class="brace">}</span> <span class="operator">=</span> <span class="variable">packed</span><span class="semicolon">;</span>
9797

9898
<span class="comment">// unsafe auto ref of packed field</span>
99-
<span class="variable">packed</span><span class="operator">.</span><span class="field">a</span><span class="operator">.</span><span class="function associated unsafe">calls_autoref</span><span class="parenthesis">(</span><span class="parenthesis">)</span><span class="semicolon">;</span>
99+
<span class="variable">packed</span><span class="operator">.</span><span class="field">a</span><span class="operator">.</span><span class="function associated trait unsafe">calls_autoref</span><span class="parenthesis">(</span><span class="parenthesis">)</span><span class="semicolon">;</span>
100100
<span class="brace">}</span>
101101
<span class="brace">}</span></code></pre>

crates/ide/src/syntax_highlighting/test_data/highlighting.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@
6767
<span class="brace">}</span>
6868

6969
<span class="keyword">trait</span> <span class="trait declaration">Bar</span> <span class="brace">{</span>
70-
<span class="keyword">fn</span> <span class="function declaration associated">bar</span><span class="parenthesis">(</span><span class="operator">&</span><span class="self_keyword declaration">self</span><span class="parenthesis">)</span> <span class="operator">-&gt;</span> <span class="builtin_type">i32</span><span class="semicolon">;</span>
70+
<span class="keyword">fn</span> <span class="function declaration associated trait">bar</span><span class="parenthesis">(</span><span class="operator">&</span><span class="self_keyword declaration">self</span><span class="parenthesis">)</span> <span class="operator">-&gt;</span> <span class="builtin_type">i32</span><span class="semicolon">;</span>
7171
<span class="brace">}</span>
7272

7373
<span class="keyword">impl</span> <span class="trait">Bar</span> <span class="keyword">for</span> <span class="struct">Foo</span> <span class="brace">{</span>
74-
<span class="keyword">fn</span> <span class="function declaration associated">bar</span><span class="parenthesis">(</span><span class="operator">&</span><span class="self_keyword declaration">self</span><span class="parenthesis">)</span> <span class="operator">-&gt;</span> <span class="builtin_type">i32</span> <span class="brace">{</span>
74+
<span class="keyword">fn</span> <span class="function declaration associated trait">bar</span><span class="parenthesis">(</span><span class="operator">&</span><span class="self_keyword declaration">self</span><span class="parenthesis">)</span> <span class="operator">-&gt;</span> <span class="builtin_type">i32</span> <span class="brace">{</span>
7575
<span class="self_keyword">self</span><span class="operator">.</span><span class="field">x</span>
7676
<span class="brace">}</span>
7777
<span class="brace">}</span>

0 commit comments

Comments
 (0)