]> git.lizzy.rs Git - rust.git/blob - crates/ra_ide_api/src/navigation_target.rs
Use the new Resolver API for goto def
[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 }
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         name: Name,
62         ptr: SyntaxNodePtr,
63     ) -> NavigationTarget {
64         NavigationTarget {
65             file_id,
66             name: name.to_string().into(),
67             full_range: ptr.range(),
68             focus_range: None,
69             kind: NAME,
70         }
71     }
72
73     pub(crate) fn from_module(db: &RootDatabase, module: hir::Module) -> NavigationTarget {
74         let (file_id, source) = module.definition_source(db);
75         let name = module
76             .name(db)
77             .map(|it| it.to_string().into())
78             .unwrap_or_default();
79         match source {
80             ModuleSource::SourceFile(node) => {
81                 NavigationTarget::from_syntax(file_id, name, None, node.syntax())
82             }
83             ModuleSource::Module(node) => {
84                 NavigationTarget::from_syntax(file_id, name, None, node.syntax())
85             }
86         }
87     }
88
89     pub(crate) fn from_module_to_decl(db: &RootDatabase, module: hir::Module) -> NavigationTarget {
90         let name = module
91             .name(db)
92             .map(|it| it.to_string().into())
93             .unwrap_or_default();
94         if let Some((file_id, source)) = module.declaration_source(db) {
95             return NavigationTarget::from_syntax(file_id, name, None, source.syntax());
96         }
97         NavigationTarget::from_module(db, module)
98     }
99
100     pub(crate) fn from_function(db: &RootDatabase, func: hir::Function) -> NavigationTarget {
101         let (file_id, fn_def) = func.source(db);
102         NavigationTarget::from_named(file_id.original_file(db), &*fn_def)
103     }
104
105     pub(crate) fn from_field(db: &RootDatabase, field: hir::StructField) -> NavigationTarget {
106         let (file_id, field) = field.source(db);
107         let file_id = file_id.original_file(db);
108         match field {
109             FieldSource::Named(it) => NavigationTarget::from_named(file_id, &*it),
110             FieldSource::Pos(it) => {
111                 NavigationTarget::from_syntax(file_id, "".into(), None, it.syntax())
112             }
113         }
114     }
115
116     pub(crate) fn from_def(db: &RootDatabase, module_def: hir::ModuleDef) -> NavigationTarget {
117         match module_def {
118             hir::ModuleDef::Module(module) => NavigationTarget::from_module(db, module),
119             hir::ModuleDef::Function(func) => NavigationTarget::from_function(db, func),
120             hir::ModuleDef::Struct(s) => {
121                 let (file_id, node) = s.source(db);
122                 NavigationTarget::from_named(file_id.original_file(db), &*node)
123             }
124             hir::ModuleDef::Const(s) => {
125                 let (file_id, node) = s.source(db);
126                 NavigationTarget::from_named(file_id.original_file(db), &*node)
127             }
128             hir::ModuleDef::Static(s) => {
129                 let (file_id, node) = s.source(db);
130                 NavigationTarget::from_named(file_id.original_file(db), &*node)
131             }
132             hir::ModuleDef::Enum(e) => {
133                 let (file_id, node) = e.source(db);
134                 NavigationTarget::from_named(file_id.original_file(db), &*node)
135             }
136             hir::ModuleDef::EnumVariant(var) => {
137                 let (file_id, node) = var.source(db);
138                 NavigationTarget::from_named(file_id.original_file(db), &*node)
139             }
140             hir::ModuleDef::Trait(e) => {
141                 let (file_id, node) = e.source(db);
142                 NavigationTarget::from_named(file_id.original_file(db), &*node)
143             }
144             hir::ModuleDef::Type(e) => {
145                 let (file_id, node) = e.source(db);
146                 NavigationTarget::from_named(file_id.original_file(db), &*node)
147             }
148         }
149     }
150
151     pub(crate) fn from_impl_block(
152         db: &RootDatabase,
153         module: hir::Module,
154         impl_block: &hir::ImplBlock,
155     ) -> NavigationTarget {
156         let (file_id, _) = module.definition_source(db);
157         let node = module.impl_source(db, impl_block.id());
158         NavigationTarget::from_syntax(file_id, "impl".into(), None, node.syntax())
159     }
160
161     #[cfg(test)]
162     pub(crate) fn assert_match(&self, expected: &str) {
163         let actual = self.debug_render();
164         test_utils::assert_eq_text!(expected.trim(), actual.trim(),);
165     }
166
167     #[cfg(test)]
168     pub(crate) fn debug_render(&self) -> String {
169         let mut buf = format!(
170             "{} {:?} {:?} {:?}",
171             self.name(),
172             self.kind(),
173             self.file_id(),
174             self.full_range()
175         );
176         if let Some(focus_range) = self.focus_range() {
177             buf.push_str(&format!(" {:?}", focus_range))
178         }
179         buf
180     }
181
182     fn from_named(file_id: FileId, node: &impl ast::NameOwner) -> NavigationTarget {
183         let name = node.name().map(|it| it.text().clone()).unwrap_or_default();
184         let focus_range = node.name().map(|it| it.syntax().range());
185         NavigationTarget::from_syntax(file_id, name, focus_range, node.syntax())
186     }
187
188     fn from_syntax(
189         file_id: FileId,
190         name: SmolStr,
191         focus_range: Option<TextRange>,
192         node: &SyntaxNode,
193     ) -> NavigationTarget {
194         NavigationTarget {
195             file_id,
196             name,
197             kind: node.kind(),
198             full_range: node.range(),
199             focus_range,
200             // ptr: Some(LocalSyntaxPtr::new(node)),
201         }
202     }
203 }