aboutsummaryrefslogtreecommitdiff
path: root/crates/ide/src/display/navigation_target.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide/src/display/navigation_target.rs')
-rw-r--r--crates/ide/src/display/navigation_target.rs70
1 files changed, 40 insertions, 30 deletions
diff --git a/crates/ide/src/display/navigation_target.rs b/crates/ide/src/display/navigation_target.rs
index ac6346b2b..cbdd4ecc2 100644
--- a/crates/ide/src/display/navigation_target.rs
+++ b/crates/ide/src/display/navigation_target.rs
@@ -1,5 +1,7 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2 2
3use std::fmt;
4
3use either::Either; 5use either::Either;
4use hir::{AssocItem, Documentation, FieldSource, HasAttrs, HasSource, InFile, ModuleSource}; 6use hir::{AssocItem, Documentation, FieldSource, HasAttrs, HasSource, InFile, ModuleSource};
5use ide_db::{ 7use ide_db::{
@@ -35,8 +37,6 @@ pub enum SymbolKind {
35 TypeAlias, 37 TypeAlias,
36 Trait, 38 Trait,
37 Macro, 39 Macro,
38 // Do we actually need this?
39 DocTest,
40} 40}
41 41
42/// `NavigationTarget` represents and element in the editor's UI which you can 42/// `NavigationTarget` represents and element in the editor's UI which you can
@@ -44,7 +44,7 @@ pub enum SymbolKind {
44/// 44///
45/// Typically, a `NavigationTarget` corresponds to some element in the source 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. 46/// code, like a function or a struct, but this is not strictly required.
47#[derive(Debug, Clone, PartialEq, Eq, Hash)] 47#[derive(Clone, PartialEq, Eq, Hash)]
48pub struct NavigationTarget { 48pub struct NavigationTarget {
49 pub file_id: FileId, 49 pub file_id: FileId,
50 /// Range which encompasses the whole element. 50 /// Range which encompasses the whole element.
@@ -64,12 +64,30 @@ pub struct NavigationTarget {
64 /// Clients should place the cursor on this range when navigating to this target. 64 /// Clients should place the cursor on this range when navigating to this target.
65 pub focus_range: Option<TextRange>, 65 pub focus_range: Option<TextRange>,
66 pub name: SmolStr, 66 pub name: SmolStr,
67 pub kind: SymbolKind, 67 pub kind: Option<SymbolKind>,
68 pub container_name: Option<SmolStr>, 68 pub container_name: Option<SmolStr>,
69 pub description: Option<String>, 69 pub description: Option<String>,
70 pub docs: Option<Documentation>, 70 pub docs: Option<Documentation>,
71} 71}
72 72
73impl fmt::Debug for NavigationTarget {
74 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75 let mut f = f.debug_struct("NavigationTarget");
76 macro_rules! opt {
77 ($($name:ident)*) => {$(
78 if let Some(it) = &self.$name {
79 f.field(stringify!($name), it);
80 }
81 )*}
82 }
83 f.field("file_id", &self.file_id).field("full_range", &self.full_range);
84 opt!(focus_range);
85 f.field("name", &self.name);
86 opt!(kind container_name description docs);
87 f.finish()
88 }
89}
90
73pub(crate) trait ToNav { 91pub(crate) trait ToNav {
74 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget; 92 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget;
75} 93}
@@ -110,8 +128,13 @@ impl NavigationTarget {
110 128
111 #[cfg(test)] 129 #[cfg(test)]
112 pub(crate) fn debug_render(&self) -> String { 130 pub(crate) fn debug_render(&self) -> String {
113 let mut buf = 131 let mut buf = format!(
114 format!("{} {:?} {:?} {:?}", self.name, self.kind, self.file_id, self.full_range); 132 "{} {:?} {:?} {:?}",
133 self.name,
134 self.kind.unwrap(),
135 self.file_id,
136 self.full_range
137 );
115 if let Some(focus_range) = self.focus_range { 138 if let Some(focus_range) = self.focus_range {
116 buf.push_str(&format!(" {:?}", focus_range)) 139 buf.push_str(&format!(" {:?}", focus_range))
117 } 140 }
@@ -146,7 +169,7 @@ impl NavigationTarget {
146 NavigationTarget { 169 NavigationTarget {
147 file_id, 170 file_id,
148 name, 171 name,
149 kind, 172 kind: Some(kind),
150 full_range, 173 full_range,
151 focus_range, 174 focus_range,
152 container_name: None, 175 container_name: None,
@@ -161,7 +184,7 @@ impl ToNav for FileSymbol {
161 NavigationTarget { 184 NavigationTarget {
162 file_id: self.file_id, 185 file_id: self.file_id,
163 name: self.name.clone(), 186 name: self.name.clone(),
164 kind: match self.kind { 187 kind: Some(match self.kind {
165 FileSymbolKind::Function => SymbolKind::Function, 188 FileSymbolKind::Function => SymbolKind::Function,
166 FileSymbolKind::Struct => SymbolKind::Struct, 189 FileSymbolKind::Struct => SymbolKind::Struct,
167 FileSymbolKind::Enum => SymbolKind::Enum, 190 FileSymbolKind::Enum => SymbolKind::Enum,
@@ -171,7 +194,7 @@ impl ToNav for FileSymbol {
171 FileSymbolKind::Const => SymbolKind::Const, 194 FileSymbolKind::Const => SymbolKind::Const,
172 FileSymbolKind::Static => SymbolKind::Static, 195 FileSymbolKind::Static => SymbolKind::Static,
173 FileSymbolKind::Macro => SymbolKind::Macro, 196 FileSymbolKind::Macro => SymbolKind::Macro,
174 }, 197 }),
175 full_range: self.range, 198 full_range: self.range,
176 focus_range: self.name_range, 199 focus_range: self.name_range,
177 container_name: self.container_name.clone(), 200 container_name: self.container_name.clone(),
@@ -386,7 +409,7 @@ impl ToNav for hir::Local {
386 NavigationTarget { 409 NavigationTarget {
387 file_id: full_range.file_id, 410 file_id: full_range.file_id,
388 name, 411 name,
389 kind: SymbolKind::Local, 412 kind: Some(SymbolKind::Local),
390 full_range: full_range.range, 413 full_range: full_range.range,
391 focus_range: None, 414 focus_range: None,
392 container_name: None, 415 container_name: None,
@@ -410,7 +433,7 @@ impl ToNav for hir::TypeParam {
410 NavigationTarget { 433 NavigationTarget {
411 file_id: src.file_id.original_file(db), 434 file_id: src.file_id.original_file(db),
412 name: self.name(db).to_string().into(), 435 name: self.name(db).to_string().into(),
413 kind: SymbolKind::TypeParam, 436 kind: Some(SymbolKind::TypeParam),
414 full_range, 437 full_range,
415 focus_range, 438 focus_range,
416 container_name: None, 439 container_name: None,
@@ -427,7 +450,7 @@ impl ToNav for hir::LifetimeParam {
427 NavigationTarget { 450 NavigationTarget {
428 file_id: src.file_id.original_file(db), 451 file_id: src.file_id.original_file(db),
429 name: self.name(db).to_string().into(), 452 name: self.name(db).to_string().into(),
430 kind: SymbolKind::LifetimeParam, 453 kind: Some(SymbolKind::LifetimeParam),
431 full_range, 454 full_range,
432 focus_range: Some(full_range), 455 focus_range: Some(full_range),
433 container_name: None, 456 container_name: None,
@@ -484,34 +507,21 @@ fn foo() { enum FooInner { } }
484 0, 507 0,
485 ), 508 ),
486 full_range: 0..17, 509 full_range: 0..17,
487 focus_range: Some( 510 focus_range: 5..13,
488 5..13,
489 ),
490 name: "FooInner", 511 name: "FooInner",
491 kind: Enum, 512 kind: Enum,
492 container_name: None, 513 description: "enum FooInner",
493 description: Some(
494 "enum FooInner",
495 ),
496 docs: None,
497 }, 514 },
498 NavigationTarget { 515 NavigationTarget {
499 file_id: FileId( 516 file_id: FileId(
500 0, 517 0,
501 ), 518 ),
502 full_range: 29..46, 519 full_range: 29..46,
503 focus_range: Some( 520 focus_range: 34..42,
504 34..42,
505 ),
506 name: "FooInner", 521 name: "FooInner",
507 kind: Enum, 522 kind: Enum,
508 container_name: Some( 523 container_name: "foo",
509 "foo", 524 description: "enum FooInner",
510 ),
511 description: Some(
512 "enum FooInner",
513 ),
514 docs: None,
515 }, 525 },
516 ] 526 ]
517 "#]] 527 "#]]