Skip to content

Commit 361f431

Browse files
committed
Support trait aliases in IDE where type support isn't needed
1 parent ebe39e0 commit 361f431

7 files changed

+95
-29
lines changed

crates/ide/src/file_structure.rs

+52-27
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ fn structure_node(node: &SyntaxNode) -> Option<StructureNode> {
149149
ast::Enum(it) => decl(it, StructureNodeKind::SymbolKind(SymbolKind::Enum)),
150150
ast::Variant(it) => decl(it, StructureNodeKind::SymbolKind(SymbolKind::Variant)),
151151
ast::Trait(it) => decl(it, StructureNodeKind::SymbolKind(SymbolKind::Trait)),
152+
ast::TraitAlias(it) => decl(it, StructureNodeKind::SymbolKind(SymbolKind::TraitAlias)),
152153
ast::Module(it) => decl(it, StructureNodeKind::SymbolKind(SymbolKind::Module)),
153154
ast::TypeAlias(it) => decl_with_type_ref(&it, it.ty(), StructureNodeKind::SymbolKind(SymbolKind::TypeAlias)),
154155
ast::RecordField(it) => decl_with_type_ref(&it, it.ty(), StructureNodeKind::SymbolKind(SymbolKind::Field)),
@@ -262,6 +263,8 @@ enum E { X, Y(i32) }
262263
type T = ();
263264
static S: i32 = 92;
264265
const C: i32 = 92;
266+
trait Tr {}
267+
trait Alias = Tr;
265268
266269
impl E {}
267270
@@ -457,11 +460,33 @@ fn g() {}
457460
),
458461
deprecated: false,
459462
},
463+
StructureNode {
464+
parent: None,
465+
label: "Tr",
466+
navigation_range: 239..241,
467+
node_range: 233..244,
468+
kind: SymbolKind(
469+
Trait,
470+
),
471+
detail: None,
472+
deprecated: false,
473+
},
474+
StructureNode {
475+
parent: None,
476+
label: "Alias",
477+
navigation_range: 251..256,
478+
node_range: 245..262,
479+
kind: SymbolKind(
480+
TraitAlias,
481+
),
482+
detail: None,
483+
deprecated: false,
484+
},
460485
StructureNode {
461486
parent: None,
462487
label: "impl E",
463-
navigation_range: 239..240,
464-
node_range: 234..243,
488+
navigation_range: 269..270,
489+
node_range: 264..273,
465490
kind: SymbolKind(
466491
Impl,
467492
),
@@ -471,8 +496,8 @@ fn g() {}
471496
StructureNode {
472497
parent: None,
473498
label: "impl fmt::Debug for E",
474-
navigation_range: 265..266,
475-
node_range: 245..269,
499+
navigation_range: 295..296,
500+
node_range: 275..299,
476501
kind: SymbolKind(
477502
Impl,
478503
),
@@ -482,8 +507,8 @@ fn g() {}
482507
StructureNode {
483508
parent: None,
484509
label: "mc",
485-
navigation_range: 284..286,
486-
node_range: 271..303,
510+
navigation_range: 314..316,
511+
node_range: 301..333,
487512
kind: SymbolKind(
488513
Macro,
489514
),
@@ -493,8 +518,8 @@ fn g() {}
493518
StructureNode {
494519
parent: None,
495520
label: "mcexp",
496-
navigation_range: 334..339,
497-
node_range: 305..356,
521+
navigation_range: 364..369,
522+
node_range: 335..386,
498523
kind: SymbolKind(
499524
Macro,
500525
),
@@ -504,8 +529,8 @@ fn g() {}
504529
StructureNode {
505530
parent: None,
506531
label: "mcexp",
507-
navigation_range: 387..392,
508-
node_range: 358..409,
532+
navigation_range: 417..422,
533+
node_range: 388..439,
509534
kind: SymbolKind(
510535
Macro,
511536
),
@@ -515,8 +540,8 @@ fn g() {}
515540
StructureNode {
516541
parent: None,
517542
label: "obsolete",
518-
navigation_range: 428..436,
519-
node_range: 411..441,
543+
navigation_range: 458..466,
544+
node_range: 441..471,
520545
kind: SymbolKind(
521546
Function,
522547
),
@@ -528,8 +553,8 @@ fn g() {}
528553
StructureNode {
529554
parent: None,
530555
label: "very_obsolete",
531-
navigation_range: 481..494,
532-
node_range: 443..499,
556+
navigation_range: 511..524,
557+
node_range: 473..529,
533558
kind: SymbolKind(
534559
Function,
535560
),
@@ -541,17 +566,17 @@ fn g() {}
541566
StructureNode {
542567
parent: None,
543568
label: "Some region name",
544-
navigation_range: 501..528,
545-
node_range: 501..528,
569+
navigation_range: 531..558,
570+
node_range: 531..558,
546571
kind: Region,
547572
detail: None,
548573
deprecated: false,
549574
},
550575
StructureNode {
551576
parent: None,
552577
label: "m",
553-
navigation_range: 568..569,
554-
node_range: 543..606,
578+
navigation_range: 598..599,
579+
node_range: 573..636,
555580
kind: SymbolKind(
556581
Module,
557582
),
@@ -560,22 +585,22 @@ fn g() {}
560585
},
561586
StructureNode {
562587
parent: Some(
563-
20,
588+
22,
564589
),
565590
label: "dontpanic",
566-
navigation_range: 543..563,
567-
node_range: 543..563,
591+
navigation_range: 573..593,
592+
node_range: 573..593,
568593
kind: Region,
569594
detail: None,
570595
deprecated: false,
571596
},
572597
StructureNode {
573598
parent: Some(
574-
20,
599+
22,
575600
),
576601
label: "f",
577-
navigation_range: 575..576,
578-
node_range: 572..581,
602+
navigation_range: 605..606,
603+
node_range: 602..611,
579604
kind: SymbolKind(
580605
Function,
581606
),
@@ -586,11 +611,11 @@ fn g() {}
586611
},
587612
StructureNode {
588613
parent: Some(
589-
20,
614+
22,
590615
),
591616
label: "g",
592-
navigation_range: 598..599,
593-
node_range: 582..604,
617+
navigation_range: 628..629,
618+
node_range: 612..634,
594619
kind: SymbolKind(
595620
Function,
596621
),

crates/ide/src/goto_definition.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,13 @@ trait Foo$0 { }
764764
"#,
765765
);
766766

767+
check(
768+
r#"
769+
trait Foo$0 = ;
770+
//^^^
771+
"#,
772+
);
773+
767774
check(
768775
r#"
769776
mod bar$0 { }
@@ -1423,7 +1430,6 @@ include!("included.rs$0");
14231430
);
14241431
}
14251432

1426-
#[cfg(test)]
14271433
mod goto_impl_of_trait_fn {
14281434
use super::check;
14291435
#[test]

crates/ide/src/markup.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ impl Markup {
3232
pub fn as_str(&self) -> &str {
3333
self.text.as_str()
3434
}
35-
pub fn fenced_block(contents: &impl fmt::Display) -> Markup {
35+
pub fn fenced_block(contents: impl fmt::Display) -> Markup {
3636
format!("```rust\n{contents}\n```").into()
3737
}
3838
}

crates/ide/src/move_item.rs

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ fn find_ancestors(item: SyntaxElement, direction: Direction, range: TextRange) -
7373
SyntaxKind::MACRO_CALL,
7474
SyntaxKind::TYPE_ALIAS,
7575
SyntaxKind::TRAIT,
76+
SyntaxKind::TRAIT_ALIAS,
7677
SyntaxKind::IMPL,
7778
SyntaxKind::MACRO_DEF,
7879
SyntaxKind::STRUCT,

crates/ide/src/navigation_target.rs

+1
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,7 @@ pub(crate) fn description_from_symbol(db: &RootDatabase, symbol: &FileSymbol) ->
549549
ast::Struct(it) => sema.to_def(&it).map(|it| it.display(db).to_string()),
550550
ast::Enum(it) => sema.to_def(&it).map(|it| it.display(db).to_string()),
551551
ast::Trait(it) => sema.to_def(&it).map(|it| it.display(db).to_string()),
552+
ast::TraitAlias(it) => sema.to_def(&it).map(|it| it.display(db).to_string()),
552553
ast::Module(it) => sema.to_def(&it).map(|it| it.display(db).to_string()),
553554
ast::TypeAlias(it) => sema.to_def(&it).map(|it| it.display(db).to_string()),
554555
ast::Const(it) => sema.to_def(&it).map(|it| it.display(db).to_string()),

crates/ide/src/references.rs

+32
Original file line numberDiff line numberDiff line change
@@ -1355,6 +1355,38 @@ impl Foo {
13551355
);
13561356
}
13571357

1358+
#[test]
1359+
fn test_trait_alias() {
1360+
check(
1361+
r#"
1362+
trait Foo {}
1363+
trait Bar$0 = Foo where Self: ;
1364+
fn foo<T: Bar>(_: impl Bar, _: &dyn Bar) {}
1365+
"#,
1366+
expect![[r#"
1367+
Bar TraitAlias FileId(0) 13..42 19..22
1368+
1369+
FileId(0) 53..56
1370+
FileId(0) 66..69
1371+
FileId(0) 79..82
1372+
"#]],
1373+
);
1374+
}
1375+
1376+
#[test]
1377+
fn test_trait_alias_self() {
1378+
check(
1379+
r#"
1380+
trait Foo = where Self$0: ;
1381+
"#,
1382+
expect![[r#"
1383+
Self TypeParam FileId(0) 6..9 6..9
1384+
1385+
FileId(0) 18..22
1386+
"#]],
1387+
);
1388+
}
1389+
13581390
#[test]
13591391
fn test_attr_differs_from_fn_with_same_name() {
13601392
check(

crates/ide/src/runnables.rs

+1
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ fn module_def_doctest(db: &RootDatabase, def: Definition) -> Option<Runnable> {
416416
Definition::Const(it) => it.attrs(db),
417417
Definition::Static(it) => it.attrs(db),
418418
Definition::Trait(it) => it.attrs(db),
419+
Definition::TraitAlias(it) => it.attrs(db),
419420
Definition::TypeAlias(it) => it.attrs(db),
420421
Definition::Macro(it) => it.attrs(db),
421422
Definition::SelfType(it) => it.attrs(db),

0 commit comments

Comments
 (0)