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