3 SyntaxNode, SyntaxNodePtr, AstNode, SmolStr, TextRange, ast,
4 SyntaxKind::{self, NAME},
6 use hir::{ModuleSource, FieldSource, Name};
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>,
22 container_name: Option<SmolStr>,
25 impl NavigationTarget {
26 pub fn name(&self) -> &SmolStr {
30 pub fn container_name(&self) -> Option<&SmolStr> {
31 self.container_name.as_ref()
34 pub fn kind(&self) -> SyntaxKind {
38 pub fn file_id(&self) -> FileId {
42 pub fn full_range(&self) -> TextRange {
46 /// A "most interesting" range withing the `range_full`.
48 /// Typically, `range` is the whole syntax node, including doc comments, and
49 /// `focus_range` is the range of the identifier.
50 pub fn focus_range(&self) -> Option<TextRange> {
54 pub(crate) fn from_symbol(symbol: FileSymbol) -> NavigationTarget {
56 file_id: symbol.file_id,
57 name: symbol.name.clone(),
58 kind: symbol.ptr.kind(),
59 full_range: symbol.ptr.range(),
61 container_name: symbol.container_name.map(|v| v.clone()),
65 pub(crate) fn from_scope_entry(
69 ) -> NavigationTarget {
72 name: name.to_string().into(),
73 full_range: ptr.range(),
80 pub(crate) fn from_module(db: &RootDatabase, module: hir::Module) -> NavigationTarget {
81 let (file_id, source) = module.definition_source(db);
82 let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default();
84 ModuleSource::SourceFile(node) => {
85 NavigationTarget::from_syntax(file_id, name, None, node.syntax())
87 ModuleSource::Module(node) => {
88 NavigationTarget::from_syntax(file_id, name, None, node.syntax())
93 pub(crate) fn from_module_to_decl(db: &RootDatabase, module: hir::Module) -> NavigationTarget {
94 let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default();
95 if let Some((file_id, source)) = module.declaration_source(db) {
96 return NavigationTarget::from_syntax(file_id, name, None, source.syntax());
98 NavigationTarget::from_module(db, module)
101 pub(crate) fn from_function(db: &RootDatabase, func: hir::Function) -> NavigationTarget {
102 let (file_id, fn_def) = func.source(db);
103 NavigationTarget::from_named(file_id.original_file(db), &*fn_def)
106 pub(crate) fn from_field(db: &RootDatabase, field: hir::StructField) -> NavigationTarget {
107 let (file_id, field) = field.source(db);
108 let file_id = file_id.original_file(db);
110 FieldSource::Named(it) => NavigationTarget::from_named(file_id, &*it),
111 FieldSource::Pos(it) => {
112 NavigationTarget::from_syntax(file_id, "".into(), None, it.syntax())
117 pub(crate) fn from_def(db: &RootDatabase, module_def: hir::ModuleDef) -> NavigationTarget {
119 hir::ModuleDef::Module(module) => NavigationTarget::from_module(db, module),
120 hir::ModuleDef::Function(func) => NavigationTarget::from_function(db, func),
121 hir::ModuleDef::Struct(s) => {
122 let (file_id, node) = s.source(db);
123 NavigationTarget::from_named(file_id.original_file(db), &*node)
125 hir::ModuleDef::Const(s) => {
126 let (file_id, node) = s.source(db);
127 NavigationTarget::from_named(file_id.original_file(db), &*node)
129 hir::ModuleDef::Static(s) => {
130 let (file_id, node) = s.source(db);
131 NavigationTarget::from_named(file_id.original_file(db), &*node)
133 hir::ModuleDef::Enum(e) => {
134 let (file_id, node) = e.source(db);
135 NavigationTarget::from_named(file_id.original_file(db), &*node)
137 hir::ModuleDef::EnumVariant(var) => {
138 let (file_id, node) = var.source(db);
139 NavigationTarget::from_named(file_id.original_file(db), &*node)
141 hir::ModuleDef::Trait(e) => {
142 let (file_id, node) = e.source(db);
143 NavigationTarget::from_named(file_id.original_file(db), &*node)
145 hir::ModuleDef::Type(e) => {
146 let (file_id, node) = e.source(db);
147 NavigationTarget::from_named(file_id.original_file(db), &*node)
152 pub(crate) fn from_impl_block(
155 impl_block: &hir::ImplBlock,
156 ) -> NavigationTarget {
157 let (file_id, _) = module.definition_source(db);
158 let node = module.impl_source(db, impl_block.id());
159 NavigationTarget::from_syntax(file_id, "impl".into(), None, node.syntax())
163 pub(crate) fn assert_match(&self, expected: &str) {
164 let actual = self.debug_render();
165 test_utils::assert_eq_text!(expected.trim(), actual.trim(),);
169 pub(crate) fn debug_render(&self) -> String {
170 let mut buf = format!(
177 if let Some(focus_range) = self.focus_range() {
178 buf.push_str(&format!(" {:?}", focus_range))
180 if let Some(container_name) = self.container_name() {
181 buf.push_str(&format!(" {:?}", container_name))
186 fn from_named(file_id: FileId, node: &impl ast::NameOwner) -> NavigationTarget {
187 let name = node.name().map(|it| it.text().clone()).unwrap_or_default();
188 let focus_range = node.name().map(|it| it.syntax().range());
189 NavigationTarget::from_syntax(file_id, name, focus_range, node.syntax())
195 focus_range: Option<TextRange>,
197 ) -> NavigationTarget {
202 full_range: node.range(),
204 // ptr: Some(LocalSyntaxPtr::new(node)),
205 container_name: None,