Skip to content

Commit

Permalink
Support trait aliases in IDE where type support isn't needed
Browse files Browse the repository at this point in the history
  • Loading branch information
lowr committed Mar 3, 2023
1 parent ebe39e0 commit 361f431
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 29 deletions.
79 changes: 52 additions & 27 deletions crates/ide/src/file_structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ fn structure_node(node: &SyntaxNode) -> Option<StructureNode> {
ast::Enum(it) => decl(it, StructureNodeKind::SymbolKind(SymbolKind::Enum)),
ast::Variant(it) => decl(it, StructureNodeKind::SymbolKind(SymbolKind::Variant)),
ast::Trait(it) => decl(it, StructureNodeKind::SymbolKind(SymbolKind::Trait)),
ast::TraitAlias(it) => decl(it, StructureNodeKind::SymbolKind(SymbolKind::TraitAlias)),
ast::Module(it) => decl(it, StructureNodeKind::SymbolKind(SymbolKind::Module)),
ast::TypeAlias(it) => decl_with_type_ref(&it, it.ty(), StructureNodeKind::SymbolKind(SymbolKind::TypeAlias)),
ast::RecordField(it) => decl_with_type_ref(&it, it.ty(), StructureNodeKind::SymbolKind(SymbolKind::Field)),
Expand Down Expand Up @@ -262,6 +263,8 @@ enum E { X, Y(i32) }
type T = ();
static S: i32 = 92;
const C: i32 = 92;
trait Tr {}
trait Alias = Tr;
impl E {}
Expand Down Expand Up @@ -457,11 +460,33 @@ fn g() {}
),
deprecated: false,
},
StructureNode {
parent: None,
label: "Tr",
navigation_range: 239..241,
node_range: 233..244,
kind: SymbolKind(
Trait,
),
detail: None,
deprecated: false,
},
StructureNode {
parent: None,
label: "Alias",
navigation_range: 251..256,
node_range: 245..262,
kind: SymbolKind(
TraitAlias,
),
detail: None,
deprecated: false,
},
StructureNode {
parent: None,
label: "impl E",
navigation_range: 239..240,
node_range: 234..243,
navigation_range: 269..270,
node_range: 264..273,
kind: SymbolKind(
Impl,
),
Expand All @@ -471,8 +496,8 @@ fn g() {}
StructureNode {
parent: None,
label: "impl fmt::Debug for E",
navigation_range: 265..266,
node_range: 245..269,
navigation_range: 295..296,
node_range: 275..299,
kind: SymbolKind(
Impl,
),
Expand All @@ -482,8 +507,8 @@ fn g() {}
StructureNode {
parent: None,
label: "mc",
navigation_range: 284..286,
node_range: 271..303,
navigation_range: 314..316,
node_range: 301..333,
kind: SymbolKind(
Macro,
),
Expand All @@ -493,8 +518,8 @@ fn g() {}
StructureNode {
parent: None,
label: "mcexp",
navigation_range: 334..339,
node_range: 305..356,
navigation_range: 364..369,
node_range: 335..386,
kind: SymbolKind(
Macro,
),
Expand All @@ -504,8 +529,8 @@ fn g() {}
StructureNode {
parent: None,
label: "mcexp",
navigation_range: 387..392,
node_range: 358..409,
navigation_range: 417..422,
node_range: 388..439,
kind: SymbolKind(
Macro,
),
Expand All @@ -515,8 +540,8 @@ fn g() {}
StructureNode {
parent: None,
label: "obsolete",
navigation_range: 428..436,
node_range: 411..441,
navigation_range: 458..466,
node_range: 441..471,
kind: SymbolKind(
Function,
),
Expand All @@ -528,8 +553,8 @@ fn g() {}
StructureNode {
parent: None,
label: "very_obsolete",
navigation_range: 481..494,
node_range: 443..499,
navigation_range: 511..524,
node_range: 473..529,
kind: SymbolKind(
Function,
),
Expand All @@ -541,17 +566,17 @@ fn g() {}
StructureNode {
parent: None,
label: "Some region name",
navigation_range: 501..528,
node_range: 501..528,
navigation_range: 531..558,
node_range: 531..558,
kind: Region,
detail: None,
deprecated: false,
},
StructureNode {
parent: None,
label: "m",
navigation_range: 568..569,
node_range: 543..606,
navigation_range: 598..599,
node_range: 573..636,
kind: SymbolKind(
Module,
),
Expand All @@ -560,22 +585,22 @@ fn g() {}
},
StructureNode {
parent: Some(
20,
22,
),
label: "dontpanic",
navigation_range: 543..563,
node_range: 543..563,
navigation_range: 573..593,
node_range: 573..593,
kind: Region,
detail: None,
deprecated: false,
},
StructureNode {
parent: Some(
20,
22,
),
label: "f",
navigation_range: 575..576,
node_range: 572..581,
navigation_range: 605..606,
node_range: 602..611,
kind: SymbolKind(
Function,
),
Expand All @@ -586,11 +611,11 @@ fn g() {}
},
StructureNode {
parent: Some(
20,
22,
),
label: "g",
navigation_range: 598..599,
node_range: 582..604,
navigation_range: 628..629,
node_range: 612..634,
kind: SymbolKind(
Function,
),
Expand Down
8 changes: 7 additions & 1 deletion crates/ide/src/goto_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,13 @@ trait Foo$0 { }
"#,
);

check(
r#"
trait Foo$0 = ;
//^^^
"#,
);

check(
r#"
mod bar$0 { }
Expand Down Expand Up @@ -1423,7 +1430,6 @@ include!("included.rs$0");
);
}

#[cfg(test)]
mod goto_impl_of_trait_fn {
use super::check;
#[test]
Expand Down
2 changes: 1 addition & 1 deletion crates/ide/src/markup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl Markup {
pub fn as_str(&self) -> &str {
self.text.as_str()
}
pub fn fenced_block(contents: &impl fmt::Display) -> Markup {
pub fn fenced_block(contents: impl fmt::Display) -> Markup {
format!("```rust\n{contents}\n```").into()
}
}
1 change: 1 addition & 0 deletions crates/ide/src/move_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ fn find_ancestors(item: SyntaxElement, direction: Direction, range: TextRange) -
SyntaxKind::MACRO_CALL,
SyntaxKind::TYPE_ALIAS,
SyntaxKind::TRAIT,
SyntaxKind::TRAIT_ALIAS,
SyntaxKind::IMPL,
SyntaxKind::MACRO_DEF,
SyntaxKind::STRUCT,
Expand Down
1 change: 1 addition & 0 deletions crates/ide/src/navigation_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,7 @@ pub(crate) fn description_from_symbol(db: &RootDatabase, symbol: &FileSymbol) ->
ast::Struct(it) => sema.to_def(&it).map(|it| it.display(db).to_string()),
ast::Enum(it) => sema.to_def(&it).map(|it| it.display(db).to_string()),
ast::Trait(it) => sema.to_def(&it).map(|it| it.display(db).to_string()),
ast::TraitAlias(it) => sema.to_def(&it).map(|it| it.display(db).to_string()),
ast::Module(it) => sema.to_def(&it).map(|it| it.display(db).to_string()),
ast::TypeAlias(it) => sema.to_def(&it).map(|it| it.display(db).to_string()),
ast::Const(it) => sema.to_def(&it).map(|it| it.display(db).to_string()),
Expand Down
32 changes: 32 additions & 0 deletions crates/ide/src/references.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1355,6 +1355,38 @@ impl Foo {
);
}

#[test]
fn test_trait_alias() {
check(
r#"
trait Foo {}
trait Bar$0 = Foo where Self: ;
fn foo<T: Bar>(_: impl Bar, _: &dyn Bar) {}
"#,
expect![[r#"
Bar TraitAlias FileId(0) 13..42 19..22
FileId(0) 53..56
FileId(0) 66..69
FileId(0) 79..82
"#]],
);
}

#[test]
fn test_trait_alias_self() {
check(
r#"
trait Foo = where Self$0: ;
"#,
expect![[r#"
Self TypeParam FileId(0) 6..9 6..9
FileId(0) 18..22
"#]],
);
}

#[test]
fn test_attr_differs_from_fn_with_same_name() {
check(
Expand Down
1 change: 1 addition & 0 deletions crates/ide/src/runnables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ fn module_def_doctest(db: &RootDatabase, def: Definition) -> Option<Runnable> {
Definition::Const(it) => it.attrs(db),
Definition::Static(it) => it.attrs(db),
Definition::Trait(it) => it.attrs(db),
Definition::TraitAlias(it) => it.attrs(db),
Definition::TypeAlias(it) => it.attrs(db),
Definition::Macro(it) => it.attrs(db),
Definition::SelfType(it) => it.attrs(db),
Expand Down

0 comments on commit 361f431

Please sign in to comment.