mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 12:29:21 +00:00
Remove node function in NavTarget
This commit is contained in:
parent
bb55111c20
commit
d46278d320
5 changed files with 94 additions and 88 deletions
|
@ -5,7 +5,6 @@ mod function_signature;
|
||||||
mod navigation_target;
|
mod navigation_target;
|
||||||
mod structure;
|
mod structure;
|
||||||
|
|
||||||
use crate::db::RootDatabase;
|
|
||||||
use ra_syntax::{ast::{self, AstNode, TypeParamsOwner}, SyntaxKind::{ATTR, COMMENT}};
|
use ra_syntax::{ast::{self, AstNode, TypeParamsOwner}, SyntaxKind::{ATTR, COMMENT}};
|
||||||
|
|
||||||
pub use navigation_target::NavigationTarget;
|
pub use navigation_target::NavigationTarget;
|
||||||
|
@ -73,8 +72,8 @@ where
|
||||||
|
|
||||||
// FIXME: this should not really use navigation target. Rather, approximately
|
// FIXME: this should not really use navigation target. Rather, approximately
|
||||||
// resolved symbol should return a `DefId`.
|
// resolved symbol should return a `DefId`.
|
||||||
pub(crate) fn doc_text_for(db: &RootDatabase, nav: NavigationTarget) -> Option<String> {
|
pub(crate) fn doc_text_for(nav: NavigationTarget) -> Option<String> {
|
||||||
match (nav.description(db), nav.docs(db)) {
|
match (nav.description, nav.docs) {
|
||||||
(Some(desc), docs) => Some(rust_code_markup_with_doc(desc, docs)),
|
(Some(desc), docs) => Some(rust_code_markup_with_doc(desc, docs)),
|
||||||
(None, Some(docs)) => Some(docs),
|
(None, Some(docs)) => Some(docs),
|
||||||
_ => None,
|
_ => None,
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use ra_db::{FileId, SourceDatabase};
|
use ra_db::{FileId, SourceDatabase};
|
||||||
use ra_syntax::{
|
use ra_syntax::{
|
||||||
SyntaxNode, AstNode, SmolStr, TextRange, TreeArc, AstPtr,
|
SyntaxNode, AstNode, SmolStr, TextRange, AstPtr,
|
||||||
SyntaxKind::{self, NAME},
|
SyntaxKind::{self, NAME},
|
||||||
ast::{self, NameOwner, VisibilityOwner, TypeAscriptionOwner},
|
ast::{self, NameOwner, VisibilityOwner, TypeAscriptionOwner},
|
||||||
algo::visit::{visitor, Visitor},
|
algo::visit::{visitor, Visitor},
|
||||||
|
@ -22,6 +22,9 @@ pub struct NavigationTarget {
|
||||||
full_range: TextRange,
|
full_range: TextRange,
|
||||||
focus_range: Option<TextRange>,
|
focus_range: Option<TextRange>,
|
||||||
container_name: Option<SmolStr>,
|
container_name: Option<SmolStr>,
|
||||||
|
|
||||||
|
pub(crate) description: Option<String>,
|
||||||
|
pub(crate) docs: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl NavigationTarget {
|
impl NavigationTarget {
|
||||||
|
@ -63,7 +66,10 @@ impl NavigationTarget {
|
||||||
NavigationTarget::from_named(file_id, pat)
|
NavigationTarget::from_named(file_id, pat)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn from_symbol(symbol: FileSymbol) -> NavigationTarget {
|
pub(crate) fn from_symbol(db: &RootDatabase, symbol: FileSymbol) -> NavigationTarget {
|
||||||
|
let file = db.parse(symbol.file_id).tree;
|
||||||
|
let node = symbol.ptr.to_node(file.syntax()).to_owned();
|
||||||
|
|
||||||
NavigationTarget {
|
NavigationTarget {
|
||||||
file_id: symbol.file_id,
|
file_id: symbol.file_id,
|
||||||
name: symbol.name.clone(),
|
name: symbol.name.clone(),
|
||||||
|
@ -71,6 +77,8 @@ impl NavigationTarget {
|
||||||
full_range: symbol.ptr.range(),
|
full_range: symbol.ptr.range(),
|
||||||
focus_range: symbol.name_range,
|
focus_range: symbol.name_range,
|
||||||
container_name: symbol.container_name.clone(),
|
container_name: symbol.container_name.clone(),
|
||||||
|
description: description_inner(&node),
|
||||||
|
docs: docs_inner(&node),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,6 +92,8 @@ impl NavigationTarget {
|
||||||
ast::PatKind::BindPat(pat) => return NavigationTarget::from_bind_pat(file_id, &pat),
|
ast::PatKind::BindPat(pat) => return NavigationTarget::from_bind_pat(file_id, &pat),
|
||||||
_ => ("_".into(), pat.syntax_node_ptr().range()),
|
_ => ("_".into(), pat.syntax_node_ptr().range()),
|
||||||
};
|
};
|
||||||
|
let node = pat.to_node(file.syntax()).syntax().to_owned();
|
||||||
|
|
||||||
NavigationTarget {
|
NavigationTarget {
|
||||||
file_id,
|
file_id,
|
||||||
name,
|
name,
|
||||||
|
@ -91,14 +101,20 @@ impl NavigationTarget {
|
||||||
focus_range: None,
|
focus_range: None,
|
||||||
kind: NAME,
|
kind: NAME,
|
||||||
container_name: None,
|
container_name: None,
|
||||||
|
description: description_inner(&node),
|
||||||
|
docs: docs_inner(&node),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn from_self_param(
|
pub(crate) fn from_self_param(
|
||||||
|
db: &RootDatabase,
|
||||||
file_id: FileId,
|
file_id: FileId,
|
||||||
par: AstPtr<ast::SelfParam>,
|
par: AstPtr<ast::SelfParam>,
|
||||||
) -> NavigationTarget {
|
) -> NavigationTarget {
|
||||||
let (name, full_range) = ("self".into(), par.syntax_node_ptr().range());
|
let (name, full_range) = ("self".into(), par.syntax_node_ptr().range());
|
||||||
|
let file = db.parse(file_id).tree;
|
||||||
|
let node = par.to_node(file.syntax()).syntax().to_owned();
|
||||||
|
|
||||||
NavigationTarget {
|
NavigationTarget {
|
||||||
file_id,
|
file_id,
|
||||||
name,
|
name,
|
||||||
|
@ -106,6 +122,8 @@ impl NavigationTarget {
|
||||||
focus_range: None,
|
focus_range: None,
|
||||||
kind: NAME,
|
kind: NAME,
|
||||||
container_name: None,
|
container_name: None,
|
||||||
|
description: description_inner(&node),
|
||||||
|
docs: docs_inner(&node),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -290,21 +308,13 @@ impl NavigationTarget {
|
||||||
focus_range,
|
focus_range,
|
||||||
// ptr: Some(LocalSyntaxPtr::new(node)),
|
// ptr: Some(LocalSyntaxPtr::new(node)),
|
||||||
container_name: None,
|
container_name: None,
|
||||||
|
description: description_inner(node),
|
||||||
|
docs: docs_inner(node),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn node(&self, db: &RootDatabase) -> Option<TreeArc<SyntaxNode>> {
|
fn docs_inner(node: &SyntaxNode) -> Option<String> {
|
||||||
let source_file = db.parse(self.file_id()).tree;
|
|
||||||
let source_file = source_file.syntax();
|
|
||||||
let node = source_file
|
|
||||||
.descendants()
|
|
||||||
.find(|node| node.kind() == self.kind() && node.range() == self.full_range())?
|
|
||||||
.to_owned();
|
|
||||||
Some(node)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn docs(&self, db: &RootDatabase) -> Option<String> {
|
|
||||||
let node = self.node(db)?;
|
|
||||||
fn doc_comments<N: ast::DocCommentsOwner>(node: &N) -> Option<String> {
|
fn doc_comments<N: ast::DocCommentsOwner>(node: &N) -> Option<String> {
|
||||||
node.doc_comment_text()
|
node.doc_comment_text()
|
||||||
}
|
}
|
||||||
|
@ -327,10 +337,8 @@ impl NavigationTarget {
|
||||||
/// Get a description of this node.
|
/// Get a description of this node.
|
||||||
///
|
///
|
||||||
/// e.g. `struct Name`, `enum Name`, `fn Name`
|
/// e.g. `struct Name`, `enum Name`, `fn Name`
|
||||||
pub(crate) fn description(&self, db: &RootDatabase) -> Option<String> {
|
fn description_inner(node: &SyntaxNode) -> Option<String> {
|
||||||
// FIXME: After type inference is done, add type information to improve the output
|
// FIXME: After type inference is done, add type information to improve the output
|
||||||
let node = self.node(db)?;
|
|
||||||
|
|
||||||
fn visit_ascribed_node<T>(node: &T, prefix: &str) -> Option<String>
|
fn visit_ascribed_node<T>(node: &T, prefix: &str) -> Option<String>
|
||||||
where
|
where
|
||||||
T: NameOwner + VisibilityOwner + TypeAscriptionOwner,
|
T: NameOwner + VisibilityOwner + TypeAscriptionOwner,
|
||||||
|
@ -369,4 +377,3 @@ impl NavigationTarget {
|
||||||
.visit(|node: &ast::EnumVariant| Some(node.name()?.text().to_string()))
|
.visit(|node: &ast::EnumVariant| Some(node.name()?.text().to_string()))
|
||||||
.accept(&node)?
|
.accept(&node)?
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
|
@ -72,7 +72,7 @@ pub(crate) fn reference_definition(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Some(Pat(pat)) => return Exact(NavigationTarget::from_pat(db, file_id, pat)),
|
Some(Pat(pat)) => return Exact(NavigationTarget::from_pat(db, file_id, pat)),
|
||||||
Some(SelfParam(par)) => return Exact(NavigationTarget::from_self_param(file_id, par)),
|
Some(SelfParam(par)) => return Exact(NavigationTarget::from_self_param(db, file_id, par)),
|
||||||
Some(GenericParam(_)) => {
|
Some(GenericParam(_)) => {
|
||||||
// FIXME: go to the generic param def
|
// FIXME: go to the generic param def
|
||||||
}
|
}
|
||||||
|
@ -82,7 +82,7 @@ pub(crate) fn reference_definition(
|
||||||
// Fallback index based approach:
|
// Fallback index based approach:
|
||||||
let navs = crate::symbol_index::index_resolve(db, name_ref)
|
let navs = crate::symbol_index::index_resolve(db, name_ref)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(NavigationTarget::from_symbol)
|
.map(|s| NavigationTarget::from_symbol(db, s))
|
||||||
.collect();
|
.collect();
|
||||||
Approximate(navs)
|
Approximate(navs)
|
||||||
}
|
}
|
||||||
|
|
|
@ -86,13 +86,13 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn
|
||||||
use crate::goto_definition::{ReferenceResult::*, reference_definition};
|
use crate::goto_definition::{ReferenceResult::*, reference_definition};
|
||||||
let ref_result = reference_definition(db, position.file_id, name_ref);
|
let ref_result = reference_definition(db, position.file_id, name_ref);
|
||||||
match ref_result {
|
match ref_result {
|
||||||
Exact(nav) => res.extend(doc_text_for(db, nav)),
|
Exact(nav) => res.extend(doc_text_for(nav)),
|
||||||
Approximate(navs) => {
|
Approximate(navs) => {
|
||||||
// We are no longer exact
|
// We are no longer exact
|
||||||
res.exact = false;
|
res.exact = false;
|
||||||
|
|
||||||
for nav in navs {
|
for nav in navs {
|
||||||
res.extend(doc_text_for(db, nav))
|
res.extend(doc_text_for(nav))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -104,7 +104,7 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn
|
||||||
|
|
||||||
if let Some(navs) = navs {
|
if let Some(navs) = navs {
|
||||||
for nav in navs {
|
for nav in navs {
|
||||||
res.extend(doc_text_for(db, nav))
|
res.extend(doc_text_for(nav))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -393,7 +393,7 @@ impl Analysis {
|
||||||
self.with_db(|db| {
|
self.with_db(|db| {
|
||||||
symbol_index::world_symbols(db, query)
|
symbol_index::world_symbols(db, query)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(NavigationTarget::from_symbol)
|
.map(|s| NavigationTarget::from_symbol(db, s))
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue