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 --- crates/ra_ide_api/src/completion/complete_dot.rs | 9 ++++---- .../ra_ide_api/src/completion/complete_fn_param.rs | 7 ++---- .../ra_ide_api/src/completion/complete_keyword.rs | 16 ++++++------- .../ra_ide_api/src/completion/complete_postfix.rs | 7 +++--- crates/ra_ide_api/src/completion/complete_scope.rs | 4 ++-- .../src/completion/complete_struct_literal.rs | 4 ++-- .../src/completion/completion_context.rs | 26 +++++++++++----------- 7 files changed, 36 insertions(+), 37 deletions(-) (limited to 'crates/ra_ide_api/src/completion') diff --git a/crates/ra_ide_api/src/completion/complete_dot.rs b/crates/ra_ide_api/src/completion/complete_dot.rs index a5f071442..536ba36df 100644 --- a/crates/ra_ide_api/src/completion/complete_dot.rs +++ b/crates/ra_ide_api/src/completion/complete_dot.rs @@ -5,10 +5,11 @@ use rustc_hash::FxHashSet; /// Complete dot accesses, i.e. fields or methods (currently only fields). pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) { - let receiver_ty = match ctx.dot_receiver.and_then(|it| ctx.analyzer.type_of(ctx.db, it)) { - Some(it) => it, - None => return, - }; + let receiver_ty = + match ctx.dot_receiver.as_ref().and_then(|it| ctx.analyzer.type_of(ctx.db, it)) { + Some(it) => it, + None => return, + }; if !ctx.is_call { complete_fields(acc, ctx, receiver_ty.clone()); } diff --git a/crates/ra_ide_api/src/completion/complete_fn_param.rs b/crates/ra_ide_api/src/completion/complete_fn_param.rs index 5a117c485..0887ef1f6 100644 --- a/crates/ra_ide_api/src/completion/complete_fn_param.rs +++ b/crates/ra_ide_api/src/completion/complete_fn_param.rs @@ -20,7 +20,7 @@ pub(super) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext) let _ = visitor_ctx(&mut params) .visit::(process) .visit::(process) - .accept(node); + .accept(&node); } params .into_iter() @@ -38,10 +38,7 @@ pub(super) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext) .add_to(acc) }); - fn process<'a, N: ast::FnDefOwner>( - node: &'a N, - params: &mut FxHashMap, - ) { + fn process(node: N, params: &mut FxHashMap) { node.functions().filter_map(|it| it.param_list()).flat_map(|it| it.params()).for_each( |param| { let text = param.syntax().text().to_string(); diff --git a/crates/ra_ide_api/src/completion/complete_keyword.rs b/crates/ra_ide_api/src/completion/complete_keyword.rs index 034ed934d..4cf34eff8 100644 --- a/crates/ra_ide_api/src/completion/complete_keyword.rs +++ b/crates/ra_ide_api/src/completion/complete_keyword.rs @@ -52,7 +52,7 @@ pub(super) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte return; } - let fn_def = match ctx.function_syntax { + let fn_def = match &ctx.function_syntax { Some(it) => it, None => return, }; @@ -65,7 +65,7 @@ pub(super) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte acc.add(keyword(ctx, "else", "else {$0}")); acc.add(keyword(ctx, "else if", "else if $0 {}")); } - if is_in_loop_body(ctx.token) { + if is_in_loop_body(&ctx.token) { if ctx.can_be_stmt { acc.add(keyword(ctx, "continue", "continue;")); acc.add(keyword(ctx, "break", "break;")); @@ -74,19 +74,19 @@ pub(super) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte acc.add(keyword(ctx, "break", "break")); } } - acc.add_all(complete_return(ctx, fn_def, ctx.can_be_stmt)); + acc.add_all(complete_return(ctx, &fn_def, ctx.can_be_stmt)); } -fn is_in_loop_body(leaf: SyntaxToken) -> bool { +fn is_in_loop_body(leaf: &SyntaxToken) -> bool { for node in leaf.parent().ancestors() { if node.kind() == FN_DEF || node.kind() == LAMBDA_EXPR { break; } let loop_body = visitor() - .visit::(LoopBodyOwner::loop_body) - .visit::(LoopBodyOwner::loop_body) - .visit::(LoopBodyOwner::loop_body) - .accept(node); + .visit::(|it| it.loop_body()) + .visit::(|it| it.loop_body()) + .visit::(|it| it.loop_body()) + .accept(&node); if let Some(Some(body)) = loop_body { if leaf.range().is_subrange(&body.syntax().range()) { return true; diff --git a/crates/ra_ide_api/src/completion/complete_postfix.rs b/crates/ra_ide_api/src/completion/complete_postfix.rs index 4f5062214..c75b1c159 100644 --- a/crates/ra_ide_api/src/completion/complete_postfix.rs +++ b/crates/ra_ide_api/src/completion/complete_postfix.rs @@ -11,7 +11,8 @@ use ra_text_edit::TextEditBuilder; fn postfix_snippet(ctx: &CompletionContext, label: &str, detail: &str, snippet: &str) -> Builder { let edit = { - let receiver_range = ctx.dot_receiver.expect("no receiver available").syntax().range(); + let receiver_range = + ctx.dot_receiver.as_ref().expect("no receiver available").syntax().range(); let delete_range = TextRange::from_to(receiver_range.start(), ctx.source_range().end()); let mut builder = TextEditBuilder::default(); builder.replace(delete_range, snippet.to_string()); @@ -38,9 +39,9 @@ fn is_bool_or_unknown(ty: Option) -> bool { } pub(super) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) { - if let Some(dot_receiver) = ctx.dot_receiver { + if let Some(dot_receiver) = &ctx.dot_receiver { let receiver_text = dot_receiver.syntax().text().to_string(); - let receiver_ty = ctx.analyzer.type_of(ctx.db, dot_receiver); + let receiver_ty = ctx.analyzer.type_of(ctx.db, &dot_receiver); if is_bool_or_unknown(receiver_ty) { postfix_snippet(ctx, "if", "if expr {}", &format!("if {} {{$0}}", receiver_text)) .add_to(acc); diff --git a/crates/ra_ide_api/src/completion/complete_scope.rs b/crates/ra_ide_api/src/completion/complete_scope.rs index 1ba871257..f92034055 100644 --- a/crates/ra_ide_api/src/completion/complete_scope.rs +++ b/crates/ra_ide_api/src/completion/complete_scope.rs @@ -20,8 +20,8 @@ pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) { let mut builder = TextEditBuilder::default(); builder.replace(ctx.source_range(), name.to_string()); auto_import::auto_import_text_edit( - ctx.token.parent(), - ctx.token.parent(), + &ctx.token.parent(), + &ctx.token.parent(), &path, &mut builder, ); diff --git a/crates/ra_ide_api/src/completion/complete_struct_literal.rs b/crates/ra_ide_api/src/completion/complete_struct_literal.rs index b6216f857..9410f740f 100644 --- a/crates/ra_ide_api/src/completion/complete_struct_literal.rs +++ b/crates/ra_ide_api/src/completion/complete_struct_literal.rs @@ -4,8 +4,8 @@ use crate::completion::{CompletionContext, Completions}; /// Complete fields in fields literals. pub(super) fn complete_struct_literal(acc: &mut Completions, ctx: &CompletionContext) { - let (ty, variant) = match ctx.struct_lit_syntax.and_then(|it| { - Some((ctx.analyzer.type_of(ctx.db, it.into())?, ctx.analyzer.resolve_variant(it)?)) + let (ty, variant) = match ctx.struct_lit_syntax.as_ref().and_then(|it| { + Some((ctx.analyzer.type_of(ctx.db, &it.clone().into())?, ctx.analyzer.resolve_variant(it)?)) }) { Some(it) => it, _ => return, diff --git a/crates/ra_ide_api/src/completion/completion_context.rs b/crates/ra_ide_api/src/completion/completion_context.rs index 4aa84751f..b803271ab 100644 --- a/crates/ra_ide_api/src/completion/completion_context.rs +++ b/crates/ra_ide_api/src/completion/completion_context.rs @@ -16,11 +16,11 @@ pub(crate) struct CompletionContext<'a> { pub(super) db: &'a db::RootDatabase, pub(super) analyzer: hir::SourceAnalyzer, pub(super) offset: TextUnit, - pub(super) token: SyntaxToken<'a>, + pub(super) token: SyntaxToken, pub(super) module: Option, - pub(super) function_syntax: Option<&'a ast::FnDef>, - pub(super) use_item_syntax: Option<&'a ast::UseItem>, - pub(super) struct_lit_syntax: Option<&'a ast::StructLit>, + pub(super) function_syntax: Option, + pub(super) use_item_syntax: Option, + pub(super) struct_lit_syntax: Option, pub(super) is_param: bool, /// If a name-binding or reference to a const in a pattern. /// Irrefutable patterns (like let) are excluded. @@ -35,7 +35,7 @@ pub(crate) 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<&'a ast::Expr>, + pub(super) dot_receiver: Option, /// If this is a call (method or function) in particular, i.e. the () are already there. pub(super) is_call: bool, } @@ -50,7 +50,7 @@ impl<'a> CompletionContext<'a> { let token = find_token_at_offset(original_parse.tree().syntax(), position.offset).left_biased()?; let analyzer = - hir::SourceAnalyzer::new(db, position.file_id, token.parent(), Some(position.offset)); + hir::SourceAnalyzer::new(db, position.file_id, &token.parent(), Some(position.offset)); let mut ctx = CompletionContext { db, analyzer, @@ -109,7 +109,7 @@ impl<'a> CompletionContext<'a> { if is_node::(name.syntax()) { let bind_pat = name.syntax().ancestors().find_map(ast::BindPat::cast).unwrap(); let parent = bind_pat.syntax().parent(); - if parent.and_then(ast::MatchArm::cast).is_some() + if parent.clone().and_then(ast::MatchArm::cast).is_some() || parent.and_then(ast::Condition::cast).is_some() { self.is_pat_binding = true; @@ -122,7 +122,7 @@ impl<'a> CompletionContext<'a> { } } - fn classify_name_ref(&mut self, original_file: &'a SourceFile, name_ref: &ast::NameRef) { + fn classify_name_ref(&mut self, original_file: SourceFile, name_ref: ast::NameRef) { let name_range = name_ref.syntax().range(); if name_ref.syntax().parent().and_then(ast::NamedField::cast).is_some() { self.struct_lit_syntax = find_node_at_offset(original_file.syntax(), self.offset); @@ -153,7 +153,7 @@ impl<'a> CompletionContext<'a> { None => return, }; - if let Some(segment) = ast::PathSegment::cast(parent) { + if let Some(segment) = ast::PathSegment::cast(parent.clone()) { let path = segment.parent_path(); self.is_call = path .syntax() @@ -162,7 +162,7 @@ impl<'a> CompletionContext<'a> { .and_then(|it| it.syntax().parent().and_then(ast::CallExpr::cast)) .is_some(); - if let Some(mut path) = hir::Path::from_ast(path) { + if let Some(mut path) = hir::Path::from_ast(path.clone()) { if !path.is_ident() { path.segments.pop().unwrap(); self.path_prefix = Some(path); @@ -179,7 +179,7 @@ impl<'a> CompletionContext<'a> { .syntax() .ancestors() .find_map(|node| { - if let Some(stmt) = ast::ExprStmt::cast(node) { + if let Some(stmt) = ast::ExprStmt::cast(node.clone()) { return Some(stmt.syntax().range() == name_ref.syntax().range()); } if let Some(block) = ast::Block::cast(node) { @@ -203,7 +203,7 @@ impl<'a> CompletionContext<'a> { } } } - if let Some(field_expr) = ast::FieldExpr::cast(parent) { + if let Some(field_expr) = ast::FieldExpr::cast(parent.clone()) { // The receiver comes before the point of insertion of the fake // ident, so it should have the same range in the non-modified file self.dot_receiver = field_expr @@ -222,7 +222,7 @@ impl<'a> CompletionContext<'a> { } } -fn find_node_with_range(syntax: &SyntaxNode, range: TextRange) -> Option<&N> { +fn find_node_with_range(syntax: &SyntaxNode, range: TextRange) -> Option { find_covering_element(syntax, range).ancestors().find_map(N::cast) } -- cgit v1.2.3