1 use ra_db::{FileId, Cancelable};
3 SyntaxNode, AstNode, SmolStr, TextRange, ast,
4 SyntaxKind::{self, NAME},
6 use hir::{Def, ModuleSource};
8 use crate::{FileSymbol, db::RootDatabase};
10 /// `NavigationTarget` represents and element in the editor's UI which you can
11 /// click on to navigate to a particular piece of code.
13 /// Typically, a `NavigationTarget` corresponds to some element in the source
14 /// code, like a function or a struct, but this is not strictly required.
15 #[derive(Debug, Clone)]
16 pub struct NavigationTarget {
20 full_range: TextRange,
21 focus_range: Option<TextRange>,
24 impl NavigationTarget {
25 pub fn name(&self) -> &SmolStr {
29 pub fn kind(&self) -> SyntaxKind {
33 pub fn file_id(&self) -> FileId {
37 pub fn full_range(&self) -> TextRange {
41 /// A "most interesting" range withing the `range_full`.
43 /// Typically, `range` is the whole syntax node, including doc comments, and
44 /// `focus_range` is the range of the identifier.
45 pub fn focus_range(&self) -> Option<TextRange> {
49 pub(crate) fn from_symbol(symbol: FileSymbol) -> NavigationTarget {
51 file_id: symbol.file_id,
52 name: symbol.name.clone(),
53 kind: symbol.ptr.kind(),
54 full_range: symbol.ptr.range(),
59 pub(crate) fn from_scope_entry(
61 entry: &hir::ScopeEntryWithSyntax,
62 ) -> NavigationTarget {
65 name: entry.name().to_string().into(),
66 full_range: entry.ptr().range(),
72 pub(crate) fn from_module(
75 ) -> Cancelable<NavigationTarget> {
76 let (file_id, source) = module.definition_source(db)?;
79 .map(|it| it.to_string().into())
81 let res = match source {
82 ModuleSource::SourceFile(node) => {
83 NavigationTarget::from_syntax(file_id, name, None, node.syntax())
85 ModuleSource::Module(node) => {
86 NavigationTarget::from_syntax(file_id, name, None, node.syntax())
92 pub(crate) fn from_module_to_decl(
95 ) -> Cancelable<NavigationTarget> {
98 .map(|it| it.to_string().into())
100 if let Some((file_id, source)) = module.declaration_source(db)? {
101 return Ok(NavigationTarget::from_syntax(
108 NavigationTarget::from_module(db, module)
111 // TODO once Def::Item is gone, this should be able to always return a NavigationTarget
112 pub(crate) fn from_def(db: &RootDatabase, def: Def) -> Cancelable<Option<NavigationTarget>> {
113 let res = match def {
115 let (file_id, node) = s.source(db)?;
116 NavigationTarget::from_named(file_id.original_file(db), &*node)
119 let (file_id, node) = e.source(db)?;
120 NavigationTarget::from_named(file_id.original_file(db), &*node)
122 Def::EnumVariant(ev) => {
123 let (file_id, node) = ev.source(db)?;
124 NavigationTarget::from_named(file_id.original_file(db), &*node)
126 Def::Function(f) => {
127 let (file_id, node) = f.source(db)?;
128 NavigationTarget::from_named(file_id.original_file(db), &*node)
131 let (file_id, node) = f.source(db)?;
132 NavigationTarget::from_named(file_id.original_file(db), &*node)
135 let (file_id, node) = f.source(db)?;
136 NavigationTarget::from_named(file_id.original_file(db), &*node)
139 let (file_id, node) = f.source(db)?;
140 NavigationTarget::from_named(file_id.original_file(db), &*node)
143 let (file_id, node) = f.source(db)?;
144 NavigationTarget::from_named(file_id.original_file(db), &*node)
146 Def::Module(m) => NavigationTarget::from_module(db, m)?,
147 Def::Item => return Ok(None),
153 pub(crate) fn assert_match(&self, expected: &str) {
154 let actual = self.debug_render();
155 test_utils::assert_eq_text!(expected.trim(), actual.trim(),);
159 pub(crate) fn debug_render(&self) -> String {
160 let mut buf = format!(
167 if let Some(focus_range) = self.focus_range() {
168 buf.push_str(&format!(" {:?}", focus_range))
173 fn from_named(file_id: FileId, node: &impl ast::NameOwner) -> NavigationTarget {
174 let name = node.name().map(|it| it.text().clone()).unwrap_or_default();
175 let focus_range = node.name().map(|it| it.syntax().range());
176 NavigationTarget::from_syntax(file_id, name, focus_range, node.syntax())
182 focus_range: Option<TextRange>,
184 ) -> NavigationTarget {
189 full_range: node.range(),
191 // ptr: Some(LocalSyntaxPtr::new(node)),