aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src/join_lines.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide/src/join_lines.rs')
-rw-r--r--crates/ra_ide/src/join_lines.rs44
1 files changed, 33 insertions, 11 deletions
diff --git a/crates/ra_ide/src/join_lines.rs b/crates/ra_ide/src/join_lines.rs
index d0def7eaa..5036c1fb0 100644
--- a/crates/ra_ide/src/join_lines.rs
+++ b/crates/ra_ide/src/join_lines.rs
@@ -1,5 +1,3 @@
1//! FIXME: write short doc here
2
3use itertools::Itertools; 1use itertools::Itertools;
4use ra_fmt::{compute_ws, extract_trivial_expression}; 2use ra_fmt::{compute_ws, extract_trivial_expression};
5use ra_syntax::{ 3use ra_syntax::{
@@ -11,6 +9,15 @@ use ra_syntax::{
11}; 9};
12use ra_text_edit::{TextEdit, TextEditBuilder}; 10use ra_text_edit::{TextEdit, TextEditBuilder};
13 11
12// Feature: Join Lines
13//
14// Join selected lines into one, smartly fixing up whitespace, trailing commas, and braces.
15//
16// |===
17// | Editor | Action Name
18//
19// | VS Code | **Rust Analyzer: Join lines**
20// |===
14pub fn join_lines(file: &SourceFile, range: TextRange) -> TextEdit { 21pub fn join_lines(file: &SourceFile, range: TextRange) -> TextEdit {
15 let range = if range.is_empty() { 22 let range = if range.is_empty() {
16 let syntax = file.syntax(); 23 let syntax = file.syntax();
@@ -129,8 +136,7 @@ fn has_comma_after(node: &SyntaxNode) -> bool {
129} 136}
130 137
131fn join_single_expr_block(edit: &mut TextEditBuilder, token: &SyntaxToken) -> Option<()> { 138fn join_single_expr_block(edit: &mut TextEditBuilder, token: &SyntaxToken) -> Option<()> {
132 let block = ast::Block::cast(token.parent())?; 139 let block_expr = ast::BlockExpr::cast(token.parent())?;
133 let block_expr = ast::BlockExpr::cast(block.syntax().parent()?)?;
134 if !block_expr.is_standalone() { 140 if !block_expr.is_standalone() {
135 return None; 141 return None;
136 } 142 }
@@ -167,16 +173,28 @@ fn is_trailing_comma(left: SyntaxKind, right: SyntaxKind) -> bool {
167 173
168#[cfg(test)] 174#[cfg(test)]
169mod tests { 175mod tests {
170 use crate::test_utils::{assert_eq_text, check_action, extract_range}; 176 use ra_syntax::SourceFile;
177 use test_utils::{add_cursor, assert_eq_text, extract_offset, extract_range};
171 178
172 use super::*; 179 use super::*;
173 180
174 fn check_join_lines(before: &str, after: &str) { 181 fn check_join_lines(before: &str, after: &str) {
175 check_action(before, after, |file, offset| { 182 let (before_cursor_pos, before) = extract_offset(before);
176 let range = TextRange::empty(offset); 183 let file = SourceFile::parse(&before).ok().unwrap();
177 let res = join_lines(file, range); 184
178 Some(res) 185 let range = TextRange::empty(before_cursor_pos);
179 }) 186 let result = join_lines(&file, range);
187
188 let actual = {
189 let mut actual = before.to_string();
190 result.apply(&mut actual);
191 actual
192 };
193 let actual_cursor_pos = result
194 .apply_to_offset(before_cursor_pos)
195 .expect("cursor position is affected by the edit");
196 let actual = add_cursor(&actual, actual_cursor_pos);
197 assert_eq_text!(after, &actual);
180 } 198 }
181 199
182 #[test] 200 #[test]
@@ -570,7 +588,11 @@ fn foo() {
570 let (sel, before) = extract_range(before); 588 let (sel, before) = extract_range(before);
571 let parse = SourceFile::parse(&before); 589 let parse = SourceFile::parse(&before);
572 let result = join_lines(&parse.tree(), sel); 590 let result = join_lines(&parse.tree(), sel);
573 let actual = result.apply(&before); 591 let actual = {
592 let mut actual = before.to_string();
593 result.apply(&mut actual);
594 actual
595 };
574 assert_eq_text!(after, &actual); 596 assert_eq_text!(after, &actual);
575 } 597 }
576 598