diff options
Diffstat (limited to 'crates/ide')
-rw-r--r-- | crates/ide/src/display.rs | 3 | ||||
-rw-r--r-- | crates/ide/src/display/navigation_target.rs | 30 | ||||
-rw-r--r-- | crates/ide/src/display/short_label.rs | 128 | ||||
-rw-r--r-- | crates/ide/src/references.rs | 23 |
4 files changed, 39 insertions, 145 deletions
diff --git a/crates/ide/src/display.rs b/crates/ide/src/display.rs index 1f7b665c0..71e97d6b4 100644 --- a/crates/ide/src/display.rs +++ b/crates/ide/src/display.rs | |||
@@ -2,8 +2,7 @@ | |||
2 | //! into types that may be used to render in a UI. | 2 | //! into types that may be used to render in a UI. |
3 | 3 | ||
4 | pub(crate) mod navigation_target; | 4 | pub(crate) mod navigation_target; |
5 | mod short_label; | ||
6 | 5 | ||
7 | pub(crate) use navigation_target::{ToNav, TryToNav}; | 6 | pub(crate) use navigation_target::{ToNav, TryToNav}; |
8 | 7 | ||
9 | pub(crate) use syntax::display::{function_declaration, macro_label}; | 8 | pub(crate) use syntax::display::macro_label; |
diff --git a/crates/ide/src/display/navigation_target.rs b/crates/ide/src/display/navigation_target.rs index 69c3751a1..c086de163 100644 --- a/crates/ide/src/display/navigation_target.rs +++ b/crates/ide/src/display/navigation_target.rs | |||
@@ -5,9 +5,10 @@ use std::fmt; | |||
5 | use either::Either; | 5 | use either::Either; |
6 | use hir::{ | 6 | use hir::{ |
7 | AssocItem, Documentation, FieldSource, HasAttrs, HasSource, HirDisplay, InFile, ModuleSource, | 7 | AssocItem, Documentation, FieldSource, HasAttrs, HasSource, HirDisplay, InFile, ModuleSource, |
8 | Semantics, | ||
8 | }; | 9 | }; |
9 | use ide_db::{ | 10 | use ide_db::{ |
10 | base_db::{FileId, FileRange, SourceDatabase}, | 11 | base_db::{FileId, FileRange}, |
11 | symbol_index::FileSymbolKind, | 12 | symbol_index::FileSymbolKind, |
12 | SymbolKind, | 13 | SymbolKind, |
13 | }; | 14 | }; |
@@ -19,8 +20,6 @@ use syntax::{ | |||
19 | 20 | ||
20 | use crate::FileSymbol; | 21 | use crate::FileSymbol; |
21 | 22 | ||
22 | use super::short_label::ShortLabel; | ||
23 | |||
24 | /// `NavigationTarget` represents and element in the editor's UI which you can | 23 | /// `NavigationTarget` represents and element in the editor's UI which you can |
25 | /// click on to navigate to a particular piece of code. | 24 | /// click on to navigate to a particular piece of code. |
26 | /// | 25 | /// |
@@ -502,21 +501,22 @@ impl TryToNav for hir::ConstParam { | |||
502 | /// | 501 | /// |
503 | /// e.g. `struct Name`, `enum Name`, `fn Name` | 502 | /// e.g. `struct Name`, `enum Name`, `fn Name` |
504 | pub(crate) fn description_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> Option<String> { | 503 | pub(crate) fn description_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> Option<String> { |
505 | let parse = db.parse(symbol.file_id); | 504 | let sema = Semantics::new(db); |
506 | let node = symbol.ptr.to_node(parse.tree().syntax()); | 505 | let parse = sema.parse(symbol.file_id); |
506 | let node = symbol.ptr.to_node(parse.syntax()); | ||
507 | 507 | ||
508 | match_ast! { | 508 | match_ast! { |
509 | match node { | 509 | match node { |
510 | ast::Fn(it) => it.short_label(), | 510 | ast::Fn(it) => sema.to_def(&it).map(|it| it.display(db).to_string()), |
511 | ast::Struct(it) => it.short_label(), | 511 | ast::Struct(it) => sema.to_def(&it).map(|it| it.display(db).to_string()), |
512 | ast::Enum(it) => it.short_label(), | 512 | ast::Enum(it) => sema.to_def(&it).map(|it| it.display(db).to_string()), |
513 | ast::Trait(it) => it.short_label(), | 513 | ast::Trait(it) => sema.to_def(&it).map(|it| it.display(db).to_string()), |
514 | ast::Module(it) => it.short_label(), | 514 | ast::Module(it) => sema.to_def(&it).map(|it| it.display(db).to_string()), |
515 | ast::TypeAlias(it) => it.short_label(), | 515 | ast::TypeAlias(it) => sema.to_def(&it).map(|it| it.display(db).to_string()), |
516 | ast::Const(it) => it.short_label(), | 516 | ast::Const(it) => sema.to_def(&it).map(|it| it.display(db).to_string()), |
517 | ast::Static(it) => it.short_label(), | 517 | ast::Static(it) => sema.to_def(&it).map(|it| it.display(db).to_string()), |
518 | ast::RecordField(it) => it.short_label(), | 518 | ast::RecordField(it) => sema.to_def(&it).map(|it| it.display(db).to_string()), |
519 | ast::Variant(it) => it.short_label(), | 519 | ast::Variant(it) => sema.to_def(&it).map(|it| it.display(db).to_string()), |
520 | _ => None, | 520 | _ => None, |
521 | } | 521 | } |
522 | } | 522 | } |
diff --git a/crates/ide/src/display/short_label.rs b/crates/ide/src/display/short_label.rs deleted file mode 100644 index 2df9266b4..000000000 --- a/crates/ide/src/display/short_label.rs +++ /dev/null | |||
@@ -1,128 +0,0 @@ | |||
1 | //! FIXME: write short doc here | ||
2 | |||
3 | use stdx::format_to; | ||
4 | use syntax::ast::{self, AstNode, NameOwner, VisibilityOwner}; | ||
5 | |||
6 | pub(crate) trait ShortLabel { | ||
7 | fn short_label(&self) -> Option<String>; | ||
8 | } | ||
9 | |||
10 | impl ShortLabel for ast::Fn { | ||
11 | fn short_label(&self) -> Option<String> { | ||
12 | Some(crate::display::function_declaration(self)) | ||
13 | } | ||
14 | } | ||
15 | |||
16 | impl ShortLabel for ast::Struct { | ||
17 | fn short_label(&self) -> Option<String> { | ||
18 | short_label_from_node(self, "struct ") | ||
19 | } | ||
20 | } | ||
21 | |||
22 | impl ShortLabel for ast::Union { | ||
23 | fn short_label(&self) -> Option<String> { | ||
24 | short_label_from_node(self, "union ") | ||
25 | } | ||
26 | } | ||
27 | |||
28 | impl ShortLabel for ast::Enum { | ||
29 | fn short_label(&self) -> Option<String> { | ||
30 | short_label_from_node(self, "enum ") | ||
31 | } | ||
32 | } | ||
33 | |||
34 | impl ShortLabel for ast::Trait { | ||
35 | fn short_label(&self) -> Option<String> { | ||
36 | if self.unsafe_token().is_some() { | ||
37 | short_label_from_node(self, "unsafe trait ") | ||
38 | } else { | ||
39 | short_label_from_node(self, "trait ") | ||
40 | } | ||
41 | } | ||
42 | } | ||
43 | |||
44 | impl ShortLabel for ast::Module { | ||
45 | fn short_label(&self) -> Option<String> { | ||
46 | short_label_from_node(self, "mod ") | ||
47 | } | ||
48 | } | ||
49 | |||
50 | impl ShortLabel for ast::SourceFile { | ||
51 | fn short_label(&self) -> Option<String> { | ||
52 | None | ||
53 | } | ||
54 | } | ||
55 | |||
56 | impl ShortLabel for ast::BlockExpr { | ||
57 | fn short_label(&self) -> Option<String> { | ||
58 | None | ||
59 | } | ||
60 | } | ||
61 | |||
62 | impl ShortLabel for ast::TypeAlias { | ||
63 | fn short_label(&self) -> Option<String> { | ||
64 | let mut buf = short_label_from_node(self, "type ")?; | ||
65 | if let Some(type_ref) = self.ty() { | ||
66 | format_to!(buf, " = {}", type_ref.syntax()); | ||
67 | } | ||
68 | Some(buf) | ||
69 | } | ||
70 | } | ||
71 | |||
72 | impl ShortLabel for ast::Const { | ||
73 | fn short_label(&self) -> Option<String> { | ||
74 | short_label_from_ty(self, self.ty(), "const ") | ||
75 | } | ||
76 | } | ||
77 | |||
78 | impl ShortLabel for ast::Static { | ||
79 | fn short_label(&self) -> Option<String> { | ||
80 | short_label_from_ty(self, self.ty(), "static ") | ||
81 | } | ||
82 | } | ||
83 | |||
84 | impl ShortLabel for ast::RecordField { | ||
85 | fn short_label(&self) -> Option<String> { | ||
86 | short_label_from_ty(self, self.ty(), "") | ||
87 | } | ||
88 | } | ||
89 | |||
90 | impl ShortLabel for ast::Variant { | ||
91 | fn short_label(&self) -> Option<String> { | ||
92 | Some(self.name()?.text().to_string()) | ||
93 | } | ||
94 | } | ||
95 | |||
96 | impl ShortLabel for ast::ConstParam { | ||
97 | fn short_label(&self) -> Option<String> { | ||
98 | let mut buf = "const ".to_owned(); | ||
99 | buf.push_str(self.name()?.text()); | ||
100 | if let Some(type_ref) = self.ty() { | ||
101 | format_to!(buf, ": {}", type_ref.syntax()); | ||
102 | } | ||
103 | Some(buf) | ||
104 | } | ||
105 | } | ||
106 | |||
107 | fn short_label_from_ty<T>(node: &T, ty: Option<ast::Type>, prefix: &str) -> Option<String> | ||
108 | where | ||
109 | T: NameOwner + VisibilityOwner, | ||
110 | { | ||
111 | let mut buf = short_label_from_node(node, prefix)?; | ||
112 | |||
113 | if let Some(type_ref) = ty { | ||
114 | format_to!(buf, ": {}", type_ref.syntax()); | ||
115 | } | ||
116 | |||
117 | Some(buf) | ||
118 | } | ||
119 | |||
120 | fn short_label_from_node<T>(node: &T, label: &str) -> Option<String> | ||
121 | where | ||
122 | T: NameOwner + VisibilityOwner, | ||
123 | { | ||
124 | let mut buf = node.visibility().map(|v| format!("{} ", v.syntax())).unwrap_or_default(); | ||
125 | buf.push_str(label); | ||
126 | buf.push_str(node.name()?.text()); | ||
127 | Some(buf) | ||
128 | } | ||
diff --git a/crates/ide/src/references.rs b/crates/ide/src/references.rs index e8a5666bc..379674530 100644 --- a/crates/ide/src/references.rs +++ b/crates/ide/src/references.rs | |||
@@ -1271,4 +1271,27 @@ fn foo(_: bool) -> bo$0ol { true } | |||
1271 | "#]], | 1271 | "#]], |
1272 | ); | 1272 | ); |
1273 | } | 1273 | } |
1274 | |||
1275 | #[test] | ||
1276 | fn test_transitive() { | ||
1277 | check( | ||
1278 | r#" | ||
1279 | //- /level3.rs new_source_root: crate:level3 | ||
1280 | pub struct Fo$0o; | ||
1281 | //- /level2.rs new_source_root: crate:level2 deps:level3 | ||
1282 | pub use level3::Foo; | ||
1283 | //- /level1.rs new_source_root: crate:level1 deps:level2 | ||
1284 | pub use level2::Foo; | ||
1285 | //- /level0.rs new_source_root: crate:level0 deps:level1 | ||
1286 | pub use level1::Foo; | ||
1287 | "#, | ||
1288 | expect![[r#" | ||
1289 | Foo Struct FileId(0) 0..15 11..14 | ||
1290 | |||
1291 | FileId(1) 16..19 | ||
1292 | FileId(2) 16..19 | ||
1293 | FileId(3) 16..19 | ||
1294 | "#]], | ||
1295 | ); | ||
1296 | } | ||
1274 | } | 1297 | } |