diff --git a/crates/ide/src/display/navigation_target.rs b/crates/ide/src/display/navigation_target.rs index ac6346b2b41..cbdd4ecc2a8 100644 --- a/crates/ide/src/display/navigation_target.rs +++ b/crates/ide/src/display/navigation_target.rs @@ -1,5 +1,7 @@ //! FIXME: write short doc here +use std::fmt; + use either::Either; use hir::{AssocItem, Documentation, FieldSource, HasAttrs, HasSource, InFile, ModuleSource}; use ide_db::{ @@ -35,8 +37,6 @@ pub enum SymbolKind { TypeAlias, Trait, Macro, - // Do we actually need this? - DocTest, } /// `NavigationTarget` represents and element in the editor's UI which you can @@ -44,7 +44,7 @@ pub enum SymbolKind { /// /// 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, PartialEq, Eq, Hash)] +#[derive(Clone, PartialEq, Eq, Hash)] pub struct NavigationTarget { pub file_id: FileId, /// Range which encompasses the whole element. @@ -64,12 +64,30 @@ pub struct NavigationTarget { /// Clients should place the cursor on this range when navigating to this target. pub focus_range: Option, pub name: SmolStr, - pub kind: SymbolKind, + pub kind: Option, pub container_name: Option, pub description: Option, pub docs: Option, } +impl fmt::Debug for NavigationTarget { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let mut f = f.debug_struct("NavigationTarget"); + macro_rules! opt { + ($($name:ident)*) => {$( + if let Some(it) = &self.$name { + f.field(stringify!($name), it); + } + )*} + } + f.field("file_id", &self.file_id).field("full_range", &self.full_range); + opt!(focus_range); + f.field("name", &self.name); + opt!(kind container_name description docs); + f.finish() + } +} + pub(crate) trait ToNav { fn to_nav(&self, db: &RootDatabase) -> NavigationTarget; } @@ -110,8 +128,13 @@ impl NavigationTarget { #[cfg(test)] pub(crate) fn debug_render(&self) -> String { - let mut buf = - format!("{} {:?} {:?} {:?}", self.name, self.kind, self.file_id, self.full_range); + let mut buf = format!( + "{} {:?} {:?} {:?}", + self.name, + self.kind.unwrap(), + self.file_id, + self.full_range + ); if let Some(focus_range) = self.focus_range { buf.push_str(&format!(" {:?}", focus_range)) } @@ -146,7 +169,7 @@ impl NavigationTarget { NavigationTarget { file_id, name, - kind, + kind: Some(kind), full_range, focus_range, container_name: None, @@ -161,7 +184,7 @@ impl ToNav for FileSymbol { NavigationTarget { file_id: self.file_id, name: self.name.clone(), - kind: match self.kind { + kind: Some(match self.kind { FileSymbolKind::Function => SymbolKind::Function, FileSymbolKind::Struct => SymbolKind::Struct, FileSymbolKind::Enum => SymbolKind::Enum, @@ -171,7 +194,7 @@ impl ToNav for FileSymbol { FileSymbolKind::Const => SymbolKind::Const, FileSymbolKind::Static => SymbolKind::Static, FileSymbolKind::Macro => SymbolKind::Macro, - }, + }), full_range: self.range, focus_range: self.name_range, container_name: self.container_name.clone(), @@ -386,7 +409,7 @@ impl ToNav for hir::Local { NavigationTarget { file_id: full_range.file_id, name, - kind: SymbolKind::Local, + kind: Some(SymbolKind::Local), full_range: full_range.range, focus_range: None, container_name: None, @@ -410,7 +433,7 @@ impl ToNav for hir::TypeParam { NavigationTarget { file_id: src.file_id.original_file(db), name: self.name(db).to_string().into(), - kind: SymbolKind::TypeParam, + kind: Some(SymbolKind::TypeParam), full_range, focus_range, container_name: None, @@ -427,7 +450,7 @@ impl ToNav for hir::LifetimeParam { NavigationTarget { file_id: src.file_id.original_file(db), name: self.name(db).to_string().into(), - kind: SymbolKind::LifetimeParam, + kind: Some(SymbolKind::LifetimeParam), full_range, focus_range: Some(full_range), container_name: None, @@ -484,34 +507,21 @@ fn foo() { enum FooInner { } } 0, ), full_range: 0..17, - focus_range: Some( - 5..13, - ), + focus_range: 5..13, name: "FooInner", kind: Enum, - container_name: None, - description: Some( - "enum FooInner", - ), - docs: None, + description: "enum FooInner", }, NavigationTarget { file_id: FileId( 0, ), full_range: 29..46, - focus_range: Some( - 34..42, - ), + focus_range: 34..42, name: "FooInner", kind: Enum, - container_name: Some( - "foo", - ), - description: Some( - "enum FooInner", - ), - docs: None, + container_name: "foo", + description: "enum FooInner", }, ] "#]] diff --git a/crates/ide/src/goto_definition.rs b/crates/ide/src/goto_definition.rs index 5bee69f4bbc..7a12e99652f 100644 --- a/crates/ide/src/goto_definition.rs +++ b/crates/ide/src/goto_definition.rs @@ -86,7 +86,7 @@ fn self_to_nav_target(self_param: ast::SelfParam, file_id: FileId) -> Option