]> git.lizzy.rs Git - rust.git/blob - crates/ra_ide_api/src/navigation_target.rs
actually produce missing def kinds
[rust.git] / crates / ra_ide_api / src / navigation_target.rs
1 use ra_db::{FileId, Cancelable};
2 use ra_syntax::{
3     SyntaxNode, AstNode, SmolStr, TextRange, ast,
4     SyntaxKind::{self, NAME},
5 };
6 use hir::{Def, ModuleSource};
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(
73         db: &RootDatabase,
74         module: hir::Module,
75     ) -> Cancelable<NavigationTarget> {
76         let (file_id, source) = module.definition_source(db)?;
77         let name = module
78             .name(db)?
79             .map(|it| it.to_string().into())
80             .unwrap_or_default();
81         let res = match source {
82             ModuleSource::SourceFile(node) => {
83                 NavigationTarget::from_syntax(file_id, name, None, node.syntax())
84             }
85             ModuleSource::Module(node) => {
86                 NavigationTarget::from_syntax(file_id, name, None, node.syntax())
87             }
88         };
89         Ok(res)
90     }
91
92     // TODO once Def::Item is gone, this should be able to always return a NavigationTarget
93     pub(crate) fn from_def(db: &RootDatabase, def: Def) -> Cancelable<Option<NavigationTarget>> {
94         let res = match def {
95             Def::Struct(s) => {
96                 let (file_id, node) = s.source(db)?;
97                 NavigationTarget::from_named(file_id.original_file(db), &*node)
98             }
99             Def::Enum(e) => {
100                 let (file_id, node) = e.source(db)?;
101                 NavigationTarget::from_named(file_id.original_file(db), &*node)
102             }
103             Def::EnumVariant(ev) => {
104                 let (file_id, node) = ev.source(db)?;
105                 NavigationTarget::from_named(file_id.original_file(db), &*node)
106             }
107             Def::Function(f) => {
108                 let (file_id, node) = f.source(db)?;
109                 NavigationTarget::from_named(file_id.original_file(db), &*node)
110             }
111             Def::Trait(f) => {
112                 let (file_id, node) = f.source(db)?;
113                 NavigationTarget::from_named(file_id.original_file(db), &*node)
114             }
115             Def::Type(f) => {
116                 let (file_id, node) = f.source(db)?;
117                 NavigationTarget::from_named(file_id.original_file(db), &*node)
118             }
119             Def::Static(f) => {
120                 let (file_id, node) = f.source(db)?;
121                 NavigationTarget::from_named(file_id.original_file(db), &*node)
122             }
123             Def::Const(f) => {
124                 let (file_id, node) = f.source(db)?;
125                 NavigationTarget::from_named(file_id.original_file(db), &*node)
126             }
127             Def::Module(m) => NavigationTarget::from_module(db, m)?,
128             Def::Item => return Ok(None),
129         };
130         Ok(Some(res))
131     }
132
133     #[cfg(test)]
134     pub(crate) fn assert_match(&self, expected: &str) {
135         let actual = self.debug_render();
136         test_utils::assert_eq_text!(expected.trim(), actual.trim(),);
137     }
138
139     #[cfg(test)]
140     pub(crate) fn debug_render(&self) -> String {
141         let mut buf = format!(
142             "{} {:?} {:?} {:?}",
143             self.name(),
144             self.kind(),
145             self.file_id(),
146             self.full_range()
147         );
148         if let Some(focus_range) = self.focus_range() {
149             buf.push_str(&format!(" {:?}", focus_range))
150         }
151         buf
152     }
153
154     fn from_named(file_id: FileId, node: &impl ast::NameOwner) -> NavigationTarget {
155         let name = node.name().map(|it| it.text().clone()).unwrap_or_default();
156         let focus_range = node.name().map(|it| it.syntax().range());
157         NavigationTarget::from_syntax(file_id, name, focus_range, node.syntax())
158     }
159
160     fn from_syntax(
161         file_id: FileId,
162         name: SmolStr,
163         focus_range: Option<TextRange>,
164         node: &SyntaxNode,
165     ) -> NavigationTarget {
166         NavigationTarget {
167             file_id,
168             name,
169             kind: node.kind(),
170             full_range: node.range(),
171             focus_range,
172             // ptr: Some(LocalSyntaxPtr::new(node)),
173         }
174     }
175 }