Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 40 additions & 30 deletions crates/ide/src/display/navigation_target.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -35,16 +37,14 @@ pub enum SymbolKind {
TypeAlias,
Trait,
Macro,
// Do we actually need this?
DocTest,
}

/// `NavigationTarget` represents and element in the editor's UI which you can
/// click on to navigate to a particular piece of code.
///
/// 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.
Expand All @@ -64,12 +64,30 @@ pub struct NavigationTarget {
/// Clients should place the cursor on this range when navigating to this target.
pub focus_range: Option<TextRange>,
pub name: SmolStr,
pub kind: SymbolKind,
pub kind: Option<SymbolKind>,
pub container_name: Option<SmolStr>,
pub description: Option<String>,
pub docs: Option<Documentation>,
}

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;
}
Expand Down Expand Up @@ -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))
}
Expand Down Expand Up @@ -146,7 +169,7 @@ impl NavigationTarget {
NavigationTarget {
file_id,
name,
kind,
kind: Some(kind),
full_range,
focus_range,
container_name: None,
Expand All @@ -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,
Expand All @@ -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(),
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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",
},
]
"#]]
Expand Down
2 changes: 1 addition & 1 deletion crates/ide/src/goto_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ fn self_to_nav_target(self_param: ast::SelfParam, file_id: FileId) -> Option<Nav
full_range: self_param.syntax().text_range(),
focus_range: Some(self_token.text_range()),
name: self_token.text().clone(),
kind: SymbolKind::SelfParam,
kind: Some(SymbolKind::SelfParam),
container_name: None,
description: None,
docs: None,
Expand Down
Loading