1 //! FIXME: write short doc here
7 AssocItem, Documentation, FieldSource, HasAttrs, HasSource, HirDisplay, InFile, ModuleSource,
10 base_db::{FileId, FileRange, SourceDatabase},
11 symbol_index::FileSymbolKind,
14 use ide_db::{defs::Definition, RootDatabase};
16 ast::{self, NameOwner},
17 match_ast, AstNode, SmolStr, TextRange,
20 use crate::FileSymbol;
22 use super::short_label::ShortLabel;
24 /// `NavigationTarget` represents and element in the editor's UI which you can
25 /// click on to navigate to a particular piece of code.
27 /// Typically, a `NavigationTarget` corresponds to some element in the source
28 /// code, like a function or a struct, but this is not strictly required.
29 #[derive(Clone, PartialEq, Eq, Hash)]
30 pub struct NavigationTarget {
32 /// Range which encompasses the whole element.
34 /// Should include body, doc comments, attributes, etc.
36 /// Clients should use this range to answer "is the cursor inside the
37 /// element?" question.
38 pub full_range: TextRange,
39 /// A "most interesting" range withing the `full_range`.
41 /// Typically, `full_range` is the whole syntax node, including doc
42 /// comments, and `focus_range` is the range of the identifier. "Most
43 /// interesting" range within the full range, typically the range of
46 /// Clients should place the cursor on this range when navigating to this target.
47 pub focus_range: Option<TextRange>,
49 pub kind: Option<SymbolKind>,
50 pub container_name: Option<SmolStr>,
51 pub description: Option<String>,
52 pub docs: Option<Documentation>,
55 impl fmt::Debug for NavigationTarget {
56 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57 let mut f = f.debug_struct("NavigationTarget");
59 ($($name:ident)*) => {$(
60 if let Some(it) = &self.$name {
61 f.field(stringify!($name), it);
65 f.field("file_id", &self.file_id).field("full_range", &self.full_range);
67 f.field("name", &self.name);
68 opt!(kind container_name description docs);
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 full_range = node.original_file_range(db);
94 .map(|name| src.with_value(name.syntax()).original_file_range(db).range);
95 let mut res = NavigationTarget::from_syntax(
102 res.docs = module.attrs(db).docs();
103 res.description = Some(module.display(db).to_string());
110 pub(crate) fn assert_match(&self, expected: &str) {
111 let actual = self.debug_render();
112 test_utils::assert_eq_text!(expected.trim(), actual.trim(),);
116 pub(crate) fn debug_render(&self) -> String {
117 let mut buf = format!(
124 if let Some(focus_range) = self.focus_range {
125 buf.push_str(&format!(" {:?}", focus_range))
127 if let Some(container_name) = &self.container_name {
128 buf.push_str(&format!(" {}", container_name))
133 /// Allows `NavigationTarget` to be created from a `NameOwner`
134 pub(crate) fn from_named(
136 node: InFile<&dyn ast::NameOwner>,
138 ) -> NavigationTarget {
139 let name = node.value.name().map(|it| it.text().into()).unwrap_or_else(|| "_".into());
141 node.value.name().map(|it| node.with_value(it.syntax()).original_file_range(db).range);
142 let frange = node.map(|it| it.syntax()).original_file_range(db);
144 NavigationTarget::from_syntax(frange.file_id, name, focus_range, frange.range, kind)
150 focus_range: Option<TextRange>,
151 full_range: TextRange,
153 ) -> NavigationTarget {
160 container_name: None,
167 impl ToNav for FileSymbol {
168 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
170 file_id: self.file_id,
171 name: self.name.clone(),
172 kind: Some(match self.kind {
173 FileSymbolKind::Function => SymbolKind::Function,
174 FileSymbolKind::Struct => SymbolKind::Struct,
175 FileSymbolKind::Enum => SymbolKind::Enum,
176 FileSymbolKind::Trait => SymbolKind::Trait,
177 FileSymbolKind::Module => SymbolKind::Module,
178 FileSymbolKind::TypeAlias => SymbolKind::TypeAlias,
179 FileSymbolKind::Const => SymbolKind::Const,
180 FileSymbolKind::Static => SymbolKind::Static,
181 FileSymbolKind::Macro => SymbolKind::Macro,
182 FileSymbolKind::Union => SymbolKind::Union,
184 full_range: self.range,
185 focus_range: self.name_range,
186 container_name: self.container_name.clone(),
187 description: description_from_symbol(db, self),
193 impl TryToNav for Definition {
194 fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
196 Definition::Macro(it) => it.try_to_nav(db),
197 Definition::Field(it) => it.try_to_nav(db),
198 Definition::ModuleDef(it) => it.try_to_nav(db),
199 Definition::SelfType(it) => it.try_to_nav(db),
200 Definition::Local(it) => Some(it.to_nav(db)),
201 Definition::GenericParam(it) => it.try_to_nav(db),
202 Definition::Label(it) => Some(it.to_nav(db)),
207 impl TryToNav for hir::ModuleDef {
208 fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
210 hir::ModuleDef::Module(it) => Some(it.to_nav(db)),
211 hir::ModuleDef::Function(it) => it.try_to_nav(db),
212 hir::ModuleDef::Adt(it) => it.try_to_nav(db),
213 hir::ModuleDef::Variant(it) => it.try_to_nav(db),
214 hir::ModuleDef::Const(it) => it.try_to_nav(db),
215 hir::ModuleDef::Static(it) => it.try_to_nav(db),
216 hir::ModuleDef::Trait(it) => it.try_to_nav(db),
217 hir::ModuleDef::TypeAlias(it) => it.try_to_nav(db),
218 hir::ModuleDef::BuiltinType(_) => 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::Variant {
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;
254 impl<D> TryToNav for D
256 D: HasSource + ToNavFromAst + Copy + HasAttrs + HirDisplay,
257 D::Ast: ast::NameOwner,
259 fn try_to_nav(&self, db: &RootDatabase) -> Option<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 = Some(self.display(db).to_string());
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()))
281 ModuleSource::BlockExpr(node) => (node.syntax(), None),
283 let frange = src.with_value(syntax).original_file_range(db);
284 NavigationTarget::from_syntax(frange.file_id, name, focus, frange.range, SymbolKind::Module)
288 impl TryToNav for hir::Impl {
289 fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
290 let src = self.source(db)?;
291 let derive_attr = self.is_builtin_derive(db);
292 let frange = if let Some(item) = &derive_attr {
293 item.syntax().original_file_range(db)
295 src.as_ref().map(|it| it.syntax()).original_file_range(db)
297 let focus_range = if derive_attr.is_some() {
300 src.value.self_ty().map(|ty| src.with_value(ty.syntax()).original_file_range(db).range)
303 Some(NavigationTarget::from_syntax(
313 impl TryToNav for hir::Field {
314 fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
315 let src = self.source(db)?;
317 let field_source = match &src.value {
318 FieldSource::Named(it) => {
320 NavigationTarget::from_named(db, src.with_value(it), SymbolKind::Field);
321 res.docs = self.docs(db);
322 res.description = Some(self.display(db).to_string());
325 FieldSource::Pos(it) => {
326 let frange = src.with_value(it.syntax()).original_file_range(db);
327 NavigationTarget::from_syntax(
340 impl TryToNav for hir::MacroDef {
341 fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
342 let src = self.source(db)?;
343 log::debug!("nav target {:#?}", src.value.syntax());
344 let mut res = NavigationTarget::from_named(
346 src.as_ref().map(|it| it as &dyn ast::NameOwner),
349 res.docs = self.docs(db);
354 impl TryToNav for hir::Adt {
355 fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
357 hir::Adt::Struct(it) => it.try_to_nav(db),
358 hir::Adt::Union(it) => it.try_to_nav(db),
359 hir::Adt::Enum(it) => it.try_to_nav(db),
364 impl TryToNav for hir::AssocItem {
365 fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
367 AssocItem::Function(it) => it.try_to_nav(db),
368 AssocItem::Const(it) => it.try_to_nav(db),
369 AssocItem::TypeAlias(it) => it.try_to_nav(db),
374 impl TryToNav for hir::GenericParam {
375 fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
377 hir::GenericParam::TypeParam(it) => it.try_to_nav(db),
378 hir::GenericParam::ConstParam(it) => it.try_to_nav(db),
379 hir::GenericParam::LifetimeParam(it) => it.try_to_nav(db),
384 impl ToNav for hir::Local {
385 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
386 let src = self.source(db);
387 let (node, name) = match &src.value {
388 Either::Left(bind_pat) => (bind_pat.syntax().clone(), bind_pat.name()),
389 Either::Right(it) => (it.syntax().clone(), it.name()),
392 name.map(|it| src.with_value(&it.syntax().clone()).original_file_range(db).range);
394 let full_range = src.with_value(&node).original_file_range(db);
395 let name = match self.name(db) {
396 Some(it) => it.to_string().into(),
399 let kind = if self.is_self(db) {
400 SymbolKind::SelfParam
401 } else if self.is_param(db) {
402 SymbolKind::ValueParam
407 file_id: full_range.file_id,
410 full_range: full_range.range,
412 container_name: None,
419 impl ToNav for hir::Label {
420 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
421 let src = self.source(db);
422 let node = src.value.syntax();
423 let FileRange { file_id, range } = src.with_value(node).original_file_range(db);
425 src.value.lifetime().and_then(|lt| lt.lifetime_ident_token()).map(|lt| lt.text_range());
426 let name = self.name(db).to_string().into();
430 kind: Some(SymbolKind::Label),
433 container_name: None,
440 impl TryToNav for hir::TypeParam {
441 fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
442 let src = self.source(db)?;
443 let full_range = match &src.value {
444 Either::Left(it) => it
446 .map_or_else(|| it.syntax().text_range(), |name| name.syntax().text_range()),
447 Either::Right(it) => it.syntax().text_range(),
449 let focus_range = match &src.value {
450 Either::Left(it) => it.name(),
451 Either::Right(it) => it.name(),
453 .map(|it| it.syntax().text_range());
454 Some(NavigationTarget {
455 file_id: src.file_id.original_file(db),
456 name: self.name(db).to_string().into(),
457 kind: Some(SymbolKind::TypeParam),
460 container_name: None,
467 impl TryToNav for hir::LifetimeParam {
468 fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
469 let src = self.source(db)?;
470 let full_range = src.value.syntax().text_range();
471 Some(NavigationTarget {
472 file_id: src.file_id.original_file(db),
473 name: self.name(db).to_string().into(),
474 kind: Some(SymbolKind::LifetimeParam),
476 focus_range: Some(full_range),
477 container_name: None,
484 impl TryToNav for hir::ConstParam {
485 fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
486 let src = self.source(db)?;
487 let full_range = src.value.syntax().text_range();
488 Some(NavigationTarget {
489 file_id: src.file_id.original_file(db),
490 name: self.name(db).to_string().into(),
491 kind: Some(SymbolKind::ConstParam),
493 focus_range: src.value.name().map(|n| n.syntax().text_range()),
494 container_name: None,
501 /// Get a description of a symbol.
503 /// e.g. `struct Name`, `enum Name`, `fn Name`
504 pub(crate) fn description_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> Option<String> {
505 let parse = db.parse(symbol.file_id);
506 let node = symbol.ptr.to_node(parse.tree().syntax());
510 ast::Fn(it) => it.short_label(),
511 ast::Struct(it) => it.short_label(),
512 ast::Enum(it) => it.short_label(),
513 ast::Trait(it) => it.short_label(),
514 ast::Module(it) => it.short_label(),
515 ast::TypeAlias(it) => it.short_label(),
516 ast::Const(it) => it.short_label(),
517 ast::Static(it) => it.short_label(),
518 ast::RecordField(it) => it.short_label(),
519 ast::Variant(it) => it.short_label(),
527 use expect_test::expect;
529 use crate::{fixture, Query};
532 fn test_nav_for_symbol() {
533 let (analysis, _) = fixture::file(
536 fn foo() { enum FooInner { } }
540 let navs = analysis.symbol_search(Query::new("FooInner".to_string())).unwrap();
551 description: "enum FooInner",
561 container_name: "foo",
562 description: "enum FooInner",
566 .assert_debug_eq(&navs);
570 fn test_world_symbols_are_case_sensitive() {
571 let (analysis, _) = fixture::file(
578 let navs = analysis.symbol_search(Query::new("foo".to_string())).unwrap();
579 assert_eq!(navs.len(), 2)