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