Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 7c2bb75

Browse files
committed
Auto merge of rust-lang#16860 - Veykril:macarons, r=Veykril
feat: Syntax highlighting improvements Specifically - Adds a new `constant` modifier, attached to keyword `const` (except for `*const ()` and `&raw const ()`), `const` items and `const` functions - Adds (or rather reveals) `associated` modifier for associated items - Fixes usage of the standard `static` modifier, now it acts like `associated` except being omitted for methods. - Splits `SymbolKind::Function` into `Function` and `Method`. We already split other things like that (notable self param from params), so the split makes sense in general as a lot special cases around it anyways.
2 parents f40c7d8 + 4b679f9 commit 7c2bb75

39 files changed

+1997
-1796
lines changed

crates/ide-completion/src/completions/dot.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -961,11 +961,11 @@ struct Foo { field: i32 }
961961
impl Foo { fn foo(&self) { $0 } }"#,
962962
expect![[r#"
963963
fd self.field i32
964+
me self.foo() fn(&self)
964965
lc self &Foo
965966
sp Self Foo
966967
st Foo Foo
967968
bt u32 u32
968-
me self.foo() fn(&self)
969969
"#]],
970970
);
971971
check(
@@ -975,11 +975,11 @@ struct Foo(i32);
975975
impl Foo { fn foo(&mut self) { $0 } }"#,
976976
expect![[r#"
977977
fd self.0 i32
978+
me self.foo() fn(&mut self)
978979
lc self &mut Foo
979980
sp Self Foo
980981
st Foo Foo
981982
bt u32 u32
982-
me self.foo() fn(&mut self)
983983
"#]],
984984
);
985985
}

crates/ide-completion/src/completions/item_list/trait_impl.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,11 @@ fn add_function_impl(
186186
if func.assoc_fn_params(ctx.db).is_empty() { "" } else { ".." }
187187
);
188188

189-
let completion_kind = if func.has_self_param(ctx.db) {
190-
CompletionItemKind::Method
189+
let completion_kind = CompletionItemKind::SymbolKind(if func.has_self_param(ctx.db) {
190+
SymbolKind::Method
191191
} else {
192-
CompletionItemKind::SymbolKind(SymbolKind::Function)
193-
};
192+
SymbolKind::Function
193+
});
194194

195195
let mut item = CompletionItem::new(completion_kind, replacement_range, label);
196196
item.lookup_by(format!("fn {}", fn_name.display(ctx.db)))

crates/ide-completion/src/completions/keyword.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ impl Future for A {}
7575
fn foo(a: A) { a.$0 }
7676
"#,
7777
expect![[r#"
78-
kw await expr.await
7978
me into_future() (as IntoFuture) fn(self) -> <Self as IntoFuture>::IntoFuture
79+
kw await expr.await
8080
sn box Box::new(expr)
8181
sn call function(expr)
8282
sn dbg dbg!(expr)
@@ -102,8 +102,8 @@ fn foo() {
102102
}
103103
"#,
104104
expect![[r#"
105-
kw await expr.await
106105
me into_future() (use core::future::IntoFuture) fn(self) -> <Self as IntoFuture>::IntoFuture
106+
kw await expr.await
107107
sn box Box::new(expr)
108108
sn call function(expr)
109109
sn dbg dbg!(expr)
@@ -131,8 +131,8 @@ impl IntoFuture for A {}
131131
fn foo(a: A) { a.$0 }
132132
"#,
133133
expect![[r#"
134-
kw await expr.await
135134
me into_future() (as IntoFuture) fn(self) -> <Self as IntoFuture>::IntoFuture
135+
kw await expr.await
136136
sn box Box::new(expr)
137137
sn call function(expr)
138138
sn dbg dbg!(expr)

crates/ide-completion/src/item.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,6 @@ pub enum CompletionItemKind {
342342
BuiltinType,
343343
InferredType,
344344
Keyword,
345-
Method,
346345
Snippet,
347346
UnresolvedReference,
348347
Expression,
@@ -369,6 +368,7 @@ impl CompletionItemKind {
369368
SymbolKind::LifetimeParam => "lt",
370369
SymbolKind::Local => "lc",
371370
SymbolKind::Macro => "ma",
371+
SymbolKind::Method => "me",
372372
SymbolKind::ProcMacro => "pm",
373373
SymbolKind::Module => "md",
374374
SymbolKind::SelfParam => "sp",
@@ -388,7 +388,6 @@ impl CompletionItemKind {
388388
CompletionItemKind::BuiltinType => "bt",
389389
CompletionItemKind::InferredType => "it",
390390
CompletionItemKind::Keyword => "kw",
391-
CompletionItemKind::Method => "me",
392391
CompletionItemKind::Snippet => "sn",
393392
CompletionItemKind::UnresolvedReference => "??",
394393
CompletionItemKind::Expression => "ex",

crates/ide-completion/src/render.rs

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -677,10 +677,11 @@ mod tests {
677677

678678
#[track_caller]
679679
fn check_function_relevance(ra_fixture: &str, expect: Expect) {
680-
let actual: Vec<_> = do_completion(ra_fixture, CompletionItemKind::Method)
681-
.into_iter()
682-
.map(|item| (item.detail.unwrap_or_default(), item.relevance.function))
683-
.collect();
680+
let actual: Vec<_> =
681+
do_completion(ra_fixture, CompletionItemKind::SymbolKind(SymbolKind::Method))
682+
.into_iter()
683+
.map(|item| (item.detail.unwrap_or_default(), item.relevance.function))
684+
.collect();
684685

685686
expect.assert_debug_eq(&actual);
686687
}
@@ -1392,15 +1393,20 @@ impl S {
13921393
/// Method docs
13931394
fn bar(self) { self.$0 }
13941395
}"#,
1395-
&[CompletionItemKind::Method, CompletionItemKind::SymbolKind(SymbolKind::Field)],
1396+
&[
1397+
CompletionItemKind::SymbolKind(SymbolKind::Method),
1398+
CompletionItemKind::SymbolKind(SymbolKind::Field),
1399+
],
13961400
expect![[r#"
13971401
[
13981402
CompletionItem {
13991403
label: "bar()",
14001404
source_range: 94..94,
14011405
delete: 94..94,
14021406
insert: "bar()$0",
1403-
kind: Method,
1407+
kind: SymbolKind(
1408+
Method,
1409+
),
14041410
lookup: "bar",
14051411
detail: "fn(self)",
14061412
documentation: Documentation(
@@ -1520,15 +1526,17 @@ impl S {
15201526
}
15211527
fn foo(s: S) { s.$0 }
15221528
"#,
1523-
CompletionItemKind::Method,
1529+
CompletionItemKind::SymbolKind(SymbolKind::Method),
15241530
expect![[r#"
15251531
[
15261532
CompletionItem {
15271533
label: "the_method()",
15281534
source_range: 81..81,
15291535
delete: 81..81,
15301536
insert: "the_method()$0",
1531-
kind: Method,
1537+
kind: SymbolKind(
1538+
Method,
1539+
),
15321540
lookup: "the_method",
15331541
detail: "fn(&self)",
15341542
relevance: CompletionRelevance {
@@ -2408,15 +2416,20 @@ impl Foo { fn baz(&self) -> u32 { 0 } }
24082416
24092417
fn foo(f: Foo) { let _: &u32 = f.b$0 }
24102418
"#,
2411-
&[CompletionItemKind::Method, CompletionItemKind::SymbolKind(SymbolKind::Field)],
2419+
&[
2420+
CompletionItemKind::SymbolKind(SymbolKind::Method),
2421+
CompletionItemKind::SymbolKind(SymbolKind::Field),
2422+
],
24122423
expect![[r#"
24132424
[
24142425
CompletionItem {
24152426
label: "baz()",
24162427
source_range: 109..110,
24172428
delete: 109..110,
24182429
insert: "baz()$0",
2419-
kind: Method,
2430+
kind: SymbolKind(
2431+
Method,
2432+
),
24202433
lookup: "baz",
24212434
detail: "fn(&self) -> u32",
24222435
relevance: CompletionRelevance {
@@ -2631,7 +2644,7 @@ fn main() {
26312644
let _: bool = (9 > 2).not$0;
26322645
}
26332646
"#,
2634-
&[CompletionItemKind::Snippet, CompletionItemKind::Method],
2647+
&[CompletionItemKind::Snippet, CompletionItemKind::SymbolKind(SymbolKind::Method)],
26352648
expect![[r#"
26362649
sn not [snippet]
26372650
me not() (use ops::Not) [type_could_unify+requires_import]
@@ -2664,7 +2677,7 @@ fn main() {
26642677
S.$0
26652678
}
26662679
"#,
2667-
&[CompletionItemKind::Snippet, CompletionItemKind::Method],
2680+
&[CompletionItemKind::Snippet, CompletionItemKind::SymbolKind(SymbolKind::Method)],
26682681
expect![[r#"
26692682
me f() []
26702683
sn ref []
@@ -2907,7 +2920,7 @@ fn main() {
29072920
}
29082921
"#,
29092922
&[
2910-
CompletionItemKind::Method,
2923+
CompletionItemKind::SymbolKind(SymbolKind::Method),
29112924
CompletionItemKind::SymbolKind(SymbolKind::Field),
29122925
CompletionItemKind::SymbolKind(SymbolKind::Function),
29132926
],
@@ -2918,7 +2931,9 @@ fn main() {
29182931
source_range: 193..193,
29192932
delete: 193..193,
29202933
insert: "flush()$0",
2921-
kind: Method,
2934+
kind: SymbolKind(
2935+
Method,
2936+
),
29222937
lookup: "flush",
29232938
detail: "fn(&self)",
29242939
relevance: CompletionRelevance {
@@ -2941,7 +2956,9 @@ fn main() {
29412956
source_range: 193..193,
29422957
delete: 193..193,
29432958
insert: "write()$0",
2944-
kind: Method,
2959+
kind: SymbolKind(
2960+
Method,
2961+
),
29452962
lookup: "write",
29462963
detail: "fn(&self)",
29472964
relevance: CompletionRelevance {

crates/ide-completion/src/render/function.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ fn render(
6868
};
6969
let has_self_param = func.self_param(db).is_some();
7070
let mut item = CompletionItem::new(
71-
if has_self_param {
72-
CompletionItemKind::Method
71+
CompletionItemKind::SymbolKind(if has_self_param {
72+
SymbolKind::Method
7373
} else {
74-
CompletionItemKind::SymbolKind(SymbolKind::Function)
75-
},
74+
SymbolKind::Function
75+
}),
7676
ctx.source_range(),
7777
call.clone(),
7878
);

crates/ide-completion/src/tests/expression.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ impl Unit {
127127
en Enum Enum
128128
fn function() fn()
129129
fn local_func() fn()
130+
me self.foo() fn(self)
130131
lc self Unit
131132
ma makro!(…) macro_rules! makro
132133
md module
@@ -166,7 +167,6 @@ impl Unit {
166167
kw use
167168
kw while
168169
kw while let
169-
me self.foo() fn(self)
170170
sn macro_rules
171171
sn pd
172172
sn ppd

crates/ide-completion/src/tests/special.rs

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Tests that don't fit into a specific category.
22
33
use expect_test::{expect, Expect};
4+
use ide_db::SymbolKind;
45

56
use crate::{
67
tests::{
@@ -316,15 +317,15 @@ trait Sub: Super {
316317
fn foo<T: Sub>() { T::$0 }
317318
"#,
318319
expect![[r#"
319-
ct C2 (as Sub) const C2: ()
320-
ct CONST (as Super) const CONST: u8
321-
fn func() (as Super) fn()
322-
fn subfunc() (as Sub) fn()
323-
ta SubTy (as Sub) type SubTy
324-
ta Ty (as Super) type Ty
325-
me method(…) (as Super) fn(&self)
326-
me submethod(…) (as Sub) fn(&self)
327-
"#]],
320+
ct C2 (as Sub) const C2: ()
321+
ct CONST (as Super) const CONST: u8
322+
fn func() (as Super) fn()
323+
fn subfunc() (as Sub) fn()
324+
me method(…) (as Super) fn(&self)
325+
me submethod(…) (as Sub) fn(&self)
326+
ta SubTy (as Sub) type SubTy
327+
ta Ty (as Super) type Ty
328+
"#]],
328329
);
329330
}
330331

@@ -356,15 +357,15 @@ impl<T> Sub for Wrap<T> {
356357
}
357358
"#,
358359
expect![[r#"
359-
ct C2 (as Sub) const C2: ()
360-
ct CONST (as Super) const CONST: u8
361-
fn func() (as Super) fn()
362-
fn subfunc() (as Sub) fn()
363-
ta SubTy (as Sub) type SubTy
364-
ta Ty (as Super) type Ty
365-
me method(…) (as Super) fn(&self)
366-
me submethod(…) (as Sub) fn(&self)
367-
"#]],
360+
ct C2 (as Sub) const C2: ()
361+
ct CONST (as Super) const CONST: u8
362+
fn func() (as Super) fn()
363+
fn subfunc() (as Sub) fn()
364+
me method(…) (as Super) fn(&self)
365+
me submethod(…) (as Sub) fn(&self)
366+
ta SubTy (as Sub) type SubTy
367+
ta Ty (as Super) type Ty
368+
"#]],
368369
);
369370
}
370371

@@ -555,10 +556,10 @@ impl Foo {
555556
}
556557
"#,
557558
expect![[r#"
558-
ev Bar Bar
559-
ev Baz Baz
560-
me foo(…) fn(self)
561-
"#]],
559+
me foo(…) fn(self)
560+
ev Bar Bar
561+
ev Baz Baz
562+
"#]],
562563
);
563564
}
564565

@@ -1399,7 +1400,7 @@ fn main() {
13991400
bar.b$0
14001401
}
14011402
"#,
1402-
CompletionItemKind::Method,
1403+
CompletionItemKind::SymbolKind(SymbolKind::Method),
14031404
expect!("const fn(&'foo mut self, &Foo) -> !"),
14041405
expect!("pub const fn baz<'foo>(&'foo mut self, x: &'foo Foo) -> !"),
14051406
);

crates/ide-db/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ pub enum SymbolKind {
346346
Enum,
347347
Field,
348348
Function,
349+
Method,
349350
Impl,
350351
Label,
351352
LifetimeParam,

crates/ide/src/file_structure.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,22 @@ fn structure_node(node: &SyntaxNode) -> Option<StructureNode> {
134134
if let Some(type_param_list) = it.generic_param_list() {
135135
collapse_ws(type_param_list.syntax(), &mut detail);
136136
}
137-
if let Some(param_list) = it.param_list() {
137+
let has_self_param = if let Some(param_list) = it.param_list() {
138138
collapse_ws(param_list.syntax(), &mut detail);
139-
}
139+
param_list.self_param().is_some()
140+
} else {
141+
false
142+
};
140143
if let Some(ret_type) = it.ret_type() {
141144
detail.push(' ');
142145
collapse_ws(ret_type.syntax(), &mut detail);
143146
}
144147

145-
decl_with_detail(&it, Some(detail), StructureNodeKind::SymbolKind(SymbolKind::Function))
148+
decl_with_detail(&it, Some(detail), StructureNodeKind::SymbolKind(if has_self_param {
149+
SymbolKind::Method
150+
} else {
151+
SymbolKind::Function
152+
}))
146153
},
147154
ast::Struct(it) => decl(it, StructureNodeKind::SymbolKind(SymbolKind::Struct)),
148155
ast::Union(it) => decl(it, StructureNodeKind::SymbolKind(SymbolKind::Union)),

0 commit comments

Comments
 (0)