From f3bdbec1b68fa0e20f0b7b6c6ef64e1507970b0d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 20 Jul 2019 12:58:27 +0300 Subject: rename range -> text_range --- crates/ra_assists/src/add_derive.rs | 6 +++--- crates/ra_assists/src/add_explicit_type.rs | 4 ++-- crates/ra_assists/src/add_impl.rs | 4 ++-- crates/ra_assists/src/add_missing_impl_members.rs | 4 ++-- crates/ra_assists/src/assist_ctx.rs | 2 +- crates/ra_assists/src/ast_editor.rs | 5 ++++- crates/ra_assists/src/auto_import.rs | 18 ++++++++--------- crates/ra_assists/src/change_visibility.rs | 23 +++++++++++----------- crates/ra_assists/src/fill_match_arms.rs | 4 ++-- crates/ra_assists/src/flip_binexpr.rs | 6 +++--- crates/ra_assists/src/flip_comma.rs | 6 +++--- crates/ra_assists/src/inline_local_variable.rs | 7 +++++-- crates/ra_assists/src/introduce_variable.rs | 12 +++++------ crates/ra_assists/src/move_guard.rs | 20 ++++++++++--------- crates/ra_assists/src/remove_dbg.rs | 4 ++-- crates/ra_assists/src/replace_if_let_with_match.rs | 4 ++-- crates/ra_assists/src/split_import.rs | 8 ++++---- 17 files changed, 73 insertions(+), 64 deletions(-) (limited to 'crates/ra_assists') diff --git a/crates/ra_assists/src/add_derive.rs b/crates/ra_assists/src/add_derive.rs index f19196f53..9c88644df 100644 --- a/crates/ra_assists/src/add_derive.rs +++ b/crates/ra_assists/src/add_derive.rs @@ -22,9 +22,9 @@ pub(crate) fn add_derive(mut ctx: AssistCtx) -> Option edit.insert(node_start, "#[derive()]\n"); node_start + TextUnit::of_str("#[derive(") } - Some(tt) => tt.syntax().range().end() - TextUnit::of_char(')'), + Some(tt) => tt.syntax().text_range().end() - TextUnit::of_char(')'), }; - edit.target(nominal.syntax().range()); + edit.target(nominal.syntax().text_range()); edit.set_cursor(offset) }); @@ -37,7 +37,7 @@ fn derive_insertion_offset(nominal: &ast::NominalDef) -> Option { .syntax() .children_with_tokens() .find(|it| it.kind() != COMMENT && it.kind() != WHITESPACE)?; - Some(non_ws_child.range().start()) + Some(non_ws_child.text_range().start()) } #[cfg(test)] diff --git a/crates/ra_assists/src/add_explicit_type.rs b/crates/ra_assists/src/add_explicit_type.rs index a69cfc8e3..88970929f 100644 --- a/crates/ra_assists/src/add_explicit_type.rs +++ b/crates/ra_assists/src/add_explicit_type.rs @@ -16,10 +16,10 @@ pub(crate) fn add_explicit_type(mut ctx: AssistCtx) -> Option< PatKind::BindPat(bind_pat) => bind_pat, _ => return None, }; - let pat_range = pat.syntax().range(); + let pat_range = pat.syntax().text_range(); // The binding must have a name let name = pat.name()?; - let name_range = name.syntax().range(); + let name_range = name.syntax().text_range(); // Assist not applicable if the type has already been specified if stmt.syntax().children_with_tokens().any(|child| child.kind() == T![:]) { return None; diff --git a/crates/ra_assists/src/add_impl.rs b/crates/ra_assists/src/add_impl.rs index cebc19539..9a0cfb4e7 100644 --- a/crates/ra_assists/src/add_impl.rs +++ b/crates/ra_assists/src/add_impl.rs @@ -11,9 +11,9 @@ pub(crate) fn add_impl(mut ctx: AssistCtx) -> Option { let nominal = ctx.node_at_offset::()?; let name = nominal.name()?; ctx.add_action(AssistId("add_impl"), "add impl", |edit| { - edit.target(nominal.syntax().range()); + edit.target(nominal.syntax().text_range()); let type_params = nominal.type_param_list(); - let start_offset = nominal.syntax().range().end(); + let start_offset = nominal.syntax().text_range().end(); let mut buf = String::new(); buf.push_str("\n\nimpl"); if let Some(type_params) = &type_params { diff --git a/crates/ra_assists/src/add_missing_impl_members.rs b/crates/ra_assists/src/add_missing_impl_members.rs index b992a4dc8..995e44d5e 100644 --- a/crates/ra_assists/src/add_missing_impl_members.rs +++ b/crates/ra_assists/src/add_missing_impl_members.rs @@ -43,7 +43,7 @@ fn add_missing_impl_members_inner( let trait_def = { let file_id = ctx.frange.file_id; - let position = FilePosition { file_id, offset: impl_node.syntax().range().start() }; + let position = FilePosition { file_id, offset: impl_node.syntax().text_range().start() }; let analyzer = hir::SourceAnalyzer::new(ctx.db, position.file_id, impl_node.syntax(), None); resolve_target_trait_def(ctx.db, &analyzer, &impl_node)? @@ -87,7 +87,7 @@ fn add_missing_impl_members_inner( ast_editor.append_items(items); let first_new_item = ast_editor.ast().impl_items().nth(n_existing_items).unwrap(); - let cursor_position = first_new_item.syntax().range().start(); + let cursor_position = first_new_item.syntax().text_range().start(); ast_editor.into_text_edit(edit.text_edit_builder()); edit.set_cursor(cursor_position); diff --git a/crates/ra_assists/src/assist_ctx.rs b/crates/ra_assists/src/assist_ctx.rs index e52085f85..4d5a76de6 100644 --- a/crates/ra_assists/src/assist_ctx.rs +++ b/crates/ra_assists/src/assist_ctx.rs @@ -141,7 +141,7 @@ impl AssistBuilder { if let Some(indent) = leading_indent(node) { replace_with = reindent(&replace_with, &indent) } - self.replace(node.range(), replace_with) + self.replace(node.text_range(), replace_with) } pub(crate) fn set_edit_builder(&mut self, edit: TextEditBuilder) { diff --git a/crates/ra_assists/src/ast_editor.rs b/crates/ra_assists/src/ast_editor.rs index a35334d7e..36235eb13 100644 --- a/crates/ra_assists/src/ast_editor.rs +++ b/crates/ra_assists/src/ast_editor.rs @@ -22,7 +22,10 @@ impl AstEditor { // FIXME: compute a more fine-grained diff here. // If *you* know a nice algorithm to compute diff between two syntax // tree, tell me about it! - builder.replace(self.original_ast.syntax().range(), self.ast().syntax().text().to_string()); + builder.replace( + self.original_ast.syntax().text_range(), + self.ast().syntax().text().to_string(), + ); } pub fn ast(&self) -> &N { diff --git a/crates/ra_assists/src/auto_import.rs b/crates/ra_assists/src/auto_import.rs index 0eb4bdb62..43e75eee1 100644 --- a/crates/ra_assists/src/auto_import.rs +++ b/crates/ra_assists/src/auto_import.rs @@ -356,7 +356,7 @@ fn best_action_for_target( // todo: we should include even whitespace blocks as anchor candidates let anchor = container .children() - .find(|n| n.range().start() < anchor.range().start()) + .find(|n| n.text_range().start() < anchor.text_range().start()) .or_else(|| Some(anchor)); ImportAction::add_new_use(anchor, false) @@ -418,7 +418,7 @@ fn make_assist_add_new_use( buf.push_str(&spaces); } } - let position = if after { anchor.range().end() } else { anchor.range().start() }; + let position = if after { anchor.text_range().end() } else { anchor.text_range().start() }; edit.insert(position, buf); } } @@ -434,10 +434,10 @@ fn make_assist_add_in_tree_list( let mut buf = String::new(); let comma = last.syntax().siblings(Direction::Next).find(|n| n.kind() == T![,]); let offset = if let Some(comma) = comma { - comma.range().end() + comma.text_range().end() } else { buf.push_str(","); - last.syntax().range().end() + last.syntax().text_range().end() }; if add_self { buf.push_str(" self") @@ -462,11 +462,11 @@ fn make_assist_add_nested_import( if let Some(use_tree) = use_tree { let (start, add_colon_colon) = if let Some(first_segment_to_split) = first_segment_to_split { - (first_segment_to_split.syntax().range().start(), false) + (first_segment_to_split.syntax().text_range().start(), false) } else { - (use_tree.syntax().range().end(), true) + (use_tree.syntax().text_range().end(), true) }; - let end = use_tree.syntax().range().end(); + let end = use_tree.syntax().text_range().end(); let mut buf = String::new(); if add_colon_colon { @@ -497,8 +497,8 @@ fn apply_auto_import( // Here we are assuming the assist will provide a correct use statement // so we can delete the path qualifier edit.delete(TextRange::from_to( - path.syntax().range().start(), - last.syntax().range().start(), + path.syntax().text_range().start(), + last.syntax().text_range().start(), )); } } diff --git a/crates/ra_assists/src/change_visibility.rs b/crates/ra_assists/src/change_visibility.rs index ab10d2aa4..d28cdd07b 100644 --- a/crates/ra_assists/src/change_visibility.rs +++ b/crates/ra_assists/src/change_visibility.rs @@ -35,14 +35,15 @@ fn add_vis(mut ctx: AssistCtx) -> Option { if parent.children().any(|child| child.kind() == VISIBILITY) { return None; } - (vis_offset(&parent), keyword.range()) + (vis_offset(&parent), keyword.text_range()) } else { let ident = ctx.token_at_offset().find(|leaf| leaf.kind() == IDENT)?; let field = ident.parent().ancestors().find_map(ast::NamedFieldDef::cast)?; - if field.name()?.syntax().range() != ident.range() && field.visibility().is_some() { + if field.name()?.syntax().text_range() != ident.text_range() && field.visibility().is_some() + { return None; } - (vis_offset(field.syntax()), ident.range()) + (vis_offset(field.syntax()), ident.text_range()) }; ctx.add_action(AssistId("change_visibility"), "make pub(crate)", |edit| { @@ -61,25 +62,25 @@ fn vis_offset(node: &SyntaxNode) -> TextUnit { _ => false, }) .next() - .map(|it| it.range().start()) - .unwrap_or_else(|| node.range().start()) + .map(|it| it.text_range().start()) + .unwrap_or_else(|| node.text_range().start()) } fn change_vis(mut ctx: AssistCtx, vis: ast::Visibility) -> Option { if vis.syntax().text() == "pub" { ctx.add_action(AssistId("change_visibility"), "change to pub(crate)", |edit| { - edit.target(vis.syntax().range()); - edit.replace(vis.syntax().range(), "pub(crate)"); - edit.set_cursor(vis.syntax().range().start()) + edit.target(vis.syntax().text_range()); + edit.replace(vis.syntax().text_range(), "pub(crate)"); + edit.set_cursor(vis.syntax().text_range().start()) }); return ctx.build(); } if vis.syntax().text() == "pub(crate)" { ctx.add_action(AssistId("change_visibility"), "change to pub", |edit| { - edit.target(vis.syntax().range()); - edit.replace(vis.syntax().range(), "pub"); - edit.set_cursor(vis.syntax().range().start()); + edit.target(vis.syntax().text_range()); + edit.replace(vis.syntax().text_range(), "pub"); + edit.set_cursor(vis.syntax().text_range().start()); }); return ctx.build(); diff --git a/crates/ra_assists/src/fill_match_arms.rs b/crates/ra_assists/src/fill_match_arms.rs index b96806ac6..939429892 100644 --- a/crates/ra_assists/src/fill_match_arms.rs +++ b/crates/ra_assists/src/fill_match_arms.rs @@ -84,8 +84,8 @@ pub(crate) fn fill_match_arms(mut ctx: AssistCtx) -> Option (),\n"); } buf.push_str("}"); - edit.target(match_expr.syntax().range()); - edit.set_cursor(expr.syntax().range().start()); + edit.target(match_expr.syntax().text_range()); + edit.set_cursor(expr.syntax().text_range().start()); edit.replace_node_and_indent(match_expr.syntax(), buf); }); diff --git a/crates/ra_assists/src/flip_binexpr.rs b/crates/ra_assists/src/flip_binexpr.rs index 2e591ad3b..b55b36a8e 100644 --- a/crates/ra_assists/src/flip_binexpr.rs +++ b/crates/ra_assists/src/flip_binexpr.rs @@ -8,7 +8,7 @@ pub(crate) fn flip_binexpr(mut ctx: AssistCtx) -> Option()?; let lhs = expr.lhs()?.syntax().clone(); let rhs = expr.rhs()?.syntax().clone(); - let op_range = expr.op_token()?.range(); + let op_range = expr.op_token()?.text_range(); // The assist should be applied only if the cursor is on the operator let cursor_in_range = ctx.frange.range.is_subrange(&op_range); if !cursor_in_range { @@ -25,8 +25,8 @@ pub(crate) fn flip_binexpr(mut ctx: AssistCtx) -> Option) -> Option let prev = non_trivia_sibling(comma.clone().into(), Direction::Prev)?; let next = non_trivia_sibling(comma.clone().into(), Direction::Next)?; ctx.add_action(AssistId("flip_comma"), "flip comma", |edit| { - edit.target(comma.range()); - edit.replace(prev.range(), next.to_string()); - edit.replace(next.range(), prev.to_string()); + edit.target(comma.text_range()); + edit.replace(prev.text_range(), next.to_string()); + edit.replace(next.text_range(), prev.to_string()); }); ctx.build() diff --git a/crates/ra_assists/src/inline_local_variable.rs b/crates/ra_assists/src/inline_local_variable.rs index 3c17089de..82c4d54b0 100644 --- a/crates/ra_assists/src/inline_local_variable.rs +++ b/crates/ra_assists/src/inline_local_variable.rs @@ -22,9 +22,12 @@ pub(crate) fn inline_local_varialbe(mut ctx: AssistCtx) -> Opt .next_sibling_or_token() .and_then(|it| ast::Whitespace::cast(it.as_token()?.clone())) { - TextRange::from_to(let_stmt.syntax().range().start(), whitespace.syntax().range().end()) + TextRange::from_to( + let_stmt.syntax().text_range().start(), + whitespace.syntax().text_range().end(), + ) } else { - let_stmt.syntax().range() + let_stmt.syntax().text_range() }; let analyzer = hir::SourceAnalyzer::new(ctx.db, ctx.frange.file_id, bind_pat.syntax(), None); let refs = analyzer.find_all_refs(&bind_pat); diff --git a/crates/ra_assists/src/introduce_variable.rs b/crates/ra_assists/src/introduce_variable.rs index ce28132c9..df6c58989 100644 --- a/crates/ra_assists/src/introduce_variable.rs +++ b/crates/ra_assists/src/introduce_variable.rs @@ -48,7 +48,7 @@ pub(crate) fn introduce_variable(mut ctx: AssistCtx) -> Option if !full_stmt.unwrap().has_semi() { buf.push_str(";"); } - edit.replace(expr.syntax().range(), buf); + edit.replace(expr.syntax().text_range(), buf); } else { buf.push_str(";"); @@ -66,14 +66,14 @@ pub(crate) fn introduce_variable(mut ctx: AssistCtx) -> Option buf.push_str(text); } - edit.target(expr.syntax().range()); - edit.replace(expr.syntax().range(), "var_name".to_string()); - edit.insert(anchor_stmt.range().start(), buf); + edit.target(expr.syntax().text_range()); + edit.replace(expr.syntax().text_range(), "var_name".to_string()); + edit.insert(anchor_stmt.text_range().start(), buf); if wrap_in_block { - edit.insert(anchor_stmt.range().end(), " }"); + edit.insert(anchor_stmt.text_range().end(), " }"); } } - edit.set_cursor(anchor_stmt.range().start() + cursor_offset); + edit.set_cursor(anchor_stmt.text_range().start() + cursor_offset); }); ctx.build() diff --git a/crates/ra_assists/src/move_guard.rs b/crates/ra_assists/src/move_guard.rs index 313c9ad18..0f3cdbe53 100644 --- a/crates/ra_assists/src/move_guard.rs +++ b/crates/ra_assists/src/move_guard.rs @@ -17,11 +17,11 @@ pub(crate) fn move_guard_to_arm_body(mut ctx: AssistCtx) -> Op let buf = format!("if {} {{ {} }}", guard_conditions.syntax().text(), arm_expr.syntax().text()); ctx.add_action(AssistId("move_guard_to_arm_body"), "move guard to arm body", |edit| { - edit.target(guard.syntax().range()); + edit.target(guard.syntax().text_range()); let offseting_amount = match &space_before_guard { Some(SyntaxElement::Token(tok)) => { if let Some(_) = ast::Whitespace::cast(tok.clone()) { - let ele = space_before_guard.unwrap().range(); + let ele = space_before_guard.unwrap().text_range(); edit.delete(ele); ele.len() } else { @@ -31,9 +31,11 @@ pub(crate) fn move_guard_to_arm_body(mut ctx: AssistCtx) -> Op _ => TextUnit::from(0), }; - edit.delete(guard.syntax().range()); + edit.delete(guard.syntax().text_range()); edit.replace_node_and_indent(arm_expr.syntax(), buf); - edit.set_cursor(arm_expr.syntax().range().start() + TextUnit::from(3) - offseting_amount); + edit.set_cursor( + arm_expr.syntax().text_range().start() + TextUnit::from(3) - offseting_amount, + ); }); ctx.build() } @@ -62,18 +64,18 @@ pub(crate) fn move_arm_cond_to_match_guard(mut ctx: AssistCtx) AssistId("move_arm_cond_to_match_guard"), "move condition to match guard", |edit| { - edit.target(if_expr.syntax().range()); + edit.target(if_expr.syntax().text_range()); let then_only_expr = then_block.statements().next().is_none(); match &then_block.expr() { Some(then_expr) if then_only_expr => { - edit.replace(if_expr.syntax().range(), then_expr.syntax().text()) + edit.replace(if_expr.syntax().text_range(), then_expr.syntax().text()) } - _ => edit.replace(if_expr.syntax().range(), then_block.syntax().text()), + _ => edit.replace(if_expr.syntax().text_range(), then_block.syntax().text()), } - edit.insert(last_match_pat.syntax().range().end(), buf); - edit.set_cursor(last_match_pat.syntax().range().end() + TextUnit::from(1)); + edit.insert(last_match_pat.syntax().text_range().end(), buf); + edit.set_cursor(last_match_pat.syntax().text_range().end() + TextUnit::from(1)); }, ); ctx.build() diff --git a/crates/ra_assists/src/remove_dbg.rs b/crates/ra_assists/src/remove_dbg.rs index 5657ee4b8..870133fda 100644 --- a/crates/ra_assists/src/remove_dbg.rs +++ b/crates/ra_assists/src/remove_dbg.rs @@ -12,7 +12,7 @@ pub(crate) fn remove_dbg(mut ctx: AssistCtx) -> Option return None; } - let macro_range = macro_call.syntax().range(); + let macro_range = macro_call.syntax().text_range(); // If the cursor is inside the macro call, we'll try to maintain the cursor // position by subtracting the length of dbg!( from the start of the file @@ -43,7 +43,7 @@ pub(crate) fn remove_dbg(mut ctx: AssistCtx) -> Option }; ctx.add_action(AssistId("remove_dbg"), "remove dbg!()", |edit| { - edit.target(macro_call.syntax().range()); + edit.target(macro_call.syntax().text_range()); edit.replace(macro_range, macro_content); edit.set_cursor(cursor_pos); }); diff --git a/crates/ra_assists/src/replace_if_let_with_match.rs b/crates/ra_assists/src/replace_if_let_with_match.rs index 5de6aa266..c0bf6d235 100644 --- a/crates/ra_assists/src/replace_if_let_with_match.rs +++ b/crates/ra_assists/src/replace_if_let_with_match.rs @@ -17,9 +17,9 @@ pub(crate) fn replace_if_let_with_match(mut ctx: AssistCtx) -> ctx.add_action(AssistId("replace_if_let_with_match"), "replace with match", |edit| { let match_expr = build_match_expr(expr, pat, then_block, else_block); - edit.target(if_expr.syntax().range()); + edit.target(if_expr.syntax().text_range()); edit.replace_node_and_indent(if_expr.syntax(), match_expr); - edit.set_cursor(if_expr.syntax().range().start()) + edit.set_cursor(if_expr.syntax().text_range().start()) }); ctx.build() diff --git a/crates/ra_assists/src/split_import.rs b/crates/ra_assists/src/split_import.rs index a8feb67c8..2c1edddb9 100644 --- a/crates/ra_assists/src/split_import.rs +++ b/crates/ra_assists/src/split_import.rs @@ -15,14 +15,14 @@ pub(crate) fn split_import(mut ctx: AssistCtx) -> Option tree.syntax().range().end(), - None => top_path.syntax().range().end(), + Some(tree) => tree.syntax().text_range().end(), + None => top_path.syntax().text_range().end(), }; ctx.add_action(AssistId("split_import"), "split import", |edit| { - edit.target(colon_colon.range()); + edit.target(colon_colon.text_range()); edit.insert(l_curly, "{"); edit.insert(r_curly, "}"); edit.set_cursor(l_curly + TextUnit::of_str("{")); -- cgit v1.2.3