1012: Move join_lines and test_utils to ra_ide_api r=matklad a=detrumi

Part of #1009 

Co-authored-by: Wilco Kusee <wilcokusee@gmail.com>
This commit is contained in:
bors[bot] 2019-03-22 17:56:23 +00:00
commit 3604f23e98
7 changed files with 27 additions and 33 deletions

1
Cargo.lock generated
View File

@ -1008,6 +1008,7 @@ dependencies = [
"proptest 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)",
"ra_assists 0.1.0",
"ra_db 0.1.0",
"ra_fmt 0.1.0",
"ra_hir 0.1.0",
"ra_ide_api_light 0.1.0",
"ra_syntax 0.1.0",

View File

@ -23,6 +23,7 @@ ra_syntax = { path = "../ra_syntax" }
ra_ide_api_light = { path = "../ra_ide_api_light" }
ra_text_edit = { path = "../ra_text_edit" }
ra_db = { path = "../ra_db" }
ra_fmt = { path = "../ra_fmt" }
hir = { path = "../ra_hir", package = "ra_hir" }
test_utils = { path = "../test_utils" }
ra_assists = { path = "../ra_assists" }

View File

@ -9,22 +9,14 @@ use ra_syntax::{
use ra_fmt::{
compute_ws, extract_trivial_expression
};
use crate::{
LocalEdit, TextEditBuilder,
};
use ra_text_edit::{TextEdit, TextEditBuilder};
pub fn join_lines(file: &SourceFile, range: TextRange) -> LocalEdit {
pub fn join_lines(file: &SourceFile, range: TextRange) -> TextEdit {
let range = if range.is_empty() {
let syntax = file.syntax();
let text = syntax.text().slice(range.start()..);
let pos = match text.find('\n') {
None => {
return LocalEdit {
label: "join lines".to_string(),
edit: TextEditBuilder::default().finish(),
cursor_position: None,
};
}
None => return TextEditBuilder::default().finish(),
Some(pos) => pos,
};
TextRange::offset_len(range.start() + pos, TextUnit::of_char('\n'))
@ -52,7 +44,7 @@ pub fn join_lines(file: &SourceFile, range: TextRange) -> LocalEdit {
}
}
LocalEdit { label: "join lines".to_string(), edit: edit.finish(), cursor_position: None }
edit.finish()
}
fn remove_newline(
@ -514,7 +506,7 @@ fn foo() {
let (sel, before) = extract_range(before);
let file = SourceFile::parse(&before);
let result = join_lines(&file, sel);
let actual = result.edit.apply(&before);
let actual = result.apply(&before);
assert_eq_text!(after, &actual);
}

View File

@ -36,9 +36,12 @@ mod syntax_tree;
mod line_index;
mod folding_ranges;
mod line_index_utils;
mod join_lines;
#[cfg(test)]
mod marks;
#[cfg(test)]
mod test_utils;
use std::sync::Arc;
@ -276,10 +279,16 @@ impl Analysis {
/// stuff like trailing commas.
pub fn join_lines(&self, frange: FileRange) -> SourceChange {
let file = self.db.parse(frange.file_id);
SourceChange::from_local_edit(
frange.file_id,
ra_ide_api_light::join_lines(&file, frange.range),
)
let file_edit = SourceFileEdit {
file_id: frange.file_id,
edit: join_lines::join_lines(&file, frange.range),
};
SourceChange {
label: "join lines".to_string(),
source_file_edits: vec![file_edit],
file_system_edits: vec![],
cursor_position: None,
}
}
/// Returns an edit which should be applied when opening a new line, fixing

View File

@ -1,9 +1,9 @@
use ra_syntax::{SourceFile, TextUnit};
use ra_text_edit::TextEdit;
use crate::LocalEdit;
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,
after: &str,
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 file = SourceFile::parse(&before);
let result = f(&file, before_cursor_pos).expect("code action is not applicable");
let actual = result.edit.apply(&before);
let actual_cursor_pos = match result.cursor_position {
None => result
.edit
.apply_to_offset(before_cursor_pos)
.expect("cursor position is affected by the edit"),
Some(off) => off,
};
let actual = result.apply(&before);
let actual_cursor_pos =
result.apply_to_offset(before_cursor_pos).expect("cursor position is affected by the edit");
let actual = add_cursor(&actual, actual_cursor_pos);
assert_eq_text!(after, &actual);
}

View File

@ -4,9 +4,6 @@
//! an edit or some auxiliary info.
mod structure;
#[cfg(test)]
mod test_utils;
mod join_lines;
mod typing;
use rustc_hash::FxHashSet;
@ -20,7 +17,6 @@ use ra_syntax::{
pub use crate::{
structure::{file_structure, StructureNode},
join_lines::join_lines,
typing::{on_enter, on_dot_typed, on_eq_typed},
};
@ -118,7 +114,7 @@ mod tests {
use ra_syntax::AstNode;
use insta::assert_debug_snapshot_matches;
use crate::test_utils::{add_cursor, assert_eq_text, extract_offset};
use test_utils::{add_cursor, assert_eq_text, extract_offset};
use super::*;

View File

@ -118,7 +118,7 @@ pub fn on_dot_typed(file: &SourceFile, dot_offset: TextUnit) -> Option<LocalEdit
#[cfg(test)]
mod tests {
use crate::test_utils::{add_cursor, assert_eq_text, extract_offset};
use test_utils::{add_cursor, assert_eq_text, extract_offset};
use super::*;