mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-03 15:15:24 +00:00
Merge #1012
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:
commit
3604f23e98
7 changed files with 27 additions and 33 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -1008,6 +1008,7 @@ dependencies = [
|
||||||
"proptest 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
"proptest 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"ra_assists 0.1.0",
|
"ra_assists 0.1.0",
|
||||||
"ra_db 0.1.0",
|
"ra_db 0.1.0",
|
||||||
|
"ra_fmt 0.1.0",
|
||||||
"ra_hir 0.1.0",
|
"ra_hir 0.1.0",
|
||||||
"ra_ide_api_light 0.1.0",
|
"ra_ide_api_light 0.1.0",
|
||||||
"ra_syntax 0.1.0",
|
"ra_syntax 0.1.0",
|
||||||
|
|
|
@ -23,6 +23,7 @@ ra_syntax = { path = "../ra_syntax" }
|
||||||
ra_ide_api_light = { path = "../ra_ide_api_light" }
|
ra_ide_api_light = { path = "../ra_ide_api_light" }
|
||||||
ra_text_edit = { path = "../ra_text_edit" }
|
ra_text_edit = { path = "../ra_text_edit" }
|
||||||
ra_db = { path = "../ra_db" }
|
ra_db = { path = "../ra_db" }
|
||||||
|
ra_fmt = { path = "../ra_fmt" }
|
||||||
hir = { path = "../ra_hir", package = "ra_hir" }
|
hir = { path = "../ra_hir", package = "ra_hir" }
|
||||||
test_utils = { path = "../test_utils" }
|
test_utils = { path = "../test_utils" }
|
||||||
ra_assists = { path = "../ra_assists" }
|
ra_assists = { path = "../ra_assists" }
|
||||||
|
|
|
@ -9,22 +9,14 @@ use ra_syntax::{
|
||||||
use ra_fmt::{
|
use ra_fmt::{
|
||||||
compute_ws, extract_trivial_expression
|
compute_ws, extract_trivial_expression
|
||||||
};
|
};
|
||||||
use crate::{
|
use ra_text_edit::{TextEdit, TextEditBuilder};
|
||||||
LocalEdit, 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 range = if range.is_empty() {
|
||||||
let syntax = file.syntax();
|
let syntax = file.syntax();
|
||||||
let text = syntax.text().slice(range.start()..);
|
let text = syntax.text().slice(range.start()..);
|
||||||
let pos = match text.find('\n') {
|
let pos = match text.find('\n') {
|
||||||
None => {
|
None => return TextEditBuilder::default().finish(),
|
||||||
return LocalEdit {
|
|
||||||
label: "join lines".to_string(),
|
|
||||||
edit: TextEditBuilder::default().finish(),
|
|
||||||
cursor_position: None,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
Some(pos) => pos,
|
Some(pos) => pos,
|
||||||
};
|
};
|
||||||
TextRange::offset_len(range.start() + pos, TextUnit::of_char('\n'))
|
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(
|
fn remove_newline(
|
||||||
|
@ -514,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);
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,9 +36,12 @@ mod syntax_tree;
|
||||||
mod line_index;
|
mod line_index;
|
||||||
mod folding_ranges;
|
mod folding_ranges;
|
||||||
mod line_index_utils;
|
mod line_index_utils;
|
||||||
|
mod join_lines;
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod marks;
|
mod marks;
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test_utils;
|
||||||
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
@ -276,10 +279,16 @@ 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);
|
||||||
SourceChange::from_local_edit(
|
let file_edit = SourceFileEdit {
|
||||||
frange.file_id,
|
file_id: frange.file_id,
|
||||||
ra_ide_api_light::join_lines(&file, frange.range),
|
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
|
/// Returns an edit which should be applied when opening a new line, fixing
|
||||||
|
|
|
@ -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);
|
||||||
}
|
}
|
|
@ -4,9 +4,6 @@
|
||||||
//! an edit or some auxiliary info.
|
//! an edit or some auxiliary info.
|
||||||
|
|
||||||
mod structure;
|
mod structure;
|
||||||
#[cfg(test)]
|
|
||||||
mod test_utils;
|
|
||||||
mod join_lines;
|
|
||||||
mod typing;
|
mod typing;
|
||||||
|
|
||||||
use rustc_hash::FxHashSet;
|
use rustc_hash::FxHashSet;
|
||||||
|
@ -20,7 +17,6 @@ use ra_syntax::{
|
||||||
|
|
||||||
pub use crate::{
|
pub use crate::{
|
||||||
structure::{file_structure, StructureNode},
|
structure::{file_structure, StructureNode},
|
||||||
join_lines::join_lines,
|
|
||||||
typing::{on_enter, on_dot_typed, on_eq_typed},
|
typing::{on_enter, on_dot_typed, on_eq_typed},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -118,7 +114,7 @@ mod tests {
|
||||||
use ra_syntax::AstNode;
|
use ra_syntax::AstNode;
|
||||||
use insta::assert_debug_snapshot_matches;
|
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::*;
|
use super::*;
|
||||||
|
|
||||||
|
|
|
@ -118,7 +118,7 @@ pub fn on_dot_typed(file: &SourceFile, dot_offset: TextUnit) -> Option<LocalEdit
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
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::*;
|
use super::*;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue