Skip to content

Commit 01bca71

Browse files
committed
Simplify changes and fix tests
1 parent 9bd8336 commit 01bca71

File tree

3 files changed

+15
-19
lines changed

3 files changed

+15
-19
lines changed

crates/ra_ide_api/src/join_lines.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use itertools::Itertools;
2-
use ra_db::FileRange;
32
use ra_syntax::{
43
SourceFile, TextRange, TextUnit, AstNode, SyntaxNode,
54
SyntaxKind::{self, WHITESPACE, COMMA, R_CURLY, R_PAREN, R_BRACK},
@@ -12,17 +11,17 @@ use ra_fmt::{
1211
};
1312
use ra_text_edit::{TextEdit, TextEditBuilder};
1413

15-
pub fn join_lines(file: &SourceFile, frange: FileRange) -> TextEdit {
16-
let range = if frange.range.is_empty() {
14+
pub fn join_lines(file: &SourceFile, range: TextRange) -> TextEdit {
15+
let range = if range.is_empty() {
1716
let syntax = file.syntax();
18-
let text = syntax.text().slice(frange.range.start()..);
17+
let text = syntax.text().slice(range.start()..);
1918
let pos = match text.find('\n') {
2019
None => return TextEditBuilder::default().finish(),
2120
Some(pos) => pos,
2221
};
23-
TextRange::offset_len(frange.range.start() + pos, TextUnit::of_char('\n'))
22+
TextRange::offset_len(range.start() + pos, TextUnit::of_char('\n'))
2423
} else {
25-
frange.range
24+
range
2625
};
2726

2827
let node = find_covering_node(file.syntax(), range);
@@ -507,7 +506,7 @@ fn foo() {
507506
let (sel, before) = extract_range(before);
508507
let file = SourceFile::parse(&before);
509508
let result = join_lines(&file, sel);
510-
let actual = result.edit.apply(&before);
509+
let actual = result.apply(&before);
511510
assert_eq_text!(after, &actual);
512511
}
513512

crates/ra_ide_api/src/lib.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,10 @@ impl Analysis {
279279
/// stuff like trailing commas.
280280
pub fn join_lines(&self, frange: FileRange) -> SourceChange {
281281
let file = self.db.parse(frange.file_id);
282-
let file_edit =
283-
SourceFileEdit { file_id: frange.file_id, edit: join_lines::join_lines(&file, frange) };
282+
let file_edit = SourceFileEdit {
283+
file_id: frange.file_id,
284+
edit: join_lines::join_lines(&file, frange.range),
285+
};
284286
SourceChange {
285287
label: "join lines".to_string(),
286288
source_file_edits: vec![file_edit],

crates/ra_ide_api/src/test_utils.rs

+5-10
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
11
use ra_syntax::{SourceFile, TextUnit};
2+
use ra_text_edit::TextEdit;
23

3-
use crate::LocalEdit;
44
pub use test_utils::*;
55

6-
pub fn check_action<F: Fn(&SourceFile, TextUnit) -> Option<LocalEdit>>(
6+
pub fn check_action<F: Fn(&SourceFile, TextUnit) -> Option<TextEdit>>(
77
before: &str,
88
after: &str,
99
f: F,
1010
) {
1111
let (before_cursor_pos, before) = extract_offset(before);
1212
let file = SourceFile::parse(&before);
1313
let result = f(&file, before_cursor_pos).expect("code action is not applicable");
14-
let actual = result.edit.apply(&before);
15-
let actual_cursor_pos = match result.cursor_position {
16-
None => result
17-
.edit
18-
.apply_to_offset(before_cursor_pos)
19-
.expect("cursor position is affected by the edit"),
20-
Some(off) => off,
21-
};
14+
let actual = result.apply(&before);
15+
let actual_cursor_pos =
16+
result.apply_to_offset(before_cursor_pos).expect("cursor position is affected by the edit");
2217
let actual = add_cursor(&actual, actual_cursor_pos);
2318
assert_eq_text!(after, &actual);
2419
}

0 commit comments

Comments
 (0)