3 SyntaxNode, AstNode, SmolStr, TextRange, ast,
4 SyntaxKind::{self, NAME},
6 use hir::{ModuleSource, FieldSource};
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(db: &RootDatabase, module: hir::Module) -> NavigationTarget {
73 let (file_id, source) = module.definition_source(db);
76 .map(|it| it.to_string().into())
79 ModuleSource::SourceFile(node) => {
80 NavigationTarget::from_syntax(file_id, name, None, node.syntax())
82 ModuleSource::Module(node) => {
83 NavigationTarget::from_syntax(file_id, name, None, node.syntax())
88 pub(crate) fn from_module_to_decl(db: &RootDatabase, module: hir::Module) -> NavigationTarget {
91 .map(|it| it.to_string().into())
93 if let Some((file_id, source)) = module.declaration_source(db) {
94 return NavigationTarget::from_syntax(file_id, name, None, source.syntax());
96 NavigationTarget::from_module(db, module)
99 pub(crate) fn from_function(db: &RootDatabase, func: hir::Function) -> NavigationTarget {
100 let (file_id, fn_def) = func.source(db);
101 NavigationTarget::from_named(file_id.original_file(db), &*fn_def)
104 pub(crate) fn from_field(db: &RootDatabase, field: hir::StructField) -> NavigationTarget {
105 let (file_id, field) = field.source(db);
106 let file_id = file_id.original_file(db);
108 FieldSource::Named(it) => NavigationTarget::from_named(file_id, &*it),
109 FieldSource::Pos(it) => {
110 NavigationTarget::from_syntax(file_id, "".into(), None, it.syntax())
115 pub(crate) fn from_def(db: &RootDatabase, module_def: hir::ModuleDef) -> NavigationTarget {
117 hir::ModuleDef::Module(module) => NavigationTarget::from_module(db, module),
118 hir::ModuleDef::Function(func) => NavigationTarget::from_function(db, func),
119 hir::ModuleDef::Struct(s) => {
120 let (file_id, node) = s.source(db);
121 NavigationTarget::from_named(file_id.original_file(db), &*node)
123 hir::ModuleDef::Const(s) => {
124 let (file_id, node) = s.source(db);
125 NavigationTarget::from_named(file_id.original_file(db), &*node)
127 hir::ModuleDef::Static(s) => {
128 let (file_id, node) = s.source(db);
129 NavigationTarget::from_named(file_id.original_file(db), &*node)
131 hir::ModuleDef::Enum(e) => {
132 let (file_id, node) = e.source(db);
133 NavigationTarget::from_named(file_id.original_file(db), &*node)
135 hir::ModuleDef::EnumVariant(var) => {
136 let (file_id, node) = var.source(db);
137 NavigationTarget::from_named(file_id.original_file(db), &*node)
139 hir::ModuleDef::Trait(e) => {
140 let (file_id, node) = e.source(db);
141 NavigationTarget::from_named(file_id.original_file(db), &*node)
143 hir::ModuleDef::Type(e) => {
144 let (file_id, node) = e.source(db);
145 NavigationTarget::from_named(file_id.original_file(db), &*node)
150 pub(crate) fn from_impl_block(
153 impl_block: &hir::ImplBlock,
154 ) -> NavigationTarget {
155 let (file_id, _) = module.definition_source(db);
156 let node = module.impl_source(db, impl_block.id());
157 NavigationTarget::from_syntax(file_id, "impl".into(), None, node.syntax())
161 pub(crate) fn assert_match(&self, expected: &str) {
162 let actual = self.debug_render();
163 test_utils::assert_eq_text!(expected.trim(), actual.trim(),);
167 pub(crate) fn debug_render(&self) -> String {
168 let mut buf = format!(
175 if let Some(focus_range) = self.focus_range() {
176 buf.push_str(&format!(" {:?}", focus_range))
181 fn from_named(file_id: FileId, node: &impl ast::NameOwner) -> NavigationTarget {
182 let name = node.name().map(|it| it.text().clone()).unwrap_or_default();
183 let focus_range = node.name().map(|it| it.syntax().range());
184 NavigationTarget::from_syntax(file_id, name, focus_range, node.syntax())
190 focus_range: Option<TextRange>,
192 ) -> NavigationTarget {
197 full_range: node.range(),
199 // ptr: Some(LocalSyntaxPtr::new(node)),