1 //! FIXME: write short doc here
6 use hir::{AssocItem, Documentation, FieldSource, HasAttrs, HasSource, InFile, ModuleSource};
8 base_db::{FileId, SourceDatabase},
9 symbol_index::FileSymbolKind,
11 use ide_db::{defs::Definition, RootDatabase};
13 ast::{self, NameOwner},
14 match_ast, AstNode, SmolStr, TextRange,
17 use crate::FileSymbol;
19 use super::short_label::ShortLabel;
21 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
42 /// `NavigationTarget` represents and element in the editor's UI which you can
43 /// click on to navigate to a particular piece of code.
45 /// Typically, a `NavigationTarget` corresponds to some element in the source
46 /// code, like a function or a struct, but this is not strictly required.
47 #[derive(Clone, PartialEq, Eq, Hash)]
48 pub struct NavigationTarget {
50 /// Range which encompasses the whole element.
52 /// Should include body, doc comments, attributes, etc.
54 /// Clients should use this range to answer "is the cursor inside the
55 /// element?" question.
56 pub full_range: TextRange,
57 /// A "most interesting" range withing the `full_range`.
59 /// Typically, `full_range` is the whole syntax node, including doc
60 /// comments, and `focus_range` is the range of the identifier. "Most
61 /// interesting" range within the full range, typically the range of
64 /// Clients should place the cursor on this range when navigating to this target.
65 pub focus_range: Option<TextRange>,
67 pub kind: Option<SymbolKind>,
68 pub container_name: Option<SmolStr>,
69 pub description: Option<String>,
70 pub docs: Option<Documentation>,
73 impl fmt::Debug for NavigationTarget {
74 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75 let mut f = f.debug_struct("NavigationTarget");
77 ($($name:ident)*) => {$(
78 if let Some(it) = &self.$name {
79 f.field(stringify!($name), it);
83 f.field("file_id", &self.file_id).field("full_range", &self.full_range);
85 f.field("name", &self.name);
86 opt!(kind container_name description docs);
91 pub(crate) trait ToNav {
92 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget;
95 pub(crate) trait TryToNav {
96 fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget>;
99 impl NavigationTarget {
100 pub fn focus_or_full_range(&self) -> TextRange {
101 self.focus_range.unwrap_or(self.full_range)
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(src) = module.declaration_source(db) {
107 let node = src.as_ref().map(|it| it.syntax());
108 let frange = node.original_file_range(db);
109 let mut res = NavigationTarget::from_syntax(
116 res.docs = module.attrs(db).docs();
117 res.description = src.value.short_label();
124 pub(crate) fn assert_match(&self, expected: &str) {
125 let actual = self.debug_render();
126 test_utils::assert_eq_text!(expected.trim(), actual.trim(),);
130 pub(crate) fn debug_render(&self) -> String {
131 let mut buf = format!(
138 if let Some(focus_range) = self.focus_range {
139 buf.push_str(&format!(" {:?}", focus_range))
141 if let Some(container_name) = &self.container_name {
142 buf.push_str(&format!(" {}", container_name))
147 /// Allows `NavigationTarget` to be created from a `NameOwner`
148 pub(crate) fn from_named(
150 node: InFile<&dyn ast::NameOwner>,
152 ) -> NavigationTarget {
154 node.value.name().map(|it| it.text().clone()).unwrap_or_else(|| SmolStr::new("_"));
156 node.value.name().map(|it| node.with_value(it.syntax()).original_file_range(db).range);
157 let frange = node.map(|it| it.syntax()).original_file_range(db);
159 NavigationTarget::from_syntax(frange.file_id, name, focus_range, frange.range, kind)
165 focus_range: Option<TextRange>,
166 full_range: TextRange,
168 ) -> NavigationTarget {
175 container_name: None,
182 impl ToNav for FileSymbol {
183 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
185 file_id: self.file_id,
186 name: self.name.clone(),
187 kind: Some(match self.kind {
188 FileSymbolKind::Function => SymbolKind::Function,
189 FileSymbolKind::Struct => SymbolKind::Struct,
190 FileSymbolKind::Enum => SymbolKind::Enum,
191 FileSymbolKind::Trait => SymbolKind::Trait,
192 FileSymbolKind::Module => SymbolKind::Module,
193 FileSymbolKind::TypeAlias => SymbolKind::TypeAlias,
194 FileSymbolKind::Const => SymbolKind::Const,
195 FileSymbolKind::Static => SymbolKind::Static,
196 FileSymbolKind::Macro => SymbolKind::Macro,
198 full_range: self.range,
199 focus_range: self.name_range,
200 container_name: self.container_name.clone(),
201 description: description_from_symbol(db, self),
207 impl TryToNav for Definition {
208 fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
210 Definition::Macro(it) => {
211 // FIXME: Currently proc-macro do not have ast-node,
212 // such that it does not have source
213 // more discussion: https://github.com/rust-analyzer/rust-analyzer/issues/6913
214 if it.is_proc_macro() {
219 Definition::Field(it) => Some(it.to_nav(db)),
220 Definition::ModuleDef(it) => it.try_to_nav(db),
221 Definition::SelfType(it) => Some(it.to_nav(db)),
222 Definition::Local(it) => Some(it.to_nav(db)),
223 Definition::TypeParam(it) => Some(it.to_nav(db)),
224 Definition::LifetimeParam(it) => Some(it.to_nav(db)),
229 impl TryToNav for hir::ModuleDef {
230 fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
231 let res = match self {
232 hir::ModuleDef::Module(it) => it.to_nav(db),
233 hir::ModuleDef::Function(it) => it.to_nav(db),
234 hir::ModuleDef::Adt(it) => it.to_nav(db),
235 hir::ModuleDef::EnumVariant(it) => it.to_nav(db),
236 hir::ModuleDef::Const(it) => it.to_nav(db),
237 hir::ModuleDef::Static(it) => it.to_nav(db),
238 hir::ModuleDef::Trait(it) => it.to_nav(db),
239 hir::ModuleDef::TypeAlias(it) => it.to_nav(db),
240 hir::ModuleDef::BuiltinType(_) => return None,
246 pub(crate) trait ToNavFromAst {
247 const KIND: SymbolKind;
249 impl ToNavFromAst for hir::Function {
250 const KIND: SymbolKind = SymbolKind::Function;
252 impl ToNavFromAst for hir::Const {
253 const KIND: SymbolKind = SymbolKind::Const;
255 impl ToNavFromAst for hir::Static {
256 const KIND: SymbolKind = SymbolKind::Static;
258 impl ToNavFromAst for hir::Struct {
259 const KIND: SymbolKind = SymbolKind::Struct;
261 impl ToNavFromAst for hir::Enum {
262 const KIND: SymbolKind = SymbolKind::Enum;
264 impl ToNavFromAst for hir::EnumVariant {
265 const KIND: SymbolKind = SymbolKind::Variant;
267 impl ToNavFromAst for hir::Union {
268 const KIND: SymbolKind = SymbolKind::Union;
270 impl ToNavFromAst for hir::TypeAlias {
271 const KIND: SymbolKind = SymbolKind::TypeAlias;
273 impl ToNavFromAst for hir::Trait {
274 const KIND: SymbolKind = SymbolKind::Trait;
279 D: HasSource + ToNavFromAst + Copy + HasAttrs,
280 D::Ast: ast::NameOwner + ShortLabel,
282 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
283 let src = self.source(db);
284 let mut res = NavigationTarget::from_named(
286 src.as_ref().map(|it| it as &dyn ast::NameOwner),
289 res.docs = self.docs(db);
290 res.description = src.value.short_label();
295 impl ToNav for hir::Module {
296 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
297 let src = self.definition_source(db);
298 let name = self.name(db).map(|it| it.to_string().into()).unwrap_or_default();
299 let (syntax, focus) = match &src.value {
300 ModuleSource::SourceFile(node) => (node.syntax(), None),
301 ModuleSource::Module(node) => {
302 (node.syntax(), node.name().map(|it| it.syntax().text_range()))
305 let frange = src.with_value(syntax).original_file_range(db);
306 NavigationTarget::from_syntax(frange.file_id, name, focus, frange.range, SymbolKind::Module)
310 impl ToNav for hir::Impl {
311 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
312 let src = self.source(db);
313 let derive_attr = self.is_builtin_derive(db);
314 let frange = if let Some(item) = &derive_attr {
315 item.syntax().original_file_range(db)
317 src.as_ref().map(|it| it.syntax()).original_file_range(db)
319 let focus_range = if derive_attr.is_some() {
322 src.value.self_ty().map(|ty| src.with_value(ty.syntax()).original_file_range(db).range)
325 NavigationTarget::from_syntax(
335 impl ToNav for hir::Field {
336 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
337 let src = self.source(db);
340 FieldSource::Named(it) => {
342 NavigationTarget::from_named(db, src.with_value(it), SymbolKind::Field);
343 res.docs = self.docs(db);
344 res.description = it.short_label();
347 FieldSource::Pos(it) => {
348 let frange = src.with_value(it.syntax()).original_file_range(db);
349 NavigationTarget::from_syntax(
361 impl ToNav for hir::MacroDef {
362 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
363 let src = self.source(db);
364 log::debug!("nav target {:#?}", src.value.syntax());
365 let mut res = NavigationTarget::from_named(
367 src.as_ref().map(|it| it as &dyn ast::NameOwner),
370 res.docs = self.docs(db);
375 impl ToNav for hir::Adt {
376 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
378 hir::Adt::Struct(it) => it.to_nav(db),
379 hir::Adt::Union(it) => it.to_nav(db),
380 hir::Adt::Enum(it) => it.to_nav(db),
385 impl ToNav for hir::AssocItem {
386 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
388 AssocItem::Function(it) => it.to_nav(db),
389 AssocItem::Const(it) => it.to_nav(db),
390 AssocItem::TypeAlias(it) => it.to_nav(db),
395 impl ToNav for hir::Local {
396 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
397 let src = self.source(db);
398 let node = match &src.value {
399 Either::Left(bind_pat) => {
400 bind_pat.name().map_or_else(|| bind_pat.syntax().clone(), |it| it.syntax().clone())
402 Either::Right(it) => it.syntax().clone(),
404 let full_range = src.with_value(&node).original_file_range(db);
405 let name = match self.name(db) {
406 Some(it) => it.to_string().into(),
410 file_id: full_range.file_id,
412 kind: Some(SymbolKind::Local),
413 full_range: full_range.range,
415 container_name: None,
422 impl ToNav for hir::TypeParam {
423 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
424 let src = self.source(db);
425 let full_range = match &src.value {
426 Either::Left(it) => it.syntax().text_range(),
427 Either::Right(it) => it.syntax().text_range(),
429 let focus_range = match &src.value {
430 Either::Left(_) => None,
431 Either::Right(it) => it.name().map(|it| it.syntax().text_range()),
434 file_id: src.file_id.original_file(db),
435 name: self.name(db).to_string().into(),
436 kind: Some(SymbolKind::TypeParam),
439 container_name: None,
446 impl ToNav for hir::LifetimeParam {
447 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
448 let src = self.source(db);
449 let full_range = src.value.syntax().text_range();
451 file_id: src.file_id.original_file(db),
452 name: self.name(db).to_string().into(),
453 kind: Some(SymbolKind::LifetimeParam),
455 focus_range: Some(full_range),
456 container_name: None,
463 /// Get a description of a symbol.
465 /// e.g. `struct Name`, `enum Name`, `fn Name`
466 pub(crate) fn description_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> Option<String> {
467 let parse = db.parse(symbol.file_id);
468 let node = symbol.ptr.to_node(parse.tree().syntax());
472 ast::Fn(it) => it.short_label(),
473 ast::Struct(it) => it.short_label(),
474 ast::Enum(it) => it.short_label(),
475 ast::Trait(it) => it.short_label(),
476 ast::Module(it) => it.short_label(),
477 ast::TypeAlias(it) => it.short_label(),
478 ast::Const(it) => it.short_label(),
479 ast::Static(it) => it.short_label(),
480 ast::RecordField(it) => it.short_label(),
481 ast::Variant(it) => it.short_label(),
489 use expect_test::expect;
491 use crate::{fixture, Query};
494 fn test_nav_for_symbol() {
495 let (analysis, _) = fixture::file(
498 fn foo() { enum FooInner { } }
502 let navs = analysis.symbol_search(Query::new("FooInner".to_string())).unwrap();
513 description: "enum FooInner",
523 container_name: "foo",
524 description: "enum FooInner",
528 .assert_debug_eq(&navs);
532 fn test_world_symbols_are_case_sensitive() {
533 let (analysis, _) = fixture::file(
540 let navs = analysis.symbol_search(Query::new("foo".to_string())).unwrap();
541 assert_eq!(navs.len(), 2)