From 3ffd5dd2a63e8efe182e79439a879ec1f9420b77 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 8 Jan 2019 11:47:28 +0300 Subject: migrate ra_analysis to new rowan --- .../src/completion/complete_fn_param.rs | 6 ++--- .../ra_analysis/src/completion/complete_keyword.rs | 6 ++--- .../src/completion/completion_context.rs | 29 ++++++++------------- crates/ra_analysis/src/extend_selection.rs | 8 +++--- crates/ra_analysis/src/goto_defenition.rs | 4 +-- crates/ra_analysis/src/hover.rs | 30 ++++++++++------------ crates/ra_analysis/src/imp.rs | 28 ++++++++++---------- crates/ra_analysis/src/lib.rs | 8 +++--- crates/ra_analysis/src/runnables.rs | 12 ++++----- crates/ra_analysis/src/symbol_index.rs | 8 +++--- crates/ra_analysis/src/syntax_highlighting.rs | 2 +- crates/ra_hir/src/expr.rs | 4 +-- 12 files changed, 67 insertions(+), 78 deletions(-) (limited to 'crates') diff --git a/crates/ra_analysis/src/completion/complete_fn_param.rs b/crates/ra_analysis/src/completion/complete_fn_param.rs index bb5fdfda0..c1739e47e 100644 --- a/crates/ra_analysis/src/completion/complete_fn_param.rs +++ b/crates/ra_analysis/src/completion/complete_fn_param.rs @@ -39,9 +39,9 @@ pub(super) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext) .add_to(acc) }); - fn process<'a, N: ast::FnDefOwner<'a>>( - node: N, - params: &mut FxHashMap)>, + fn process<'a, N: ast::FnDefOwner>( + node: &'a N, + params: &mut FxHashMap, ) { node.functions() .filter_map(|it| it.param_list()) diff --git a/crates/ra_analysis/src/completion/complete_keyword.rs b/crates/ra_analysis/src/completion/complete_keyword.rs index 28194c908..d350f06ce 100644 --- a/crates/ra_analysis/src/completion/complete_keyword.rs +++ b/crates/ra_analysis/src/completion/complete_keyword.rs @@ -2,7 +2,7 @@ use ra_syntax::{ algo::visit::{visitor, Visitor}, AstNode, ast::{self, LoopBodyOwner}, - SyntaxKind::*, SyntaxNodeRef, + SyntaxKind::*, SyntaxNode, }; use crate::completion::{CompletionContext, CompletionItem, Completions, CompletionKind, CompletionItemKind}; @@ -76,7 +76,7 @@ pub(super) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte acc.add_all(complete_return(fn_def, ctx.can_be_stmt)); } -fn is_in_loop_body(leaf: SyntaxNodeRef) -> bool { +fn is_in_loop_body(leaf: &SyntaxNode) -> bool { for node in leaf.ancestors() { if node.kind() == FN_DEF || node.kind() == LAMBDA_EXPR { break; @@ -95,7 +95,7 @@ fn is_in_loop_body(leaf: SyntaxNodeRef) -> bool { false } -fn complete_return(fn_def: ast::FnDef, can_be_stmt: bool) -> Option { +fn complete_return(fn_def: &ast::FnDef, can_be_stmt: bool) -> Option { let snip = match (can_be_stmt, fn_def.ret_type().is_some()) { (true, true) => "return $0;", (true, false) => "return;", diff --git a/crates/ra_analysis/src/completion/completion_context.rs b/crates/ra_analysis/src/completion/completion_context.rs index 4584f355d..988c21c58 100644 --- a/crates/ra_analysis/src/completion/completion_context.rs +++ b/crates/ra_analysis/src/completion/completion_context.rs @@ -1,13 +1,9 @@ use ra_editor::find_node_at_offset; use ra_text_edit::AtomTextEdit; use ra_syntax::{ - algo::{find_leaf_at_offset, find_covering_node}, + AstNode, SyntaxNode, SourceFile, TextUnit, TextRange, ast, - AstNode, - SyntaxNodeRef, - SourceFileNode, - TextUnit, - TextRange, + algo::{find_leaf_at_offset, find_covering_node}, SyntaxKind::*, }; use hir::source_binder; @@ -20,11 +16,11 @@ use crate::{db, FilePosition, Cancelable}; pub(super) struct CompletionContext<'a> { pub(super) db: &'a db::RootDatabase, pub(super) offset: TextUnit, - pub(super) leaf: SyntaxNodeRef<'a>, + pub(super) leaf: &'a SyntaxNode, pub(super) module: Option, pub(super) function: Option, - pub(super) function_syntax: Option>, - pub(super) use_item_syntax: Option>, + pub(super) function_syntax: Option<&'a ast::FnDef>, + pub(super) use_item_syntax: Option<&'a ast::UseItem>, pub(super) is_param: bool, /// A single-indent path, like `foo`. pub(super) is_trivial_path: bool, @@ -36,7 +32,7 @@ pub(super) struct CompletionContext<'a> { /// Something is typed at the "top" level, in module or impl/trait. pub(super) is_new_item: bool, /// The receiver if this is a field or method access, i.e. writing something.<|> - pub(super) dot_receiver: Option>, + pub(super) dot_receiver: Option<&'a ast::Expr>, /// If this is a method call in particular, i.e. the () are already there. pub(super) is_method_call: bool, } @@ -44,7 +40,7 @@ pub(super) struct CompletionContext<'a> { impl<'a> CompletionContext<'a> { pub(super) fn new( db: &'a db::RootDatabase, - original_file: &'a SourceFileNode, + original_file: &'a SourceFile, position: FilePosition, ) -> Cancelable>> { let module = source_binder::module_from_position(db, position)?; @@ -71,7 +67,7 @@ impl<'a> CompletionContext<'a> { Ok(Some(ctx)) } - fn fill(&mut self, original_file: &'a SourceFileNode, offset: TextUnit) { + fn fill(&mut self, original_file: &'a SourceFile, offset: TextUnit) { // Insert a fake ident to get a valid parse tree. We will use this file // to determine context, though the original_file will be used for // actual completion. @@ -100,7 +96,7 @@ impl<'a> CompletionContext<'a> { } } } - fn classify_name_ref(&mut self, original_file: &'a SourceFileNode, name_ref: ast::NameRef) { + fn classify_name_ref(&mut self, original_file: &'a SourceFile, name_ref: &ast::NameRef) { let name_range = name_ref.syntax().range(); let top_node = name_ref .syntax() @@ -197,15 +193,12 @@ impl<'a> CompletionContext<'a> { } } -fn find_node_with_range<'a, N: AstNode<'a>>( - syntax: SyntaxNodeRef<'a>, - range: TextRange, -) -> Option { +fn find_node_with_range(syntax: &SyntaxNode, range: TextRange) -> Option<&N> { let node = find_covering_node(syntax, range); node.ancestors().find_map(N::cast) } -fn is_node<'a, N: AstNode<'a>>(node: SyntaxNodeRef<'a>) -> bool { +fn is_node(node: &SyntaxNode) -> bool { match node.ancestors().filter_map(N::cast).next() { None => false, Some(n) => n.syntax().range() == node.range(), diff --git a/crates/ra_analysis/src/extend_selection.rs b/crates/ra_analysis/src/extend_selection.rs index f1b77f981..3b130f966 100644 --- a/crates/ra_analysis/src/extend_selection.rs +++ b/crates/ra_analysis/src/extend_selection.rs @@ -1,6 +1,6 @@ use ra_db::SyntaxDatabase; use ra_syntax::{ - SyntaxNodeRef, AstNode, SourceFileNode, + SyntaxNode, AstNode, SourceFile, ast, algo::find_covering_node, }; @@ -19,18 +19,18 @@ pub(crate) fn extend_selection(db: &RootDatabase, frange: FileRange) -> TextRang fn extend_selection_in_macro( _db: &RootDatabase, - source_file: &SourceFileNode, + source_file: &SourceFile, frange: FileRange, ) -> Option { let macro_call = find_macro_call(source_file.syntax(), frange.range)?; let (off, exp) = hir::MacroDef::ast_expand(macro_call)?; let dst_range = exp.map_range_forward(frange.range - off)?; - let dst_range = ra_editor::extend_selection(exp.syntax().borrowed(), dst_range)?; + let dst_range = ra_editor::extend_selection(&exp.syntax(), dst_range)?; let src_range = exp.map_range_back(dst_range)? + off; Some(src_range) } -fn find_macro_call(node: SyntaxNodeRef, range: TextRange) -> Option { +fn find_macro_call(node: &SyntaxNode, range: TextRange) -> Option<&ast::MacroCall> { find_covering_node(node, range) .ancestors() .find_map(ast::MacroCall::cast) diff --git a/crates/ra_analysis/src/goto_defenition.rs b/crates/ra_analysis/src/goto_defenition.rs index aa0616e3b..0bcf13ebd 100644 --- a/crates/ra_analysis/src/goto_defenition.rs +++ b/crates/ra_analysis/src/goto_defenition.rs @@ -23,7 +23,7 @@ pub(crate) fn goto_defenition( pub(crate) fn reference_defenition( db: &RootDatabase, file_id: FileId, - name_ref: ast::NameRef, + name_ref: &ast::NameRef, ) -> Cancelable> { if let Some(fn_descr) = hir::source_binder::function_from_child_node(db, file_id, name_ref.syntax())? @@ -53,7 +53,7 @@ pub(crate) fn reference_defenition( fn name_defenition( db: &RootDatabase, file_id: FileId, - name: ast::Name, + name: &ast::Name, ) -> Cancelable>> { if let Some(module) = name.syntax().parent().and_then(ast::Module::cast) { if module.has_semi() { diff --git a/crates/ra_analysis/src/hover.rs b/crates/ra_analysis/src/hover.rs index 06632df4f..5607c3ef3 100644 --- a/crates/ra_analysis/src/hover.rs +++ b/crates/ra_analysis/src/hover.rs @@ -1,7 +1,7 @@ use ra_db::{Cancelable, SyntaxDatabase}; use ra_editor::find_node_at_offset; use ra_syntax::{ - AstNode, SyntaxNode, + AstNode, SyntaxNode, TreePtr, ast::{self, NameOwner}, algo::{find_covering_node, find_leaf_at_offset, visit::{visitor, Visitor}}, }; @@ -88,20 +88,19 @@ fn doc_text_for(db: &RootDatabase, nav: NavigationTarget) -> Cancelable Option { + fn node(&self, db: &RootDatabase) -> Option> { let source_file = db.source_file(self.file_id); let source_file = source_file.syntax(); let node = source_file .descendants() .find(|node| node.kind() == self.kind && node.range() == self.range)? - .owned(); + .to_owned(); Some(node) } fn docs(&self, db: &RootDatabase) -> Option { let node = self.node(db)?; - let node = node.borrowed(); - fn doc_comments<'a, N: ast::DocCommentsOwner<'a>>(node: N) -> Option { + fn doc_comments(node: &N) -> Option { let comments = node.doc_comment_text(); if comments.is_empty() { None @@ -119,7 +118,7 @@ impl NavigationTarget { .visit(doc_comments::) .visit(doc_comments::) .visit(doc_comments::) - .accept(node)? + .accept(&node)? } /// Get a description of this node. @@ -128,50 +127,49 @@ impl NavigationTarget { fn description(&self, db: &RootDatabase) -> Option { // TODO: After type inference is done, add type information to improve the output let node = self.node(db)?; - let node = node.borrowed(); // TODO: Refactor to be have less repetition visitor() - .visit(|node: ast::FnDef| { + .visit(|node: &ast::FnDef| { let mut string = "fn ".to_string(); node.name()?.syntax().text().push_to(&mut string); Some(string) }) - .visit(|node: ast::StructDef| { + .visit(|node: &ast::StructDef| { let mut string = "struct ".to_string(); node.name()?.syntax().text().push_to(&mut string); Some(string) }) - .visit(|node: ast::EnumDef| { + .visit(|node: &ast::EnumDef| { let mut string = "enum ".to_string(); node.name()?.syntax().text().push_to(&mut string); Some(string) }) - .visit(|node: ast::TraitDef| { + .visit(|node: &ast::TraitDef| { let mut string = "trait ".to_string(); node.name()?.syntax().text().push_to(&mut string); Some(string) }) - .visit(|node: ast::Module| { + .visit(|node: &ast::Module| { let mut string = "mod ".to_string(); node.name()?.syntax().text().push_to(&mut string); Some(string) }) - .visit(|node: ast::TypeDef| { + .visit(|node: &ast::TypeDef| { let mut string = "type ".to_string(); node.name()?.syntax().text().push_to(&mut string); Some(string) }) - .visit(|node: ast::ConstDef| { + .visit(|node: &ast::ConstDef| { let mut string = "const ".to_string(); node.name()?.syntax().text().push_to(&mut string); Some(string) }) - .visit(|node: ast::StaticDef| { + .visit(|node: &ast::StaticDef| { let mut string = "static ".to_string(); node.name()?.syntax().text().push_to(&mut string); Some(string) }) - .accept(node)? + .accept(&node)? } } diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs index 07a966290..8ac430e41 100644 --- a/crates/ra_analysis/src/imp.rs +++ b/crates/ra_analysis/src/imp.rs @@ -8,10 +8,9 @@ use hir::{ use ra_db::{FilesDatabase, SourceRoot, SourceRootId, SyntaxDatabase}; use ra_editor::{self, find_node_at_offset, assists, LocalEdit, Severity}; use ra_syntax::{ - ast::{self, ArgListOwner, Expr, NameOwner}, - AstNode, SourceFileNode, + SyntaxNode, TextRange, TextUnit, AstNode, SourceFile, + ast::{self, ArgListOwner, NameOwner}, SyntaxKind::*, - SyntaxNodeRef, TextRange, TextUnit, }; use crate::{ @@ -113,7 +112,6 @@ impl db::RootDatabase { None => return Ok(Vec::new()), Some(it) => it, }; - let ast_module = ast_module.borrowed(); let name = ast_module.name().unwrap(); Ok(vec![NavigationTarget { file_id, @@ -163,9 +161,9 @@ impl db::RootDatabase { fn find_binding<'a>( db: &db::RootDatabase, - source_file: &'a SourceFileNode, + source_file: &'a SourceFile, position: FilePosition, - ) -> Cancelable, hir::Function)>> { + ) -> Cancelable> { let syntax = source_file.syntax(); if let Some(binding) = find_node_at_offset::(syntax, position.offset) { let descr = ctry!(source_binder::function_from_child_node( @@ -281,7 +279,7 @@ impl db::RootDatabase { if symbol.ptr.kind() == FN_DEF { let fn_file = self.source_file(symbol.file_id); let fn_def = symbol.ptr.resolve(&fn_file); - let fn_def = ast::FnDef::cast(fn_def.borrowed()).unwrap(); + let fn_def = ast::FnDef::cast(&fn_def).unwrap(); let descr = ctry!(source_binder::function_from_source( self, symbol.file_id, @@ -352,7 +350,7 @@ impl db::RootDatabase { .collect::>(); Ok(res) } - pub(crate) fn index_resolve(&self, name_ref: ast::NameRef) -> Cancelable> { + pub(crate) fn index_resolve(&self, name_ref: &ast::NameRef) -> Cancelable> { let name = name_ref.text(); let mut query = Query::new(name.to_string()); query.exact(); @@ -379,12 +377,12 @@ impl SourceChange { } enum FnCallNode<'a> { - CallExpr(ast::CallExpr<'a>), - MethodCallExpr(ast::MethodCallExpr<'a>), + CallExpr(&'a ast::CallExpr), + MethodCallExpr(&'a ast::MethodCallExpr), } impl<'a> FnCallNode<'a> { - pub fn with_node(syntax: SyntaxNodeRef, offset: TextUnit) -> Option { + pub fn with_node(syntax: &'a SyntaxNode, offset: TextUnit) -> Option> { if let Some(expr) = find_node_at_offset::(syntax, offset) { return Some(FnCallNode::CallExpr(expr)); } @@ -394,10 +392,10 @@ impl<'a> FnCallNode<'a> { None } - pub fn name_ref(&self) -> Option { + pub fn name_ref(&self) -> Option<&'a ast::NameRef> { match *self { - FnCallNode::CallExpr(call_expr) => Some(match call_expr.expr()? { - Expr::PathExpr(path_expr) => path_expr.path()?.segment()?.name_ref()?, + FnCallNode::CallExpr(call_expr) => Some(match call_expr.expr()?.kind() { + ast::ExprKind::PathExpr(path_expr) => path_expr.path()?.segment()?.name_ref()?, _ => return None, }), @@ -409,7 +407,7 @@ impl<'a> FnCallNode<'a> { } } - pub fn arg_list(&self) -> Option { + pub fn arg_list(&self) -> Option<&'a ast::ArgList> { match *self { FnCallNode::CallExpr(expr) => expr.arg_list(), FnCallNode::MethodCallExpr(expr) => expr.arg_list(), diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs index c8f846c56..ec400ffe2 100644 --- a/crates/ra_analysis/src/lib.rs +++ b/crates/ra_analysis/src/lib.rs @@ -26,7 +26,7 @@ mod syntax_highlighting; use std::{fmt, sync::Arc}; -use ra_syntax::{SmolStr, SourceFileNode, SyntaxKind, TextRange, TextUnit}; +use ra_syntax::{SmolStr, SourceFile, TreePtr, SyntaxKind, TextRange, TextUnit}; use ra_text_edit::TextEdit; use rayon::prelude::*; use relative_path::RelativePathBuf; @@ -308,7 +308,7 @@ impl Analysis { self.db.file_text(file_id) } /// Gets the syntax tree of the file. - pub fn file_syntax(&self, file_id: FileId) -> SourceFileNode { + pub fn file_syntax(&self, file_id: FileId) -> TreePtr { self.db.source_file(file_id).clone() } /// Gets the file's `LineIndex`: data structure to convert between absolute @@ -322,7 +322,7 @@ impl Analysis { } /// Returns position of the mathcing brace (all types of braces are /// supported). - pub fn matching_brace(&self, file: &SourceFileNode, offset: TextUnit) -> Option { + pub fn matching_brace(&self, file: &SourceFile, offset: TextUnit) -> Option { ra_editor::matching_brace(file, offset) } /// Returns a syntax tree represented as `String`, for debug purposes. @@ -469,7 +469,7 @@ impl LibraryData { files: Vec<(FileId, RelativePathBuf, Arc)>, ) -> LibraryData { let symbol_index = SymbolIndex::for_files(files.par_iter().map(|(file_id, _, text)| { - let file = SourceFileNode::parse(text); + let file = SourceFile::parse(text); (*file_id, file) })); let mut root_change = RootChange::default(); diff --git a/crates/ra_analysis/src/runnables.rs b/crates/ra_analysis/src/runnables.rs index 216209098..98b1d2d55 100644 --- a/crates/ra_analysis/src/runnables.rs +++ b/crates/ra_analysis/src/runnables.rs @@ -1,7 +1,7 @@ use itertools::Itertools; use ra_syntax::{ + TextRange, SyntaxNode, ast::{self, AstNode, NameOwner, ModuleItemOwner}, - TextRange, SyntaxNodeRef, }; use ra_db::{Cancelable, SyntaxDatabase}; @@ -30,7 +30,7 @@ pub(crate) fn runnables(db: &RootDatabase, file_id: FileId) -> Cancelable Option { +fn runnable(db: &RootDatabase, file_id: FileId, item: &SyntaxNode) -> Option { if let Some(fn_def) = ast::FnDef::cast(item) { runnable_fn(fn_def) } else if let Some(m) = ast::Module::cast(item) { @@ -40,7 +40,7 @@ fn runnable(db: &RootDatabase, file_id: FileId, item: SyntaxNodeRef) -> Option Option { +fn runnable_fn(fn_def: &ast::FnDef) -> Option { let name = fn_def.name()?.text(); let kind = if name == "main" { RunnableKind::Bin @@ -57,12 +57,12 @@ fn runnable_fn(fn_def: ast::FnDef) -> Option { }) } -fn runnable_mod(db: &RootDatabase, file_id: FileId, module: ast::Module) -> Option { +fn runnable_mod(db: &RootDatabase, file_id: FileId, module: &ast::Module) -> Option { let has_test_function = module .item_list()? .items() - .filter_map(|it| match it { - ast::ModuleItem::FnDef(it) => Some(it), + .filter_map(|it| match it.kind() { + ast::ModuleItemKind::FnDef(it) => Some(it), _ => None, }) .any(|f| f.has_atom_attr("test")); diff --git a/crates/ra_analysis/src/symbol_index.rs b/crates/ra_analysis/src/symbol_index.rs index e2b1c88fe..ed1796756 100644 --- a/crates/ra_analysis/src/symbol_index.rs +++ b/crates/ra_analysis/src/symbol_index.rs @@ -27,7 +27,7 @@ use std::{ use fst::{self, Streamer}; use ra_syntax::{ - SyntaxNodeRef, SourceFileNode, SmolStr, + SyntaxNode, SourceFile, SmolStr, TreePtr, AstNode, algo::{visit::{visitor, Visitor}, find_covering_node}, SyntaxKind::{self, *}, ast::{self, NameOwner}, @@ -141,7 +141,7 @@ impl SymbolIndex { } pub(crate) fn for_files( - files: impl ParallelIterator, + files: impl ParallelIterator)>, ) -> SymbolIndex { let symbols = files .flat_map(|(file_id, file)| { @@ -203,8 +203,8 @@ pub(crate) struct FileSymbol { pub(crate) ptr: LocalSyntaxPtr, } -fn to_symbol(node: SyntaxNodeRef) -> Option<(SmolStr, LocalSyntaxPtr)> { - fn decl<'a, N: NameOwner<'a>>(node: N) -> Option<(SmolStr, LocalSyntaxPtr)> { +fn to_symbol(node: &SyntaxNode) -> Option<(SmolStr, LocalSyntaxPtr)> { + fn decl(node: &N) -> Option<(SmolStr, LocalSyntaxPtr)> { let name = node.name()?.text(); let ptr = LocalSyntaxPtr::new(node.syntax()); Some((name, ptr)) diff --git a/crates/ra_analysis/src/syntax_highlighting.rs b/crates/ra_analysis/src/syntax_highlighting.rs index 35e153ca0..d2dc6cfbb 100644 --- a/crates/ra_analysis/src/syntax_highlighting.rs +++ b/crates/ra_analysis/src/syntax_highlighting.rs @@ -16,7 +16,7 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Cancelable Option { self.expr_syntax_mapping.get(&ptr).cloned() } - pub fn node_expr(&self, node: ast::Expr) -> Option { + pub fn node_expr(&self, node: &ast::Expr) -> Option { self.expr_syntax_mapping .get(&LocalSyntaxPtr::new(node.syntax())) .cloned() @@ -88,7 +88,7 @@ impl BodySyntaxMapping { pub fn syntax_pat(&self, ptr: LocalSyntaxPtr) -> Option { self.pat_syntax_mapping.get(&ptr).cloned() } - pub fn node_pat(&self, node: ast::Pat) -> Option { + pub fn node_pat(&self, node: &ast::Pat) -> Option { self.pat_syntax_mapping .get(&LocalSyntaxPtr::new(node.syntax())) .cloned() -- cgit v1.2.3