Cleanup TextEdit

This commit is contained in:
Aleksey Kladov 2020-05-21 15:56:18 +02:00
parent 3cba0dc26b
commit 5f57491c98
5 changed files with 21 additions and 18 deletions

View file

@ -63,8 +63,8 @@ impl fmt::Debug for CompletionItem {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut s = f.debug_struct("CompletionItem"); let mut s = f.debug_struct("CompletionItem");
s.field("label", &self.label()).field("source_range", &self.source_range()); s.field("label", &self.label()).field("source_range", &self.source_range());
if self.text_edit().as_indels().len() == 1 { if self.text_edit().len() == 1 {
let atom = &self.text_edit().as_indels()[0]; let atom = &self.text_edit().iter().next().unwrap();
s.field("delete", &atom.delete); s.field("delete", &atom.delete);
s.field("insert", &atom.insert); s.field("insert", &atom.insert);
} else { } else {

View file

@ -983,8 +983,8 @@ mod tests {
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 {
file_id = Some(edit.file_id); file_id = Some(edit.file_id);
for indel in edit.edit.as_indels() { for indel in edit.edit.into_iter() {
text_edit_builder.replace(indel.delete, indel.insert.clone()); text_edit_builder.replace(indel.delete, indel.insert);
} }
} }
} }

View file

@ -3,9 +3,9 @@
use ra_syntax::{SourceFile, TextSize}; use ra_syntax::{SourceFile, TextSize};
use ra_text_edit::TextEdit; use ra_text_edit::TextEdit;
pub use test_utils::*; pub(crate) use test_utils::*;
pub fn check_action<F: Fn(&SourceFile, TextSize) -> Option<TextEdit>>( pub(crate) fn check_action<F: Fn(&SourceFile, TextSize) -> Option<TextEdit>>(
before: &str, before: &str,
after: &str, after: &str,
f: F, f: F,

View file

@ -3,6 +3,7 @@
//! `rust-analyzer` never mutates text itself and only sends diffs to clients, //! `rust-analyzer` never mutates text itself and only sends diffs to clients,
//! so `TextEdit` is the ultimate representation of the work done by //! so `TextEdit` is the ultimate representation of the work done by
//! rust-analyzer. //! rust-analyzer.
use std::{slice, vec};
pub use text_size::{TextRange, TextSize}; pub use text_size::{TextRange, TextSize};
@ -71,17 +72,24 @@ impl TextEdit {
TextEdit { indels } TextEdit { indels }
} }
pub fn len(&self) -> usize {
self.indels.len()
}
pub fn is_empty(&self) -> bool { pub fn is_empty(&self) -> bool {
self.indels.is_empty() self.indels.is_empty()
} }
// FXME: impl IntoIter instead pub fn iter(&self) -> slice::Iter<'_, Indel> {
pub fn as_indels(&self) -> &[Indel] { self.indels.iter()
&self.indels }
pub fn into_iter(self) -> vec::IntoIter<Indel> {
self.indels.into_iter()
} }
pub fn apply(&self, text: &mut String) { pub fn apply(&self, text: &mut String) {
match self.indels.len() { match self.len() {
0 => return, 0 => return,
1 => { 1 => {
self.indels[0].apply(text); self.indels[0].apply(text);

View file

@ -133,11 +133,7 @@ pub(crate) fn text_edit_vec(
line_endings: LineEndings, line_endings: LineEndings,
text_edit: TextEdit, text_edit: TextEdit,
) -> Vec<lsp_types::TextEdit> { ) -> Vec<lsp_types::TextEdit> {
text_edit text_edit.into_iter().map(|indel| self::text_edit(line_index, line_endings, indel)).collect()
.as_indels()
.iter()
.map(|it| self::text_edit(line_index, line_endings, it.clone()))
.collect()
} }
pub(crate) fn completion_item( pub(crate) fn completion_item(
@ -150,7 +146,7 @@ pub(crate) fn completion_item(
// LSP does not allow arbitrary edits in completion, so we have to do a // LSP does not allow arbitrary edits in completion, so we have to do a
// non-trivial mapping here. // non-trivial mapping here.
let source_range = completion_item.source_range(); let source_range = completion_item.source_range();
for indel in completion_item.text_edit().as_indels() { for indel in completion_item.text_edit().iter() {
if indel.delete.contains_range(source_range) { if indel.delete.contains_range(source_range) {
text_edit = Some(if indel.delete == source_range { text_edit = Some(if indel.delete == source_range {
self::text_edit(line_index, line_endings, indel.clone()) self::text_edit(line_index, line_endings, indel.clone())
@ -459,8 +455,7 @@ pub(crate) fn snippet_text_document_edit(
let line_endings = world.file_line_endings(source_file_edit.file_id); let line_endings = world.file_line_endings(source_file_edit.file_id);
let edits = source_file_edit let edits = source_file_edit
.edit .edit
.as_indels() .into_iter()
.iter()
.map(|it| snippet_text_edit(&line_index, line_endings, is_snippet, it.clone())) .map(|it| snippet_text_edit(&line_index, line_endings, is_snippet, it.clone()))
.collect(); .collect();
Ok(lsp_ext::SnippetTextDocumentEdit { text_document, edits }) Ok(lsp_ext::SnippetTextDocumentEdit { text_document, edits })