2019-01-15 09:50:16 -06:00
|
|
|
use ra_db::FileId;
|
2019-01-11 04:28:59 -06:00
|
|
|
use ra_syntax::{
|
2019-01-11 05:00:54 -06:00
|
|
|
SyntaxNode, AstNode, SmolStr, TextRange, ast,
|
|
|
|
SyntaxKind::{self, NAME},
|
2019-01-11 04:28:59 -06:00
|
|
|
};
|
|
|
|
use hir::{Def, ModuleSource};
|
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-01-11 04:01:35 -06:00
|
|
|
|
|
|
|
impl NavigationTarget {
|
2019-01-11 05:00:54 -06:00
|
|
|
pub fn name(&self) -> &SmolStr {
|
|
|
|
&self.name
|
|
|
|
}
|
|
|
|
|
|
|
|
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-01-11 09:17:20 -06:00
|
|
|
/// A "most interesting" range withing the `range_full`.
|
2019-01-11 05:00:54 -06:00
|
|
|
///
|
|
|
|
/// Typically, `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
|
|
|
|
}
|
|
|
|
|
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-01-11 05:00:54 -06:00
|
|
|
focus_range: None,
|
2019-01-11 04:01:35 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-11 05:00:54 -06:00
|
|
|
pub(crate) fn from_scope_entry(
|
|
|
|
file_id: FileId,
|
|
|
|
entry: &hir::ScopeEntryWithSyntax,
|
|
|
|
) -> NavigationTarget {
|
|
|
|
NavigationTarget {
|
|
|
|
file_id,
|
|
|
|
name: entry.name().to_string().into(),
|
2019-01-11 09:17:20 -06:00
|
|
|
full_range: entry.ptr().range(),
|
2019-01-11 05:00:54 -06:00
|
|
|
focus_range: None,
|
|
|
|
kind: NAME,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-01-11 05:00:54 -06:00
|
|
|
let name = module
|
2019-01-15 09:26:29 -06:00
|
|
|
.name(db)
|
2019-01-11 05:00:54 -06:00
|
|
|
.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-01-13 12:56:20 -06:00
|
|
|
let name = module
|
2019-01-15 09:26:29 -06:00
|
|
|
.name(db)
|
2019-01-13 12:56:20 -06:00
|
|
|
.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-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-11 04:01:35 -06:00
|
|
|
// TODO once Def::Item is gone, this should be able to always return a NavigationTarget
|
2019-01-23 14:14:13 -06:00
|
|
|
pub(crate) fn from_def(
|
|
|
|
db: &RootDatabase,
|
|
|
|
module_def: hir::ModuleDef,
|
|
|
|
) -> Option<NavigationTarget> {
|
|
|
|
let def = match module_def {
|
|
|
|
hir::ModuleDef::Def(def_id) => def_id.resolve(db),
|
|
|
|
hir::ModuleDef::Module(module) => {
|
|
|
|
return Some(NavigationTarget::from_module(db, module));
|
|
|
|
}
|
2019-01-24 06:40:52 -06:00
|
|
|
hir::ModuleDef::Function(func) => {
|
|
|
|
return Some(NavigationTarget::from_function(db, func));
|
|
|
|
}
|
2019-01-23 14:14:13 -06:00
|
|
|
};
|
|
|
|
|
2019-01-11 04:29:53 -06:00
|
|
|
let res = match def {
|
2019-01-11 04:01:35 -06:00
|
|
|
Def::Struct(s) => {
|
2019-01-15 09:43:25 -06:00
|
|
|
let (file_id, node) = s.source(db);
|
2019-01-11 04:29:53 -06:00
|
|
|
NavigationTarget::from_named(file_id.original_file(db), &*node)
|
2019-01-11 04:01:35 -06:00
|
|
|
}
|
|
|
|
Def::Enum(e) => {
|
2019-01-15 09:43:25 -06:00
|
|
|
let (file_id, node) = e.source(db);
|
2019-01-11 04:29:53 -06:00
|
|
|
NavigationTarget::from_named(file_id.original_file(db), &*node)
|
2019-01-11 04:01:35 -06:00
|
|
|
}
|
|
|
|
Def::EnumVariant(ev) => {
|
2019-01-15 09:43:25 -06:00
|
|
|
let (file_id, node) = ev.source(db);
|
2019-01-11 04:29:53 -06:00
|
|
|
NavigationTarget::from_named(file_id.original_file(db), &*node)
|
2019-01-11 04:01:35 -06:00
|
|
|
}
|
2019-01-11 12:02:12 -06:00
|
|
|
Def::Trait(f) => {
|
2019-01-15 09:45:01 -06:00
|
|
|
let (file_id, node) = f.source(db);
|
2019-01-11 12:02:12 -06:00
|
|
|
NavigationTarget::from_named(file_id.original_file(db), &*node)
|
|
|
|
}
|
|
|
|
Def::Type(f) => {
|
2019-01-15 09:45:01 -06:00
|
|
|
let (file_id, node) = f.source(db);
|
2019-01-11 12:02:12 -06:00
|
|
|
NavigationTarget::from_named(file_id.original_file(db), &*node)
|
|
|
|
}
|
|
|
|
Def::Static(f) => {
|
2019-01-15 09:45:01 -06:00
|
|
|
let (file_id, node) = f.source(db);
|
2019-01-11 12:02:12 -06:00
|
|
|
NavigationTarget::from_named(file_id.original_file(db), &*node)
|
|
|
|
}
|
|
|
|
Def::Const(f) => {
|
2019-01-15 09:45:01 -06:00
|
|
|
let (file_id, node) = f.source(db);
|
2019-01-11 12:02:12 -06:00
|
|
|
NavigationTarget::from_named(file_id.original_file(db), &*node)
|
|
|
|
}
|
2019-01-15 09:50:16 -06:00
|
|
|
Def::Item => return None,
|
2019-01-11 04:29:53 -06:00
|
|
|
};
|
2019-01-15 09:50:16 -06:00
|
|
|
Some(res)
|
2019-01-11 04:01:35 -06:00
|
|
|
}
|
2019-01-11 04:05:45 -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))
|
|
|
|
}
|
|
|
|
buf
|
|
|
|
}
|
|
|
|
|
2019-01-11 04:28:59 -06:00
|
|
|
fn from_named(file_id: FileId, node: &impl ast::NameOwner) -> NavigationTarget {
|
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-01-11 04:05:45 -06:00
|
|
|
}
|
|
|
|
}
|
2019-01-11 04:01:35 -06:00
|
|
|
}
|