1 //! FIXME: write short doc here
4 use hir::{AssocItem, FieldSource, HasSource, InFile, ModuleSource};
5 use ide_db::base_db::{FileId, SourceDatabase};
6 use ide_db::{defs::Definition, RootDatabase};
8 ast::{self, DocCommentsOwner, NameOwner},
9 match_ast, AstNode, SmolStr,
10 SyntaxKind::{self, IDENT_PAT, TYPE_PARAM},
14 use crate::FileSymbol;
16 use super::short_label::ShortLabel;
18 /// `NavigationTarget` represents and element in the editor's UI which you can
19 /// click on to navigate to a particular piece of code.
21 /// Typically, a `NavigationTarget` corresponds to some element in the source
22 /// code, like a function or a struct, but this is not strictly required.
23 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
24 pub struct NavigationTarget {
26 /// Range which encompasses the whole element.
28 /// Should include body, doc comments, attributes, etc.
30 /// Clients should use this range to answer "is the cursor inside the
31 /// element?" question.
32 pub full_range: TextRange,
33 /// A "most interesting" range withing the `full_range`.
35 /// Typically, `full_range` is the whole syntax node, including doc
36 /// comments, and `focus_range` is the range of the identifier. "Most
37 /// interesting" range within the full range, typically the range of
40 /// Clients should place the cursor on this range when navigating to this target.
41 pub focus_range: Option<TextRange>,
44 pub container_name: Option<SmolStr>,
45 pub description: Option<String>,
46 pub docs: Option<String>,
49 pub(crate) trait ToNav {
50 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget;
53 pub(crate) trait TryToNav {
54 fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget>;
57 impl NavigationTarget {
58 pub fn focus_or_full_range(&self) -> TextRange {
59 self.focus_range.unwrap_or(self.full_range)
62 pub(crate) fn from_module_to_decl(db: &RootDatabase, module: hir::Module) -> NavigationTarget {
63 let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default();
64 if let Some(src) = module.declaration_source(db) {
65 let node = src.as_ref().map(|it| it.syntax());
66 let frange = node.original_file_range(db);
67 let mut res = NavigationTarget::from_syntax(
72 src.value.syntax().kind(),
74 res.docs = src.value.doc_comment_text();
75 res.description = src.value.short_label();
82 pub(crate) fn assert_match(&self, expected: &str) {
83 let actual = self.debug_render();
84 test_utils::assert_eq_text!(expected.trim(), actual.trim(),);
88 pub(crate) fn debug_render(&self) -> String {
90 format!("{} {:?} {:?} {:?}", self.name, self.kind, self.file_id, self.full_range);
91 if let Some(focus_range) = self.focus_range {
92 buf.push_str(&format!(" {:?}", focus_range))
94 if let Some(container_name) = &self.container_name {
95 buf.push_str(&format!(" {}", container_name))
100 /// Allows `NavigationTarget` to be created from a `NameOwner`
101 pub(crate) fn from_named(
103 node: InFile<&dyn ast::NameOwner>,
104 ) -> NavigationTarget {
106 node.value.name().map(|it| it.text().clone()).unwrap_or_else(|| SmolStr::new("_"));
108 node.value.name().map(|it| node.with_value(it.syntax()).original_file_range(db).range);
109 let frange = node.map(|it| it.syntax()).original_file_range(db);
111 NavigationTarget::from_syntax(
116 node.value.syntax().kind(),
120 /// Allows `NavigationTarget` to be created from a `DocCommentsOwner` and a `NameOwner`
121 pub(crate) fn from_doc_commented(
123 named: InFile<&dyn ast::NameOwner>,
124 node: InFile<&dyn ast::DocCommentsOwner>,
125 ) -> NavigationTarget {
127 named.value.name().map(|it| it.text().clone()).unwrap_or_else(|| SmolStr::new("_"));
128 let frange = node.map(|it| it.syntax()).original_file_range(db);
130 NavigationTarget::from_syntax(
135 node.value.syntax().kind(),
142 focus_range: Option<TextRange>,
143 full_range: TextRange,
145 ) -> NavigationTarget {
152 container_name: None,
159 impl ToNav for FileSymbol {
160 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
162 file_id: self.file_id,
163 name: self.name.clone(),
165 full_range: self.range,
166 focus_range: self.name_range,
167 container_name: self.container_name.clone(),
168 description: description_from_symbol(db, self),
169 docs: docs_from_symbol(db, self),
174 impl TryToNav for Definition {
175 fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
177 Definition::Macro(it) => Some(it.to_nav(db)),
178 Definition::Field(it) => Some(it.to_nav(db)),
179 Definition::ModuleDef(it) => it.try_to_nav(db),
180 Definition::SelfType(it) => Some(it.to_nav(db)),
181 Definition::Local(it) => Some(it.to_nav(db)),
182 Definition::TypeParam(it) => Some(it.to_nav(db)),
187 impl TryToNav for hir::ModuleDef {
188 fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
189 let res = match self {
190 hir::ModuleDef::Module(it) => it.to_nav(db),
191 hir::ModuleDef::Function(it) => it.to_nav(db),
192 hir::ModuleDef::Adt(it) => it.to_nav(db),
193 hir::ModuleDef::EnumVariant(it) => it.to_nav(db),
194 hir::ModuleDef::Const(it) => it.to_nav(db),
195 hir::ModuleDef::Static(it) => it.to_nav(db),
196 hir::ModuleDef::Trait(it) => it.to_nav(db),
197 hir::ModuleDef::TypeAlias(it) => it.to_nav(db),
198 hir::ModuleDef::BuiltinType(_) => return None,
204 pub(crate) trait ToNavFromAst {}
205 impl ToNavFromAst for hir::Function {}
206 impl ToNavFromAst for hir::Const {}
207 impl ToNavFromAst for hir::Static {}
208 impl ToNavFromAst for hir::Struct {}
209 impl ToNavFromAst for hir::Enum {}
210 impl ToNavFromAst for hir::EnumVariant {}
211 impl ToNavFromAst for hir::Union {}
212 impl ToNavFromAst for hir::TypeAlias {}
213 impl ToNavFromAst for hir::Trait {}
217 D: HasSource + ToNavFromAst + Copy,
218 D::Ast: ast::DocCommentsOwner + ast::NameOwner + ShortLabel,
220 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
221 let src = self.source(db);
223 NavigationTarget::from_named(db, src.as_ref().map(|it| it as &dyn ast::NameOwner));
224 res.docs = src.value.doc_comment_text();
225 res.description = src.value.short_label();
230 impl ToNav for hir::Module {
231 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
232 let src = self.definition_source(db);
233 let name = self.name(db).map(|it| it.to_string().into()).unwrap_or_default();
234 let (syntax, focus) = match &src.value {
235 ModuleSource::SourceFile(node) => (node.syntax(), None),
236 ModuleSource::Module(node) => {
237 (node.syntax(), node.name().map(|it| it.syntax().text_range()))
240 let frange = src.with_value(syntax).original_file_range(db);
241 NavigationTarget::from_syntax(frange.file_id, name, focus, frange.range, syntax.kind())
245 impl ToNav for hir::ImplDef {
246 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
247 let src = self.source(db);
248 let derive_attr = self.is_builtin_derive(db);
249 let frange = if let Some(item) = &derive_attr {
250 item.syntax().original_file_range(db)
252 src.as_ref().map(|it| it.syntax()).original_file_range(db)
254 let focus_range = if derive_attr.is_some() {
257 src.value.self_ty().map(|ty| src.with_value(ty.syntax()).original_file_range(db).range)
260 NavigationTarget::from_syntax(
265 src.value.syntax().kind(),
270 impl ToNav for hir::Field {
271 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
272 let src = self.source(db);
275 FieldSource::Named(it) => {
276 let mut res = NavigationTarget::from_named(db, src.with_value(it));
277 res.docs = it.doc_comment_text();
278 res.description = it.short_label();
281 FieldSource::Pos(it) => {
282 let frange = src.with_value(it.syntax()).original_file_range(db);
283 NavigationTarget::from_syntax(
295 impl ToNav for hir::MacroDef {
296 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
297 let src = self.source(db);
298 log::debug!("nav target {:#?}", src.value.syntax());
300 NavigationTarget::from_named(db, src.as_ref().map(|it| it as &dyn ast::NameOwner));
301 res.docs = src.value.doc_comment_text();
306 impl ToNav for hir::Adt {
307 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
309 hir::Adt::Struct(it) => it.to_nav(db),
310 hir::Adt::Union(it) => it.to_nav(db),
311 hir::Adt::Enum(it) => it.to_nav(db),
316 impl ToNav for hir::AssocItem {
317 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
319 AssocItem::Function(it) => it.to_nav(db),
320 AssocItem::Const(it) => it.to_nav(db),
321 AssocItem::TypeAlias(it) => it.to_nav(db),
326 impl ToNav for hir::Local {
327 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
328 let src = self.source(db);
329 let node = match &src.value {
330 Either::Left(bind_pat) => {
331 bind_pat.name().map_or_else(|| bind_pat.syntax().clone(), |it| it.syntax().clone())
333 Either::Right(it) => it.syntax().clone(),
335 let full_range = src.with_value(&node).original_file_range(db);
336 let name = match self.name(db) {
337 Some(it) => it.to_string().into(),
341 file_id: full_range.file_id,
344 full_range: full_range.range,
346 container_name: None,
353 impl ToNav for hir::TypeParam {
354 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
355 let src = self.source(db);
356 let full_range = match &src.value {
357 Either::Left(it) => it.syntax().text_range(),
358 Either::Right(it) => it.syntax().text_range(),
360 let focus_range = match &src.value {
361 Either::Left(_) => None,
362 Either::Right(it) => it.name().map(|it| it.syntax().text_range()),
365 file_id: src.file_id.original_file(db),
366 name: self.name(db).to_string().into(),
370 container_name: None,
377 pub(crate) fn docs_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> Option<String> {
378 let parse = db.parse(symbol.file_id);
379 let node = symbol.ptr.to_node(parse.tree().syntax());
383 ast::Fn(it) => it.doc_comment_text(),
384 ast::Struct(it) => it.doc_comment_text(),
385 ast::Enum(it) => it.doc_comment_text(),
386 ast::Trait(it) => it.doc_comment_text(),
387 ast::Module(it) => it.doc_comment_text(),
388 ast::TypeAlias(it) => it.doc_comment_text(),
389 ast::Const(it) => it.doc_comment_text(),
390 ast::Static(it) => it.doc_comment_text(),
391 ast::RecordField(it) => it.doc_comment_text(),
392 ast::Variant(it) => it.doc_comment_text(),
393 ast::MacroCall(it) => it.doc_comment_text(),
399 /// Get a description of a symbol.
401 /// e.g. `struct Name`, `enum Name`, `fn Name`
402 pub(crate) fn description_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> Option<String> {
403 let parse = db.parse(symbol.file_id);
404 let node = symbol.ptr.to_node(parse.tree().syntax());
408 ast::Fn(it) => it.short_label(),
409 ast::Struct(it) => it.short_label(),
410 ast::Enum(it) => it.short_label(),
411 ast::Trait(it) => it.short_label(),
412 ast::Module(it) => it.short_label(),
413 ast::TypeAlias(it) => it.short_label(),
414 ast::Const(it) => it.short_label(),
415 ast::Static(it) => it.short_label(),
416 ast::RecordField(it) => it.short_label(),
417 ast::Variant(it) => it.short_label(),
425 use expect_test::expect;
427 use crate::{fixture, Query};
430 fn test_nav_for_symbol() {
431 let (analysis, _) = fixture::file(
434 fn foo() { enum FooInner { } }
438 let navs = analysis.symbol_search(Query::new("FooInner".to_string())).unwrap();
451 container_name: None,
467 container_name: Some(
477 .assert_debug_eq(&navs);
481 fn test_world_symbols_are_case_sensitive() {
482 let (analysis, _) = fixture::file(
489 let navs = analysis.symbol_search(Query::new("foo".to_string())).unwrap();
490 assert_eq!(navs.len(), 2)