mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-27 05:23:24 +00:00
Merge #2079
2079: avoid TextEditorBuilder for simple edits r=matklad a=matklad Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
733fd64260
5 changed files with 21 additions and 37 deletions
|
@ -9,16 +9,14 @@ use crate::{
|
||||||
};
|
};
|
||||||
use hir::{Ty, TypeCtor};
|
use hir::{Ty, TypeCtor};
|
||||||
use ra_syntax::{ast::AstNode, TextRange, TextUnit};
|
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 {
|
fn postfix_snippet(ctx: &CompletionContext, label: &str, detail: &str, snippet: &str) -> Builder {
|
||||||
let edit = {
|
let edit = {
|
||||||
let receiver_range =
|
let receiver_range =
|
||||||
ctx.dot_receiver.as_ref().expect("no receiver available").syntax().text_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 delete_range = TextRange::from_to(receiver_range.start(), ctx.source_range().end());
|
||||||
let mut builder = TextEditBuilder::default();
|
TextEdit::replace(delete_range, snippet.to_string())
|
||||||
builder.replace(delete_range, snippet.to_string());
|
|
||||||
builder.finish()
|
|
||||||
};
|
};
|
||||||
CompletionItem::new(CompletionKind::Postfix, ctx.source_range(), label)
|
CompletionItem::new(CompletionKind::Postfix, ctx.source_range(), label)
|
||||||
.detail(detail)
|
.detail(detail)
|
||||||
|
|
|
@ -4,7 +4,7 @@ use std::fmt;
|
||||||
|
|
||||||
use hir::Documentation;
|
use hir::Documentation;
|
||||||
use ra_syntax::TextRange;
|
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.
|
/// `CompletionItem` describes a single completion variant in the editor pop-up.
|
||||||
/// It is basically a POD with various properties. To construct a
|
/// It is basically a POD with various properties. To construct a
|
||||||
|
@ -192,12 +192,10 @@ impl Builder {
|
||||||
let label = self.label;
|
let label = self.label;
|
||||||
let text_edit = match self.text_edit {
|
let text_edit = match self.text_edit {
|
||||||
Some(it) => it,
|
Some(it) => it,
|
||||||
None => {
|
None => TextEdit::replace(
|
||||||
let mut builder = TextEditBuilder::default();
|
self.source_range,
|
||||||
builder
|
self.insert_text.unwrap_or_else(|| label.clone()),
|
||||||
.replace(self.source_range, self.insert_text.unwrap_or_else(|| label.clone()));
|
),
|
||||||
builder.finish()
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
CompletionItem {
|
CompletionItem {
|
||||||
|
|
|
@ -85,10 +85,9 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic>
|
||||||
})
|
})
|
||||||
.on::<hir::diagnostics::MissingOkInTailExpr, _>(|d| {
|
.on::<hir::diagnostics::MissingOkInTailExpr, _>(|d| {
|
||||||
let node = d.ast(db);
|
let node = d.ast(db);
|
||||||
let mut builder = TextEditBuilder::default();
|
|
||||||
let replacement = format!("Ok({})", node.syntax());
|
let replacement = format!("Ok({})", node.syntax());
|
||||||
builder.replace(node.syntax().text_range(), replacement);
|
let edit = TextEdit::replace(node.syntax().text_range(), replacement);
|
||||||
let fix = SourceChange::source_file_edit_from("wrap with ok", file_id, builder.finish());
|
let fix = SourceChange::source_file_edit_from("wrap with ok", file_id, edit);
|
||||||
res.borrow_mut().push(Diagnostic {
|
res.borrow_mut().push(Diagnostic {
|
||||||
range: d.highlight_range(),
|
range: d.highlight_range(),
|
||||||
message: d.message(),
|
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 start = use_tree_list_node.prev_sibling_or_token()?.text_range().start();
|
||||||
let end = use_tree_list_node.text_range().end();
|
let end = use_tree_list_node.text_range().end();
|
||||||
let range = TextRange::from_to(start, end);
|
let range = TextRange::from_to(start, end);
|
||||||
let mut edit_builder = TextEditBuilder::default();
|
return Some(TextEdit::delete(range));
|
||||||
edit_builder.delete(range);
|
|
||||||
return Some(edit_builder.finish());
|
|
||||||
}
|
}
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
use hir::ModuleSource;
|
use hir::ModuleSource;
|
||||||
use ra_db::{SourceDatabase, SourceDatabaseExt};
|
use ra_db::{SourceDatabase, SourceDatabaseExt};
|
||||||
use ra_syntax::{algo::find_node_at_offset, ast, AstNode, SyntaxNode};
|
use ra_syntax::{algo::find_node_at_offset, ast, AstNode, SyntaxNode};
|
||||||
|
use ra_text_edit::TextEdit;
|
||||||
use relative_path::{RelativePath, RelativePathBuf};
|
use relative_path::{RelativePath, RelativePathBuf};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
@ -43,14 +44,7 @@ fn source_edit_from_file_id_range(
|
||||||
range: TextRange,
|
range: TextRange,
|
||||||
new_name: &str,
|
new_name: &str,
|
||||||
) -> SourceFileEdit {
|
) -> SourceFileEdit {
|
||||||
SourceFileEdit {
|
SourceFileEdit { file_id, edit: TextEdit::replace(range, new_name.into()) }
|
||||||
file_id,
|
|
||||||
edit: {
|
|
||||||
let mut builder = ra_text_edit::TextEditBuilder::default();
|
|
||||||
builder.replace(range, new_name.into());
|
|
||||||
builder.finish()
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rename_mod(
|
fn rename_mod(
|
||||||
|
@ -94,11 +88,7 @@ fn rename_mod(
|
||||||
|
|
||||||
let edit = SourceFileEdit {
|
let edit = SourceFileEdit {
|
||||||
file_id: position.file_id,
|
file_id: position.file_id,
|
||||||
edit: {
|
edit: TextEdit::replace(ast_name.syntax().text_range(), new_name.into()),
|
||||||
let mut builder = ra_text_edit::TextEditBuilder::default();
|
|
||||||
builder.replace(ast_name.syntax().text_range(), new_name.into());
|
|
||||||
builder.finish()
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
source_file_edits.push(edit);
|
source_file_edits.push(edit);
|
||||||
|
|
||||||
|
@ -126,12 +116,14 @@ fn rename_reference(
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use insta::assert_debug_snapshot;
|
||||||
|
use ra_text_edit::TextEditBuilder;
|
||||||
|
use test_utils::assert_eq_text;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
mock_analysis::analysis_and_position, mock_analysis::single_file_with_position, FileId,
|
mock_analysis::analysis_and_position, mock_analysis::single_file_with_position, FileId,
|
||||||
ReferenceSearchResult,
|
ReferenceSearchResult,
|
||||||
};
|
};
|
||||||
use insta::assert_debug_snapshot;
|
|
||||||
use test_utils::assert_eq_text;
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_find_all_refs_for_local() {
|
fn test_find_all_refs_for_local() {
|
||||||
|
@ -452,7 +444,7 @@ mod tests {
|
||||||
fn test_rename(text: &str, new_name: &str, expected: &str) {
|
fn test_rename(text: &str, new_name: &str, expected: &str) {
|
||||||
let (analysis, position) = single_file_with_position(text);
|
let (analysis, position) = single_file_with_position(text);
|
||||||
let source_change = analysis.rename(position, new_name).unwrap();
|
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;
|
let mut file_id: Option<FileId> = None;
|
||||||
if let Some(change) = source_change {
|
if let Some(change) = source_change {
|
||||||
for edit in change.info.source_file_edits {
|
for edit in change.info.source_file_edits {
|
||||||
|
|
|
@ -22,7 +22,7 @@ use ra_syntax::{
|
||||||
SyntaxKind::*,
|
SyntaxKind::*,
|
||||||
SyntaxToken, TextRange, TextUnit, TokenAtOffset,
|
SyntaxToken, TextRange, TextUnit, TokenAtOffset,
|
||||||
};
|
};
|
||||||
use ra_text_edit::{TextEdit, TextEditBuilder};
|
use ra_text_edit::TextEdit;
|
||||||
|
|
||||||
use crate::{db::RootDatabase, source_change::SingleFileChange, SourceChange, SourceFileEdit};
|
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 indent = node_indent(&file, comment.syntax())?;
|
||||||
let inserted = format!("\n{}{} ", indent, prefix);
|
let inserted = format!("\n{}{} ", indent, prefix);
|
||||||
let cursor_position = position.offset + TextUnit::of_str(&inserted);
|
let cursor_position = position.offset + TextUnit::of_str(&inserted);
|
||||||
let mut edit = TextEditBuilder::default();
|
let edit = TextEdit::insert(position.offset, inserted);
|
||||||
edit.insert(position.offset, inserted);
|
|
||||||
|
|
||||||
Some(
|
Some(
|
||||||
SourceChange::source_file_edit(
|
SourceChange::source_file_edit(
|
||||||
"on enter",
|
"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 }),
|
.with_cursor(FilePosition { offset: cursor_position, file_id: position.file_id }),
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in a new issue