diff --git a/Cargo.lock b/Cargo.lock index 9c7afe74b0..eafe8d64b5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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", diff --git a/crates/ra_ide_api/Cargo.toml b/crates/ra_ide_api/Cargo.toml index ac8c8057bc..8bd5eec2b3 100644 --- a/crates/ra_ide_api/Cargo.toml +++ b/crates/ra_ide_api/Cargo.toml @@ -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" } diff --git a/crates/ra_ide_api_light/src/join_lines.rs b/crates/ra_ide_api/src/join_lines.rs similarity index 95% rename from crates/ra_ide_api_light/src/join_lines.rs rename to crates/ra_ide_api/src/join_lines.rs index b5bcd62fb2..8fb3eaa064 100644 --- a/crates/ra_ide_api_light/src/join_lines.rs +++ b/crates/ra_ide_api/src/join_lines.rs @@ -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); } diff --git a/crates/ra_ide_api/src/lib.rs b/crates/ra_ide_api/src/lib.rs index d6f63490dc..a838c30da7 100644 --- a/crates/ra_ide_api/src/lib.rs +++ b/crates/ra_ide_api/src/lib.rs @@ -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 diff --git a/crates/ra_ide_api_light/src/test_utils.rs b/crates/ra_ide_api/src/test_utils.rs similarity index 51% rename from crates/ra_ide_api_light/src/test_utils.rs rename to crates/ra_ide_api/src/test_utils.rs index bfac0fce36..d0bd3a1e46 100644 --- a/crates/ra_ide_api_light/src/test_utils.rs +++ b/crates/ra_ide_api/src/test_utils.rs @@ -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 Option>( +pub fn check_action Option>( before: &str, after: &str, f: F, @@ -11,14 +11,9 @@ pub fn check_action Option>( 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); } diff --git a/crates/ra_ide_api_light/src/lib.rs b/crates/ra_ide_api_light/src/lib.rs index 4036a598e5..f21a91e185 100644 --- a/crates/ra_ide_api_light/src/lib.rs +++ b/crates/ra_ide_api_light/src/lib.rs @@ -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::*; diff --git a/crates/ra_ide_api_light/src/typing.rs b/crates/ra_ide_api_light/src/typing.rs index 9dd9f1c1d9..c69270333c 100644 --- a/crates/ra_ide_api_light/src/typing.rs +++ b/crates/ra_ide_api_light/src/typing.rs @@ -118,7 +118,7 @@ pub fn on_dot_typed(file: &SourceFile, dot_offset: TextUnit) -> Option