avoid TextEditorBuilder for simple edits

This commit is contained in:
Aleksey Kladov 2019-10-26 20:07:24 +03:00
parent 568ef921ac
commit 431e4ff4ef
5 changed files with 21 additions and 37 deletions

View File

@ -9,16 +9,14 @@
};
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)

View File

@ -4,7 +4,7 @@
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 @@ pub(crate) fn build(self) -> CompletionItem {
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 {

View File

@ -85,10 +85,9 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic>
})
.on::<hir::diagnostics::MissingOkInTailExpr, _>(|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
}

View File

@ -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 @@ fn test_rename_mod_in_dir() {
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<FileId> = None;
if let Some(change) = source_change {
for edit in change.info.source_file_edits {

View File

@ -22,7 +22,7 @@
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<Sour
let indent = node_indent(&file, comment.syntax())?;
let inserted = format!("\n{}{} ", indent, prefix);
let cursor_position = position.offset + TextUnit::of_str(&inserted);
let mut edit = TextEditBuilder::default();
edit.insert(position.offset, inserted);
let edit = TextEdit::insert(position.offset, inserted);
Some(
SourceChange::source_file_edit(
"on enter",
SourceFileEdit { edit: edit.finish(), file_id: position.file_id },
SourceFileEdit { edit, file_id: position.file_id },
)
.with_cursor(FilePosition { offset: cursor_position, file_id: position.file_id }),
)