From 431e4ff4ef83455adc7e2c0e3f732d6dc482641e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 26 Oct 2019 20:07:24 +0300 Subject: avoid TextEditorBuilder for simple edits --- .../ra_ide_api/src/completion/complete_postfix.rs | 6 ++---- .../ra_ide_api/src/completion/completion_item.rs | 12 +++++------ crates/ra_ide_api/src/diagnostics.rs | 9 +++----- crates/ra_ide_api/src/references/rename.rs | 24 ++++++++-------------- crates/ra_ide_api/src/typing.rs | 7 +++---- 5 files changed, 21 insertions(+), 37 deletions(-) diff --git a/crates/ra_ide_api/src/completion/complete_postfix.rs b/crates/ra_ide_api/src/completion/complete_postfix.rs index 555cecb73..60ed3518b 100644 --- a/crates/ra_ide_api/src/completion/complete_postfix.rs +++ b/crates/ra_ide_api/src/completion/complete_postfix.rs @@ -9,16 +9,14 @@ use crate::{ }; use hir::{Ty, TypeCtor}; use ra_syntax::{ast::AstNode, TextRange, TextUnit}; -use ra_text_edit::TextEditBuilder; +use ra_text_edit::TextEdit; fn postfix_snippet(ctx: &CompletionContext, label: &str, detail: &str, snippet: &str) -> Builder { let edit = { let receiver_range = ctx.dot_receiver.as_ref().expect("no receiver available").syntax().text_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()); - builder.finish() + TextEdit::replace(delete_range, snippet.to_string()) }; CompletionItem::new(CompletionKind::Postfix, ctx.source_range(), label) .detail(detail) diff --git a/crates/ra_ide_api/src/completion/completion_item.rs b/crates/ra_ide_api/src/completion/completion_item.rs index 3e6933bc1..5c9c44704 100644 --- a/crates/ra_ide_api/src/completion/completion_item.rs +++ b/crates/ra_ide_api/src/completion/completion_item.rs @@ -4,7 +4,7 @@ use std::fmt; use hir::Documentation; use ra_syntax::TextRange; -use ra_text_edit::{TextEdit, TextEditBuilder}; +use ra_text_edit::TextEdit; /// `CompletionItem` describes a single completion variant in the editor pop-up. /// It is basically a POD with various properties. To construct a @@ -192,12 +192,10 @@ impl Builder { let label = self.label; let text_edit = match self.text_edit { Some(it) => it, - None => { - let mut builder = TextEditBuilder::default(); - builder - .replace(self.source_range, self.insert_text.unwrap_or_else(|| label.clone())); - builder.finish() - } + None => TextEdit::replace( + self.source_range, + self.insert_text.unwrap_or_else(|| label.clone()), + ), }; CompletionItem { diff --git a/crates/ra_ide_api/src/diagnostics.rs b/crates/ra_ide_api/src/diagnostics.rs index 8743a3a79..1f1f5cd74 100644 --- a/crates/ra_ide_api/src/diagnostics.rs +++ b/crates/ra_ide_api/src/diagnostics.rs @@ -85,10 +85,9 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec }) .on::(|d| { let node = d.ast(db); - let mut builder = TextEditBuilder::default(); let replacement = format!("Ok({})", node.syntax()); - builder.replace(node.syntax().text_range(), replacement); - let fix = SourceChange::source_file_edit_from("wrap with ok", file_id, builder.finish()); + let edit = TextEdit::replace(node.syntax().text_range(), replacement); + let fix = SourceChange::source_file_edit_from("wrap with ok", file_id, edit); res.borrow_mut().push(Diagnostic { range: d.highlight_range(), message: d.message(), @@ -152,9 +151,7 @@ fn text_edit_for_remove_unnecessary_braces_with_self_in_use_statement( let start = use_tree_list_node.prev_sibling_or_token()?.text_range().start(); let end = use_tree_list_node.text_range().end(); let range = TextRange::from_to(start, end); - let mut edit_builder = TextEditBuilder::default(); - edit_builder.delete(range); - return Some(edit_builder.finish()); + return Some(TextEdit::delete(range)); } None } diff --git a/crates/ra_ide_api/src/references/rename.rs b/crates/ra_ide_api/src/references/rename.rs index ee6e73e1b..a8783d7a0 100644 --- a/crates/ra_ide_api/src/references/rename.rs +++ b/crates/ra_ide_api/src/references/rename.rs @@ -3,6 +3,7 @@ use hir::ModuleSource; use ra_db::{SourceDatabase, SourceDatabaseExt}; use ra_syntax::{algo::find_node_at_offset, ast, AstNode, SyntaxNode}; +use ra_text_edit::TextEdit; use relative_path::{RelativePath, RelativePathBuf}; use crate::{ @@ -43,14 +44,7 @@ fn source_edit_from_file_id_range( range: TextRange, new_name: &str, ) -> SourceFileEdit { - SourceFileEdit { - file_id, - edit: { - let mut builder = ra_text_edit::TextEditBuilder::default(); - builder.replace(range, new_name.into()); - builder.finish() - }, - } + SourceFileEdit { file_id, edit: TextEdit::replace(range, new_name.into()) } } fn rename_mod( @@ -94,11 +88,7 @@ fn rename_mod( let edit = SourceFileEdit { file_id: position.file_id, - edit: { - let mut builder = ra_text_edit::TextEditBuilder::default(); - builder.replace(ast_name.syntax().text_range(), new_name.into()); - builder.finish() - }, + edit: TextEdit::replace(ast_name.syntax().text_range(), new_name.into()), }; source_file_edits.push(edit); @@ -126,12 +116,14 @@ fn rename_reference( #[cfg(test)] mod tests { + use insta::assert_debug_snapshot; + use ra_text_edit::TextEditBuilder; + use test_utils::assert_eq_text; + use crate::{ mock_analysis::analysis_and_position, mock_analysis::single_file_with_position, FileId, ReferenceSearchResult, }; - use insta::assert_debug_snapshot; - use test_utils::assert_eq_text; #[test] fn test_find_all_refs_for_local() { @@ -452,7 +444,7 @@ mod tests { fn test_rename(text: &str, new_name: &str, expected: &str) { let (analysis, position) = single_file_with_position(text); let source_change = analysis.rename(position, new_name).unwrap(); - let mut text_edit_builder = ra_text_edit::TextEditBuilder::default(); + let mut text_edit_builder = TextEditBuilder::default(); let mut file_id: Option = None; if let Some(change) = source_change { for edit in change.info.source_file_edits { diff --git a/crates/ra_ide_api/src/typing.rs b/crates/ra_ide_api/src/typing.rs index 26a3111fd..2dfbe6944 100644 --- a/crates/ra_ide_api/src/typing.rs +++ b/crates/ra_ide_api/src/typing.rs @@ -22,7 +22,7 @@ use ra_syntax::{ SyntaxKind::*, SyntaxToken, TextRange, TextUnit, TokenAtOffset, }; -use ra_text_edit::{TextEdit, TextEditBuilder}; +use ra_text_edit::TextEdit; use crate::{db::RootDatabase, source_change::SingleFileChange, SourceChange, SourceFileEdit}; @@ -49,13 +49,12 @@ pub(crate) fn on_enter(db: &RootDatabase, position: FilePosition) -> Option