about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-05-21 14:14:53 +0000
committerGitHub <noreply@github.com>2020-05-21 14:14:53 +0000
commitba6cf638fbf3d0a025e804f2d354d91abc8afd28 (patch)
treea1d66d2b8e06d2e32f9a4d0ce9857948dd981459
parent3cba0dc26b707bebc1865671fd2c5139c1e1c537 (diff)
parentef0da3bbeccdaab3813a1f6a17c566ca9087615f (diff)
downloadrust-ba6cf638fbf3d0a025e804f2d354d91abc8afd28.tar.gz
rust-ba6cf638fbf3d0a025e804f2d354d91abc8afd28.zip
Merge #4553
4553: Cleanup r=matklad a=matklad



bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
-rw-r--r--crates/ra_ide/src/completion/completion_item.rs4
-rw-r--r--crates/ra_ide/src/join_lines.rs24
-rw-r--r--crates/ra_ide/src/lib.rs3
-rw-r--r--crates/ra_ide/src/references/rename.rs4
-rw-r--r--crates/ra_ide/src/test_utils.rs25
-rw-r--r--crates/ra_text_edit/src/lib.rs16
-rw-r--r--crates/rust-analyzer/src/to_proto.rs11
7 files changed, 37 insertions, 50 deletions
diff --git a/crates/ra_ide/src/completion/completion_item.rs b/crates/ra_ide/src/completion/completion_item.rs
index 6021f7279f6..cfb7c1e380b 100644
--- a/crates/ra_ide/src/completion/completion_item.rs
+++ b/crates/ra_ide/src/completion/completion_item.rs
@@ -63,8 +63,8 @@ impl fmt::Debug for CompletionItem {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         let mut s = f.debug_struct("CompletionItem");
         s.field("label", &self.label()).field("source_range", &self.source_range());
-        if self.text_edit().as_indels().len() == 1 {
-            let atom = &self.text_edit().as_indels()[0];
+        if self.text_edit().len() == 1 {
+            let atom = &self.text_edit().iter().next().unwrap();
             s.field("delete", &atom.delete);
             s.field("insert", &atom.insert);
         } else {
diff --git a/crates/ra_ide/src/join_lines.rs b/crates/ra_ide/src/join_lines.rs
index d3af780c450..af1ade8a1e1 100644
--- a/crates/ra_ide/src/join_lines.rs
+++ b/crates/ra_ide/src/join_lines.rs
@@ -166,16 +166,28 @@ fn is_trailing_comma(left: SyntaxKind, right: SyntaxKind) -> bool {
 
 #[cfg(test)]
 mod tests {
-    use crate::test_utils::{assert_eq_text, check_action, extract_range};
+    use ra_syntax::SourceFile;
+    use test_utils::{add_cursor, assert_eq_text, extract_offset, extract_range};
 
     use super::*;
 
     fn check_join_lines(before: &str, after: &str) {
-        check_action(before, after, |file, offset| {
-            let range = TextRange::empty(offset);
-            let res = join_lines(file, range);
-            Some(res)
-        })
+        let (before_cursor_pos, before) = extract_offset(before);
+        let file = SourceFile::parse(&before).ok().unwrap();
+
+        let range = TextRange::empty(before_cursor_pos);
+        let result = join_lines(&file, range);
+
+        let actual = {
+            let mut actual = before.to_string();
+            result.apply(&mut actual);
+            actual
+        };
+        let actual_cursor_pos = result
+            .apply_to_offset(before_cursor_pos)
+            .expect("cursor position is affected by the edit");
+        let actual = add_cursor(&actual, actual_cursor_pos);
+        assert_eq_text!(after, &actual);
     }
 
     #[test]
diff --git a/crates/ra_ide/src/lib.rs b/crates/ra_ide/src/lib.rs
index 1d7bacbf6d2..d0aeb3ba72d 100644
--- a/crates/ra_ide/src/lib.rs
+++ b/crates/ra_ide/src/lib.rs
@@ -42,9 +42,6 @@ mod inlay_hints;
 mod expand_macro;
 mod ssr;
 
-#[cfg(test)]
-mod test_utils;
-
 use std::sync::Arc;
 
 use ra_cfg::CfgOptions;
diff --git a/crates/ra_ide/src/references/rename.rs b/crates/ra_ide/src/references/rename.rs
index 55c3319cbcb..fd2163dad66 100644
--- a/crates/ra_ide/src/references/rename.rs
+++ b/crates/ra_ide/src/references/rename.rs
@@ -983,8 +983,8 @@ mod tests {
         if let Some(change) = source_change {
             for edit in change.info.source_file_edits {
                 file_id = Some(edit.file_id);
-                for indel in edit.edit.as_indels() {
-                    text_edit_builder.replace(indel.delete, indel.insert.clone());
+                for indel in edit.edit.into_iter() {
+                    text_edit_builder.replace(indel.delete, indel.insert);
                 }
             }
         }
diff --git a/crates/ra_ide/src/test_utils.rs b/crates/ra_ide/src/test_utils.rs
deleted file mode 100644
index 48c8fd1f466..00000000000
--- a/crates/ra_ide/src/test_utils.rs
+++ /dev/null
@@ -1,25 +0,0 @@
-//! FIXME: write short doc here
-
-use ra_syntax::{SourceFile, TextSize};
-use ra_text_edit::TextEdit;
-
-pub use test_utils::*;
-
-pub fn check_action<F: Fn(&SourceFile, TextSize) -> Option<TextEdit>>(
-    before: &str,
-    after: &str,
-    f: F,
-) {
-    let (before_cursor_pos, before) = extract_offset(before);
-    let file = SourceFile::parse(&before).ok().unwrap();
-    let result = f(&file, before_cursor_pos).expect("code action is not applicable");
-    let actual = {
-        let mut actual = before.to_string();
-        result.apply(&mut actual);
-        actual
-    };
-    let actual_cursor_pos =
-        result.apply_to_offset(before_cursor_pos).expect("cursor position is affected by the edit");
-    let actual = add_cursor(&actual, actual_cursor_pos);
-    assert_eq_text!(after, &actual);
-}
diff --git a/crates/ra_text_edit/src/lib.rs b/crates/ra_text_edit/src/lib.rs
index c4f945101e1..199fd109687 100644
--- a/crates/ra_text_edit/src/lib.rs
+++ b/crates/ra_text_edit/src/lib.rs
@@ -3,6 +3,7 @@
 //! `rust-analyzer` never mutates text itself and only sends diffs to clients,
 //! so `TextEdit` is the ultimate representation of the work done by
 //! rust-analyzer.
+use std::{slice, vec};
 
 pub use text_size::{TextRange, TextSize};
 
@@ -71,17 +72,24 @@ impl TextEdit {
         TextEdit { indels }
     }
 
+    pub fn len(&self) -> usize {
+        self.indels.len()
+    }
+
     pub fn is_empty(&self) -> bool {
         self.indels.is_empty()
     }
 
-    // FXME: impl IntoIter instead
-    pub fn as_indels(&self) -> &[Indel] {
-        &self.indels
+    pub fn iter(&self) -> slice::Iter<'_, Indel> {
+        self.indels.iter()
+    }
+
+    pub fn into_iter(self) -> vec::IntoIter<Indel> {
+        self.indels.into_iter()
     }
 
     pub fn apply(&self, text: &mut String) {
-        match self.indels.len() {
+        match self.len() {
             0 => return,
             1 => {
                 self.indels[0].apply(text);
diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs
index 9a8e9e174a9..6171979634b 100644
--- a/crates/rust-analyzer/src/to_proto.rs
+++ b/crates/rust-analyzer/src/to_proto.rs
@@ -133,11 +133,7 @@ pub(crate) fn text_edit_vec(
     line_endings: LineEndings,
     text_edit: TextEdit,
 ) -> Vec<lsp_types::TextEdit> {
-    text_edit
-        .as_indels()
-        .iter()
-        .map(|it| self::text_edit(line_index, line_endings, it.clone()))
-        .collect()
+    text_edit.into_iter().map(|indel| self::text_edit(line_index, line_endings, indel)).collect()
 }
 
 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
     // non-trivial mapping here.
     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) {
             text_edit = Some(if indel.delete == source_range {
                 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 edits = source_file_edit
         .edit
-        .as_indels()
-        .iter()
+        .into_iter()
         .map(|it| snippet_text_edit(&line_index, line_endings, is_snippet, it.clone()))
         .collect();
     Ok(lsp_ext::SnippetTextDocumentEdit { text_document, edits })