From f1abc7bdc63fedd5a699b4c495bb23f1b6d254f9 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 19 Jul 2019 12:56:47 +0300 Subject: migrate ra_ide_api to the new rowan --- .../ra_ide_api/src/display/function_signature.rs | 2 +- crates/ra_ide_api/src/display/navigation_target.rs | 56 ++++++++++------------ crates/ra_ide_api/src/display/structure.rs | 50 ++++++++++--------- 3 files changed, 55 insertions(+), 53 deletions(-) (limited to 'crates/ra_ide_api/src/display') diff --git a/crates/ra_ide_api/src/display/function_signature.rs b/crates/ra_ide_api/src/display/function_signature.rs index e7ad5a0d1..644a4532b 100644 --- a/crates/ra_ide_api/src/display/function_signature.rs +++ b/crates/ra_ide_api/src/display/function_signature.rs @@ -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) } } diff --git a/crates/ra_ide_api/src/display/navigation_target.rs b/crates/ra_ide_api/src/display/navigation_target.rs index 20a8d418e..8cc853dd1 100644 --- a/crates/ra_ide_api/src/display/navigation_target.rs +++ b/crates/ra_ide_api/src/display/navigation_target.rs @@ -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(db: &RootDatabase, def: D) -> NavigationTarget where - D: HasSource>, + D: HasSource, 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(node: &N) -> Option { - node.doc_comment_text() - } - visitor() - .visit(doc_comments::) - .visit(doc_comments::) - .visit(doc_comments::) - .visit(doc_comments::) - .visit(doc_comments::) - .visit(doc_comments::) - .visit(doc_comments::) - .visit(doc_comments::) - .visit(doc_comments::) - .visit(doc_comments::) - .visit(doc_comments::) + .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)? } diff --git a/crates/ra_ide_api/src/display/structure.rs b/crates/ra_ide_api/src/display/structure.rs index 638484a9b..2e183d2f6 100644 --- a/crates/ra_ide_api/src/display/structure.rs +++ b/crates/ra_ide_api/src/display/structure.rs @@ -24,14 +24,14 @@ pub fn file_structure(file: &SourceFile) -> Vec { 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 { } fn structure_node(node: &SyntaxNode) -> Option { - fn decl(node: &N) -> Option { + fn decl(node: N) -> Option { decl_with_detail(node, None) } fn decl_with_ascription( - node: &N, + node: N, ) -> Option { - decl_with_type_ref(node, node.ascribed_type()) + let ty = node.ascribed_type(); + decl_with_type_ref(node, ty) } fn decl_with_type_ref( - node: &N, - type_ref: Option<&ast::TypeRef>, + node: N, + type_ref: Option, ) -> Option { let detail = type_ref.map(|type_ref| { let mut detail = String::new(); @@ -64,7 +65,7 @@ fn structure_node(node: &SyntaxNode) -> Option { } fn decl_with_detail( - node: &N, + node: N, detail: Option, ) -> Option { let name = node.name()?; @@ -82,22 +83,24 @@ fn structure_node(node: &SyntaxNode) -> Option { 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 { .visit(decl::) .visit(decl::) .visit(decl::) - .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::) .visit(decl_with_ascription::) .visit(decl_with_ascription::) - .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 { }; 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)] -- cgit v1.2.3