aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-05-21 15:14:53 +0100
committerGitHub <[email protected]>2020-05-21 15:14:53 +0100
commitba6cf638fbf3d0a025e804f2d354d91abc8afd28 (patch)
treea1d66d2b8e06d2e32f9a4d0ce9857948dd981459
parent3cba0dc26b707bebc1865671fd2c5139c1e1c537 (diff)
parentef0da3bbeccdaab3813a1f6a17c566ca9087615f (diff)
Merge #4553
4553: Cleanup r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
-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 6021f7279..cfb7c1e38 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 {
63 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 63 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
64 let mut s = f.debug_struct("CompletionItem"); 64 let mut s = f.debug_struct("CompletionItem");
65 s.field("label", &self.label()).field("source_range", &self.source_range()); 65 s.field("label", &self.label()).field("source_range", &self.source_range());
66 if self.text_edit().as_indels().len() == 1 { 66 if self.text_edit().len() == 1 {
67 let atom = &self.text_edit().as_indels()[0]; 67 let atom = &self.text_edit().iter().next().unwrap();
68 s.field("delete", &atom.delete); 68 s.field("delete", &atom.delete);
69 s.field("insert", &atom.insert); 69 s.field("insert", &atom.insert);
70 } else { 70 } else {
diff --git a/crates/ra_ide/src/join_lines.rs b/crates/ra_ide/src/join_lines.rs
index d3af780c4..af1ade8a1 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 {
166 166
167#[cfg(test)] 167#[cfg(test)]
168mod tests { 168mod tests {
169 use crate::test_utils::{assert_eq_text, check_action, extract_range}; 169 use ra_syntax::SourceFile;
170 use test_utils::{add_cursor, assert_eq_text, extract_offset, extract_range};
170 171
171 use super::*; 172 use super::*;
172 173
173 fn check_join_lines(before: &str, after: &str) { 174 fn check_join_lines(before: &str, after: &str) {
174 check_action(before, after, |file, offset| { 175 let (before_cursor_pos, before) = extract_offset(before);
175 let range = TextRange::empty(offset); 176 let file = SourceFile::parse(&before).ok().unwrap();
176 let res = join_lines(file, range); 177
177 Some(res) 178 let range = TextRange::empty(before_cursor_pos);
178 }) 179 let result = join_lines(&file, range);
180
181 let actual = {
182 let mut actual = before.to_string();
183 result.apply(&mut actual);
184 actual
185 };
186 let actual_cursor_pos = result
187 .apply_to_offset(before_cursor_pos)
188 .expect("cursor position is affected by the edit");
189 let actual = add_cursor(&actual, actual_cursor_pos);
190 assert_eq_text!(after, &actual);
179 } 191 }
180 192
181 #[test] 193 #[test]
diff --git a/crates/ra_ide/src/lib.rs b/crates/ra_ide/src/lib.rs
index 1d7bacbf6..d0aeb3ba7 100644
--- a/crates/ra_ide/src/lib.rs
+++ b/crates/ra_ide/src/lib.rs
@@ -42,9 +42,6 @@ mod inlay_hints;
42mod expand_macro; 42mod expand_macro;
43mod ssr; 43mod ssr;
44 44
45#[cfg(test)]
46mod test_utils;
47
48use std::sync::Arc; 45use std::sync::Arc;
49 46
50use ra_cfg::CfgOptions; 47use ra_cfg::CfgOptions;
diff --git a/crates/ra_ide/src/references/rename.rs b/crates/ra_ide/src/references/rename.rs
index 55c3319cb..fd2163dad 100644
--- a/crates/ra_ide/src/references/rename.rs
+++ b/crates/ra_ide/src/references/rename.rs
@@ -983,8 +983,8 @@ mod tests {
983 if let Some(change) = source_change { 983 if let Some(change) = source_change {
984 for edit in change.info.source_file_edits { 984 for edit in change.info.source_file_edits {
985 file_id = Some(edit.file_id); 985 file_id = Some(edit.file_id);
986 for indel in edit.edit.as_indels() { 986 for indel in edit.edit.into_iter() {
987 text_edit_builder.replace(indel.delete, indel.insert.clone()); 987 text_edit_builder.replace(indel.delete, indel.insert);
988 } 988 }
989 } 989 }
990 } 990 }
diff --git a/crates/ra_ide/src/test_utils.rs b/crates/ra_ide/src/test_utils.rs
deleted file mode 100644
index 48c8fd1f4..000000000
--- a/crates/ra_ide/src/test_utils.rs
+++ /dev/null
@@ -1,25 +0,0 @@
1//! FIXME: write short doc here
2
3use ra_syntax::{SourceFile, TextSize};
4use ra_text_edit::TextEdit;
5
6pub use test_utils::*;
7
8pub fn check_action<F: Fn(&SourceFile, TextSize) -> Option<TextEdit>>(
9 before: &str,
10 after: &str,
11 f: F,
12) {
13 let (before_cursor_pos, before) = extract_offset(before);
14 let file = SourceFile::parse(&before).ok().unwrap();
15 let result = f(&file, before_cursor_pos).expect("code action is not applicable");
16 let actual = {
17 let mut actual = before.to_string();
18 result.apply(&mut actual);
19 actual
20 };
21 let actual_cursor_pos =
22 result.apply_to_offset(before_cursor_pos).expect("cursor position is affected by the edit");
23 let actual = add_cursor(&actual, actual_cursor_pos);
24 assert_eq_text!(after, &actual);
25}
diff --git a/crates/ra_text_edit/src/lib.rs b/crates/ra_text_edit/src/lib.rs
index c4f945101..199fd1096 100644
--- a/crates/ra_text_edit/src/lib.rs
+++ b/crates/ra_text_edit/src/lib.rs
@@ -3,6 +3,7 @@
3//! `rust-analyzer` never mutates text itself and only sends diffs to clients, 3//! `rust-analyzer` never mutates text itself and only sends diffs to clients,
4//! so `TextEdit` is the ultimate representation of the work done by 4//! so `TextEdit` is the ultimate representation of the work done by
5//! rust-analyzer. 5//! rust-analyzer.
6use std::{slice, vec};
6 7
7pub use text_size::{TextRange, TextSize}; 8pub use text_size::{TextRange, TextSize};
8 9
@@ -71,17 +72,24 @@ impl TextEdit {
71 TextEdit { indels } 72 TextEdit { indels }
72 } 73 }
73 74
75 pub fn len(&self) -> usize {
76 self.indels.len()
77 }
78
74 pub fn is_empty(&self) -> bool { 79 pub fn is_empty(&self) -> bool {
75 self.indels.is_empty() 80 self.indels.is_empty()
76 } 81 }
77 82
78 // FXME: impl IntoIter instead 83 pub fn iter(&self) -> slice::Iter<'_, Indel> {
79 pub fn as_indels(&self) -> &[Indel] { 84 self.indels.iter()
80 &self.indels 85 }
86
87 pub fn into_iter(self) -> vec::IntoIter<Indel> {
88 self.indels.into_iter()
81 } 89 }
82 90
83 pub fn apply(&self, text: &mut String) { 91 pub fn apply(&self, text: &mut String) {
84 match self.indels.len() { 92 match self.len() {
85 0 => return, 93 0 => return,
86 1 => { 94 1 => {
87 self.indels[0].apply(text); 95 self.indels[0].apply(text);
diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs
index 9a8e9e174..617197963 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(
133 line_endings: LineEndings, 133 line_endings: LineEndings,
134 text_edit: TextEdit, 134 text_edit: TextEdit,
135) -> Vec<lsp_types::TextEdit> { 135) -> Vec<lsp_types::TextEdit> {
136 text_edit 136 text_edit.into_iter().map(|indel| self::text_edit(line_index, line_endings, indel)).collect()
137 .as_indels()
138 .iter()
139 .map(|it| self::text_edit(line_index, line_endings, it.clone()))
140 .collect()
141} 137}
142 138
143pub(crate) fn completion_item( 139pub(crate) fn completion_item(
@@ -150,7 +146,7 @@ pub(crate) fn completion_item(
150 // LSP does not allow arbitrary edits in completion, so we have to do a 146 // LSP does not allow arbitrary edits in completion, so we have to do a
151 // non-trivial mapping here. 147 // non-trivial mapping here.
152 let source_range = completion_item.source_range(); 148 let source_range = completion_item.source_range();
153 for indel in completion_item.text_edit().as_indels() { 149 for indel in completion_item.text_edit().iter() {
154 if indel.delete.contains_range(source_range) { 150 if indel.delete.contains_range(source_range) {
155 text_edit = Some(if indel.delete == source_range { 151 text_edit = Some(if indel.delete == source_range {
156 self::text_edit(line_index, line_endings, indel.clone()) 152 self::text_edit(line_index, line_endings, indel.clone())
@@ -459,8 +455,7 @@ pub(crate) fn snippet_text_document_edit(
459 let line_endings = world.file_line_endings(source_file_edit.file_id); 455 let line_endings = world.file_line_endings(source_file_edit.file_id);
460 let edits = source_file_edit 456 let edits = source_file_edit
461 .edit 457 .edit
462 .as_indels() 458 .into_iter()
463 .iter()
464 .map(|it| snippet_text_edit(&line_index, line_endings, is_snippet, it.clone())) 459 .map(|it| snippet_text_edit(&line_index, line_endings, is_snippet, it.clone()))
465 .collect(); 460 .collect();
466 Ok(lsp_ext::SnippetTextDocumentEdit { text_document, edits }) 461 Ok(lsp_ext::SnippetTextDocumentEdit { text_document, edits })