migrate ra_ide_api to the new rowan

This commit is contained in:
Aleksey Kladov
2019-07-19 12:56:47 +03:00
parent 0343c4a815
commit f1abc7bdc6
29 changed files with 342 additions and 290 deletions

View File

@@ -38,7 +38,7 @@ impl FunctionSignature {
pub(crate) fn from_hir(db: &db::RootDatabase, function: hir::Function) -> Self {
let doc = function.docs(db);
let ast_node = function.source(db).ast;
FunctionSignature::from(&*ast_node).with_doc_opt(doc)
FunctionSignature::from(&ast_node).with_doc_opt(doc)
}
}

View File

@@ -5,7 +5,7 @@ use ra_syntax::{
ast::{self, DocCommentsOwner},
AstNode, AstPtr, SmolStr,
SyntaxKind::{self, NAME},
SyntaxNode, TextRange, TreeArc,
SyntaxNode, TextRange,
};
use super::short_label::ShortLabel;
@@ -169,7 +169,7 @@ impl NavigationTarget {
let file_id = src.file_id.original_file(db);
match src.ast {
FieldSource::Named(it) => {
NavigationTarget::from_named(file_id, &*it, it.doc_comment_text(), it.short_label())
NavigationTarget::from_named(file_id, &it, it.doc_comment_text(), it.short_label())
}
FieldSource::Pos(it) => {
NavigationTarget::from_syntax(file_id, "".into(), None, it.syntax(), None, None)
@@ -179,13 +179,13 @@ impl NavigationTarget {
pub(crate) fn from_def_source<A, D>(db: &RootDatabase, def: D) -> NavigationTarget
where
D: HasSource<Ast = TreeArc<A>>,
D: HasSource<Ast = A>,
A: ast::DocCommentsOwner + ast::NameOwner + ShortLabel,
{
let src = def.source(db);
NavigationTarget::from_named(
src.file_id.original_file(db),
&*src.ast,
&src.ast,
src.ast.doc_comment_text(),
src.ast.short_label(),
)
@@ -249,7 +249,7 @@ impl NavigationTarget {
log::debug!("nav target {}", src.ast.syntax().debug_dump());
NavigationTarget::from_named(
src.file_id.original_file(db),
&*src.ast,
&src.ast,
src.ast.doc_comment_text(),
None,
)
@@ -318,22 +318,18 @@ pub(crate) fn docs_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> Option
let parse = db.parse(symbol.file_id);
let node = symbol.ptr.to_node(parse.tree().syntax()).to_owned();
fn doc_comments<N: ast::DocCommentsOwner>(node: &N) -> Option<String> {
node.doc_comment_text()
}
visitor()
.visit(doc_comments::<ast::FnDef>)
.visit(doc_comments::<ast::StructDef>)
.visit(doc_comments::<ast::EnumDef>)
.visit(doc_comments::<ast::TraitDef>)
.visit(doc_comments::<ast::Module>)
.visit(doc_comments::<ast::TypeAliasDef>)
.visit(doc_comments::<ast::ConstDef>)
.visit(doc_comments::<ast::StaticDef>)
.visit(doc_comments::<ast::NamedFieldDef>)
.visit(doc_comments::<ast::EnumVariant>)
.visit(doc_comments::<ast::MacroCall>)
.visit(|it: ast::FnDef| it.doc_comment_text())
.visit(|it: ast::StructDef| it.doc_comment_text())
.visit(|it: ast::EnumDef| it.doc_comment_text())
.visit(|it: ast::TraitDef| it.doc_comment_text())
.visit(|it: ast::Module| it.doc_comment_text())
.visit(|it: ast::TypeAliasDef| it.doc_comment_text())
.visit(|it: ast::ConstDef| it.doc_comment_text())
.visit(|it: ast::StaticDef| it.doc_comment_text())
.visit(|it: ast::NamedFieldDef| it.doc_comment_text())
.visit(|it: ast::EnumVariant| it.doc_comment_text())
.visit(|it: ast::MacroCall| it.doc_comment_text())
.accept(&node)?
}
@@ -345,15 +341,15 @@ pub(crate) fn description_from_symbol(db: &RootDatabase, symbol: &FileSymbol) ->
let node = symbol.ptr.to_node(parse.tree().syntax()).to_owned();
visitor()
.visit(|node: &ast::FnDef| node.short_label())
.visit(|node: &ast::StructDef| node.short_label())
.visit(|node: &ast::EnumDef| node.short_label())
.visit(|node: &ast::TraitDef| node.short_label())
.visit(|node: &ast::Module| node.short_label())
.visit(|node: &ast::TypeAliasDef| node.short_label())
.visit(|node: &ast::ConstDef| node.short_label())
.visit(|node: &ast::StaticDef| node.short_label())
.visit(|node: &ast::NamedFieldDef| node.short_label())
.visit(|node: &ast::EnumVariant| node.short_label())
.visit(|node: ast::FnDef| node.short_label())
.visit(|node: ast::StructDef| node.short_label())
.visit(|node: ast::EnumDef| node.short_label())
.visit(|node: ast::TraitDef| node.short_label())
.visit(|node: ast::Module| node.short_label())
.visit(|node: ast::TypeAliasDef| node.short_label())
.visit(|node: ast::ConstDef| node.short_label())
.visit(|node: ast::StaticDef| node.short_label())
.visit(|node: ast::NamedFieldDef| node.short_label())
.visit(|node: ast::EnumVariant| node.short_label())
.accept(&node)?
}

View File

@@ -24,14 +24,14 @@ pub fn file_structure(file: &SourceFile) -> Vec<StructureNode> {
for event in file.syntax().preorder() {
match event {
WalkEvent::Enter(node) => {
if let Some(mut symbol) = structure_node(node) {
if let Some(mut symbol) = structure_node(&node) {
symbol.parent = stack.last().copied();
stack.push(res.len());
res.push(symbol);
}
}
WalkEvent::Leave(node) => {
if structure_node(node).is_some() {
if structure_node(&node).is_some() {
stack.pop().unwrap();
}
}
@@ -41,19 +41,20 @@ pub fn file_structure(file: &SourceFile) -> Vec<StructureNode> {
}
fn structure_node(node: &SyntaxNode) -> Option<StructureNode> {
fn decl<N: NameOwner + AttrsOwner>(node: &N) -> Option<StructureNode> {
fn decl<N: NameOwner + AttrsOwner>(node: N) -> Option<StructureNode> {
decl_with_detail(node, None)
}
fn decl_with_ascription<N: NameOwner + AttrsOwner + TypeAscriptionOwner>(
node: &N,
node: N,
) -> Option<StructureNode> {
decl_with_type_ref(node, node.ascribed_type())
let ty = node.ascribed_type();
decl_with_type_ref(node, ty)
}
fn decl_with_type_ref<N: NameOwner + AttrsOwner>(
node: &N,
type_ref: Option<&ast::TypeRef>,
node: N,
type_ref: Option<ast::TypeRef>,
) -> Option<StructureNode> {
let detail = type_ref.map(|type_ref| {
let mut detail = String::new();
@@ -64,7 +65,7 @@ fn structure_node(node: &SyntaxNode) -> Option<StructureNode> {
}
fn decl_with_detail<N: NameOwner + AttrsOwner>(
node: &N,
node: N,
detail: Option<String>,
) -> Option<StructureNode> {
let name = node.name()?;
@@ -82,22 +83,24 @@ fn structure_node(node: &SyntaxNode) -> Option<StructureNode> {
fn collapse_ws(node: &SyntaxNode, output: &mut String) {
let mut can_insert_ws = false;
for line in node.text().chunks().flat_map(|chunk| chunk.lines()) {
let line = line.trim();
if line.is_empty() {
if can_insert_ws {
output.push_str(" ");
can_insert_ws = false;
for chunk in node.text().chunks() {
for line in chunk.lines() {
let line = line.trim();
if line.is_empty() {
if can_insert_ws {
output.push_str(" ");
can_insert_ws = false;
}
} else {
output.push_str(line);
can_insert_ws = true;
}
} else {
output.push_str(line);
can_insert_ws = true;
}
}
}
visitor()
.visit(|fn_def: &ast::FnDef| {
.visit(|fn_def: ast::FnDef| {
let mut detail = String::from("fn");
if let Some(type_param_list) = fn_def.type_param_list() {
collapse_ws(type_param_list.syntax(), &mut detail);
@@ -117,11 +120,14 @@ fn structure_node(node: &SyntaxNode) -> Option<StructureNode> {
.visit(decl::<ast::EnumVariant>)
.visit(decl::<ast::TraitDef>)
.visit(decl::<ast::Module>)
.visit(|td: &ast::TypeAliasDef| decl_with_type_ref(td, td.type_ref()))
.visit(|td: ast::TypeAliasDef| {
let ty = td.type_ref();
decl_with_type_ref(td, ty)
})
.visit(decl_with_ascription::<ast::NamedFieldDef>)
.visit(decl_with_ascription::<ast::ConstDef>)
.visit(decl_with_ascription::<ast::StaticDef>)
.visit(|im: &ast::ImplBlock| {
.visit(|im: ast::ImplBlock| {
let target_type = im.target_type()?;
let target_trait = im.target_trait();
let label = match target_trait {
@@ -142,14 +148,14 @@ fn structure_node(node: &SyntaxNode) -> Option<StructureNode> {
};
Some(node)
})
.visit(|mc: &ast::MacroCall| {
.visit(|mc: ast::MacroCall| {
let first_token = mc.syntax().first_token().unwrap();
if first_token.text().as_str() != "macro_rules" {
return None;
}
decl(mc)
})
.accept(node)?
.accept(&node)?
}
#[cfg(test)]