1 //! FIXME: write short doc here
4 use hir::{AssocItem, Documentation, FieldSource, HasAttrs, HasSource, InFile, ModuleSource};
6 base_db::{FileId, SourceDatabase},
7 symbol_index::FileSymbolKind,
9 use ide_db::{defs::Definition, RootDatabase};
11 ast::{self, NameOwner},
12 match_ast, AstNode, SmolStr, TextRange,
15 use crate::FileSymbol;
17 use super::short_label::ShortLabel;
19 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
38 // Do we actually need this?
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(Debug, 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>,
68 pub container_name: Option<SmolStr>,
69 pub description: Option<String>,
70 pub docs: Option<Documentation>,
73 pub(crate) trait ToNav {
74 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget;
77 pub(crate) trait TryToNav {
78 fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget>;
81 impl NavigationTarget {
82 pub fn focus_or_full_range(&self) -> TextRange {
83 self.focus_range.unwrap_or(self.full_range)
86 pub(crate) fn from_module_to_decl(db: &RootDatabase, module: hir::Module) -> NavigationTarget {
87 let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default();
88 if let Some(src) = module.declaration_source(db) {
89 let node = src.as_ref().map(|it| it.syntax());
90 let frange = node.original_file_range(db);
91 let mut res = NavigationTarget::from_syntax(
98 res.docs = module.attrs(db).docs();
99 res.description = src.value.short_label();
106 pub(crate) fn assert_match(&self, expected: &str) {
107 let actual = self.debug_render();
108 test_utils::assert_eq_text!(expected.trim(), actual.trim(),);
112 pub(crate) fn debug_render(&self) -> String {
114 format!("{} {:?} {:?} {:?}", self.name, self.kind, self.file_id, self.full_range);
115 if let Some(focus_range) = self.focus_range {
116 buf.push_str(&format!(" {:?}", focus_range))
118 if let Some(container_name) = &self.container_name {
119 buf.push_str(&format!(" {}", container_name))
124 /// Allows `NavigationTarget` to be created from a `NameOwner`
125 pub(crate) fn from_named(
127 node: InFile<&dyn ast::NameOwner>,
129 ) -> NavigationTarget {
131 node.value.name().map(|it| it.text().clone()).unwrap_or_else(|| SmolStr::new("_"));
133 node.value.name().map(|it| node.with_value(it.syntax()).original_file_range(db).range);
134 let frange = node.map(|it| it.syntax()).original_file_range(db);
136 NavigationTarget::from_syntax(frange.file_id, name, focus_range, frange.range, kind)
142 focus_range: Option<TextRange>,
143 full_range: TextRange,
145 ) -> NavigationTarget {
152 container_name: None,
159 impl ToNav for FileSymbol {
160 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
162 file_id: self.file_id,
163 name: self.name.clone(),
164 kind: match self.kind {
165 FileSymbolKind::Function => SymbolKind::Function,
166 FileSymbolKind::Struct => SymbolKind::Struct,
167 FileSymbolKind::Enum => SymbolKind::Enum,
168 FileSymbolKind::Trait => SymbolKind::Trait,
169 FileSymbolKind::Module => SymbolKind::Module,
170 FileSymbolKind::TypeAlias => SymbolKind::TypeAlias,
171 FileSymbolKind::Const => SymbolKind::Const,
172 FileSymbolKind::Static => SymbolKind::Static,
173 FileSymbolKind::Macro => SymbolKind::Macro,
175 full_range: self.range,
176 focus_range: self.name_range,
177 container_name: self.container_name.clone(),
178 description: description_from_symbol(db, self),
184 impl TryToNav for Definition {
185 fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
187 Definition::Macro(it) => {
188 // FIXME: Currently proc-macro do not have ast-node,
189 // such that it does not have source
190 // more discussion: https://github.com/rust-analyzer/rust-analyzer/issues/6913
191 if it.is_proc_macro() {
196 Definition::Field(it) => Some(it.to_nav(db)),
197 Definition::ModuleDef(it) => it.try_to_nav(db),
198 Definition::SelfType(it) => Some(it.to_nav(db)),
199 Definition::Local(it) => Some(it.to_nav(db)),
200 Definition::TypeParam(it) => Some(it.to_nav(db)),
201 Definition::LifetimeParam(it) => Some(it.to_nav(db)),
206 impl TryToNav for hir::ModuleDef {
207 fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
208 let res = match self {
209 hir::ModuleDef::Module(it) => it.to_nav(db),
210 hir::ModuleDef::Function(it) => it.to_nav(db),
211 hir::ModuleDef::Adt(it) => it.to_nav(db),
212 hir::ModuleDef::EnumVariant(it) => it.to_nav(db),
213 hir::ModuleDef::Const(it) => it.to_nav(db),
214 hir::ModuleDef::Static(it) => it.to_nav(db),
215 hir::ModuleDef::Trait(it) => it.to_nav(db),
216 hir::ModuleDef::TypeAlias(it) => it.to_nav(db),
217 hir::ModuleDef::BuiltinType(_) => return None,
223 pub(crate) trait ToNavFromAst {
224 const KIND: SymbolKind;
226 impl ToNavFromAst for hir::Function {
227 const KIND: SymbolKind = SymbolKind::Function;
229 impl ToNavFromAst for hir::Const {
230 const KIND: SymbolKind = SymbolKind::Const;
232 impl ToNavFromAst for hir::Static {
233 const KIND: SymbolKind = SymbolKind::Static;
235 impl ToNavFromAst for hir::Struct {
236 const KIND: SymbolKind = SymbolKind::Struct;
238 impl ToNavFromAst for hir::Enum {
239 const KIND: SymbolKind = SymbolKind::Enum;
241 impl ToNavFromAst for hir::EnumVariant {
242 const KIND: SymbolKind = SymbolKind::Variant;
244 impl ToNavFromAst for hir::Union {
245 const KIND: SymbolKind = SymbolKind::Union;
247 impl ToNavFromAst for hir::TypeAlias {
248 const KIND: SymbolKind = SymbolKind::TypeAlias;
250 impl ToNavFromAst for hir::Trait {
251 const KIND: SymbolKind = SymbolKind::Trait;
256 D: HasSource + ToNavFromAst + Copy + HasAttrs,
257 D::Ast: ast::NameOwner + ShortLabel,
259 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
260 let src = self.source(db);
261 let mut res = NavigationTarget::from_named(
263 src.as_ref().map(|it| it as &dyn ast::NameOwner),
266 res.docs = self.docs(db);
267 res.description = src.value.short_label();
272 impl ToNav for hir::Module {
273 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
274 let src = self.definition_source(db);
275 let name = self.name(db).map(|it| it.to_string().into()).unwrap_or_default();
276 let (syntax, focus) = match &src.value {
277 ModuleSource::SourceFile(node) => (node.syntax(), None),
278 ModuleSource::Module(node) => {
279 (node.syntax(), node.name().map(|it| it.syntax().text_range()))
282 let frange = src.with_value(syntax).original_file_range(db);
283 NavigationTarget::from_syntax(frange.file_id, name, focus, frange.range, SymbolKind::Module)
287 impl ToNav for hir::Impl {
288 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
289 let src = self.source(db);
290 let derive_attr = self.is_builtin_derive(db);
291 let frange = if let Some(item) = &derive_attr {
292 item.syntax().original_file_range(db)
294 src.as_ref().map(|it| it.syntax()).original_file_range(db)
296 let focus_range = if derive_attr.is_some() {
299 src.value.self_ty().map(|ty| src.with_value(ty.syntax()).original_file_range(db).range)
302 NavigationTarget::from_syntax(
312 impl ToNav for hir::Field {
313 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
314 let src = self.source(db);
317 FieldSource::Named(it) => {
319 NavigationTarget::from_named(db, src.with_value(it), SymbolKind::Field);
320 res.docs = self.docs(db);
321 res.description = it.short_label();
324 FieldSource::Pos(it) => {
325 let frange = src.with_value(it.syntax()).original_file_range(db);
326 NavigationTarget::from_syntax(
338 impl ToNav for hir::MacroDef {
339 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
340 let src = self.source(db);
341 log::debug!("nav target {:#?}", src.value.syntax());
342 let mut res = NavigationTarget::from_named(
344 src.as_ref().map(|it| it as &dyn ast::NameOwner),
347 res.docs = self.docs(db);
352 impl ToNav for hir::Adt {
353 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
355 hir::Adt::Struct(it) => it.to_nav(db),
356 hir::Adt::Union(it) => it.to_nav(db),
357 hir::Adt::Enum(it) => it.to_nav(db),
362 impl ToNav for hir::AssocItem {
363 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
365 AssocItem::Function(it) => it.to_nav(db),
366 AssocItem::Const(it) => it.to_nav(db),
367 AssocItem::TypeAlias(it) => it.to_nav(db),
372 impl ToNav for hir::Local {
373 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
374 let src = self.source(db);
375 let node = match &src.value {
376 Either::Left(bind_pat) => {
377 bind_pat.name().map_or_else(|| bind_pat.syntax().clone(), |it| it.syntax().clone())
379 Either::Right(it) => it.syntax().clone(),
381 let full_range = src.with_value(&node).original_file_range(db);
382 let name = match self.name(db) {
383 Some(it) => it.to_string().into(),
387 file_id: full_range.file_id,
389 kind: SymbolKind::Local,
390 full_range: full_range.range,
392 container_name: None,
399 impl ToNav for hir::TypeParam {
400 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
401 let src = self.source(db);
402 let full_range = match &src.value {
403 Either::Left(it) => it.syntax().text_range(),
404 Either::Right(it) => it.syntax().text_range(),
406 let focus_range = match &src.value {
407 Either::Left(_) => None,
408 Either::Right(it) => it.name().map(|it| it.syntax().text_range()),
411 file_id: src.file_id.original_file(db),
412 name: self.name(db).to_string().into(),
413 kind: SymbolKind::TypeParam,
416 container_name: None,
423 impl ToNav for hir::LifetimeParam {
424 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
425 let src = self.source(db);
426 let full_range = src.value.syntax().text_range();
428 file_id: src.file_id.original_file(db),
429 name: self.name(db).to_string().into(),
430 kind: SymbolKind::LifetimeParam,
432 focus_range: Some(full_range),
433 container_name: None,
440 /// Get a description of a symbol.
442 /// e.g. `struct Name`, `enum Name`, `fn Name`
443 pub(crate) fn description_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> Option<String> {
444 let parse = db.parse(symbol.file_id);
445 let node = symbol.ptr.to_node(parse.tree().syntax());
449 ast::Fn(it) => it.short_label(),
450 ast::Struct(it) => it.short_label(),
451 ast::Enum(it) => it.short_label(),
452 ast::Trait(it) => it.short_label(),
453 ast::Module(it) => it.short_label(),
454 ast::TypeAlias(it) => it.short_label(),
455 ast::Const(it) => it.short_label(),
456 ast::Static(it) => it.short_label(),
457 ast::RecordField(it) => it.short_label(),
458 ast::Variant(it) => it.short_label(),
466 use expect_test::expect;
468 use crate::{fixture, Query};
471 fn test_nav_for_symbol() {
472 let (analysis, _) = fixture::file(
475 fn foo() { enum FooInner { } }
479 let navs = analysis.symbol_search(Query::new("FooInner".to_string())).unwrap();
492 container_name: None,
508 container_name: Some(
518 .assert_debug_eq(&navs);
522 fn test_world_symbols_are_case_sensitive() {
523 let (analysis, _) = fixture::file(
530 let navs = analysis.symbol_search(Query::new("foo".to_string())).unwrap();
531 assert_eq!(navs.len(), 2)