2019-09-30 03:58:53 -05:00
|
|
|
//! FIXME: write short doc here
|
|
|
|
|
2019-12-03 10:07:56 -06:00
|
|
|
use either::Either;
|
2020-02-18 11:35:10 -06:00
|
|
|
use hir::{original_range, AssocItem, FieldSource, HasSource, InFile, ModuleSource};
|
2019-04-09 06:43:11 -05:00
|
|
|
use ra_db::{FileId, SourceDatabase};
|
2020-03-03 11:54:39 -06:00
|
|
|
use ra_ide_db::{defs::Definition, RootDatabase};
|
2019-01-11 04:28:59 -06:00
|
|
|
use ra_syntax::{
|
2019-11-09 15:32:00 -06:00
|
|
|
ast::{self, DocCommentsOwner, NameOwner},
|
|
|
|
match_ast, AstNode, SmolStr,
|
2019-12-07 11:48:35 -06:00
|
|
|
SyntaxKind::{self, BIND_PAT, TYPE_PARAM},
|
2019-11-18 05:36:11 -06:00
|
|
|
TextRange,
|
2019-01-11 04:28:59 -06:00
|
|
|
};
|
2019-01-11 04:01:35 -06:00
|
|
|
|
2020-06-30 06:27:13 -05:00
|
|
|
use crate::FileSymbol;
|
2019-11-18 05:23:24 -06:00
|
|
|
|
2019-06-09 14:28:53 -05:00
|
|
|
use super::short_label::ShortLabel;
|
2019-01-11 05:00:54 -06:00
|
|
|
|
|
|
|
/// `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.
|
2019-12-30 08:12:06 -06:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2019-01-11 05:00:54 -06:00
|
|
|
pub struct NavigationTarget {
|
2020-05-10 06:55:24 -05:00
|
|
|
// FIXME: use FileRange?
|
2019-01-11 05:00:54 -06:00
|
|
|
file_id: FileId,
|
2020-05-10 06:55:24 -05:00
|
|
|
full_range: TextRange,
|
2019-01-11 05:00:54 -06:00
|
|
|
name: SmolStr,
|
|
|
|
kind: SyntaxKind,
|
|
|
|
focus_range: Option<TextRange>,
|
2019-02-12 13:47:51 -06:00
|
|
|
container_name: Option<SmolStr>,
|
2019-06-09 11:03:38 -05:00
|
|
|
description: Option<String>,
|
|
|
|
docs: Option<String>,
|
2019-01-11 05:00:54 -06:00
|
|
|
}
|
2019-01-11 04:01:35 -06:00
|
|
|
|
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>;
|
|
|
|
}
|
|
|
|
|
2019-01-11 04:01:35 -06:00
|
|
|
impl NavigationTarget {
|
2019-02-17 05:38:32 -06:00
|
|
|
/// When `focus_range` is specified, returns it. otherwise
|
|
|
|
/// returns `full_range`
|
|
|
|
pub fn range(&self) -> TextRange {
|
|
|
|
self.focus_range.unwrap_or(self.full_range)
|
|
|
|
}
|
2020-06-30 06:27:13 -05:00
|
|
|
/// A "most interesting" range withing the `full_range`.
|
|
|
|
///
|
|
|
|
/// Typically, `full_range` is the whole syntax node,
|
|
|
|
/// including doc comments, and `focus_range` is the range of the identifier.
|
|
|
|
pub fn focus_range(&self) -> Option<TextRange> {
|
|
|
|
self.focus_range
|
|
|
|
}
|
|
|
|
pub fn full_range(&self) -> TextRange {
|
|
|
|
self.full_range
|
|
|
|
}
|
|
|
|
pub fn file_id(&self) -> FileId {
|
|
|
|
self.file_id
|
|
|
|
}
|
2019-02-17 05:38:32 -06:00
|
|
|
|
2019-01-11 05:00:54 -06:00
|
|
|
pub fn name(&self) -> &SmolStr {
|
|
|
|
&self.name
|
|
|
|
}
|
|
|
|
|
2019-02-12 13:47:51 -06:00
|
|
|
pub fn container_name(&self) -> Option<&SmolStr> {
|
|
|
|
self.container_name.as_ref()
|
|
|
|
}
|
|
|
|
|
2019-01-11 05:00:54 -06:00
|
|
|
pub fn kind(&self) -> SyntaxKind {
|
|
|
|
self.kind
|
|
|
|
}
|
|
|
|
|
2019-06-09 14:37:34 -05:00
|
|
|
pub fn docs(&self) -> Option<&str> {
|
2020-02-18 07:32:19 -06:00
|
|
|
self.docs.as_deref()
|
2019-06-09 11:03:38 -05:00
|
|
|
}
|
|
|
|
|
2019-06-09 14:37:34 -05:00
|
|
|
pub fn description(&self) -> Option<&str> {
|
2020-02-18 07:32:19 -06:00
|
|
|
self.description.as_deref()
|
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 {
|
2019-02-08 05:49:43 -06:00
|
|
|
let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default();
|
2019-06-11 09:48:27 -05:00
|
|
|
if let Some(src) = module.declaration_source(db) {
|
2019-11-18 05:23:24 -06:00
|
|
|
let frange = original_range(db, src.as_ref().map(|it| it.syntax()));
|
2020-06-02 10:22:23 -05:00
|
|
|
let mut res = NavigationTarget::from_syntax(
|
2019-11-18 05:23:24 -06:00
|
|
|
frange.file_id,
|
2019-06-08 14:27:01 -05:00
|
|
|
name,
|
|
|
|
None,
|
2019-11-18 05:23:24 -06:00
|
|
|
frange.range,
|
2019-11-20 00:40:36 -06:00
|
|
|
src.value.syntax().kind(),
|
2019-06-08 14:27:01 -05:00
|
|
|
);
|
2020-06-02 10:22:23 -05:00
|
|
|
res.docs = src.value.doc_comment_text();
|
|
|
|
res.description = src.value.short_label();
|
|
|
|
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 assert_match(&self, expected: &str) {
|
|
|
|
let actual = self.debug_render();
|
|
|
|
test_utils::assert_eq_text!(expected.trim(), actual.trim(),);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
pub(crate) fn debug_render(&self) -> String {
|
|
|
|
let mut buf = format!(
|
|
|
|
"{} {:?} {:?} {:?}",
|
|
|
|
self.name(),
|
|
|
|
self.kind(),
|
|
|
|
self.file_id(),
|
|
|
|
self.full_range()
|
|
|
|
);
|
|
|
|
if let Some(focus_range) = self.focus_range() {
|
|
|
|
buf.push_str(&format!(" {:?}", focus_range))
|
|
|
|
}
|
2019-02-12 13:47:51 -06: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,
|
2019-11-28 03:50:26 -06:00
|
|
|
node: InFile<&dyn ast::NameOwner>,
|
2019-06-08 14:27:01 -05:00
|
|
|
) -> NavigationTarget {
|
2020-06-09 14:28:51 -05:00
|
|
|
let name =
|
|
|
|
node.value.name().map(|it| it.text().clone()).unwrap_or_else(|| SmolStr::new("_"));
|
2019-11-18 05:07:13 -06:00
|
|
|
let focus_range =
|
2019-11-20 04:09:21 -06:00
|
|
|
node.value.name().map(|it| original_range(db, node.with_value(it.syntax())).range);
|
2019-11-18 05:36:11 -06:00
|
|
|
let frange = original_range(db, node.map(|it| it.syntax()));
|
2019-11-03 11:49:41 -06:00
|
|
|
|
|
|
|
NavigationTarget::from_syntax(
|
2019-11-18 05:23:24 -06:00
|
|
|
frange.file_id,
|
2019-11-03 11:49:41 -06:00
|
|
|
name,
|
|
|
|
focus_range,
|
2019-11-18 05:23:24 -06:00
|
|
|
frange.range,
|
2019-11-20 00:40:36 -06:00
|
|
|
node.value.syntax().kind(),
|
2019-11-03 11:49:41 -06:00
|
|
|
)
|
2019-01-11 04:28:59 -06:00
|
|
|
}
|
|
|
|
|
2020-06-09 14:28:51 -05:00
|
|
|
/// Allows `NavigationTarget` to be created from a `DocCommentsOwner` and a `NameOwner`
|
|
|
|
pub(crate) fn from_doc_commented(
|
|
|
|
db: &RootDatabase,
|
|
|
|
named: InFile<&dyn ast::NameOwner>,
|
|
|
|
node: InFile<&dyn ast::DocCommentsOwner>,
|
|
|
|
) -> NavigationTarget {
|
|
|
|
let name =
|
|
|
|
named.value.name().map(|it| it.text().clone()).unwrap_or_else(|| SmolStr::new("_"));
|
|
|
|
let frange = original_range(db, node.map(|it| it.syntax()));
|
|
|
|
|
|
|
|
NavigationTarget::from_syntax(
|
|
|
|
frange.file_id,
|
|
|
|
name,
|
2020-06-18 04:31:15 -05:00
|
|
|
None,
|
2020-06-09 14:28:51 -05:00
|
|
|
frange.range,
|
|
|
|
node.value.syntax().kind(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
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,
|
2019-11-18 05:36:11 -06:00
|
|
|
kind: SyntaxKind,
|
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,
|
2019-11-18 05:36:11 -06:00
|
|
|
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
|
|
|
|
2019-11-11 02:15:19 -06:00
|
|
|
impl ToNav for FileSymbol {
|
|
|
|
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
|
|
|
|
NavigationTarget {
|
|
|
|
file_id: self.file_id,
|
|
|
|
name: self.name.clone(),
|
2020-04-16 12:21:18 -05:00
|
|
|
kind: self.kind,
|
2020-04-23 09:33:01 -05:00
|
|
|
full_range: self.range,
|
2019-11-11 02:15:19 -06:00
|
|
|
focus_range: self.name_range,
|
|
|
|
container_name: self.container_name.clone(),
|
|
|
|
description: description_from_symbol(db, self),
|
|
|
|
docs: docs_from_symbol(db, self),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2020-03-03 11:36:39 -06:00
|
|
|
Definition::Macro(it) => Some(it.to_nav(db)),
|
2020-04-25 07:23:34 -05:00
|
|
|
Definition::Field(it) => Some(it.to_nav(db)),
|
2020-03-03 11:36:39 -06:00
|
|
|
Definition::ModuleDef(it) => it.try_to_nav(db),
|
|
|
|
Definition::SelfType(it) => Some(it.to_nav(db)),
|
|
|
|
Definition::Local(it) => Some(it.to_nav(db)),
|
|
|
|
Definition::TypeParam(it) => Some(it.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> {
|
|
|
|
let res = match self {
|
|
|
|
hir::ModuleDef::Module(it) => it.to_nav(db),
|
|
|
|
hir::ModuleDef::Function(it) => it.to_nav(db),
|
|
|
|
hir::ModuleDef::Adt(it) => it.to_nav(db),
|
|
|
|
hir::ModuleDef::EnumVariant(it) => it.to_nav(db),
|
|
|
|
hir::ModuleDef::Const(it) => it.to_nav(db),
|
|
|
|
hir::ModuleDef::Static(it) => it.to_nav(db),
|
|
|
|
hir::ModuleDef::Trait(it) => it.to_nav(db),
|
|
|
|
hir::ModuleDef::TypeAlias(it) => it.to_nav(db),
|
|
|
|
hir::ModuleDef::BuiltinType(_) => return None,
|
|
|
|
};
|
|
|
|
Some(res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-11 02:15:19 -06:00
|
|
|
pub(crate) trait ToNavFromAst {}
|
|
|
|
impl ToNavFromAst for hir::Function {}
|
|
|
|
impl ToNavFromAst for hir::Const {}
|
|
|
|
impl ToNavFromAst for hir::Static {}
|
|
|
|
impl ToNavFromAst for hir::Struct {}
|
|
|
|
impl ToNavFromAst for hir::Enum {}
|
|
|
|
impl ToNavFromAst for hir::EnumVariant {}
|
|
|
|
impl ToNavFromAst for hir::Union {}
|
|
|
|
impl ToNavFromAst for hir::TypeAlias {}
|
|
|
|
impl ToNavFromAst for hir::Trait {}
|
|
|
|
|
|
|
|
impl<D> ToNav for D
|
|
|
|
where
|
|
|
|
D: HasSource + ToNavFromAst + Copy,
|
|
|
|
D::Ast: ast::DocCommentsOwner + ast::NameOwner + ShortLabel,
|
|
|
|
{
|
|
|
|
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
|
|
|
|
let src = self.source(db);
|
2020-06-02 10:22:23 -05:00
|
|
|
let mut res =
|
|
|
|
NavigationTarget::from_named(db, src.as_ref().map(|it| it as &dyn ast::NameOwner));
|
|
|
|
res.docs = src.value.doc_comment_text();
|
|
|
|
res.description = src.value.short_label();
|
|
|
|
res
|
2019-11-11 02:15:19 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToNav for hir::Module {
|
|
|
|
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
|
|
|
|
let src = self.definition_source(db);
|
|
|
|
let name = self.name(db).map(|it| it.to_string().into()).unwrap_or_default();
|
2020-02-22 09:57:29 -06:00
|
|
|
let (syntax, focus) = match &src.value {
|
|
|
|
ModuleSource::SourceFile(node) => (node.syntax(), None),
|
|
|
|
ModuleSource::Module(node) => {
|
|
|
|
(node.syntax(), node.name().map(|it| it.syntax().text_range()))
|
|
|
|
}
|
2019-12-03 14:24:02 -06:00
|
|
|
};
|
|
|
|
let frange = original_range(db, src.with_value(syntax));
|
2020-06-02 10:22:23 -05:00
|
|
|
NavigationTarget::from_syntax(frange.file_id, name, focus, frange.range, syntax.kind())
|
2019-11-11 02:15:19 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-29 14:24:40 -06:00
|
|
|
impl ToNav for hir::ImplDef {
|
2019-11-11 02:15:19 -06:00
|
|
|
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
|
|
|
|
let src = self.source(db);
|
2020-06-30 06:20:16 -05:00
|
|
|
let derive_attr = self.is_builtin_derive(db);
|
|
|
|
let frange = if let Some(item) = &derive_attr {
|
2020-01-12 04:08:53 -06:00
|
|
|
original_range(db, item.syntax())
|
|
|
|
} else {
|
|
|
|
original_range(db, src.as_ref().map(|it| it.syntax()))
|
|
|
|
};
|
2020-06-30 06:20:16 -05:00
|
|
|
let focus_range = if derive_attr.is_some() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
src.value.target_type().map(|ty| original_range(db, src.with_value(ty.syntax())).range)
|
|
|
|
};
|
2019-11-11 02:15:19 -06:00
|
|
|
|
|
|
|
NavigationTarget::from_syntax(
|
2019-11-18 05:23:24 -06:00
|
|
|
frange.file_id,
|
2019-11-11 02:15:19 -06:00
|
|
|
"impl".into(),
|
2020-06-30 06:03:08 -05:00
|
|
|
focus_range,
|
2019-11-18 05:23:24 -06:00
|
|
|
frange.range,
|
2019-11-20 00:40:36 -06:00
|
|
|
src.value.syntax().kind(),
|
2019-11-11 02:15:19 -06:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-25 07:23:34 -05:00
|
|
|
impl ToNav for hir::Field {
|
2019-11-11 02:15:19 -06:00
|
|
|
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
|
|
|
|
let src = self.source(db);
|
|
|
|
|
2019-11-20 00:40:36 -06:00
|
|
|
match &src.value {
|
2020-06-02 10:22:23 -05:00
|
|
|
FieldSource::Named(it) => {
|
|
|
|
let mut res = NavigationTarget::from_named(db, src.with_value(it));
|
|
|
|
res.docs = it.doc_comment_text();
|
|
|
|
res.description = it.short_label();
|
|
|
|
res
|
|
|
|
}
|
2019-11-11 02:15:19 -06:00
|
|
|
FieldSource::Pos(it) => {
|
2019-11-20 04:09:21 -06:00
|
|
|
let frange = original_range(db, src.with_value(it.syntax()));
|
2019-11-11 02:15:19 -06:00
|
|
|
NavigationTarget::from_syntax(
|
2019-11-18 05:23:24 -06:00
|
|
|
frange.file_id,
|
2019-11-11 02:15:19 -06:00
|
|
|
"".into(),
|
|
|
|
None,
|
2019-11-18 05:23:24 -06:00
|
|
|
frange.range,
|
2019-11-18 05:36:11 -06:00
|
|
|
it.syntax().kind(),
|
2019-11-11 02:15:19 -06:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToNav for hir::MacroDef {
|
|
|
|
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
|
|
|
|
let src = self.source(db);
|
2019-11-20 00:40:36 -06:00
|
|
|
log::debug!("nav target {:#?}", src.value.syntax());
|
2020-06-02 10:22:23 -05:00
|
|
|
let mut res =
|
|
|
|
NavigationTarget::from_named(db, src.as_ref().map(|it| it as &dyn ast::NameOwner));
|
|
|
|
res.docs = src.value.doc_comment_text();
|
|
|
|
res
|
2019-11-11 02:15:19 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToNav for hir::Adt {
|
|
|
|
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
|
|
|
|
match self {
|
|
|
|
hir::Adt::Struct(it) => it.to_nav(db),
|
|
|
|
hir::Adt::Union(it) => it.to_nav(db),
|
|
|
|
hir::Adt::Enum(it) => it.to_nav(db),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToNav for hir::AssocItem {
|
|
|
|
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
|
|
|
|
match self {
|
|
|
|
AssocItem::Function(it) => it.to_nav(db),
|
|
|
|
AssocItem::Const(it) => it.to_nav(db),
|
|
|
|
AssocItem::TypeAlias(it) => it.to_nav(db),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-09 15:32:00 -06:00
|
|
|
impl ToNav for hir::Local {
|
|
|
|
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
|
|
|
|
let src = self.source(db);
|
2019-12-18 09:25:15 -06:00
|
|
|
let node = match &src.value {
|
|
|
|
Either::Left(bind_pat) => {
|
|
|
|
bind_pat.name().map_or_else(|| bind_pat.syntax().clone(), |it| it.syntax().clone())
|
2019-11-09 15:32:00 -06:00
|
|
|
}
|
2019-12-18 09:25:15 -06:00
|
|
|
Either::Right(it) => it.syntax().clone(),
|
2019-11-09 15:32:00 -06:00
|
|
|
};
|
2019-12-18 09:25:15 -06:00
|
|
|
let full_range = original_range(db, src.with_value(&node));
|
2019-11-09 15:32:00 -06:00
|
|
|
let name = match self.name(db) {
|
|
|
|
Some(it) => it.to_string().into(),
|
|
|
|
None => "".into(),
|
|
|
|
};
|
|
|
|
NavigationTarget {
|
2019-12-18 09:25:15 -06:00
|
|
|
file_id: full_range.file_id,
|
2019-11-09 15:32:00 -06:00
|
|
|
name,
|
|
|
|
kind: BIND_PAT,
|
2019-12-18 09:25:15 -06:00
|
|
|
full_range: full_range.range,
|
|
|
|
focus_range: None,
|
2019-11-09 15:32:00 -06:00
|
|
|
container_name: None,
|
|
|
|
description: None,
|
|
|
|
docs: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-07 13:09:53 -06:00
|
|
|
impl ToNav for hir::TypeParam {
|
2019-12-07 11:48:35 -06:00
|
|
|
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
|
|
|
|
let src = self.source(db);
|
2020-05-04 05:20:38 -05:00
|
|
|
let full_range = match &src.value {
|
2019-12-07 11:48:35 -06:00
|
|
|
Either::Left(it) => it.syntax().text_range(),
|
|
|
|
Either::Right(it) => it.syntax().text_range(),
|
|
|
|
};
|
2020-05-04 05:20:38 -05:00
|
|
|
let focus_range = match &src.value {
|
|
|
|
Either::Left(_) => None,
|
|
|
|
Either::Right(it) => it.name().map(|it| it.syntax().text_range()),
|
|
|
|
};
|
2019-12-07 11:48:35 -06:00
|
|
|
NavigationTarget {
|
|
|
|
file_id: src.file_id.original_file(db),
|
|
|
|
name: self.name(db).to_string().into(),
|
|
|
|
kind: TYPE_PARAM,
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-10 11:34:43 -05:00
|
|
|
pub(crate) fn docs_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> Option<String> {
|
2019-07-12 11:41:13 -05:00
|
|
|
let parse = db.parse(symbol.file_id);
|
2019-09-24 20:32:01 -05:00
|
|
|
let node = symbol.ptr.to_node(parse.tree().syntax());
|
2019-06-08 14:27:01 -05:00
|
|
|
|
2019-10-05 09:03:03 -05:00
|
|
|
match_ast! {
|
|
|
|
match node {
|
2020-04-06 09:21:33 -05:00
|
|
|
ast::FnDef(it) => it.doc_comment_text(),
|
|
|
|
ast::StructDef(it) => it.doc_comment_text(),
|
|
|
|
ast::EnumDef(it) => it.doc_comment_text(),
|
|
|
|
ast::TraitDef(it) => it.doc_comment_text(),
|
|
|
|
ast::Module(it) => it.doc_comment_text(),
|
|
|
|
ast::TypeAliasDef(it) => it.doc_comment_text(),
|
|
|
|
ast::ConstDef(it) => it.doc_comment_text(),
|
|
|
|
ast::StaticDef(it) => it.doc_comment_text(),
|
|
|
|
ast::RecordFieldDef(it) => it.doc_comment_text(),
|
|
|
|
ast::EnumVariant(it) => it.doc_comment_text(),
|
|
|
|
ast::MacroCall(it) => it.doc_comment_text(),
|
2019-10-05 09:03:03 -05:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2019-06-08 09:26:27 -05:00
|
|
|
}
|
2019-04-09 06:43:11 -05: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> {
|
2019-07-12 11:41:13 -05:00
|
|
|
let parse = db.parse(symbol.file_id);
|
2019-09-24 20:32:01 -05:00
|
|
|
let node = symbol.ptr.to_node(parse.tree().syntax());
|
2019-06-09 11:20:49 -05:00
|
|
|
|
2019-10-05 09:03:03 -05:00
|
|
|
match_ast! {
|
|
|
|
match node {
|
2020-04-06 09:21:33 -05:00
|
|
|
ast::FnDef(it) => it.short_label(),
|
|
|
|
ast::StructDef(it) => it.short_label(),
|
|
|
|
ast::EnumDef(it) => it.short_label(),
|
|
|
|
ast::TraitDef(it) => it.short_label(),
|
|
|
|
ast::Module(it) => it.short_label(),
|
|
|
|
ast::TypeAliasDef(it) => it.short_label(),
|
|
|
|
ast::ConstDef(it) => it.short_label(),
|
|
|
|
ast::StaticDef(it) => it.short_label(),
|
|
|
|
ast::RecordFieldDef(it) => it.short_label(),
|
|
|
|
ast::EnumVariant(it) => it.short_label(),
|
2019-10-05 09:03:03 -05:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2019-01-11 04:01:35 -06:00
|
|
|
}
|