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 /// When `focus_range` is specified, returns it. otherwise
27 /// returns `full_range`
28 pub fn range(&self) -> TextRange {
29 self.focus_range.unwrap_or(self.full_range)
32 pub fn name(&self) -> &SmolStr {
36 pub fn container_name(&self) -> Option<&SmolStr> {
37 self.container_name.as_ref()
40 pub fn kind(&self) -> SyntaxKind {
44 pub fn file_id(&self) -> FileId {
48 pub fn full_range(&self) -> TextRange {
52 /// A "most interesting" range withing the `full_range`.
54 /// Typically, `full_range` is the whole syntax node,
55 /// including doc comments, and `focus_range` is the range of the identifier.
56 pub fn focus_range(&self) -> Option<TextRange> {
60 pub(crate) fn from_bind_pat(file_id: FileId, pat: &ast::BindPat) -> NavigationTarget {
61 NavigationTarget::from_named(file_id, pat)
64 pub(crate) fn from_symbol(symbol: FileSymbol) -> NavigationTarget {
66 file_id: symbol.file_id,
67 name: symbol.name.clone(),
68 kind: symbol.ptr.kind(),
69 full_range: symbol.ptr.range(),
71 container_name: symbol.container_name.clone(),
75 pub(crate) fn from_scope_entry(
79 ) -> NavigationTarget {
82 name: name.to_string().into(),
83 full_range: ptr.range(),
90 pub(crate) fn from_module(db: &RootDatabase, module: hir::Module) -> NavigationTarget {
91 let (file_id, source) = module.definition_source(db);
92 let file_id = file_id.as_original_file();
93 let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default();
95 ModuleSource::SourceFile(node) => {
96 NavigationTarget::from_syntax(file_id, name, None, node.syntax())
98 ModuleSource::Module(node) => {
99 NavigationTarget::from_syntax(file_id, name, None, node.syntax())
104 pub(crate) fn from_module_to_decl(db: &RootDatabase, module: hir::Module) -> NavigationTarget {
105 let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default();
106 if let Some((file_id, source)) = module.declaration_source(db) {
107 let file_id = file_id.as_original_file();
108 return NavigationTarget::from_syntax(file_id, name, None, source.syntax());
110 NavigationTarget::from_module(db, module)
113 pub(crate) fn from_function(db: &RootDatabase, func: hir::Function) -> NavigationTarget {
114 let (file_id, fn_def) = func.source(db);
115 NavigationTarget::from_named(file_id.original_file(db), &*fn_def)
118 pub(crate) fn from_field(db: &RootDatabase, field: hir::StructField) -> NavigationTarget {
119 let (file_id, field) = field.source(db);
120 let file_id = file_id.original_file(db);
122 FieldSource::Named(it) => NavigationTarget::from_named(file_id, &*it),
123 FieldSource::Pos(it) => {
124 NavigationTarget::from_syntax(file_id, "".into(), None, it.syntax())
129 pub(crate) fn from_def(db: &RootDatabase, module_def: hir::ModuleDef) -> NavigationTarget {
131 hir::ModuleDef::Module(module) => NavigationTarget::from_module(db, module),
132 hir::ModuleDef::Function(func) => NavigationTarget::from_function(db, func),
133 hir::ModuleDef::Struct(s) => {
134 let (file_id, node) = s.source(db);
135 NavigationTarget::from_named(file_id.original_file(db), &*node)
137 hir::ModuleDef::Const(s) => {
138 let (file_id, node) = s.source(db);
139 NavigationTarget::from_named(file_id.original_file(db), &*node)
141 hir::ModuleDef::Static(s) => {
142 let (file_id, node) = s.source(db);
143 NavigationTarget::from_named(file_id.original_file(db), &*node)
145 hir::ModuleDef::Enum(e) => {
146 let (file_id, node) = e.source(db);
147 NavigationTarget::from_named(file_id.original_file(db), &*node)
149 hir::ModuleDef::EnumVariant(var) => {
150 let (file_id, node) = var.source(db);
151 NavigationTarget::from_named(file_id.original_file(db), &*node)
153 hir::ModuleDef::Trait(e) => {
154 let (file_id, node) = e.source(db);
155 NavigationTarget::from_named(file_id.original_file(db), &*node)
157 hir::ModuleDef::Type(e) => {
158 let (file_id, node) = e.source(db);
159 NavigationTarget::from_named(file_id.original_file(db), &*node)
164 pub(crate) fn from_impl_block(
166 impl_block: hir::ImplBlock,
167 ) -> NavigationTarget {
168 let (file_id, node) = impl_block.source(db);
169 NavigationTarget::from_syntax(
170 file_id.as_original_file(),
178 pub(crate) fn assert_match(&self, expected: &str) {
179 let actual = self.debug_render();
180 test_utils::assert_eq_text!(expected.trim(), actual.trim(),);
184 pub(crate) fn debug_render(&self) -> String {
185 let mut buf = format!(
192 if let Some(focus_range) = self.focus_range() {
193 buf.push_str(&format!(" {:?}", focus_range))
195 if let Some(container_name) = self.container_name() {
196 buf.push_str(&format!(" {:?}", container_name))
201 fn from_named(file_id: FileId, node: &impl ast::NameOwner) -> NavigationTarget {
202 let name = node.name().map(|it| it.text().clone()).unwrap_or_default();
203 let focus_range = node.name().map(|it| it.syntax().range());
204 NavigationTarget::from_syntax(file_id, name, focus_range, node.syntax())
210 focus_range: Option<TextRange>,
212 ) -> NavigationTarget {
217 full_range: node.range(),
219 // ptr: Some(LocalSyntaxPtr::new(node)),
220 container_name: None,