mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 12:54:58 +00:00
Remove duplication
This commit is contained in:
parent
2d7b4cc548
commit
da326dca60
1 changed files with 23 additions and 74 deletions
|
@ -1,7 +1,7 @@
|
||||||
use ra_db::{Cancelable, SyntaxDatabase};
|
use ra_db::{Cancelable, SyntaxDatabase};
|
||||||
use ra_syntax::{
|
use ra_syntax::{
|
||||||
AstNode, SyntaxNode, TreeArc,
|
AstNode, SyntaxNode, TreeArc,
|
||||||
ast::{self, NameOwner, VisibilityOwner},
|
ast::self,
|
||||||
algo::{find_covering_node, find_node_at_offset, find_leaf_at_offset, visit::{visitor, Visitor}},
|
algo::{find_covering_node, find_node_at_offset, find_leaf_at_offset, visit::{visitor, Visitor}},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -137,80 +137,29 @@ impl NavigationTarget {
|
||||||
fn description(&self, db: &RootDatabase) -> Option<String> {
|
fn description(&self, db: &RootDatabase) -> Option<String> {
|
||||||
// TODO: After type inference is done, add type information to improve the output
|
// TODO: After type inference is done, add type information to improve the output
|
||||||
let node = self.node(db)?;
|
let node = self.node(db)?;
|
||||||
// TODO: Refactor to be have less repetition
|
|
||||||
|
fn visit_node<T>(node: &T, label: &str) -> Option<String>
|
||||||
|
where
|
||||||
|
T: ast::NameOwner + ast::VisibilityOwner,
|
||||||
|
{
|
||||||
|
let mut string = node
|
||||||
|
.visibility()
|
||||||
|
.map(|v| format!("{} ", v.syntax().text()))
|
||||||
|
.unwrap_or_default();
|
||||||
|
string.push_str(label);
|
||||||
|
node.name()?.syntax().text().push_to(&mut string);
|
||||||
|
Some(string)
|
||||||
|
}
|
||||||
|
|
||||||
visitor()
|
visitor()
|
||||||
.visit(|node: &ast::FnDef| {
|
.visit(|node: &ast::FnDef| visit_node(node, "fn "))
|
||||||
let mut string = node
|
.visit(|node: &ast::StructDef| visit_node(node, "struct "))
|
||||||
.visibility()
|
.visit(|node: &ast::EnumDef| visit_node(node, "enum "))
|
||||||
.map(|v| format!("{} ", v.syntax().text()))
|
.visit(|node: &ast::TraitDef| visit_node(node, "trait "))
|
||||||
.unwrap_or_default();
|
.visit(|node: &ast::Module| visit_node(node, "mod "))
|
||||||
string.push_str("fn ");
|
.visit(|node: &ast::TypeDef| visit_node(node, "type "))
|
||||||
node.name()?.syntax().text().push_to(&mut string);
|
.visit(|node: &ast::ConstDef| visit_node(node, "const "))
|
||||||
Some(string)
|
.visit(|node: &ast::StaticDef| visit_node(node, "static "))
|
||||||
})
|
|
||||||
.visit(|node: &ast::StructDef| {
|
|
||||||
let mut string = node
|
|
||||||
.visibility()
|
|
||||||
.map(|v| format!("{} ", v.syntax().text()))
|
|
||||||
.unwrap_or_default();
|
|
||||||
string.push_str("struct ");
|
|
||||||
node.name()?.syntax().text().push_to(&mut string);
|
|
||||||
Some(string)
|
|
||||||
})
|
|
||||||
.visit(|node: &ast::EnumDef| {
|
|
||||||
let mut string = node
|
|
||||||
.visibility()
|
|
||||||
.map(|v| format!("{} ", v.syntax().text()))
|
|
||||||
.unwrap_or_default();
|
|
||||||
string.push_str("enum ");
|
|
||||||
node.name()?.syntax().text().push_to(&mut string);
|
|
||||||
Some(string)
|
|
||||||
})
|
|
||||||
.visit(|node: &ast::TraitDef| {
|
|
||||||
let mut string = node
|
|
||||||
.visibility()
|
|
||||||
.map(|v| format!("{} ", v.syntax().text()))
|
|
||||||
.unwrap_or_default();
|
|
||||||
string.push_str("trait ");
|
|
||||||
node.name()?.syntax().text().push_to(&mut string);
|
|
||||||
Some(string)
|
|
||||||
})
|
|
||||||
.visit(|node: &ast::Module| {
|
|
||||||
let mut string = node
|
|
||||||
.visibility()
|
|
||||||
.map(|v| format!("{} ", v.syntax().text()))
|
|
||||||
.unwrap_or_default();
|
|
||||||
string.push_str("mod ");
|
|
||||||
node.name()?.syntax().text().push_to(&mut string);
|
|
||||||
Some(string)
|
|
||||||
})
|
|
||||||
.visit(|node: &ast::TypeDef| {
|
|
||||||
let mut string = node
|
|
||||||
.visibility()
|
|
||||||
.map(|v| format!("{} ", v.syntax().text()))
|
|
||||||
.unwrap_or_default();
|
|
||||||
string.push_str("type ");
|
|
||||||
node.name()?.syntax().text().push_to(&mut string);
|
|
||||||
Some(string)
|
|
||||||
})
|
|
||||||
.visit(|node: &ast::ConstDef| {
|
|
||||||
let mut string = node
|
|
||||||
.visibility()
|
|
||||||
.map(|v| format!("{} ", v.syntax().text()))
|
|
||||||
.unwrap_or_default();
|
|
||||||
string.push_str("const ");
|
|
||||||
node.name()?.syntax().text().push_to(&mut string);
|
|
||||||
Some(string)
|
|
||||||
})
|
|
||||||
.visit(|node: &ast::StaticDef| {
|
|
||||||
let mut string = node
|
|
||||||
.visibility()
|
|
||||||
.map(|v| format!("{} ", v.syntax().text()))
|
|
||||||
.unwrap_or_default();
|
|
||||||
string.push_str("static ");
|
|
||||||
node.name()?.syntax().text().push_to(&mut string);
|
|
||||||
Some(string)
|
|
||||||
})
|
|
||||||
.accept(&node)?
|
.accept(&node)?
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue