From 0e3581e8232461baa50191a2c7474a117b649b1b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 18 Dec 2020 21:10:13 +0300 Subject: NavTarget doesn't assume that it points to a symbol --- crates/ide/src/display/navigation_target.rs | 33 +++++++++++++++++------------ 1 file changed, 20 insertions(+), 13 deletions(-) (limited to 'crates/ide/src/display/navigation_target.rs') diff --git a/crates/ide/src/display/navigation_target.rs b/crates/ide/src/display/navigation_target.rs index ac6346b2b..9b9295955 100644 --- a/crates/ide/src/display/navigation_target.rs +++ b/crates/ide/src/display/navigation_target.rs @@ -35,8 +35,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 @@ -64,7 +62,7 @@ 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, @@ -110,8 +108,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 +149,7 @@ impl NavigationTarget { NavigationTarget { file_id, name, - kind, + kind: Some(kind), full_range, focus_range, container_name: None, @@ -161,7 +164,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 +174,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 +389,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 +413,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 +430,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, @@ -488,7 +491,9 @@ fn foo() { enum FooInner { } } 5..13, ), name: "FooInner", - kind: Enum, + kind: Some( + Enum, + ), container_name: None, description: Some( "enum FooInner", @@ -504,7 +509,9 @@ fn foo() { enum FooInner { } } 34..42, ), name: "FooInner", - kind: Enum, + kind: Some( + Enum, + ), container_name: Some( "foo", ), -- cgit v1.2.3 From ade2f5cd124049614801fc821298205006c6bdf4 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 18 Dec 2020 21:26:47 +0300 Subject: Reduce test verbosity --- crates/ide/src/display/navigation_target.rs | 53 +++++++++++++++-------------- 1 file changed, 28 insertions(+), 25 deletions(-) (limited to 'crates/ide/src/display/navigation_target.rs') diff --git a/crates/ide/src/display/navigation_target.rs b/crates/ide/src/display/navigation_target.rs index 9b9295955..cbdd4ecc2 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::{ @@ -42,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. @@ -68,6 +70,24 @@ pub struct NavigationTarget { 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; } @@ -487,38 +507,21 @@ fn foo() { enum FooInner { } } 0, ), full_range: 0..17, - focus_range: Some( - 5..13, - ), + focus_range: 5..13, name: "FooInner", - kind: Some( - Enum, - ), - container_name: None, - description: Some( - "enum FooInner", - ), - docs: None, + kind: Enum, + description: "enum FooInner", }, NavigationTarget { file_id: FileId( 0, ), full_range: 29..46, - focus_range: Some( - 34..42, - ), + focus_range: 34..42, name: "FooInner", - kind: Some( - Enum, - ), - container_name: Some( - "foo", - ), - description: Some( - "enum FooInner", - ), - docs: None, + kind: Enum, + container_name: "foo", + description: "enum FooInner", }, ] "#]] -- cgit v1.2.3