1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
|
use ra_db::{FileId, SourceDatabase};
use ra_syntax::{
SyntaxNode, AstNode, SmolStr, TextRange, AstPtr,
SyntaxKind::{self, NAME},
ast::{self, NameOwner, VisibilityOwner, TypeAscriptionOwner},
algo::visit::{visitor, Visitor},
};
use hir::{ModuleSource, FieldSource, ImplItem};
use crate::{FileSymbol, db::RootDatabase};
/// `NavigationTarget` represents and element in the editor's UI which you can
/// click on to navigate to a particular piece of code.
///
/// Typically, a `NavigationTarget` corresponds to some element in the source
/// code, like a function or a struct, but this is not strictly required.
#[derive(Debug, Clone)]
pub struct NavigationTarget {
file_id: FileId,
name: SmolStr,
kind: SyntaxKind,
full_range: TextRange,
focus_range: Option<TextRange>,
container_name: Option<SmolStr>,
pub(crate) description: Option<String>,
pub(crate) docs: Option<String>,
}
impl NavigationTarget {
/// When `focus_range` is specified, returns it. otherwise
/// returns `full_range`
pub fn range(&self) -> TextRange {
self.focus_range.unwrap_or(self.full_range)
}
pub fn name(&self) -> &SmolStr {
&self.name
}
pub fn container_name(&self) -> Option<&SmolStr> {
self.container_name.as_ref()
}
pub fn kind(&self) -> SyntaxKind {
self.kind
}
pub fn file_id(&self) -> FileId {
self.file_id
}
pub fn full_range(&self) -> TextRange {
self.full_range
}
/// A "most interesting" range withing the `full_range`.
///
/// Typically, `full_range` is the whole syntax node,
/// including doc comments, and `focus_range` is the range of the identifier.
pub fn focus_range(&self) -> Option<TextRange> {
self.focus_range
}
pub(crate) fn from_bind_pat(file_id: FileId, pat: &ast::BindPat) -> NavigationTarget {
NavigationTarget::from_named(file_id, pat)
}
pub(crate) fn from_symbol(db: &RootDatabase, symbol: FileSymbol) -> NavigationTarget {
let file = db.parse(symbol.file_id).tree;
let node = symbol.ptr.to_node(file.syntax()).to_owned();
NavigationTarget {
file_id: symbol.file_id,
name: symbol.name.clone(),
kind: symbol.ptr.kind(),
full_range: symbol.ptr.range(),
focus_range: symbol.name_range,
container_name: symbol.container_name.clone(),
description: description_inner(&node),
docs: docs_inner(&node),
}
}
pub(crate) fn from_pat(
db: &RootDatabase,
file_id: FileId,
pat: AstPtr<ast::Pat>,
) -> NavigationTarget {
let file = db.parse(file_id).tree;
let (name, full_range) = match pat.to_node(file.syntax()).kind() {
ast::PatKind::BindPat(pat) => return NavigationTarget::from_bind_pat(file_id, &pat),
_ => ("_".into(), pat.syntax_node_ptr().range()),
};
let node = pat.to_node(file.syntax()).syntax().to_owned();
NavigationTarget {
file_id,
name,
full_range,
focus_range: None,
kind: NAME,
container_name: None,
description: description_inner(&node),
docs: docs_inner(&node),
}
}
pub(crate) fn from_self_param(
db: &RootDatabase,
file_id: FileId,
par: AstPtr<ast::SelfParam>,
) -> NavigationTarget {
let (name, full_range) = ("self".into(), par.syntax_node_ptr().range());
let file = db.parse(file_id).tree;
let node = par.to_node(file.syntax()).syntax().to_owned();
NavigationTarget {
file_id,
name,
full_range,
focus_range: None,
kind: NAME,
container_name: None,
description: description_inner(&node),
docs: docs_inner(&node),
}
}
pub(crate) fn from_module(db: &RootDatabase, module: hir::Module) -> NavigationTarget {
let (file_id, source) = module.definition_source(db);
let file_id = file_id.as_original_file();
let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default();
match source {
ModuleSource::SourceFile(node) => {
NavigationTarget::from_syntax(file_id, name, None, node.syntax())
}
ModuleSource::Module(node) => {
NavigationTarget::from_syntax(file_id, name, None, node.syntax())
}
}
}
pub(crate) fn from_module_to_decl(db: &RootDatabase, module: hir::Module) -> NavigationTarget {
let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default();
if let Some((file_id, source)) = module.declaration_source(db) {
let file_id = file_id.as_original_file();
return NavigationTarget::from_syntax(file_id, name, None, source.syntax());
}
NavigationTarget::from_module(db, module)
}
pub(crate) fn from_function(db: &RootDatabase, func: hir::Function) -> NavigationTarget {
let (file_id, fn_def) = func.source(db);
NavigationTarget::from_named(file_id.original_file(db), &*fn_def)
}
pub(crate) fn from_field(db: &RootDatabase, field: hir::StructField) -> NavigationTarget {
let (file_id, field) = field.source(db);
let file_id = file_id.original_file(db);
match field {
FieldSource::Named(it) => NavigationTarget::from_named(file_id, &*it),
FieldSource::Pos(it) => {
NavigationTarget::from_syntax(file_id, "".into(), None, it.syntax())
}
}
}
pub(crate) fn from_adt_def(db: &RootDatabase, adt_def: hir::AdtDef) -> NavigationTarget {
match adt_def {
hir::AdtDef::Struct(s) => {
let (file_id, node) = s.source(db);
NavigationTarget::from_named(file_id.original_file(db), &*node)
}
hir::AdtDef::Union(s) => {
let (file_id, node) = s.source(db);
NavigationTarget::from_named(file_id.original_file(db), &*node)
}
hir::AdtDef::Enum(s) => {
let (file_id, node) = s.source(db);
NavigationTarget::from_named(file_id.original_file(db), &*node)
}
}
}
pub(crate) fn from_def(
db: &RootDatabase,
module_def: hir::ModuleDef,
) -> Option<NavigationTarget> {
let nav = match module_def {
hir::ModuleDef::Module(module) => NavigationTarget::from_module(db, module),
hir::ModuleDef::Function(func) => NavigationTarget::from_function(db, func),
hir::ModuleDef::Struct(s) => {
let (file_id, node) = s.source(db);
NavigationTarget::from_named(file_id.original_file(db), &*node)
}
hir::ModuleDef::Union(s) => {
let (file_id, node) = s.source(db);
NavigationTarget::from_named(file_id.original_file(db), &*node)
}
hir::ModuleDef::Const(s) => {
let (file_id, node) = s.source(db);
NavigationTarget::from_named(file_id.original_file(db), &*node)
}
hir::ModuleDef::Static(s) => {
let (file_id, node) = s.source(db);
NavigationTarget::from_named(file_id.original_file(db), &*node)
}
hir::ModuleDef::Enum(e) => {
let (file_id, node) = e.source(db);
NavigationTarget::from_named(file_id.original_file(db), &*node)
}
hir::ModuleDef::EnumVariant(var) => {
let (file_id, node) = var.source(db);
NavigationTarget::from_named(file_id.original_file(db), &*node)
}
hir::ModuleDef::Trait(e) => {
let (file_id, node) = e.source(db);
NavigationTarget::from_named(file_id.original_file(db), &*node)
}
hir::ModuleDef::TypeAlias(e) => {
let (file_id, node) = e.source(db);
NavigationTarget::from_named(file_id.original_file(db), &*node)
}
hir::ModuleDef::BuiltinType(..) => {
return None;
}
};
Some(nav)
}
pub(crate) fn from_impl_block(
db: &RootDatabase,
impl_block: hir::ImplBlock,
) -> NavigationTarget {
let (file_id, node) = impl_block.source(db);
NavigationTarget::from_syntax(
file_id.as_original_file(),
"impl".into(),
None,
node.syntax(),
)
}
pub(crate) fn from_impl_item(db: &RootDatabase, impl_item: hir::ImplItem) -> NavigationTarget {
match impl_item {
ImplItem::Method(f) => NavigationTarget::from_function(db, f),
ImplItem::Const(c) => {
let (file_id, node) = c.source(db);
NavigationTarget::from_named(file_id.original_file(db), &*node)
}
ImplItem::TypeAlias(a) => {
let (file_id, node) = a.source(db);
NavigationTarget::from_named(file_id.original_file(db), &*node)
}
}
}
pub(crate) fn from_macro_def(db: &RootDatabase, macro_call: hir::MacroDef) -> NavigationTarget {
let (file_id, node) = macro_call.source(db);
log::debug!("nav target {}", node.syntax().debug_dump());
NavigationTarget::from_named(file_id.original_file(db), &*node)
}
#[cfg(test)]
pub(crate) fn assert_match(&self, expected: &str) {
let actual = self.debug_render();
test_utils::assert_eq_text!(expected.trim(), actual.trim(),);
}
#[cfg(test)]
pub(crate) fn debug_render(&self) -> String {
let mut buf = format!(
"{} {:?} {:?} {:?}",
self.name(),
self.kind(),
self.file_id(),
self.full_range()
);
if let Some(focus_range) = self.focus_range() {
buf.push_str(&format!(" {:?}", focus_range))
}
if let Some(container_name) = self.container_name() {
buf.push_str(&format!(" {}", container_name))
}
buf
}
/// Allows `NavigationTarget` to be created from a `NameOwner`
pub(crate) fn from_named(file_id: FileId, node: &impl ast::NameOwner) -> NavigationTarget {
//FIXME: use `_` instead of empty string
let name = node.name().map(|it| it.text().clone()).unwrap_or_default();
let focus_range = node.name().map(|it| it.syntax().range());
NavigationTarget::from_syntax(file_id, name, focus_range, node.syntax())
}
fn from_syntax(
file_id: FileId,
name: SmolStr,
focus_range: Option<TextRange>,
node: &SyntaxNode,
) -> NavigationTarget {
NavigationTarget {
file_id,
name,
kind: node.kind(),
full_range: node.range(),
focus_range,
// ptr: Some(LocalSyntaxPtr::new(node)),
container_name: None,
description: description_inner(node),
docs: docs_inner(node),
}
}
}
fn docs_inner(node: &SyntaxNode) -> Option<String> {
fn doc_comments<N: ast::DocCommentsOwner>(node: &N) -> Option<String> {
node.doc_comment_text()
}
visitor()
.visit(doc_comments::<ast::FnDef>)
.visit(doc_comments::<ast::StructDef>)
.visit(doc_comments::<ast::EnumDef>)
.visit(doc_comments::<ast::TraitDef>)
.visit(doc_comments::<ast::Module>)
.visit(doc_comments::<ast::TypeAliasDef>)
.visit(doc_comments::<ast::ConstDef>)
.visit(doc_comments::<ast::StaticDef>)
.visit(doc_comments::<ast::NamedFieldDef>)
.visit(doc_comments::<ast::EnumVariant>)
.visit(doc_comments::<ast::MacroCall>)
.accept(&node)?
}
/// Get a description of this node.
///
/// e.g. `struct Name`, `enum Name`, `fn Name`
fn description_inner(node: &SyntaxNode) -> Option<String> {
// FIXME: After type inference is done, add type information to improve the output
fn visit_ascribed_node<T>(node: &T, prefix: &str) -> Option<String>
where
T: NameOwner + VisibilityOwner + TypeAscriptionOwner,
{
let mut string = visit_node(node, prefix)?;
if let Some(type_ref) = node.ascribed_type() {
string.push_str(": ");
type_ref.syntax().text().push_to(&mut string);
}
Some(string)
}
fn visit_node<T>(node: &T, label: &str) -> Option<String>
where
T: NameOwner + VisibilityOwner,
{
let mut string =
node.visibility().map(|v| format!("{} ", v.syntax().text())).unwrap_or_default();
string.push_str(label);
string.push_str(node.name()?.text().as_str());
Some(string)
}
visitor()
.visit(|node: &ast::FnDef| Some(crate::display::function_label(node)))
.visit(|node: &ast::StructDef| visit_node(node, "struct "))
.visit(|node: &ast::EnumDef| visit_node(node, "enum "))
.visit(|node: &ast::TraitDef| visit_node(node, "trait "))
.visit(|node: &ast::Module| visit_node(node, "mod "))
.visit(|node: &ast::TypeAliasDef| visit_node(node, "type "))
.visit(|node: &ast::ConstDef| visit_ascribed_node(node, "const "))
.visit(|node: &ast::StaticDef| visit_ascribed_node(node, "static "))
.visit(|node: &ast::NamedFieldDef| visit_ascribed_node(node, ""))
.visit(|node: &ast::EnumVariant| Some(node.name()?.text().to_string()))
.accept(&node)?
}
|