]> git.lizzy.rs Git - rust.git/blob - crates/ra_ide_api/src/navigation_target.rs
Turn ImplBlock into a copy type just containing IDs
[rust.git] / crates / ra_ide_api / src / navigation_target.rs
1 use ra_db::FileId;
2 use ra_syntax::{
3     SyntaxNode, SyntaxNodePtr, AstNode, SmolStr, TextRange, ast,
4     SyntaxKind::{self, NAME},
5 };
6 use hir::{ModuleSource, FieldSource, Name};
7
8 use crate::{FileSymbol, db::RootDatabase};
9
10 /// `NavigationTarget` represents and element in the editor's UI which you can
11 /// click on to navigate to a particular piece of code.
12 ///
13 /// Typically, a `NavigationTarget` corresponds to some element in the source
14 /// code, like a function or a struct, but this is not strictly required.
15 #[derive(Debug, Clone)]
16 pub struct NavigationTarget {
17     file_id: FileId,
18     name: SmolStr,
19     kind: SyntaxKind,
20     full_range: TextRange,
21     focus_range: Option<TextRange>,
22     container_name: Option<SmolStr>,
23 }
24
25 impl NavigationTarget {
26     pub fn name(&self) -> &SmolStr {
27         &self.name
28     }
29
30     pub fn container_name(&self) -> Option<&SmolStr> {
31         self.container_name.as_ref()
32     }
33
34     pub fn kind(&self) -> SyntaxKind {
35         self.kind
36     }
37
38     pub fn file_id(&self) -> FileId {
39         self.file_id
40     }
41
42     pub fn full_range(&self) -> TextRange {
43         self.full_range
44     }
45
46     /// A "most interesting" range withing the `range_full`.
47     ///
48     /// Typically, `range` is the whole syntax node, including doc comments, and
49     /// `focus_range` is the range of the identifier.
50     pub fn focus_range(&self) -> Option<TextRange> {
51         self.focus_range
52     }
53
54     pub(crate) fn from_symbol(symbol: FileSymbol) -> NavigationTarget {
55         NavigationTarget {
56             file_id: symbol.file_id,
57             name: symbol.name.clone(),
58             kind: symbol.ptr.kind(),
59             full_range: symbol.ptr.range(),
60             focus_range: None,
61             container_name: symbol.container_name.clone(),
62         }
63     }
64
65     pub(crate) fn from_scope_entry(
66         file_id: FileId,
67         name: Name,
68         ptr: SyntaxNodePtr,
69     ) -> NavigationTarget {
70         NavigationTarget {
71             file_id,
72             name: name.to_string().into(),
73             full_range: ptr.range(),
74             focus_range: None,
75             kind: NAME,
76             container_name: None,
77         }
78     }
79
80     pub(crate) fn from_module(db: &RootDatabase, module: hir::Module) -> NavigationTarget {
81         let (file_id, source) = module.definition_source(db);
82         let file_id = file_id.as_original_file();
83         let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default();
84         match source {
85             ModuleSource::SourceFile(node) => {
86                 NavigationTarget::from_syntax(file_id, name, None, node.syntax())
87             }
88             ModuleSource::Module(node) => {
89                 NavigationTarget::from_syntax(file_id, name, None, node.syntax())
90             }
91         }
92     }
93
94     pub(crate) fn from_module_to_decl(db: &RootDatabase, module: hir::Module) -> NavigationTarget {
95         let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default();
96         if let Some((file_id, source)) = module.declaration_source(db) {
97             let file_id = file_id.as_original_file();
98             return NavigationTarget::from_syntax(file_id, name, None, source.syntax());
99         }
100         NavigationTarget::from_module(db, module)
101     }
102
103     pub(crate) fn from_function(db: &RootDatabase, func: hir::Function) -> NavigationTarget {
104         let (file_id, fn_def) = func.source(db);
105         NavigationTarget::from_named(file_id.original_file(db), &*fn_def)
106     }
107
108     pub(crate) fn from_field(db: &RootDatabase, field: hir::StructField) -> NavigationTarget {
109         let (file_id, field) = field.source(db);
110         let file_id = file_id.original_file(db);
111         match field {
112             FieldSource::Named(it) => NavigationTarget::from_named(file_id, &*it),
113             FieldSource::Pos(it) => {
114                 NavigationTarget::from_syntax(file_id, "".into(), None, it.syntax())
115             }
116         }
117     }
118
119     pub(crate) fn from_def(db: &RootDatabase, module_def: hir::ModuleDef) -> NavigationTarget {
120         match module_def {
121             hir::ModuleDef::Module(module) => NavigationTarget::from_module(db, module),
122             hir::ModuleDef::Function(func) => NavigationTarget::from_function(db, func),
123             hir::ModuleDef::Struct(s) => {
124                 let (file_id, node) = s.source(db);
125                 NavigationTarget::from_named(file_id.original_file(db), &*node)
126             }
127             hir::ModuleDef::Const(s) => {
128                 let (file_id, node) = s.source(db);
129                 NavigationTarget::from_named(file_id.original_file(db), &*node)
130             }
131             hir::ModuleDef::Static(s) => {
132                 let (file_id, node) = s.source(db);
133                 NavigationTarget::from_named(file_id.original_file(db), &*node)
134             }
135             hir::ModuleDef::Enum(e) => {
136                 let (file_id, node) = e.source(db);
137                 NavigationTarget::from_named(file_id.original_file(db), &*node)
138             }
139             hir::ModuleDef::EnumVariant(var) => {
140                 let (file_id, node) = var.source(db);
141                 NavigationTarget::from_named(file_id.original_file(db), &*node)
142             }
143             hir::ModuleDef::Trait(e) => {
144                 let (file_id, node) = e.source(db);
145                 NavigationTarget::from_named(file_id.original_file(db), &*node)
146             }
147             hir::ModuleDef::Type(e) => {
148                 let (file_id, node) = e.source(db);
149                 NavigationTarget::from_named(file_id.original_file(db), &*node)
150             }
151         }
152     }
153
154     pub(crate) fn from_impl_block(
155         db: &RootDatabase,
156         impl_block: hir::ImplBlock,
157     ) -> NavigationTarget {
158         let (file_id, node) = impl_block.source(db);
159         NavigationTarget::from_syntax(
160             file_id.as_original_file(),
161             "impl".into(),
162             None,
163             node.syntax(),
164         )
165     }
166
167     #[cfg(test)]
168     pub(crate) fn assert_match(&self, expected: &str) {
169         let actual = self.debug_render();
170         test_utils::assert_eq_text!(expected.trim(), actual.trim(),);
171     }
172
173     #[cfg(test)]
174     pub(crate) fn debug_render(&self) -> String {
175         let mut buf = format!(
176             "{} {:?} {:?} {:?}",
177             self.name(),
178             self.kind(),
179             self.file_id(),
180             self.full_range()
181         );
182         if let Some(focus_range) = self.focus_range() {
183             buf.push_str(&format!(" {:?}", focus_range))
184         }
185         if let Some(container_name) = self.container_name() {
186             buf.push_str(&format!(" {:?}", container_name))
187         }
188         buf
189     }
190
191     fn from_named(file_id: FileId, node: &impl ast::NameOwner) -> NavigationTarget {
192         let name = node.name().map(|it| it.text().clone()).unwrap_or_default();
193         let focus_range = node.name().map(|it| it.syntax().range());
194         NavigationTarget::from_syntax(file_id, name, focus_range, node.syntax())
195     }
196
197     fn from_syntax(
198         file_id: FileId,
199         name: SmolStr,
200         focus_range: Option<TextRange>,
201         node: &SyntaxNode,
202     ) -> NavigationTarget {
203         NavigationTarget {
204             file_id,
205             name,
206             kind: node.kind(),
207             full_range: node.range(),
208             focus_range,
209             // ptr: Some(LocalSyntaxPtr::new(node)),
210             container_name: None,
211         }
212     }
213 }