Simplify changes and fix tests

This commit is contained in:
Wilco Kusee 2019-03-22 17:10:20 +01:00
parent 9bd8336c51
commit 01bca7114c
3 changed files with 15 additions and 19 deletions

View file

@ -1,5 +1,4 @@
use itertools::Itertools; use itertools::Itertools;
use ra_db::FileRange;
use ra_syntax::{ use ra_syntax::{
SourceFile, TextRange, TextUnit, AstNode, SyntaxNode, SourceFile, TextRange, TextUnit, AstNode, SyntaxNode,
SyntaxKind::{self, WHITESPACE, COMMA, R_CURLY, R_PAREN, R_BRACK}, SyntaxKind::{self, WHITESPACE, COMMA, R_CURLY, R_PAREN, R_BRACK},
@ -12,17 +11,17 @@ use ra_fmt::{
}; };
use ra_text_edit::{TextEdit, TextEditBuilder}; use ra_text_edit::{TextEdit, TextEditBuilder};
pub fn join_lines(file: &SourceFile, frange: FileRange) -> TextEdit { pub fn join_lines(file: &SourceFile, range: TextRange) -> TextEdit {
let range = if frange.range.is_empty() { let range = if range.is_empty() {
let syntax = file.syntax(); let syntax = file.syntax();
let text = syntax.text().slice(frange.range.start()..); let text = syntax.text().slice(range.start()..);
let pos = match text.find('\n') { let pos = match text.find('\n') {
None => return TextEditBuilder::default().finish(), None => return TextEditBuilder::default().finish(),
Some(pos) => pos, Some(pos) => pos,
}; };
TextRange::offset_len(frange.range.start() + pos, TextUnit::of_char('\n')) TextRange::offset_len(range.start() + pos, TextUnit::of_char('\n'))
} else { } else {
frange.range range
}; };
let node = find_covering_node(file.syntax(), range); let node = find_covering_node(file.syntax(), range);
@ -507,7 +506,7 @@ fn foo() {
let (sel, before) = extract_range(before); let (sel, before) = extract_range(before);
let file = SourceFile::parse(&before); let file = SourceFile::parse(&before);
let result = join_lines(&file, sel); let result = join_lines(&file, sel);
let actual = result.edit.apply(&before); let actual = result.apply(&before);
assert_eq_text!(after, &actual); assert_eq_text!(after, &actual);
} }

View file

@ -279,8 +279,10 @@ impl Analysis {
/// stuff like trailing commas. /// stuff like trailing commas.
pub fn join_lines(&self, frange: FileRange) -> SourceChange { pub fn join_lines(&self, frange: FileRange) -> SourceChange {
let file = self.db.parse(frange.file_id); let file = self.db.parse(frange.file_id);
let file_edit = let file_edit = SourceFileEdit {
SourceFileEdit { file_id: frange.file_id, edit: join_lines::join_lines(&file, frange) }; file_id: frange.file_id,
edit: join_lines::join_lines(&file, frange.range),
};
SourceChange { SourceChange {
label: "join lines".to_string(), label: "join lines".to_string(),
source_file_edits: vec![file_edit], source_file_edits: vec![file_edit],

View file

@ -1,9 +1,9 @@
use ra_syntax::{SourceFile, TextUnit}; use ra_syntax::{SourceFile, TextUnit};
use ra_text_edit::TextEdit;
use crate::LocalEdit;
pub use test_utils::*; pub use test_utils::*;
pub fn check_action<F: Fn(&SourceFile, TextUnit) -> Option<LocalEdit>>( pub fn check_action<F: Fn(&SourceFile, TextUnit) -> Option<TextEdit>>(
before: &str, before: &str,
after: &str, after: &str,
f: F, f: F,
@ -11,14 +11,9 @@ pub fn check_action<F: Fn(&SourceFile, TextUnit) -> Option<LocalEdit>>(
let (before_cursor_pos, before) = extract_offset(before); let (before_cursor_pos, before) = extract_offset(before);
let file = SourceFile::parse(&before); let file = SourceFile::parse(&before);
let result = f(&file, before_cursor_pos).expect("code action is not applicable"); let result = f(&file, before_cursor_pos).expect("code action is not applicable");
let actual = result.edit.apply(&before); let actual = result.apply(&before);
let actual_cursor_pos = match result.cursor_position { let actual_cursor_pos =
None => result result.apply_to_offset(before_cursor_pos).expect("cursor position is affected by the edit");
.edit
.apply_to_offset(before_cursor_pos)
.expect("cursor position is affected by the edit"),
Some(off) => off,
};
let actual = add_cursor(&actual, actual_cursor_pos); let actual = add_cursor(&actual, actual_cursor_pos);
assert_eq_text!(after, &actual); assert_eq_text!(after, &actual);
} }