aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_ide_api/src/join_lines.rs13
-rw-r--r--crates/ra_ide_api/src/lib.rs6
-rw-r--r--crates/ra_ide_api/src/test_utils.rs15
3 files changed, 15 insertions, 19 deletions
diff --git a/crates/ra_ide_api/src/join_lines.rs b/crates/ra_ide_api/src/join_lines.rs
index 6e84e4b10..8fb3eaa06 100644
--- a/crates/ra_ide_api/src/join_lines.rs
+++ b/crates/ra_ide_api/src/join_lines.rs
@@ -1,5 +1,4 @@
1use itertools::Itertools; 1use itertools::Itertools;
2use ra_db::FileRange;
3use ra_syntax::{ 2use ra_syntax::{
4 SourceFile, TextRange, TextUnit, AstNode, SyntaxNode, 3 SourceFile, TextRange, TextUnit, AstNode, SyntaxNode,
5 SyntaxKind::{self, WHITESPACE, COMMA, R_CURLY, R_PAREN, R_BRACK}, 4 SyntaxKind::{self, WHITESPACE, COMMA, R_CURLY, R_PAREN, R_BRACK},
@@ -12,17 +11,17 @@ use ra_fmt::{
12}; 11};
13use ra_text_edit::{TextEdit, TextEditBuilder}; 12use ra_text_edit::{TextEdit, TextEditBuilder};
14 13
15pub fn join_lines(file: &SourceFile, frange: FileRange) -> TextEdit { 14pub fn join_lines(file: &SourceFile, range: TextRange) -> TextEdit {
16 let range = if frange.range.is_empty() { 15 let range = if range.is_empty() {
17 let syntax = file.syntax(); 16 let syntax = file.syntax();
18 let text = syntax.text().slice(frange.range.start()..); 17 let text = syntax.text().slice(range.start()..);
19 let pos = match text.find('\n') { 18 let pos = match text.find('\n') {
20 None => return TextEditBuilder::default().finish(), 19 None => return TextEditBuilder::default().finish(),
21 Some(pos) => pos, 20 Some(pos) => pos,
22 }; 21 };
23 TextRange::offset_len(frange.range.start() + pos, TextUnit::of_char('\n')) 22 TextRange::offset_len(range.start() + pos, TextUnit::of_char('\n'))
24 } else { 23 } else {
25 frange.range 24 range
26 }; 25 };
27 26
28 let node = find_covering_node(file.syntax(), range); 27 let node = find_covering_node(file.syntax(), range);
@@ -507,7 +506,7 @@ fn foo() {
507 let (sel, before) = extract_range(before); 506 let (sel, before) = extract_range(before);
508 let file = SourceFile::parse(&before); 507 let file = SourceFile::parse(&before);
509 let result = join_lines(&file, sel); 508 let result = join_lines(&file, sel);
510 let actual = result.edit.apply(&before); 509 let actual = result.apply(&before);
511 assert_eq_text!(after, &actual); 510 assert_eq_text!(after, &actual);
512 } 511 }
513 512
diff --git a/crates/ra_ide_api/src/lib.rs b/crates/ra_ide_api/src/lib.rs
index d4dba1eeb..a838c30da 100644
--- a/crates/ra_ide_api/src/lib.rs
+++ b/crates/ra_ide_api/src/lib.rs
@@ -279,8 +279,10 @@ impl Analysis {
279 /// stuff like trailing commas. 279 /// stuff like trailing commas.
280 pub fn join_lines(&self, frange: FileRange) -> SourceChange { 280 pub fn join_lines(&self, frange: FileRange) -> SourceChange {
281 let file = self.db.parse(frange.file_id); 281 let file = self.db.parse(frange.file_id);
282 let file_edit = 282 let file_edit = SourceFileEdit {
283 SourceFileEdit { file_id: frange.file_id, edit: join_lines::join_lines(&file, frange) }; 283 file_id: frange.file_id,
284 edit: join_lines::join_lines(&file, frange.range),
285 };
284 SourceChange { 286 SourceChange {
285 label: "join lines".to_string(), 287 label: "join lines".to_string(),
286 source_file_edits: vec![file_edit], 288 source_file_edits: vec![file_edit],
diff --git a/crates/ra_ide_api/src/test_utils.rs b/crates/ra_ide_api/src/test_utils.rs
index bfac0fce3..d0bd3a1e4 100644
--- a/crates/ra_ide_api/src/test_utils.rs
+++ b/crates/ra_ide_api/src/test_utils.rs
@@ -1,9 +1,9 @@
1use ra_syntax::{SourceFile, TextUnit}; 1use ra_syntax::{SourceFile, TextUnit};
2use ra_text_edit::TextEdit;
2 3
3use crate::LocalEdit;
4pub use test_utils::*; 4pub use test_utils::*;
5 5
6pub fn check_action<F: Fn(&SourceFile, TextUnit) -> Option<LocalEdit>>( 6pub fn check_action<F: Fn(&SourceFile, TextUnit) -> Option<TextEdit>>(
7 before: &str, 7 before: &str,
8 after: &str, 8 after: &str,
9 f: F, 9 f: F,
@@ -11,14 +11,9 @@ pub fn check_action<F: Fn(&SourceFile, TextUnit) -> Option<LocalEdit>>(
11 let (before_cursor_pos, before) = extract_offset(before); 11 let (before_cursor_pos, before) = extract_offset(before);
12 let file = SourceFile::parse(&before); 12 let file = SourceFile::parse(&before);
13 let result = f(&file, before_cursor_pos).expect("code action is not applicable"); 13 let result = f(&file, before_cursor_pos).expect("code action is not applicable");
14 let actual = result.edit.apply(&before); 14 let actual = result.apply(&before);
15 let actual_cursor_pos = match result.cursor_position { 15 let actual_cursor_pos =
16 None => result 16 result.apply_to_offset(before_cursor_pos).expect("cursor position is affected by the edit");
17 .edit
18 .apply_to_offset(before_cursor_pos)
19 .expect("cursor position is affected by the edit"),
20 Some(off) => off,
21 };
22 let actual = add_cursor(&actual, actual_cursor_pos); 17 let actual = add_cursor(&actual, actual_cursor_pos);
23 assert_eq_text!(after, &actual); 18 assert_eq_text!(after, &actual);
24} 19}