rust/crates/ra_ide_api/src/display/navigation_target.rs

363 lines
12 KiB
Rust
Raw Normal View History

use ra_db::{FileId, SourceDatabase};
2019-01-11 04:28:59 -06:00
use ra_syntax::{
2019-06-11 08:40:49 -05:00
SyntaxNode, AstNode, SmolStr, TextRange, AstPtr, TreeArc,
2019-01-11 05:00:54 -06:00
SyntaxKind::{self, NAME},
2019-06-09 10:59:59 -05:00
ast::{self, DocCommentsOwner},
algo::visit::{visitor, Visitor},
2019-01-11 04:28:59 -06:00
};
use hir::{ModuleSource, FieldSource, ImplItem};
2019-01-11 04:01:35 -06:00
2019-01-11 05:00:54 -06:00
use crate::{FileSymbol, db::RootDatabase};
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.
#[derive(Debug, Clone)]
pub struct NavigationTarget {
file_id: FileId,
name: SmolStr,
kind: SyntaxKind,
2019-01-11 09:17:20 -06:00
full_range: TextRange,
2019-01-11 05:00:54 -06:00
focus_range: Option<TextRange>,
container_name: Option<SmolStr>,
description: Option<String>,
docs: Option<String>,
2019-01-11 05:00:54 -06:00
}
2019-01-11 04:01:35 -06:00
impl NavigationTarget {
/// When `focus_range` is specified, returns it. otherwise
/// returns `full_range`
pub fn range(&self) -> TextRange {
self.focus_range.unwrap_or(self.full_range)
}
2019-01-11 05:00:54 -06:00
pub fn name(&self) -> &SmolStr {
&self.name
}
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
}
pub fn file_id(&self) -> FileId {
self.file_id
}
2019-01-11 09:17:20 -06:00
pub fn full_range(&self) -> TextRange {
self.full_range
2019-01-11 05:00:54 -06:00
}
2019-06-09 14:37:34 -05:00
pub fn docs(&self) -> Option<&str> {
self.docs.as_ref().map(String::as_str)
}
2019-06-09 14:37:34 -05:00
pub fn description(&self) -> Option<&str> {
self.description.as_ref().map(String::as_str)
}
/// A "most interesting" range withing the `full_range`.
2019-01-11 05:00:54 -06:00
///
/// Typically, `full_range` is the whole syntax node,
/// including doc comments, and `focus_range` is the range of the identifier.
2019-01-11 05:00:54 -06:00
pub fn focus_range(&self) -> Option<TextRange> {
self.focus_range
}
pub(crate) fn from_bind_pat(file_id: FileId, pat: &ast::BindPat) -> NavigationTarget {
2019-06-09 10:59:59 -05:00
NavigationTarget::from_named(file_id, pat, None, None)
}
2019-06-08 09:26:27 -05:00
pub(crate) fn from_symbol(db: &RootDatabase, symbol: FileSymbol) -> NavigationTarget {
2019-01-11 04:01:35 -06:00
NavigationTarget {
file_id: symbol.file_id,
name: symbol.name.clone(),
kind: symbol.ptr.kind(),
2019-01-11 09:17:20 -06:00
full_range: symbol.ptr.range(),
focus_range: symbol.name_range,
container_name: symbol.container_name.clone(),
description: description_from_symbol(db, &symbol),
2019-06-08 14:27:01 -05:00
docs: docs_from_symbol(db, &symbol),
2019-01-11 04:01:35 -06:00
}
}
2019-04-10 03:15:55 -05:00
pub(crate) fn from_pat(
db: &RootDatabase,
2019-01-11 05:00:54 -06:00
file_id: FileId,
pat: AstPtr<ast::Pat>,
2019-01-11 05:00:54 -06:00
) -> NavigationTarget {
2019-05-28 10:46:11 -05:00
let file = db.parse(file_id).tree;
let (name, full_range) = match pat.to_node(file.syntax()).kind() {
ast::PatKind::BindPat(pat) => return NavigationTarget::from_bind_pat(file_id, &pat),
_ => ("_".into(), pat.syntax_node_ptr().range()),
2019-04-10 03:15:55 -05:00
};
2019-06-08 09:26:27 -05:00
2019-01-11 05:00:54 -06:00
NavigationTarget {
file_id,
2019-04-10 03:15:55 -05:00
name,
full_range,
2019-01-11 05:00:54 -06:00
focus_range: None,
kind: NAME,
container_name: None,
2019-06-09 10:59:59 -05:00
description: None, //< No documentation for Description
docs: None, //< No documentation for Pattern
2019-01-11 05:00:54 -06:00
}
}
pub(crate) fn from_self_param(
file_id: FileId,
par: AstPtr<ast::SelfParam>,
) -> NavigationTarget {
let (name, full_range) = ("self".into(), par.syntax_node_ptr().range());
2019-06-08 09:26:27 -05:00
NavigationTarget {
file_id,
name,
full_range,
focus_range: None,
kind: NAME,
container_name: None,
2019-06-09 10:59:59 -05:00
description: None, //< No document node for SelfParam
docs: None, //< No document node for SelfParam
}
}
pub(crate) fn from_module(db: &RootDatabase, module: hir::Module) -> NavigationTarget {
2019-01-15 09:26:29 -06:00
let (file_id, source) = module.definition_source(db);
let file_id = file_id.as_original_file();
2019-02-08 05:49:43 -06:00
let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default();
match source {
2019-01-11 05:00:54 -06:00
ModuleSource::SourceFile(node) => {
2019-06-09 10:59:59 -05:00
NavigationTarget::from_syntax(file_id, name, None, node.syntax(), None, None)
2019-01-11 05:00:54 -06:00
}
2019-06-08 14:27:01 -05:00
ModuleSource::Module(node) => NavigationTarget::from_syntax(
file_id,
name,
None,
node.syntax(),
node.doc_comment_text(),
2019-06-09 14:28:53 -05:00
node.short_label(),
2019-06-08 14:27:01 -05:00
),
}
2019-01-11 05:00:54 -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-01-15 09:26:29 -06:00
if let Some((file_id, source)) = module.declaration_source(db) {
let file_id = file_id.as_original_file();
2019-06-08 14:27:01 -05:00
return NavigationTarget::from_syntax(
file_id,
name,
None,
source.syntax(),
source.doc_comment_text(),
2019-06-09 14:28:53 -05:00
source.short_label(),
2019-06-08 14:27:01 -05:00
);
2019-01-13 12:56:20 -06:00
}
NavigationTarget::from_module(db, module)
}
2019-01-24 06:40:52 -06:00
pub(crate) fn from_function(db: &RootDatabase, func: hir::Function) -> NavigationTarget {
2019-06-11 08:49:56 -05:00
NavigationTarget::from_def_source(db, func)
2019-01-24 06:40:52 -06:00
}
2019-01-25 11:32:34 -06:00
pub(crate) fn from_field(db: &RootDatabase, field: hir::StructField) -> NavigationTarget {
let (file_id, field) = field.source(db);
let file_id = file_id.original_file(db);
match field {
2019-06-08 14:27:01 -05:00
FieldSource::Named(it) => {
2019-06-09 14:28:53 -05:00
NavigationTarget::from_named(file_id, &*it, it.doc_comment_text(), it.short_label())
2019-06-08 14:27:01 -05:00
}
2019-01-25 11:32:34 -06:00
FieldSource::Pos(it) => {
2019-06-09 10:59:59 -05:00
NavigationTarget::from_syntax(file_id, "".into(), None, it.syntax(), None, None)
2019-01-25 11:32:34 -06:00
}
}
}
2019-06-11 08:40:49 -05:00
pub(crate) fn from_def_source<A, D>(db: &RootDatabase, def: D) -> NavigationTarget
where
D: hir::HasSource<Ast = TreeArc<A>>,
A: ast::DocCommentsOwner + ast::NameOwner + ShortLabel,
{
let src = def.source(db);
NavigationTarget::from_named(
src.file_id.original_file(db),
&*src.ast,
src.ast.doc_comment_text(),
src.ast.short_label(),
)
}
pub(crate) fn from_adt_def(db: &RootDatabase, adt_def: hir::AdtDef) -> NavigationTarget {
match adt_def {
2019-06-11 08:40:49 -05:00
hir::AdtDef::Struct(it) => NavigationTarget::from_def_source(db, it),
hir::AdtDef::Union(it) => NavigationTarget::from_def_source(db, it),
hir::AdtDef::Enum(it) => NavigationTarget::from_def_source(db, it),
}
}
2019-05-30 08:10:07 -05:00
pub(crate) fn from_def(
db: &RootDatabase,
module_def: hir::ModuleDef,
) -> Option<NavigationTarget> {
let nav = match module_def {
2019-01-25 11:55:38 -06:00
hir::ModuleDef::Module(module) => NavigationTarget::from_module(db, module),
hir::ModuleDef::Function(func) => NavigationTarget::from_function(db, func),
2019-06-10 19:17:29 -05:00
hir::ModuleDef::Struct(it) => NavigationTarget::from_adt_def(db, it.into()),
hir::ModuleDef::Enum(it) => NavigationTarget::from_adt_def(db, it.into()),
hir::ModuleDef::Union(it) => NavigationTarget::from_adt_def(db, it.into()),
2019-06-11 09:13:20 -05:00
hir::ModuleDef::Const(it) => NavigationTarget::from_def_source(db, it),
hir::ModuleDef::Static(it) => NavigationTarget::from_def_source(db, it),
2019-06-11 09:34:01 -05:00
hir::ModuleDef::EnumVariant(it) => NavigationTarget::from_def_source(db, it),
hir::ModuleDef::Trait(it) => NavigationTarget::from_def_source(db, it),
2019-06-11 09:25:55 -05:00
hir::ModuleDef::TypeAlias(it) => NavigationTarget::from_def_source(db, it),
2019-05-30 08:10:07 -05:00
hir::ModuleDef::BuiltinType(..) => {
return None;
}
};
Some(nav)
2019-01-11 04:01:35 -06:00
}
2019-01-11 04:05:45 -06:00
pub(crate) fn from_impl_block(
db: &RootDatabase,
impl_block: hir::ImplBlock,
) -> NavigationTarget {
2019-06-11 09:36:52 -05:00
let src = impl_block.source(db);
NavigationTarget::from_syntax(
2019-06-11 09:36:52 -05:00
src.file_id.as_original_file(),
"impl".into(),
None,
2019-06-11 09:36:52 -05:00
src.ast.syntax(),
2019-06-08 14:27:01 -05:00
None,
2019-06-09 10:59:59 -05:00
None,
)
}
2019-03-04 07:34:41 -06:00
pub(crate) fn from_impl_item(db: &RootDatabase, impl_item: hir::ImplItem) -> NavigationTarget {
2019-03-02 13:57:40 -06:00
match impl_item {
2019-06-11 09:13:20 -05:00
ImplItem::Method(it) => NavigationTarget::from_function(db, it),
ImplItem::Const(it) => NavigationTarget::from_def_source(db, it),
2019-06-11 09:25:55 -05:00
ImplItem::TypeAlias(it) => NavigationTarget::from_def_source(db, it),
2019-03-02 13:57:40 -06:00
}
}
2019-06-08 06:48:56 -05:00
pub(crate) fn from_macro_def(db: &RootDatabase, macro_call: hir::MacroDef) -> NavigationTarget {
2019-06-11 09:40:18 -05:00
let src = macro_call.source(db);
log::debug!("nav target {}", src.ast.syntax().debug_dump());
2019-06-09 10:59:59 -05:00
NavigationTarget::from_named(
2019-06-11 09:40:18 -05:00
src.file_id.original_file(db),
&*src.ast,
src.ast.doc_comment_text(),
2019-06-09 10:59:59 -05:00
None,
)
2019-04-24 15:16:50 -05: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))
}
if let Some(container_name) = self.container_name() {
buf.push_str(&format!(" {}", container_name))
}
2019-01-11 09:17:20 -06:00
buf
}
/// Allows `NavigationTarget` to be created from a `NameOwner`
2019-06-08 14:27:01 -05:00
pub(crate) fn from_named(
file_id: FileId,
node: &impl ast::NameOwner,
docs: Option<String>,
2019-06-09 10:59:59 -05:00
description: Option<String>,
2019-06-08 14:27:01 -05:00
) -> NavigationTarget {
2019-04-10 03:15:55 -05:00
//FIXME: use `_` instead of empty string
2019-01-11 04:31:21 -06:00
let name = node.name().map(|it| it.text().clone()).unwrap_or_default();
2019-01-11 05:00:54 -06:00
let focus_range = node.name().map(|it| it.syntax().range());
2019-06-09 10:59:59 -05:00
NavigationTarget::from_syntax(file_id, name, focus_range, node.syntax(), docs, description)
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>,
node: &SyntaxNode,
2019-06-08 14:27:01 -05:00
docs: Option<String>,
2019-06-09 10:59:59 -05:00
description: Option<String>,
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-01-11 04:05:45 -06:00
kind: node.kind(),
2019-01-11 09:17:20 -06:00
full_range: node.range(),
2019-01-11 05:00:54 -06:00
focus_range,
2019-01-11 09:35:04 -06:00
// ptr: Some(LocalSyntaxPtr::new(node)),
container_name: None,
2019-06-09 10:59:59 -05:00
description,
2019-06-08 14:27:01 -05:00
docs,
2019-01-11 04:05:45 -06:00
}
}
2019-06-08 09:26:27 -05:00
}
2019-06-10 11:34:43 -05:00
pub(crate) fn docs_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> Option<String> {
2019-06-08 14:27:01 -05:00
let file = db.parse(symbol.file_id).tree;
let node = symbol.ptr.to_node(file.syntax()).to_owned();
2019-06-08 09:26:27 -05:00
fn doc_comments<N: ast::DocCommentsOwner>(node: &N) -> Option<String> {
node.doc_comment_text()
}
2019-06-08 09:26:27 -05:00
visitor()
.visit(doc_comments::<ast::FnDef>)
.visit(doc_comments::<ast::StructDef>)
.visit(doc_comments::<ast::EnumDef>)
.visit(doc_comments::<ast::TraitDef>)
.visit(doc_comments::<ast::Module>)
.visit(doc_comments::<ast::TypeAliasDef>)
.visit(doc_comments::<ast::ConstDef>)
.visit(doc_comments::<ast::StaticDef>)
.visit(doc_comments::<ast::NamedFieldDef>)
.visit(doc_comments::<ast::EnumVariant>)
.visit(doc_comments::<ast::MacroCall>)
.accept(&node)?
}
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> {
let file = db.parse(symbol.file_id).tree;
let node = symbol.ptr.to_node(file.syntax()).to_owned();
2019-06-08 09:26:27 -05:00
visitor()
2019-06-09 14:28:53 -05:00
.visit(|node: &ast::FnDef| node.short_label())
.visit(|node: &ast::StructDef| node.short_label())
.visit(|node: &ast::EnumDef| node.short_label())
.visit(|node: &ast::TraitDef| node.short_label())
.visit(|node: &ast::Module| node.short_label())
.visit(|node: &ast::TypeAliasDef| node.short_label())
.visit(|node: &ast::ConstDef| node.short_label())
.visit(|node: &ast::StaticDef| node.short_label())
.visit(|node: &ast::NamedFieldDef| node.short_label())
.visit(|node: &ast::EnumVariant| node.short_label())
2019-06-08 09:26:27 -05:00
.accept(&node)?
2019-01-11 04:01:35 -06:00
}