Skip to content

Commit

Permalink
Auto merge of #16860 - Veykril:macarons, r=Veykril
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed Mar 18, 2024
2 parents f40c7d8 + 4b679f9 commit 7c2bb75
Show file tree
Hide file tree
Showing 39 changed files with 1,997 additions and 1,796 deletions.
4 changes: 2 additions & 2 deletions crates/ide-completion/src/completions/dot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -961,11 +961,11 @@ struct Foo { field: i32 }
impl Foo { fn foo(&self) { $0 } }"#,
expect![[r#"
fd self.field i32
me self.foo() fn(&self)
lc self &Foo
sp Self Foo
st Foo Foo
bt u32 u32
me self.foo() fn(&self)
"#]],
);
check(
Expand All @@ -975,11 +975,11 @@ struct Foo(i32);
impl Foo { fn foo(&mut self) { $0 } }"#,
expect![[r#"
fd self.0 i32
me self.foo() fn(&mut self)
lc self &mut Foo
sp Self Foo
st Foo Foo
bt u32 u32
me self.foo() fn(&mut self)
"#]],
);
}
Expand Down
8 changes: 4 additions & 4 deletions crates/ide-completion/src/completions/item_list/trait_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,11 @@ fn add_function_impl(
if func.assoc_fn_params(ctx.db).is_empty() { "" } else { ".." }
);

let completion_kind = if func.has_self_param(ctx.db) {
CompletionItemKind::Method
let completion_kind = CompletionItemKind::SymbolKind(if func.has_self_param(ctx.db) {
SymbolKind::Method
} else {
CompletionItemKind::SymbolKind(SymbolKind::Function)
};
SymbolKind::Function
});

let mut item = CompletionItem::new(completion_kind, replacement_range, label);
item.lookup_by(format!("fn {}", fn_name.display(ctx.db)))
Expand Down
6 changes: 3 additions & 3 deletions crates/ide-completion/src/completions/keyword.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ impl Future for A {}
fn foo(a: A) { a.$0 }
"#,
expect![[r#"
kw await expr.await
me into_future() (as IntoFuture) fn(self) -> <Self as IntoFuture>::IntoFuture
kw await expr.await
sn box Box::new(expr)
sn call function(expr)
sn dbg dbg!(expr)
Expand All @@ -102,8 +102,8 @@ fn foo() {
}
"#,
expect![[r#"
kw await expr.await
me into_future() (use core::future::IntoFuture) fn(self) -> <Self as IntoFuture>::IntoFuture
kw await expr.await
sn box Box::new(expr)
sn call function(expr)
sn dbg dbg!(expr)
Expand Down Expand Up @@ -131,8 +131,8 @@ impl IntoFuture for A {}
fn foo(a: A) { a.$0 }
"#,
expect![[r#"
kw await expr.await
me into_future() (as IntoFuture) fn(self) -> <Self as IntoFuture>::IntoFuture
kw await expr.await
sn box Box::new(expr)
sn call function(expr)
sn dbg dbg!(expr)
Expand Down
3 changes: 1 addition & 2 deletions crates/ide-completion/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,6 @@ pub enum CompletionItemKind {
BuiltinType,
InferredType,
Keyword,
Method,
Snippet,
UnresolvedReference,
Expression,
Expand All @@ -369,6 +368,7 @@ impl CompletionItemKind {
SymbolKind::LifetimeParam => "lt",
SymbolKind::Local => "lc",
SymbolKind::Macro => "ma",
SymbolKind::Method => "me",
SymbolKind::ProcMacro => "pm",
SymbolKind::Module => "md",
SymbolKind::SelfParam => "sp",
Expand All @@ -388,7 +388,6 @@ impl CompletionItemKind {
CompletionItemKind::BuiltinType => "bt",
CompletionItemKind::InferredType => "it",
CompletionItemKind::Keyword => "kw",
CompletionItemKind::Method => "me",
CompletionItemKind::Snippet => "sn",
CompletionItemKind::UnresolvedReference => "??",
CompletionItemKind::Expression => "ex",
Expand Down
47 changes: 32 additions & 15 deletions crates/ide-completion/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -677,10 +677,11 @@ mod tests {

#[track_caller]
fn check_function_relevance(ra_fixture: &str, expect: Expect) {
let actual: Vec<_> = do_completion(ra_fixture, CompletionItemKind::Method)
.into_iter()
.map(|item| (item.detail.unwrap_or_default(), item.relevance.function))
.collect();
let actual: Vec<_> =
do_completion(ra_fixture, CompletionItemKind::SymbolKind(SymbolKind::Method))
.into_iter()
.map(|item| (item.detail.unwrap_or_default(), item.relevance.function))
.collect();

expect.assert_debug_eq(&actual);
}
Expand Down Expand Up @@ -1392,15 +1393,20 @@ impl S {
/// Method docs
fn bar(self) { self.$0 }
}"#,
&[CompletionItemKind::Method, CompletionItemKind::SymbolKind(SymbolKind::Field)],
&[
CompletionItemKind::SymbolKind(SymbolKind::Method),
CompletionItemKind::SymbolKind(SymbolKind::Field),
],
expect![[r#"
[
CompletionItem {
label: "bar()",
source_range: 94..94,
delete: 94..94,
insert: "bar()$0",
kind: Method,
kind: SymbolKind(
Method,
),
lookup: "bar",
detail: "fn(self)",
documentation: Documentation(
Expand Down Expand Up @@ -1520,15 +1526,17 @@ impl S {
}
fn foo(s: S) { s.$0 }
"#,
CompletionItemKind::Method,
CompletionItemKind::SymbolKind(SymbolKind::Method),
expect![[r#"
[
CompletionItem {
label: "the_method()",
source_range: 81..81,
delete: 81..81,
insert: "the_method()$0",
kind: Method,
kind: SymbolKind(
Method,
),
lookup: "the_method",
detail: "fn(&self)",
relevance: CompletionRelevance {
Expand Down Expand Up @@ -2408,15 +2416,20 @@ impl Foo { fn baz(&self) -> u32 { 0 } }
fn foo(f: Foo) { let _: &u32 = f.b$0 }
"#,
&[CompletionItemKind::Method, CompletionItemKind::SymbolKind(SymbolKind::Field)],
&[
CompletionItemKind::SymbolKind(SymbolKind::Method),
CompletionItemKind::SymbolKind(SymbolKind::Field),
],
expect![[r#"
[
CompletionItem {
label: "baz()",
source_range: 109..110,
delete: 109..110,
insert: "baz()$0",
kind: Method,
kind: SymbolKind(
Method,
),
lookup: "baz",
detail: "fn(&self) -> u32",
relevance: CompletionRelevance {
Expand Down Expand Up @@ -2631,7 +2644,7 @@ fn main() {
let _: bool = (9 > 2).not$0;
}
"#,
&[CompletionItemKind::Snippet, CompletionItemKind::Method],
&[CompletionItemKind::Snippet, CompletionItemKind::SymbolKind(SymbolKind::Method)],
expect![[r#"
sn not [snippet]
me not() (use ops::Not) [type_could_unify+requires_import]
Expand Down Expand Up @@ -2664,7 +2677,7 @@ fn main() {
S.$0
}
"#,
&[CompletionItemKind::Snippet, CompletionItemKind::Method],
&[CompletionItemKind::Snippet, CompletionItemKind::SymbolKind(SymbolKind::Method)],
expect![[r#"
me f() []
sn ref []
Expand Down Expand Up @@ -2907,7 +2920,7 @@ fn main() {
}
"#,
&[
CompletionItemKind::Method,
CompletionItemKind::SymbolKind(SymbolKind::Method),
CompletionItemKind::SymbolKind(SymbolKind::Field),
CompletionItemKind::SymbolKind(SymbolKind::Function),
],
Expand All @@ -2918,7 +2931,9 @@ fn main() {
source_range: 193..193,
delete: 193..193,
insert: "flush()$0",
kind: Method,
kind: SymbolKind(
Method,
),
lookup: "flush",
detail: "fn(&self)",
relevance: CompletionRelevance {
Expand All @@ -2941,7 +2956,9 @@ fn main() {
source_range: 193..193,
delete: 193..193,
insert: "write()$0",
kind: Method,
kind: SymbolKind(
Method,
),
lookup: "write",
detail: "fn(&self)",
relevance: CompletionRelevance {
Expand Down
8 changes: 4 additions & 4 deletions crates/ide-completion/src/render/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ fn render(
};
let has_self_param = func.self_param(db).is_some();
let mut item = CompletionItem::new(
if has_self_param {
CompletionItemKind::Method
CompletionItemKind::SymbolKind(if has_self_param {
SymbolKind::Method
} else {
CompletionItemKind::SymbolKind(SymbolKind::Function)
},
SymbolKind::Function
}),
ctx.source_range(),
call.clone(),
);
Expand Down
2 changes: 1 addition & 1 deletion crates/ide-completion/src/tests/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ impl Unit {
en Enum Enum
fn function() fn()
fn local_func() fn()
me self.foo() fn(self)
lc self Unit
ma makro!(…) macro_rules! makro
md module
Expand Down Expand Up @@ -166,7 +167,6 @@ impl Unit {
kw use
kw while
kw while let
me self.foo() fn(self)
sn macro_rules
sn pd
sn ppd
Expand Down
47 changes: 24 additions & 23 deletions crates/ide-completion/src/tests/special.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Tests that don't fit into a specific category.

use expect_test::{expect, Expect};
use ide_db::SymbolKind;

use crate::{
tests::{
Expand Down Expand Up @@ -316,15 +317,15 @@ trait Sub: Super {
fn foo<T: Sub>() { T::$0 }
"#,
expect![[r#"
ct C2 (as Sub) const C2: ()
ct CONST (as Super) const CONST: u8
fn func() (as Super) fn()
fn subfunc() (as Sub) fn()
ta SubTy (as Sub) type SubTy
ta Ty (as Super) type Ty
me method(…) (as Super) fn(&self)
me submethod(…) (as Sub) fn(&self)
"#]],
ct C2 (as Sub) const C2: ()
ct CONST (as Super) const CONST: u8
fn func() (as Super) fn()
fn subfunc() (as Sub) fn()
me method(…) (as Super) fn(&self)
me submethod(…) (as Sub) fn(&self)
ta SubTy (as Sub) type SubTy
ta Ty (as Super) type Ty
"#]],
);
}

Expand Down Expand Up @@ -356,15 +357,15 @@ impl<T> Sub for Wrap<T> {
}
"#,
expect![[r#"
ct C2 (as Sub) const C2: ()
ct CONST (as Super) const CONST: u8
fn func() (as Super) fn()
fn subfunc() (as Sub) fn()
ta SubTy (as Sub) type SubTy
ta Ty (as Super) type Ty
me method(…) (as Super) fn(&self)
me submethod(…) (as Sub) fn(&self)
"#]],
ct C2 (as Sub) const C2: ()
ct CONST (as Super) const CONST: u8
fn func() (as Super) fn()
fn subfunc() (as Sub) fn()
me method(…) (as Super) fn(&self)
me submethod(…) (as Sub) fn(&self)
ta SubTy (as Sub) type SubTy
ta Ty (as Super) type Ty
"#]],
);
}

Expand Down Expand Up @@ -555,10 +556,10 @@ impl Foo {
}
"#,
expect![[r#"
ev Bar Bar
ev Baz Baz
me foo(…) fn(self)
"#]],
me foo(…) fn(self)
ev Bar Bar
ev Baz Baz
"#]],
);
}

Expand Down Expand Up @@ -1399,7 +1400,7 @@ fn main() {
bar.b$0
}
"#,
CompletionItemKind::Method,
CompletionItemKind::SymbolKind(SymbolKind::Method),
expect!("const fn(&'foo mut self, &Foo) -> !"),
expect!("pub const fn baz<'foo>(&'foo mut self, x: &'foo Foo) -> !"),
);
Expand Down
1 change: 1 addition & 0 deletions crates/ide-db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ pub enum SymbolKind {
Enum,
Field,
Function,
Method,
Impl,
Label,
LifetimeParam,
Expand Down
13 changes: 10 additions & 3 deletions crates/ide/src/file_structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,22 @@ fn structure_node(node: &SyntaxNode) -> Option<StructureNode> {
if let Some(type_param_list) = it.generic_param_list() {
collapse_ws(type_param_list.syntax(), &mut detail);
}
if let Some(param_list) = it.param_list() {
let has_self_param = if let Some(param_list) = it.param_list() {
collapse_ws(param_list.syntax(), &mut detail);
}
param_list.self_param().is_some()
} else {
false
};
if let Some(ret_type) = it.ret_type() {
detail.push(' ');
collapse_ws(ret_type.syntax(), &mut detail);
}

decl_with_detail(&it, Some(detail), StructureNodeKind::SymbolKind(SymbolKind::Function))
decl_with_detail(&it, Some(detail), StructureNodeKind::SymbolKind(if has_self_param {
SymbolKind::Method
} else {
SymbolKind::Function
}))
},
ast::Struct(it) => decl(it, StructureNodeKind::SymbolKind(SymbolKind::Struct)),
ast::Union(it) => decl(it, StructureNodeKind::SymbolKind(SymbolKind::Union)),
Expand Down
Loading

0 comments on commit 7c2bb75

Please sign in to comment.