2021-05-22 08:53:47 -05:00
|
|
|
//! See [`NavigationTarget`].
|
2019-09-30 03:58:53 -05:00
|
|
|
|
2020-12-18 12:26:47 -06:00
|
|
|
use std::fmt;
|
|
|
|
|
2019-12-03 10:07:56 -06:00
|
|
|
use either::Either;
|
2021-03-15 11:58:42 -05:00
|
|
|
use hir::{
|
2022-01-12 12:56:47 -06:00
|
|
|
symbols::FileSymbol, AssocItem, Documentation, FieldSource, HasAttrs, HasSource, HirDisplay,
|
|
|
|
InFile, ModuleSource, Semantics,
|
2021-03-15 11:58:42 -05:00
|
|
|
};
|
2020-12-17 05:29:05 -06:00
|
|
|
use ide_db::{
|
2021-03-16 09:44:31 -05:00
|
|
|
base_db::{FileId, FileRange},
|
2021-01-20 08:25:34 -06:00
|
|
|
SymbolKind,
|
2020-12-17 05:29:05 -06:00
|
|
|
};
|
2020-08-13 09:39:16 -05:00
|
|
|
use ide_db::{defs::Definition, RootDatabase};
|
2021-12-29 07:35:59 -06:00
|
|
|
use stdx::never;
|
2020-08-12 11:26:51 -05:00
|
|
|
use syntax::{
|
2021-09-27 05:54:24 -05:00
|
|
|
ast::{self, HasName},
|
2022-01-31 06:56:14 -06:00
|
|
|
match_ast, AstNode, SmolStr, SyntaxNode, TextRange,
|
2019-01-11 04:28:59 -06:00
|
|
|
};
|
2019-01-11 04:01:35 -06:00
|
|
|
|
2021-04-25 15:28:38 -05:00
|
|
|
/// `NavigationTarget` represents an element in the editor's UI which you can
|
2019-01-11 05:00:54 -06:00
|
|
|
/// 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.
|
2020-12-18 12:26:47 -06:00
|
|
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
2019-01-11 05:00:54 -06:00
|
|
|
pub struct NavigationTarget {
|
2020-07-17 05:42:48 -05:00
|
|
|
pub file_id: FileId,
|
|
|
|
/// Range which encompasses the whole element.
|
|
|
|
///
|
|
|
|
/// Should include body, doc comments, attributes, etc.
|
|
|
|
///
|
|
|
|
/// Clients should use this range to answer "is the cursor inside the
|
|
|
|
/// element?" question.
|
|
|
|
pub full_range: TextRange,
|
2021-04-25 15:28:38 -05:00
|
|
|
/// A "most interesting" range within the `full_range`.
|
2020-07-17 05:42:48 -05:00
|
|
|
///
|
|
|
|
/// Typically, `full_range` is the whole syntax node, including doc
|
2021-04-25 15:28:38 -05:00
|
|
|
/// comments, and `focus_range` is the range of the identifier.
|
2020-07-17 05:42:48 -05:00
|
|
|
///
|
|
|
|
/// Clients should place the cursor on this range when navigating to this target.
|
|
|
|
pub focus_range: Option<TextRange>,
|
|
|
|
pub name: SmolStr,
|
2020-12-18 12:10:13 -06:00
|
|
|
pub kind: Option<SymbolKind>,
|
2020-07-17 05:42:48 -05:00
|
|
|
pub container_name: Option<SmolStr>,
|
|
|
|
pub description: Option<String>,
|
2020-12-11 14:19:58 -06:00
|
|
|
pub docs: Option<Documentation>,
|
2019-01-11 05:00:54 -06:00
|
|
|
}
|
2019-01-11 04:01:35 -06:00
|
|
|
|
2020-12-18 12:26:47 -06:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-11 02:15:19 -06:00
|
|
|
pub(crate) trait ToNav {
|
|
|
|
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget;
|
2019-11-03 11:49:41 -06:00
|
|
|
}
|
|
|
|
|
2020-02-22 09:57:29 -06:00
|
|
|
pub(crate) trait TryToNav {
|
|
|
|
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget>;
|
|
|
|
}
|
|
|
|
|
2021-08-02 07:33:09 -05:00
|
|
|
impl<T: TryToNav, U: TryToNav> TryToNav for Either<T, U> {
|
|
|
|
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
|
|
|
|
match self {
|
|
|
|
Either::Left(it) => it.try_to_nav(db),
|
|
|
|
Either::Right(it) => it.try_to_nav(db),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-11 04:01:35 -06:00
|
|
|
impl NavigationTarget {
|
2020-07-17 05:42:48 -05:00
|
|
|
pub fn focus_or_full_range(&self) -> TextRange {
|
2019-02-17 05:38:32 -06:00
|
|
|
self.focus_range.unwrap_or(self.full_range)
|
|
|
|
}
|
2019-06-09 11:03:38 -05:00
|
|
|
|
2019-01-15 09:50:16 -06:00
|
|
|
pub(crate) fn from_module_to_decl(db: &RootDatabase, module: hir::Module) -> NavigationTarget {
|
2021-11-04 12:12:05 -05:00
|
|
|
let name = module.name(db).map(|it| it.to_smol_str()).unwrap_or_default();
|
2021-12-07 08:02:22 -06:00
|
|
|
if let Some(src @ InFile { value, .. }) = &module.declaration_source(db) {
|
|
|
|
let FileRange { file_id, range: full_range } = src.syntax().original_file_range(db);
|
2022-01-31 06:56:14 -06:00
|
|
|
let focus_range =
|
|
|
|
value.name().and_then(|it| orig_focus_range(db, src.file_id, it.syntax()));
|
2020-06-02 10:22:23 -05:00
|
|
|
let mut res = NavigationTarget::from_syntax(
|
2021-12-07 08:02:22 -06:00
|
|
|
file_id,
|
2019-06-08 14:27:01 -05:00
|
|
|
name,
|
2021-02-09 09:21:09 -06:00
|
|
|
focus_range,
|
2021-12-07 08:02:22 -06:00
|
|
|
full_range,
|
2020-12-17 05:29:05 -06:00
|
|
|
SymbolKind::Module,
|
2019-06-08 14:27:01 -05:00
|
|
|
);
|
2020-12-11 14:19:58 -06:00
|
|
|
res.docs = module.attrs(db).docs();
|
2021-03-15 11:58:42 -05:00
|
|
|
res.description = Some(module.display(db).to_string());
|
2020-06-02 10:22:23 -05:00
|
|
|
return res;
|
2019-01-13 12:56:20 -06:00
|
|
|
}
|
2019-11-11 02:15:19 -06:00
|
|
|
module.to_nav(db)
|
2019-03-07 02:32:39 -06:00
|
|
|
}
|
|
|
|
|
2019-01-11 09:17:20 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
pub(crate) fn debug_render(&self) -> String {
|
2020-12-18 12:10:13 -06:00
|
|
|
let mut buf = format!(
|
|
|
|
"{} {:?} {:?} {:?}",
|
|
|
|
self.name,
|
|
|
|
self.kind.unwrap(),
|
|
|
|
self.file_id,
|
|
|
|
self.full_range
|
|
|
|
);
|
2020-07-17 05:42:48 -05:00
|
|
|
if let Some(focus_range) = self.focus_range {
|
2019-01-11 09:17:20 -06:00
|
|
|
buf.push_str(&format!(" {:?}", focus_range))
|
|
|
|
}
|
2020-07-17 05:42:48 -05:00
|
|
|
if let Some(container_name) = &self.container_name {
|
2019-02-23 04:53:53 -06:00
|
|
|
buf.push_str(&format!(" {}", container_name))
|
2019-02-12 13:47:51 -06:00
|
|
|
}
|
2019-01-11 09:17:20 -06:00
|
|
|
buf
|
|
|
|
}
|
|
|
|
|
2019-02-23 03:02:42 -06:00
|
|
|
/// Allows `NavigationTarget` to be created from a `NameOwner`
|
2020-06-02 10:22:23 -05:00
|
|
|
pub(crate) fn from_named(
|
2019-11-03 11:49:41 -06:00
|
|
|
db: &RootDatabase,
|
2022-01-31 06:56:14 -06:00
|
|
|
node @ InFile { file_id, value }: InFile<&dyn ast::HasName>,
|
2020-12-17 05:29:05 -06:00
|
|
|
kind: SymbolKind,
|
2019-06-08 14:27:01 -05:00
|
|
|
) -> NavigationTarget {
|
2022-01-31 06:56:14 -06:00
|
|
|
let name = value.name().map(|it| it.text().into()).unwrap_or_else(|| "_".into());
|
|
|
|
let focus_range = value.name().and_then(|it| orig_focus_range(db, file_id, it.syntax()));
|
2021-12-07 08:02:22 -06:00
|
|
|
let FileRange { file_id, range } = node.map(|it| it.syntax()).original_file_range(db);
|
2019-11-03 11:49:41 -06:00
|
|
|
|
2021-12-07 08:02:22 -06:00
|
|
|
NavigationTarget::from_syntax(file_id, name, focus_range, range, kind)
|
2019-01-11 04:28:59 -06:00
|
|
|
}
|
|
|
|
|
2019-01-11 05:00:54 -06:00
|
|
|
fn from_syntax(
|
|
|
|
file_id: FileId,
|
|
|
|
name: SmolStr,
|
|
|
|
focus_range: Option<TextRange>,
|
2019-11-03 11:49:41 -06:00
|
|
|
full_range: TextRange,
|
2020-12-17 05:29:05 -06:00
|
|
|
kind: SymbolKind,
|
2019-01-11 05:00:54 -06:00
|
|
|
) -> NavigationTarget {
|
2019-01-11 04:05:45 -06:00
|
|
|
NavigationTarget {
|
|
|
|
file_id,
|
2019-01-11 04:28:59 -06:00
|
|
|
name,
|
2020-12-18 12:10:13 -06:00
|
|
|
kind: Some(kind),
|
2019-11-03 11:49:41 -06:00
|
|
|
full_range,
|
2019-01-11 05:00:54 -06:00
|
|
|
focus_range,
|
2019-02-12 13:47:51 -06:00
|
|
|
container_name: None,
|
2020-06-02 10:22:23 -05:00
|
|
|
description: None,
|
|
|
|
docs: None,
|
2019-01-11 04:05:45 -06:00
|
|
|
}
|
|
|
|
}
|
2019-06-08 09:26:27 -05:00
|
|
|
}
|
2019-04-09 06:43:11 -05:00
|
|
|
|
2021-11-27 18:42:42 -06:00
|
|
|
impl TryToNav for FileSymbol {
|
|
|
|
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
|
2021-11-29 22:50:09 -06:00
|
|
|
let full_range = self.loc.original_range(db)?;
|
|
|
|
let name_range = self.loc.original_name_range(db)?;
|
2021-11-27 18:42:42 -06:00
|
|
|
|
|
|
|
Some(NavigationTarget {
|
|
|
|
file_id: full_range.file_id,
|
2019-11-11 02:15:19 -06:00
|
|
|
name: self.name.clone(),
|
2021-12-07 08:06:56 -06:00
|
|
|
kind: Some(self.kind.into()),
|
2021-11-27 18:42:42 -06:00
|
|
|
full_range: full_range.range,
|
|
|
|
focus_range: Some(name_range.range),
|
2019-11-11 02:15:19 -06:00
|
|
|
container_name: self.container_name.clone(),
|
|
|
|
description: description_from_symbol(db, self),
|
2020-12-17 08:45:26 -06:00
|
|
|
docs: None,
|
2021-11-27 18:42:42 -06:00
|
|
|
})
|
2019-11-11 02:15:19 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-03 11:36:39 -06:00
|
|
|
impl TryToNav for Definition {
|
2020-02-22 09:57:29 -06:00
|
|
|
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
|
|
|
|
match self {
|
2021-11-10 15:02:50 -06:00
|
|
|
Definition::Local(it) => Some(it.to_nav(db)),
|
|
|
|
Definition::Label(it) => Some(it.to_nav(db)),
|
|
|
|
Definition::Module(it) => Some(it.to_nav(db)),
|
2020-12-31 21:14:09 -06:00
|
|
|
Definition::Macro(it) => it.try_to_nav(db),
|
2021-01-01 00:13:15 -06:00
|
|
|
Definition::Field(it) => it.try_to_nav(db),
|
|
|
|
Definition::SelfType(it) => it.try_to_nav(db),
|
2021-01-08 05:28:02 -06:00
|
|
|
Definition::GenericParam(it) => it.try_to_nav(db),
|
2021-11-10 15:02:50 -06:00
|
|
|
Definition::Function(it) => it.try_to_nav(db),
|
|
|
|
Definition::Adt(it) => it.try_to_nav(db),
|
|
|
|
Definition::Variant(it) => it.try_to_nav(db),
|
|
|
|
Definition::Const(it) => it.try_to_nav(db),
|
|
|
|
Definition::Static(it) => it.try_to_nav(db),
|
|
|
|
Definition::Trait(it) => it.try_to_nav(db),
|
|
|
|
Definition::TypeAlias(it) => it.try_to_nav(db),
|
|
|
|
Definition::BuiltinType(_) => None,
|
2021-12-03 10:15:19 -06:00
|
|
|
Definition::ToolModule(_) => None,
|
2021-12-03 09:32:14 -06:00
|
|
|
Definition::BuiltinAttr(_) => None,
|
2022-07-24 07:05:37 -05:00
|
|
|
// FIXME: The focus range should be set to the helper declaration
|
|
|
|
Definition::DeriveHelper(it) => it.derive().try_to_nav(db),
|
2020-02-22 09:57:29 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryToNav for hir::ModuleDef {
|
|
|
|
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
|
2021-01-01 00:13:15 -06:00
|
|
|
match self {
|
|
|
|
hir::ModuleDef::Module(it) => Some(it.to_nav(db)),
|
|
|
|
hir::ModuleDef::Function(it) => it.try_to_nav(db),
|
|
|
|
hir::ModuleDef::Adt(it) => it.try_to_nav(db),
|
|
|
|
hir::ModuleDef::Variant(it) => it.try_to_nav(db),
|
|
|
|
hir::ModuleDef::Const(it) => it.try_to_nav(db),
|
|
|
|
hir::ModuleDef::Static(it) => it.try_to_nav(db),
|
|
|
|
hir::ModuleDef::Trait(it) => it.try_to_nav(db),
|
|
|
|
hir::ModuleDef::TypeAlias(it) => it.try_to_nav(db),
|
2022-03-08 16:52:26 -06:00
|
|
|
hir::ModuleDef::Macro(it) => it.try_to_nav(db),
|
2021-01-01 00:13:15 -06:00
|
|
|
hir::ModuleDef::BuiltinType(_) => None,
|
|
|
|
}
|
2020-02-22 09:57:29 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 05:29:05 -06:00
|
|
|
pub(crate) trait ToNavFromAst {
|
|
|
|
const KIND: SymbolKind;
|
|
|
|
}
|
|
|
|
impl ToNavFromAst for hir::Function {
|
|
|
|
const KIND: SymbolKind = SymbolKind::Function;
|
|
|
|
}
|
|
|
|
impl ToNavFromAst for hir::Const {
|
|
|
|
const KIND: SymbolKind = SymbolKind::Const;
|
|
|
|
}
|
|
|
|
impl ToNavFromAst for hir::Static {
|
|
|
|
const KIND: SymbolKind = SymbolKind::Static;
|
|
|
|
}
|
|
|
|
impl ToNavFromAst for hir::Struct {
|
|
|
|
const KIND: SymbolKind = SymbolKind::Struct;
|
|
|
|
}
|
|
|
|
impl ToNavFromAst for hir::Enum {
|
|
|
|
const KIND: SymbolKind = SymbolKind::Enum;
|
|
|
|
}
|
2020-12-20 01:05:24 -06:00
|
|
|
impl ToNavFromAst for hir::Variant {
|
2020-12-17 05:29:05 -06:00
|
|
|
const KIND: SymbolKind = SymbolKind::Variant;
|
|
|
|
}
|
|
|
|
impl ToNavFromAst for hir::Union {
|
|
|
|
const KIND: SymbolKind = SymbolKind::Union;
|
|
|
|
}
|
|
|
|
impl ToNavFromAst for hir::TypeAlias {
|
|
|
|
const KIND: SymbolKind = SymbolKind::TypeAlias;
|
|
|
|
}
|
|
|
|
impl ToNavFromAst for hir::Trait {
|
|
|
|
const KIND: SymbolKind = SymbolKind::Trait;
|
|
|
|
}
|
2019-11-11 02:15:19 -06:00
|
|
|
|
2021-01-01 00:13:15 -06:00
|
|
|
impl<D> TryToNav for D
|
2019-11-11 02:15:19 -06:00
|
|
|
where
|
2021-03-15 11:58:42 -05:00
|
|
|
D: HasSource + ToNavFromAst + Copy + HasAttrs + HirDisplay,
|
2021-09-27 05:54:24 -05:00
|
|
|
D::Ast: ast::HasName,
|
2019-11-11 02:15:19 -06:00
|
|
|
{
|
2021-01-01 00:13:15 -06:00
|
|
|
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
|
|
|
|
let src = self.source(db)?;
|
2020-12-17 05:29:05 -06:00
|
|
|
let mut res = NavigationTarget::from_named(
|
|
|
|
db,
|
2021-09-27 05:54:24 -05:00
|
|
|
src.as_ref().map(|it| it as &dyn ast::HasName),
|
2020-12-17 05:29:05 -06:00
|
|
|
D::KIND,
|
|
|
|
);
|
2020-12-11 14:19:58 -06:00
|
|
|
res.docs = self.docs(db);
|
2021-03-15 11:58:42 -05:00
|
|
|
res.description = Some(self.display(db).to_string());
|
2021-01-01 00:13:15 -06:00
|
|
|
Some(res)
|
2019-11-11 02:15:19 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToNav for hir::Module {
|
|
|
|
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
|
2021-12-07 08:02:22 -06:00
|
|
|
let InFile { file_id, value } = self.definition_source(db);
|
|
|
|
|
2021-11-04 12:12:05 -05:00
|
|
|
let name = self.name(db).map(|it| it.to_smol_str()).unwrap_or_default();
|
2021-12-07 08:02:22 -06:00
|
|
|
let (syntax, focus) = match &value {
|
2020-02-22 09:57:29 -06:00
|
|
|
ModuleSource::SourceFile(node) => (node.syntax(), None),
|
2021-12-07 08:02:22 -06:00
|
|
|
ModuleSource::Module(node) => (
|
|
|
|
node.syntax(),
|
2022-01-31 06:56:14 -06:00
|
|
|
node.name().and_then(|it| orig_focus_range(db, file_id, it.syntax())),
|
2021-12-07 08:02:22 -06:00
|
|
|
),
|
2021-01-20 13:05:48 -06:00
|
|
|
ModuleSource::BlockExpr(node) => (node.syntax(), None),
|
2019-12-03 14:24:02 -06:00
|
|
|
};
|
2021-12-07 08:02:22 -06:00
|
|
|
let FileRange { file_id, range: full_range } =
|
|
|
|
InFile::new(file_id, syntax).original_file_range(db);
|
|
|
|
NavigationTarget::from_syntax(file_id, name, focus, full_range, SymbolKind::Module)
|
2019-11-11 02:15:19 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-01 00:13:15 -06:00
|
|
|
impl TryToNav for hir::Impl {
|
|
|
|
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
|
2021-12-07 08:02:22 -06:00
|
|
|
let InFile { file_id, value } = self.source(db)?;
|
2020-06-30 06:20:16 -05:00
|
|
|
let derive_attr = self.is_builtin_derive(db);
|
2021-12-07 08:02:22 -06:00
|
|
|
|
2020-06-30 06:20:16 -05:00
|
|
|
let focus_range = if derive_attr.is_some() {
|
|
|
|
None
|
|
|
|
} else {
|
2022-01-31 06:56:14 -06:00
|
|
|
value.self_ty().and_then(|ty| orig_focus_range(db, file_id, ty.syntax()))
|
2021-12-07 08:02:22 -06:00
|
|
|
};
|
2021-12-07 08:42:54 -06:00
|
|
|
|
2021-12-07 08:02:22 -06:00
|
|
|
let FileRange { file_id, range: full_range } = match &derive_attr {
|
2021-12-07 08:42:54 -06:00
|
|
|
Some(attr) => attr.syntax().original_file_range(db),
|
|
|
|
None => InFile::new(file_id, value.syntax()).original_file_range(db),
|
2020-06-30 06:20:16 -05:00
|
|
|
};
|
2019-11-11 02:15:19 -06:00
|
|
|
|
2021-01-01 00:13:15 -06:00
|
|
|
Some(NavigationTarget::from_syntax(
|
2021-12-07 08:02:22 -06:00
|
|
|
file_id,
|
2019-11-11 02:15:19 -06:00
|
|
|
"impl".into(),
|
2020-06-30 06:03:08 -05:00
|
|
|
focus_range,
|
2021-12-07 08:02:22 -06:00
|
|
|
full_range,
|
2020-12-17 05:29:05 -06:00
|
|
|
SymbolKind::Impl,
|
2021-01-01 00:13:15 -06:00
|
|
|
))
|
2019-11-11 02:15:19 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-01 00:13:15 -06:00
|
|
|
impl TryToNav for hir::Field {
|
|
|
|
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
|
|
|
|
let src = self.source(db)?;
|
2019-11-11 02:15:19 -06:00
|
|
|
|
2021-01-01 00:13:15 -06:00
|
|
|
let field_source = match &src.value {
|
2020-06-02 10:22:23 -05:00
|
|
|
FieldSource::Named(it) => {
|
2020-12-17 05:29:05 -06:00
|
|
|
let mut res =
|
|
|
|
NavigationTarget::from_named(db, src.with_value(it), SymbolKind::Field);
|
2020-12-11 14:19:58 -06:00
|
|
|
res.docs = self.docs(db);
|
2021-03-15 11:58:42 -05:00
|
|
|
res.description = Some(self.display(db).to_string());
|
2020-06-02 10:22:23 -05:00
|
|
|
res
|
|
|
|
}
|
2019-11-11 02:15:19 -06:00
|
|
|
FieldSource::Pos(it) => {
|
2021-12-07 08:02:22 -06:00
|
|
|
let FileRange { file_id, range } =
|
|
|
|
src.with_value(it.syntax()).original_file_range(db);
|
|
|
|
NavigationTarget::from_syntax(file_id, "".into(), None, range, SymbolKind::Field)
|
2019-11-11 02:15:19 -06:00
|
|
|
}
|
2021-01-01 00:13:15 -06:00
|
|
|
};
|
|
|
|
Some(field_source)
|
2019-11-11 02:15:19 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-08 16:52:26 -06:00
|
|
|
impl TryToNav for hir::Macro {
|
2020-12-31 21:14:09 -06:00
|
|
|
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
|
|
|
|
let src = self.source(db)?;
|
2021-09-27 05:54:24 -05:00
|
|
|
let name_owner: &dyn ast::HasName = match &src.value {
|
2021-03-18 10:11:18 -05:00
|
|
|
Either::Left(it) => it,
|
|
|
|
Either::Right(it) => it,
|
|
|
|
};
|
2020-12-17 05:29:05 -06:00
|
|
|
let mut res = NavigationTarget::from_named(
|
|
|
|
db,
|
2021-03-18 10:11:18 -05:00
|
|
|
src.as_ref().with_value(name_owner),
|
2022-03-08 16:52:26 -06:00
|
|
|
self.kind(db).into(),
|
2020-12-17 05:29:05 -06:00
|
|
|
);
|
2020-12-11 14:19:58 -06:00
|
|
|
res.docs = self.docs(db);
|
2020-12-31 21:14:09 -06:00
|
|
|
Some(res)
|
2019-11-11 02:15:19 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-01 00:13:15 -06:00
|
|
|
impl TryToNav for hir::Adt {
|
|
|
|
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
|
2019-11-11 02:15:19 -06:00
|
|
|
match self {
|
2021-01-01 00:13:15 -06:00
|
|
|
hir::Adt::Struct(it) => it.try_to_nav(db),
|
|
|
|
hir::Adt::Union(it) => it.try_to_nav(db),
|
|
|
|
hir::Adt::Enum(it) => it.try_to_nav(db),
|
2019-11-11 02:15:19 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-01 00:13:15 -06:00
|
|
|
impl TryToNav for hir::AssocItem {
|
|
|
|
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
|
2019-11-11 02:15:19 -06:00
|
|
|
match self {
|
2021-01-01 00:13:15 -06:00
|
|
|
AssocItem::Function(it) => it.try_to_nav(db),
|
|
|
|
AssocItem::Const(it) => it.try_to_nav(db),
|
|
|
|
AssocItem::TypeAlias(it) => it.try_to_nav(db),
|
2019-11-11 02:15:19 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-08 05:28:02 -06:00
|
|
|
impl TryToNav for hir::GenericParam {
|
|
|
|
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
|
|
|
|
match self {
|
|
|
|
hir::GenericParam::TypeParam(it) => it.try_to_nav(db),
|
|
|
|
hir::GenericParam::ConstParam(it) => it.try_to_nav(db),
|
|
|
|
hir::GenericParam::LifetimeParam(it) => it.try_to_nav(db),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-09 15:32:00 -06:00
|
|
|
impl ToNav for hir::Local {
|
|
|
|
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
|
2021-12-07 08:02:22 -06:00
|
|
|
let InFile { file_id, value } = self.source(db);
|
|
|
|
let (node, name) = match &value {
|
|
|
|
Either::Left(bind_pat) => (bind_pat.syntax(), bind_pat.name()),
|
|
|
|
Either::Right(it) => (it.syntax(), it.name()),
|
2019-11-09 15:32:00 -06:00
|
|
|
};
|
2022-01-31 06:56:14 -06:00
|
|
|
let focus_range = name.and_then(|it| orig_focus_range(db, file_id, it.syntax()));
|
2021-12-07 08:02:22 -06:00
|
|
|
let FileRange { file_id, range: full_range } =
|
|
|
|
InFile::new(file_id, node).original_file_range(db);
|
2021-01-15 14:07:38 -06:00
|
|
|
|
2022-02-26 08:06:11 -06:00
|
|
|
let name = self.name(db).to_smol_str();
|
2021-01-15 11:57:32 -06:00
|
|
|
let kind = if self.is_self(db) {
|
|
|
|
SymbolKind::SelfParam
|
|
|
|
} else if self.is_param(db) {
|
|
|
|
SymbolKind::ValueParam
|
|
|
|
} else {
|
|
|
|
SymbolKind::Local
|
|
|
|
};
|
2019-11-09 15:32:00 -06:00
|
|
|
NavigationTarget {
|
2021-12-07 08:02:22 -06:00
|
|
|
file_id,
|
2019-11-09 15:32:00 -06:00
|
|
|
name,
|
2020-12-18 14:00:43 -06:00
|
|
|
kind: Some(kind),
|
2021-12-07 08:02:22 -06:00
|
|
|
full_range,
|
2021-01-15 11:57:32 -06:00
|
|
|
focus_range,
|
2019-11-09 15:32:00 -06:00
|
|
|
container_name: None,
|
|
|
|
description: None,
|
|
|
|
docs: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-23 10:15:01 -06:00
|
|
|
impl ToNav for hir::Label {
|
|
|
|
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
|
2021-12-07 08:02:22 -06:00
|
|
|
let InFile { file_id, value } = self.source(db);
|
2021-11-04 12:12:05 -05:00
|
|
|
let name = self.name(db).to_smol_str();
|
2021-12-07 08:02:22 -06:00
|
|
|
|
|
|
|
let range = |syntax: &_| InFile::new(file_id, syntax).original_file_range(db);
|
|
|
|
let FileRange { file_id, range: full_range } = range(value.syntax());
|
|
|
|
let focus_range = value.lifetime().map(|lt| range(lt.syntax()).range);
|
|
|
|
|
2020-12-23 10:15:01 -06:00
|
|
|
NavigationTarget {
|
|
|
|
file_id,
|
|
|
|
name,
|
|
|
|
kind: Some(SymbolKind::Label),
|
2021-12-07 08:02:22 -06:00
|
|
|
full_range,
|
2020-12-23 10:15:01 -06:00
|
|
|
focus_range,
|
|
|
|
container_name: None,
|
|
|
|
description: None,
|
|
|
|
docs: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-01 00:13:15 -06:00
|
|
|
impl TryToNav for hir::TypeParam {
|
|
|
|
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
|
2021-12-29 07:35:59 -06:00
|
|
|
let InFile { file_id, value } = self.merge().source(db)?;
|
2021-12-07 08:02:22 -06:00
|
|
|
let name = self.name(db).to_smol_str();
|
|
|
|
|
2021-12-29 07:35:59 -06:00
|
|
|
let value = match value {
|
|
|
|
Either::Left(ast::TypeOrConstParam::Type(x)) => Either::Left(x),
|
|
|
|
Either::Left(ast::TypeOrConstParam::Const(_)) => {
|
|
|
|
never!();
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
Either::Right(x) => Either::Right(x),
|
|
|
|
};
|
|
|
|
|
2021-12-07 08:02:22 -06:00
|
|
|
let range = |syntax: &_| InFile::new(file_id, syntax).original_file_range(db);
|
|
|
|
let focus_range = |syntax: &_| InFile::new(file_id, syntax).original_file_range_opt(db);
|
|
|
|
let FileRange { file_id, range: full_range } = match &value {
|
|
|
|
Either::Left(type_param) => range(type_param.syntax()),
|
2021-06-14 14:42:43 -05:00
|
|
|
Either::Right(trait_) => trait_
|
2021-01-25 17:44:27 -06:00
|
|
|
.name()
|
2021-12-07 08:02:22 -06:00
|
|
|
.and_then(|name| focus_range(name.syntax()))
|
|
|
|
.unwrap_or_else(|| range(trait_.syntax())),
|
2019-12-07 11:48:35 -06:00
|
|
|
};
|
2021-12-07 08:02:22 -06:00
|
|
|
let focus_range = value
|
|
|
|
.either(|it| it.name(), |it| it.name())
|
|
|
|
.and_then(|it| focus_range(it.syntax()))
|
|
|
|
.map(|it| it.range);
|
2021-01-01 00:13:15 -06:00
|
|
|
Some(NavigationTarget {
|
2021-12-07 08:02:22 -06:00
|
|
|
file_id,
|
|
|
|
name,
|
2020-12-18 12:10:13 -06:00
|
|
|
kind: Some(SymbolKind::TypeParam),
|
2020-05-04 05:20:38 -05:00
|
|
|
full_range,
|
|
|
|
focus_range,
|
2019-12-07 11:48:35 -06:00
|
|
|
container_name: None,
|
|
|
|
description: None,
|
|
|
|
docs: None,
|
2021-01-01 00:13:15 -06:00
|
|
|
})
|
2020-12-16 14:35:15 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-29 07:35:59 -06:00
|
|
|
impl TryToNav for hir::TypeOrConstParam {
|
|
|
|
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
|
|
|
|
self.split(db).try_to_nav(db)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-01 00:13:15 -06:00
|
|
|
impl TryToNav for hir::LifetimeParam {
|
|
|
|
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
|
2021-12-07 08:02:22 -06:00
|
|
|
let InFile { file_id, value } = self.source(db)?;
|
|
|
|
let name = self.name(db).to_smol_str();
|
|
|
|
|
|
|
|
let FileRange { file_id, range: full_range } =
|
|
|
|
InFile::new(file_id, value.syntax()).original_file_range(db);
|
2021-01-01 00:13:15 -06:00
|
|
|
Some(NavigationTarget {
|
2021-12-07 08:02:22 -06:00
|
|
|
file_id,
|
|
|
|
name,
|
2020-12-18 12:10:13 -06:00
|
|
|
kind: Some(SymbolKind::LifetimeParam),
|
2020-12-16 14:35:15 -06:00
|
|
|
full_range,
|
|
|
|
focus_range: Some(full_range),
|
|
|
|
container_name: None,
|
|
|
|
description: None,
|
|
|
|
docs: None,
|
2021-01-01 00:13:15 -06:00
|
|
|
})
|
2019-12-07 11:48:35 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-02 05:11:25 -06:00
|
|
|
impl TryToNav for hir::ConstParam {
|
|
|
|
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
|
2021-12-29 07:35:59 -06:00
|
|
|
let InFile { file_id, value } = self.merge().source(db)?;
|
2021-12-07 08:02:22 -06:00
|
|
|
let name = self.name(db).to_smol_str();
|
|
|
|
|
2021-12-29 07:35:59 -06:00
|
|
|
let value = match value {
|
|
|
|
Either::Left(ast::TypeOrConstParam::Const(x)) => x,
|
|
|
|
_ => {
|
|
|
|
never!();
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-01-31 06:56:14 -06:00
|
|
|
let focus_range = value.name().and_then(|it| orig_focus_range(db, file_id, it.syntax()));
|
2021-12-07 08:02:22 -06:00
|
|
|
let FileRange { file_id, range: full_range } =
|
|
|
|
InFile::new(file_id, value.syntax()).original_file_range(db);
|
2021-01-02 05:11:25 -06:00
|
|
|
Some(NavigationTarget {
|
2021-12-07 08:02:22 -06:00
|
|
|
file_id,
|
|
|
|
name,
|
2021-01-01 03:07:01 -06:00
|
|
|
kind: Some(SymbolKind::ConstParam),
|
|
|
|
full_range,
|
2021-12-07 08:02:22 -06:00
|
|
|
focus_range,
|
2021-01-01 03:07:01 -06:00
|
|
|
container_name: None,
|
|
|
|
description: None,
|
|
|
|
docs: None,
|
2021-01-02 05:11:25 -06:00
|
|
|
})
|
2021-01-01 03:07:01 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-09 10:59:59 -05:00
|
|
|
/// Get a description of a symbol.
|
2019-06-08 09:26:27 -05:00
|
|
|
///
|
|
|
|
/// e.g. `struct Name`, `enum Name`, `fn Name`
|
2019-06-10 11:34:43 -05:00
|
|
|
pub(crate) fn description_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> Option<String> {
|
2021-03-16 09:44:31 -05:00
|
|
|
let sema = Semantics::new(db);
|
2021-11-27 18:42:42 -06:00
|
|
|
let node = symbol.loc.syntax(&sema)?;
|
2019-06-09 11:20:49 -05:00
|
|
|
|
2019-10-05 09:03:03 -05:00
|
|
|
match_ast! {
|
|
|
|
match node {
|
2021-03-16 09:44:31 -05:00
|
|
|
ast::Fn(it) => sema.to_def(&it).map(|it| it.display(db).to_string()),
|
|
|
|
ast::Struct(it) => sema.to_def(&it).map(|it| it.display(db).to_string()),
|
|
|
|
ast::Enum(it) => sema.to_def(&it).map(|it| it.display(db).to_string()),
|
|
|
|
ast::Trait(it) => sema.to_def(&it).map(|it| it.display(db).to_string()),
|
|
|
|
ast::Module(it) => sema.to_def(&it).map(|it| it.display(db).to_string()),
|
|
|
|
ast::TypeAlias(it) => sema.to_def(&it).map(|it| it.display(db).to_string()),
|
|
|
|
ast::Const(it) => sema.to_def(&it).map(|it| it.display(db).to_string()),
|
|
|
|
ast::Static(it) => sema.to_def(&it).map(|it| it.display(db).to_string()),
|
|
|
|
ast::RecordField(it) => sema.to_def(&it).map(|it| it.display(db).to_string()),
|
|
|
|
ast::Variant(it) => sema.to_def(&it).map(|it| it.display(db).to_string()),
|
2021-08-14 11:02:51 -05:00
|
|
|
ast::Union(it) => sema.to_def(&it).map(|it| it.display(db).to_string()),
|
2019-10-05 09:03:03 -05:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2019-01-11 04:01:35 -06:00
|
|
|
}
|
2020-07-17 06:28:21 -05:00
|
|
|
|
2022-01-31 06:56:14 -06:00
|
|
|
fn orig_focus_range(
|
|
|
|
db: &RootDatabase,
|
|
|
|
file_id: hir::HirFileId,
|
|
|
|
syntax: &SyntaxNode,
|
|
|
|
) -> Option<TextRange> {
|
|
|
|
InFile::new(file_id, syntax).original_file_range_opt(db).map(|it| it.range)
|
|
|
|
}
|
|
|
|
|
2020-07-17 06:28:21 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-08-21 06:19:31 -05:00
|
|
|
use expect_test::expect;
|
2020-07-17 06:28:21 -05:00
|
|
|
|
2020-10-02 10:34:31 -05:00
|
|
|
use crate::{fixture, Query};
|
2020-07-17 06:28:21 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_nav_for_symbol() {
|
2020-10-02 10:34:31 -05:00
|
|
|
let (analysis, _) = fixture::file(
|
2020-07-17 06:28:21 -05:00
|
|
|
r#"
|
|
|
|
enum FooInner { }
|
|
|
|
fn foo() { enum FooInner { } }
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
|
|
|
|
let navs = analysis.symbol_search(Query::new("FooInner".to_string())).unwrap();
|
|
|
|
expect![[r#"
|
|
|
|
[
|
|
|
|
NavigationTarget {
|
|
|
|
file_id: FileId(
|
2020-10-02 09:13:48 -05:00
|
|
|
0,
|
2020-07-17 06:28:21 -05:00
|
|
|
),
|
|
|
|
full_range: 0..17,
|
2020-12-18 12:26:47 -06:00
|
|
|
focus_range: 5..13,
|
2020-07-17 06:28:21 -05:00
|
|
|
name: "FooInner",
|
2020-12-18 12:26:47 -06:00
|
|
|
kind: Enum,
|
|
|
|
description: "enum FooInner",
|
2020-07-17 06:28:21 -05:00
|
|
|
},
|
|
|
|
NavigationTarget {
|
|
|
|
file_id: FileId(
|
2020-10-02 09:13:48 -05:00
|
|
|
0,
|
2020-07-17 06:28:21 -05:00
|
|
|
),
|
|
|
|
full_range: 29..46,
|
2020-12-18 12:26:47 -06:00
|
|
|
focus_range: 34..42,
|
2020-07-17 06:28:21 -05:00
|
|
|
name: "FooInner",
|
2020-12-18 12:26:47 -06:00
|
|
|
kind: Enum,
|
|
|
|
container_name: "foo",
|
|
|
|
description: "enum FooInner",
|
2020-07-17 06:28:21 -05:00
|
|
|
},
|
|
|
|
]
|
|
|
|
"#]]
|
|
|
|
.assert_debug_eq(&navs);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_world_symbols_are_case_sensitive() {
|
2020-10-02 10:34:31 -05:00
|
|
|
let (analysis, _) = fixture::file(
|
2020-07-17 06:28:21 -05:00
|
|
|
r#"
|
|
|
|
fn foo() {}
|
|
|
|
struct Foo;
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
|
|
|
|
let navs = analysis.symbol_search(Query::new("foo".to_string())).unwrap();
|
|
|
|
assert_eq!(navs.len(), 2)
|
|
|
|
}
|
|
|
|
}
|