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