Skip to content

Commit ba6cf63

Browse files
bors[bot]matklad
andauthored
Merge #4553
4553: Cleanup r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 3cba0dc + ef0da3b commit ba6cf63

File tree

7 files changed

+37
-50
lines changed

7 files changed

+37
-50
lines changed

crates/ra_ide/src/completion/completion_item.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ impl fmt::Debug for CompletionItem {
6363
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6464
let mut s = f.debug_struct("CompletionItem");
6565
s.field("label", &self.label()).field("source_range", &self.source_range());
66-
if self.text_edit().as_indels().len() == 1 {
67-
let atom = &self.text_edit().as_indels()[0];
66+
if self.text_edit().len() == 1 {
67+
let atom = &self.text_edit().iter().next().unwrap();
6868
s.field("delete", &atom.delete);
6969
s.field("insert", &atom.insert);
7070
} else {

crates/ra_ide/src/join_lines.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,16 +166,28 @@ fn is_trailing_comma(left: SyntaxKind, right: SyntaxKind) -> bool {
166166

167167
#[cfg(test)]
168168
mod 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};
170171

171172
use super::*;
172173

173174
fn check_join_lines(before: &str, after: &str) {
174-
check_action(before, after, |file, offset| {
175-
let range = TextRange::empty(offset);
176-
let res = join_lines(file, range);
177-
Some(res)
178-
})
175+
let (before_cursor_pos, before) = extract_offset(before);
176+
let file = SourceFile::parse(&before).ok().unwrap();
177+
178+
let range = TextRange::empty(before_cursor_pos);
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);
179191
}
180192

181193
#[test]

crates/ra_ide/src/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,6 @@ mod inlay_hints;
4242
mod expand_macro;
4343
mod ssr;
4444

45-
#[cfg(test)]
46-
mod test_utils;
47-
4845
use std::sync::Arc;
4946

5047
use ra_cfg::CfgOptions;

crates/ra_ide/src/references/rename.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -983,8 +983,8 @@ mod tests {
983983
if let Some(change) = source_change {
984984
for edit in change.info.source_file_edits {
985985
file_id = Some(edit.file_id);
986-
for indel in edit.edit.as_indels() {
987-
text_edit_builder.replace(indel.delete, indel.insert.clone());
986+
for indel in edit.edit.into_iter() {
987+
text_edit_builder.replace(indel.delete, indel.insert);
988988
}
989989
}
990990
}

crates/ra_ide/src/test_utils.rs

Lines changed: 0 additions & 25 deletions
This file was deleted.

crates/ra_text_edit/src/lib.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//! `rust-analyzer` never mutates text itself and only sends diffs to clients,
44
//! so `TextEdit` is the ultimate representation of the work done by
55
//! rust-analyzer.
6+
use std::{slice, vec};
67

78
pub use text_size::{TextRange, TextSize};
89

@@ -71,17 +72,24 @@ impl TextEdit {
7172
TextEdit { indels }
7273
}
7374

75+
pub fn len(&self) -> usize {
76+
self.indels.len()
77+
}
78+
7479
pub fn is_empty(&self) -> bool {
7580
self.indels.is_empty()
7681
}
7782

78-
// FXME: impl IntoIter instead
79-
pub fn as_indels(&self) -> &[Indel] {
80-
&self.indels
83+
pub fn iter(&self) -> slice::Iter<'_, Indel> {
84+
self.indels.iter()
85+
}
86+
87+
pub fn into_iter(self) -> vec::IntoIter<Indel> {
88+
self.indels.into_iter()
8189
}
8290

8391
pub fn apply(&self, text: &mut String) {
84-
match self.indels.len() {
92+
match self.len() {
8593
0 => return,
8694
1 => {
8795
self.indels[0].apply(text);

crates/rust-analyzer/src/to_proto.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,7 @@ pub(crate) fn text_edit_vec(
133133
line_endings: LineEndings,
134134
text_edit: TextEdit,
135135
) -> Vec<lsp_types::TextEdit> {
136-
text_edit
137-
.as_indels()
138-
.iter()
139-
.map(|it| self::text_edit(line_index, line_endings, it.clone()))
140-
.collect()
136+
text_edit.into_iter().map(|indel| self::text_edit(line_index, line_endings, indel)).collect()
141137
}
142138

143139
pub(crate) fn completion_item(
@@ -150,7 +146,7 @@ pub(crate) fn completion_item(
150146
// LSP does not allow arbitrary edits in completion, so we have to do a
151147
// non-trivial mapping here.
152148
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() {
154150
if indel.delete.contains_range(source_range) {
155151
text_edit = Some(if indel.delete == source_range {
156152
self::text_edit(line_index, line_endings, indel.clone())
@@ -459,8 +455,7 @@ pub(crate) fn snippet_text_document_edit(
459455
let line_endings = world.file_line_endings(source_file_edit.file_id);
460456
let edits = source_file_edit
461457
.edit
462-
.as_indels()
463-
.iter()
458+
.into_iter()
464459
.map(|it| snippet_text_edit(&line_index, line_endings, is_snippet, it.clone()))
465460
.collect();
466461
Ok(lsp_ext::SnippetTextDocumentEdit { text_document, edits })

0 commit comments

Comments
 (0)