2019-04-09 06:43:11 -05:00
|
|
|
use ra_db::{FileId, SourceDatabase};
|
2019-01-11 04:28:59 -06:00
|
|
|
use ra_syntax::{
|
2019-04-10 03:15:55 -05:00
|
|
|
SyntaxNode, AstNode, SmolStr, TextRange, TreeArc, AstPtr,
|
2019-01-11 05:00:54 -06:00
|
|
|
SyntaxKind::{self, NAME},
|
2019-04-09 06:43:11 -05:00
|
|
|
ast::{self, NameOwner, VisibilityOwner, TypeAscriptionOwner},
|
|
|
|
algo::visit::{visitor, Visitor},
|
2019-01-11 04:28:59 -06:00
|
|
|
};
|
2019-05-22 11:49:22 -05: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};
|
|
|
|
|
|
|
|
/// `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>,
|
2019-02-12 13:47:51 -06:00
|
|
|
container_name: Option<SmolStr>,
|
2019-01-11 05:00:54 -06:00
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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-02-17 05:38:32 -06:00
|
|
|
/// A "most interesting" range withing the `full_range`.
|
2019-01-11 05:00:54 -06:00
|
|
|
///
|
2019-02-17 05:38:32 -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
|
|
|
|
}
|
|
|
|
|
2019-02-17 05:38:32 -06:00
|
|
|
pub(crate) fn from_bind_pat(file_id: FileId, pat: &ast::BindPat) -> NavigationTarget {
|
|
|
|
NavigationTarget::from_named(file_id, pat)
|
|
|
|
}
|
|
|
|
|
2019-01-11 04:01:35 -06:00
|
|
|
pub(crate) fn from_symbol(symbol: FileSymbol) -> NavigationTarget {
|
|
|
|
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(),
|
2019-02-23 05:05:45 -06:00
|
|
|
focus_range: symbol.name_range,
|
2019-02-13 09:28:15 -06:00
|
|
|
container_name: symbol.container_name.clone(),
|
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,
|
2019-05-22 11:49:22 -05:00
|
|
|
pat: AstPtr<ast::Pat>,
|
2019-01-11 05:00:54 -06:00
|
|
|
) -> NavigationTarget {
|
2019-04-10 03:15:55 -05:00
|
|
|
let file = db.parse(file_id);
|
2019-05-22 11:49:22 -05:00
|
|
|
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-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,
|
2019-02-12 13:47:51 -06:00
|
|
|
container_name: None,
|
2019-01-11 05:00:54 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-22 11:49:22 -05: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());
|
|
|
|
NavigationTarget {
|
|
|
|
file_id,
|
|
|
|
name,
|
|
|
|
full_range,
|
|
|
|
focus_range: None,
|
|
|
|
kind: NAME,
|
|
|
|
container_name: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-15 09:50:16 -06:00
|
|
|
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);
|
2019-02-16 14:09:58 -06:00
|
|
|
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();
|
2019-01-15 09:50:16 -06:00
|
|
|
match source {
|
2019-01-11 05:00:54 -06:00
|
|
|
ModuleSource::SourceFile(node) => {
|
|
|
|
NavigationTarget::from_syntax(file_id, name, None, node.syntax())
|
|
|
|
}
|
|
|
|
ModuleSource::Module(node) => {
|
|
|
|
NavigationTarget::from_syntax(file_id, name, None, node.syntax())
|
|
|
|
}
|
2019-01-15 09:50:16 -06:00
|
|
|
}
|
2019-01-11 05:00:54 -06: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-01-15 09:26:29 -06:00
|
|
|
if let Some((file_id, source)) = module.declaration_source(db) {
|
2019-02-16 14:09:58 -06:00
|
|
|
let file_id = file_id.as_original_file();
|
2019-01-15 09:50:16 -06:00
|
|
|
return NavigationTarget::from_syntax(file_id, name, None, source.syntax());
|
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 {
|
|
|
|
let (file_id, fn_def) = func.source(db);
|
|
|
|
NavigationTarget::from_named(file_id.original_file(db), &*fn_def)
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
FieldSource::Named(it) => NavigationTarget::from_named(file_id, &*it),
|
|
|
|
FieldSource::Pos(it) => {
|
|
|
|
NavigationTarget::from_syntax(file_id, "".into(), None, it.syntax())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-07 02:32:39 -06:00
|
|
|
pub(crate) fn from_adt_def(db: &RootDatabase, adt_def: hir::AdtDef) -> NavigationTarget {
|
|
|
|
match adt_def {
|
|
|
|
hir::AdtDef::Struct(s) => {
|
|
|
|
let (file_id, node) = s.source(db);
|
|
|
|
NavigationTarget::from_named(file_id.original_file(db), &*node)
|
|
|
|
}
|
|
|
|
hir::AdtDef::Enum(s) => {
|
|
|
|
let (file_id, node) = s.source(db);
|
|
|
|
NavigationTarget::from_named(file_id.original_file(db), &*node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-25 11:55:38 -06:00
|
|
|
pub(crate) fn from_def(db: &RootDatabase, module_def: hir::ModuleDef) -> NavigationTarget {
|
2019-01-24 16:31:32 -06:00
|
|
|
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-01-24 10:04:02 -06:00
|
|
|
hir::ModuleDef::Struct(s) => {
|
2019-01-15 09:43:25 -06:00
|
|
|
let (file_id, node) = s.source(db);
|
2019-01-25 11:55:38 -06:00
|
|
|
NavigationTarget::from_named(file_id.original_file(db), &*node)
|
2019-01-11 04:01:35 -06:00
|
|
|
}
|
2019-01-24 15:50:08 -06:00
|
|
|
hir::ModuleDef::Const(s) => {
|
|
|
|
let (file_id, node) = s.source(db);
|
2019-01-25 11:55:38 -06:00
|
|
|
NavigationTarget::from_named(file_id.original_file(db), &*node)
|
2019-01-24 15:50:08 -06:00
|
|
|
}
|
|
|
|
hir::ModuleDef::Static(s) => {
|
|
|
|
let (file_id, node) = s.source(db);
|
2019-01-25 11:55:38 -06:00
|
|
|
NavigationTarget::from_named(file_id.original_file(db), &*node)
|
2019-01-24 15:50:08 -06:00
|
|
|
}
|
2019-01-24 10:04:02 -06:00
|
|
|
hir::ModuleDef::Enum(e) => {
|
2019-01-15 09:43:25 -06:00
|
|
|
let (file_id, node) = e.source(db);
|
2019-01-25 11:55:38 -06:00
|
|
|
NavigationTarget::from_named(file_id.original_file(db), &*node)
|
2019-01-11 04:01:35 -06:00
|
|
|
}
|
2019-01-24 14:36:16 -06:00
|
|
|
hir::ModuleDef::EnumVariant(var) => {
|
|
|
|
let (file_id, node) = var.source(db);
|
2019-01-25 11:55:38 -06:00
|
|
|
NavigationTarget::from_named(file_id.original_file(db), &*node)
|
2019-01-24 14:36:16 -06:00
|
|
|
}
|
2019-01-24 16:31:32 -06:00
|
|
|
hir::ModuleDef::Trait(e) => {
|
|
|
|
let (file_id, node) = e.source(db);
|
2019-01-25 11:55:38 -06:00
|
|
|
NavigationTarget::from_named(file_id.original_file(db), &*node)
|
2019-01-11 12:02:12 -06:00
|
|
|
}
|
2019-02-24 14:36:49 -06:00
|
|
|
hir::ModuleDef::TypeAlias(e) => {
|
2019-01-24 16:31:32 -06:00
|
|
|
let (file_id, node) = e.source(db);
|
2019-01-25 11:55:38 -06:00
|
|
|
NavigationTarget::from_named(file_id.original_file(db), &*node)
|
2019-01-11 12:02:12 -06:00
|
|
|
}
|
2019-01-24 16:31:32 -06:00
|
|
|
}
|
2019-01-11 04:01:35 -06:00
|
|
|
}
|
2019-01-11 04:05:45 -06:00
|
|
|
|
2019-01-28 08:26:32 -06:00
|
|
|
pub(crate) fn from_impl_block(
|
|
|
|
db: &RootDatabase,
|
2019-02-16 14:09:58 -06:00
|
|
|
impl_block: hir::ImplBlock,
|
2019-01-28 08:26:32 -06:00
|
|
|
) -> NavigationTarget {
|
2019-02-16 14:09:58 -06:00
|
|
|
let (file_id, node) = impl_block.source(db);
|
|
|
|
NavigationTarget::from_syntax(
|
|
|
|
file_id.as_original_file(),
|
|
|
|
"impl".into(),
|
|
|
|
None,
|
|
|
|
node.syntax(),
|
|
|
|
)
|
2019-01-28 08:26:32 -06:00
|
|
|
}
|
|
|
|
|
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-03-04 07:34:41 -06:00
|
|
|
ImplItem::Method(f) => NavigationTarget::from_function(db, f),
|
2019-03-02 13:57:40 -06:00
|
|
|
ImplItem::Const(c) => {
|
|
|
|
let (file_id, node) = c.source(db);
|
|
|
|
NavigationTarget::from_named(file_id.original_file(db), &*node)
|
|
|
|
}
|
|
|
|
ImplItem::TypeAlias(a) => {
|
|
|
|
let (file_id, node) = a.source(db);
|
|
|
|
NavigationTarget::from_named(file_id.original_file(db), &*node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-24 15:16:50 -05:00
|
|
|
pub(crate) fn from_macro_def(
|
|
|
|
db: &RootDatabase,
|
|
|
|
macro_call: hir::MacroByExampleDef,
|
|
|
|
) -> NavigationTarget {
|
|
|
|
let (file_id, node) = macro_call.source(db);
|
|
|
|
log::debug!("nav target {}", node.syntax().debug_dump());
|
|
|
|
NavigationTarget::from_named(file_id.original_file(db), &*node)
|
|
|
|
}
|
|
|
|
|
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`
|
|
|
|
pub(crate) fn from_named(file_id: FileId, node: &impl ast::NameOwner) -> 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());
|
|
|
|
NavigationTarget::from_syntax(file_id, name, focus_range, node.syntax())
|
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,
|
|
|
|
) -> 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)),
|
2019-02-12 13:47:51 -06:00
|
|
|
container_name: None,
|
2019-01-11 04:05:45 -06:00
|
|
|
}
|
|
|
|
}
|
2019-04-09 06:43:11 -05:00
|
|
|
|
|
|
|
pub(crate) fn node(&self, db: &RootDatabase) -> Option<TreeArc<SyntaxNode>> {
|
|
|
|
let source_file = db.parse(self.file_id());
|
|
|
|
let source_file = source_file.syntax();
|
|
|
|
let node = source_file
|
|
|
|
.descendants()
|
|
|
|
.find(|node| node.kind() == self.kind() && node.range() == self.full_range())?
|
|
|
|
.to_owned();
|
|
|
|
Some(node)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn docs(&self, db: &RootDatabase) -> Option<String> {
|
|
|
|
let node = self.node(db)?;
|
|
|
|
fn doc_comments<N: ast::DocCommentsOwner>(node: &N) -> Option<String> {
|
|
|
|
node.doc_comment_text()
|
|
|
|
}
|
|
|
|
|
|
|
|
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>)
|
2019-04-24 15:16:50 -05:00
|
|
|
.visit(doc_comments::<ast::MacroCall>)
|
2019-04-09 06:43:11 -05:00
|
|
|
.accept(&node)?
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a description of this node.
|
|
|
|
///
|
|
|
|
/// e.g. `struct Name`, `enum Name`, `fn Name`
|
|
|
|
pub(crate) fn description(&self, db: &RootDatabase) -> Option<String> {
|
|
|
|
// FIXME: After type inference is done, add type information to improve the output
|
|
|
|
let node = self.node(db)?;
|
|
|
|
|
|
|
|
fn visit_ascribed_node<T>(node: &T, prefix: &str) -> Option<String>
|
|
|
|
where
|
|
|
|
T: NameOwner + VisibilityOwner + TypeAscriptionOwner,
|
|
|
|
{
|
|
|
|
let mut string = visit_node(node, prefix)?;
|
|
|
|
|
|
|
|
if let Some(type_ref) = node.ascribed_type() {
|
|
|
|
string.push_str(": ");
|
|
|
|
type_ref.syntax().text().push_to(&mut string);
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_node<T>(node: &T, label: &str) -> Option<String>
|
|
|
|
where
|
|
|
|
T: NameOwner + VisibilityOwner,
|
|
|
|
{
|
|
|
|
let mut string =
|
|
|
|
node.visibility().map(|v| format!("{} ", v.syntax().text())).unwrap_or_default();
|
|
|
|
string.push_str(label);
|
|
|
|
string.push_str(node.name()?.text().as_str());
|
|
|
|
Some(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
visitor()
|
|
|
|
.visit(|node: &ast::FnDef| Some(crate::display::function_label(node)))
|
|
|
|
.visit(|node: &ast::StructDef| visit_node(node, "struct "))
|
|
|
|
.visit(|node: &ast::EnumDef| visit_node(node, "enum "))
|
|
|
|
.visit(|node: &ast::TraitDef| visit_node(node, "trait "))
|
|
|
|
.visit(|node: &ast::Module| visit_node(node, "mod "))
|
|
|
|
.visit(|node: &ast::TypeAliasDef| visit_node(node, "type "))
|
|
|
|
.visit(|node: &ast::ConstDef| visit_ascribed_node(node, "const "))
|
|
|
|
.visit(|node: &ast::StaticDef| visit_ascribed_node(node, "static "))
|
|
|
|
.visit(|node: &ast::NamedFieldDef| visit_ascribed_node(node, ""))
|
|
|
|
.visit(|node: &ast::EnumVariant| Some(node.name()?.text().to_string()))
|
|
|
|
.accept(&node)?
|
|
|
|
}
|
2019-01-11 04:01:35 -06:00
|
|
|
}
|