aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_ide_api_light/src/join_lines.rs493
-rw-r--r--crates/ra_ide_api_light/src/lib.rs7
-rw-r--r--crates/ra_ide_api_light/src/typing.rs490
3 files changed, 503 insertions, 487 deletions
diff --git a/crates/ra_ide_api_light/src/join_lines.rs b/crates/ra_ide_api_light/src/join_lines.rs
new file mode 100644
index 000000000..bda6be878
--- /dev/null
+++ b/crates/ra_ide_api_light/src/join_lines.rs
@@ -0,0 +1,493 @@
1use std::mem;
2
3use itertools::Itertools;
4use ra_syntax::{
5 SourceFile, TextRange, TextUnit, AstNode, SyntaxNode,
6 SyntaxKind::{self, WHITESPACE, COMMA, L_CURLY, R_CURLY, L_PAREN, R_PAREN, L_BRACK, R_BRACK, USE_TREE, DOT},
7 algo::find_covering_node,
8 ast,
9};
10
11use crate::{LocalEdit, TextEditBuilder};
12
13pub fn join_lines(file: &SourceFile, range: TextRange) -> LocalEdit {
14 let range = if range.is_empty() {
15 let syntax = file.syntax();
16 let text = syntax.text().slice(range.start()..);
17 let pos = match text.find('\n') {
18 None => {
19 return LocalEdit {
20 label: "join lines".to_string(),
21 edit: TextEditBuilder::default().finish(),
22 cursor_position: None,
23 };
24 }
25 Some(pos) => pos,
26 };
27 TextRange::offset_len(range.start() + pos, TextUnit::of_char('\n'))
28 } else {
29 range
30 };
31
32 let node = find_covering_node(file.syntax(), range);
33 let mut edit = TextEditBuilder::default();
34 for node in node.descendants() {
35 let text = match node.leaf_text() {
36 Some(text) => text,
37 None => continue,
38 };
39 let range = match range.intersection(&node.range()) {
40 Some(range) => range,
41 None => continue,
42 } - node.range().start();
43 for (pos, _) in text[range].bytes().enumerate().filter(|&(_, b)| b == b'\n') {
44 let pos: TextUnit = (pos as u32).into();
45 let off = node.range().start() + range.start() + pos;
46 if !edit.invalidates_offset(off) {
47 remove_newline(&mut edit, node, text.as_str(), off);
48 }
49 }
50 }
51
52 LocalEdit {
53 label: "join lines".to_string(),
54 edit: edit.finish(),
55 cursor_position: None,
56 }
57}
58
59fn remove_newline(
60 edit: &mut TextEditBuilder,
61 node: &SyntaxNode,
62 node_text: &str,
63 offset: TextUnit,
64) {
65 if node.kind() != WHITESPACE || node_text.bytes().filter(|&b| b == b'\n').count() != 1 {
66 // The node is either the first or the last in the file
67 let suff = &node_text[TextRange::from_to(
68 offset - node.range().start() + TextUnit::of_char('\n'),
69 TextUnit::of_str(node_text),
70 )];
71 let spaces = suff.bytes().take_while(|&b| b == b' ').count();
72
73 edit.replace(
74 TextRange::offset_len(offset, ((spaces + 1) as u32).into()),
75 " ".to_string(),
76 );
77 return;
78 }
79
80 // Special case that turns something like:
81 //
82 // ```
83 // my_function({<|>
84 // <some-expr>
85 // })
86 // ```
87 //
88 // into `my_function(<some-expr>)`
89 if join_single_expr_block(edit, node).is_some() {
90 return;
91 }
92 // ditto for
93 //
94 // ```
95 // use foo::{<|>
96 // bar
97 // };
98 // ```
99 if join_single_use_tree(edit, node).is_some() {
100 return;
101 }
102
103 // The node is between two other nodes
104 let prev = node.prev_sibling().unwrap();
105 let next = node.next_sibling().unwrap();
106 if is_trailing_comma(prev.kind(), next.kind()) {
107 // Removes: trailing comma, newline (incl. surrounding whitespace)
108 edit.delete(TextRange::from_to(prev.range().start(), node.range().end()));
109 } else if prev.kind() == COMMA && next.kind() == R_CURLY {
110 // Removes: comma, newline (incl. surrounding whitespace)
111 let space = if let Some(left) = prev.prev_sibling() {
112 compute_ws(left, next)
113 } else {
114 " "
115 };
116 edit.replace(
117 TextRange::from_to(prev.range().start(), node.range().end()),
118 space.to_string(),
119 );
120 } else if let (Some(_), Some(next)) = (ast::Comment::cast(prev), ast::Comment::cast(next)) {
121 // Removes: newline (incl. surrounding whitespace), start of the next comment
122 edit.delete(TextRange::from_to(
123 node.range().start(),
124 next.syntax().range().start() + TextUnit::of_str(next.prefix()),
125 ));
126 } else {
127 // Remove newline but add a computed amount of whitespace characters
128 edit.replace(node.range(), compute_ws(prev, next).to_string());
129 }
130}
131
132fn join_single_expr_block(edit: &mut TextEditBuilder, node: &SyntaxNode) -> Option<()> {
133 let block = ast::Block::cast(node.parent()?)?;
134 let block_expr = ast::BlockExpr::cast(block.syntax().parent()?)?;
135 let expr = single_expr(block)?;
136 edit.replace(
137 block_expr.syntax().range(),
138 expr.syntax().text().to_string(),
139 );
140 Some(())
141}
142
143fn single_expr(block: &ast::Block) -> Option<&ast::Expr> {
144 let mut res = None;
145 for child in block.syntax().children() {
146 if let Some(expr) = ast::Expr::cast(child) {
147 if expr.syntax().text().contains('\n') {
148 return None;
149 }
150 if mem::replace(&mut res, Some(expr)).is_some() {
151 return None;
152 }
153 } else {
154 match child.kind() {
155 WHITESPACE | L_CURLY | R_CURLY => (),
156 _ => return None,
157 }
158 }
159 }
160 res
161}
162
163fn join_single_use_tree(edit: &mut TextEditBuilder, node: &SyntaxNode) -> Option<()> {
164 let use_tree_list = ast::UseTreeList::cast(node.parent()?)?;
165 let (tree,) = use_tree_list.use_trees().collect_tuple()?;
166 edit.replace(
167 use_tree_list.syntax().range(),
168 tree.syntax().text().to_string(),
169 );
170 Some(())
171}
172
173fn is_trailing_comma(left: SyntaxKind, right: SyntaxKind) -> bool {
174 match (left, right) {
175 (COMMA, R_PAREN) | (COMMA, R_BRACK) => true,
176 _ => false,
177 }
178}
179
180fn compute_ws(left: &SyntaxNode, right: &SyntaxNode) -> &'static str {
181 match left.kind() {
182 L_PAREN | L_BRACK => return "",
183 L_CURLY => {
184 if let USE_TREE = right.kind() {
185 return "";
186 }
187 }
188 _ => (),
189 }
190 match right.kind() {
191 R_PAREN | R_BRACK => return "",
192 R_CURLY => {
193 if let USE_TREE = left.kind() {
194 return "";
195 }
196 }
197 DOT => return "",
198 _ => (),
199 }
200 " "
201}
202
203#[cfg(test)]
204mod tests {
205 use crate::test_utils::{assert_eq_text, check_action, extract_range};
206
207 use super::*;
208
209 fn check_join_lines(before: &str, after: &str) {
210 check_action(before, after, |file, offset| {
211 let range = TextRange::offset_len(offset, 0.into());
212 let res = join_lines(file, range);
213 Some(res)
214 })
215 }
216
217 #[test]
218 fn test_join_lines_comma() {
219 check_join_lines(
220 r"
221fn foo() {
222 <|>foo(1,
223 )
224}
225",
226 r"
227fn foo() {
228 <|>foo(1)
229}
230",
231 );
232 }
233
234 #[test]
235 fn test_join_lines_lambda_block() {
236 check_join_lines(
237 r"
238pub fn reparse(&self, edit: &AtomTextEdit) -> File {
239 <|>self.incremental_reparse(edit).unwrap_or_else(|| {
240 self.full_reparse(edit)
241 })
242}
243",
244 r"
245pub fn reparse(&self, edit: &AtomTextEdit) -> File {
246 <|>self.incremental_reparse(edit).unwrap_or_else(|| self.full_reparse(edit))
247}
248",
249 );
250 }
251
252 #[test]
253 fn test_join_lines_block() {
254 check_join_lines(
255 r"
256fn foo() {
257 foo(<|>{
258 92
259 })
260}",
261 r"
262fn foo() {
263 foo(<|>92)
264}",
265 );
266 }
267
268 #[test]
269 fn test_join_lines_use_items_left() {
270 // No space after the '{'
271 check_join_lines(
272 r"
273<|>use ra_syntax::{
274 TextUnit, TextRange,
275};",
276 r"
277<|>use ra_syntax::{TextUnit, TextRange,
278};",
279 );
280 }
281
282 #[test]
283 fn test_join_lines_use_items_right() {
284 // No space after the '}'
285 check_join_lines(
286 r"
287use ra_syntax::{
288<|> TextUnit, TextRange
289};",
290 r"
291use ra_syntax::{
292<|> TextUnit, TextRange};",
293 );
294 }
295
296 #[test]
297 fn test_join_lines_use_items_right_comma() {
298 // No space after the '}'
299 check_join_lines(
300 r"
301use ra_syntax::{
302<|> TextUnit, TextRange,
303};",
304 r"
305use ra_syntax::{
306<|> TextUnit, TextRange};",
307 );
308 }
309
310 #[test]
311 fn test_join_lines_use_tree() {
312 check_join_lines(
313 r"
314use ra_syntax::{
315 algo::<|>{
316 find_leaf_at_offset,
317 },
318 ast,
319};",
320 r"
321use ra_syntax::{
322 algo::<|>find_leaf_at_offset,
323 ast,
324};",
325 );
326 }
327
328 #[test]
329 fn test_join_lines_normal_comments() {
330 check_join_lines(
331 r"
332fn foo() {
333 // Hello<|>
334 // world!
335}
336",
337 r"
338fn foo() {
339 // Hello<|> world!
340}
341",
342 );
343 }
344
345 #[test]
346 fn test_join_lines_doc_comments() {
347 check_join_lines(
348 r"
349fn foo() {
350 /// Hello<|>
351 /// world!
352}
353",
354 r"
355fn foo() {
356 /// Hello<|> world!
357}
358",
359 );
360 }
361
362 #[test]
363 fn test_join_lines_mod_comments() {
364 check_join_lines(
365 r"
366fn foo() {
367 //! Hello<|>
368 //! world!
369}
370",
371 r"
372fn foo() {
373 //! Hello<|> world!
374}
375",
376 );
377 }
378
379 #[test]
380 fn test_join_lines_multiline_comments_1() {
381 check_join_lines(
382 r"
383fn foo() {
384 // Hello<|>
385 /* world! */
386}
387",
388 r"
389fn foo() {
390 // Hello<|> world! */
391}
392",
393 );
394 }
395
396 #[test]
397 fn test_join_lines_multiline_comments_2() {
398 check_join_lines(
399 r"
400fn foo() {
401 // The<|>
402 /* quick
403 brown
404 fox! */
405}
406",
407 r"
408fn foo() {
409 // The<|> quick
410 brown
411 fox! */
412}
413",
414 );
415 }
416
417 fn check_join_lines_sel(before: &str, after: &str) {
418 let (sel, before) = extract_range(before);
419 let file = SourceFile::parse(&before);
420 let result = join_lines(&file, sel);
421 let actual = result.edit.apply(&before);
422 assert_eq_text!(after, &actual);
423 }
424
425 #[test]
426 fn test_join_lines_selection_fn_args() {
427 check_join_lines_sel(
428 r"
429fn foo() {
430 <|>foo(1,
431 2,
432 3,
433 <|>)
434}
435 ",
436 r"
437fn foo() {
438 foo(1, 2, 3)
439}
440 ",
441 );
442 }
443
444 #[test]
445 fn test_join_lines_selection_struct() {
446 check_join_lines_sel(
447 r"
448struct Foo <|>{
449 f: u32,
450}<|>
451 ",
452 r"
453struct Foo { f: u32 }
454 ",
455 );
456 }
457
458 #[test]
459 fn test_join_lines_selection_dot_chain() {
460 check_join_lines_sel(
461 r"
462fn foo() {
463 join(<|>type_params.type_params()
464 .filter_map(|it| it.name())
465 .map(|it| it.text())<|>)
466}",
467 r"
468fn foo() {
469 join(type_params.type_params().filter_map(|it| it.name()).map(|it| it.text()))
470}",
471 );
472 }
473
474 #[test]
475 fn test_join_lines_selection_lambda_block_body() {
476 check_join_lines_sel(
477 r"
478pub fn handle_find_matching_brace() {
479 params.offsets
480 .map(|offset| <|>{
481 world.analysis().matching_brace(&file, offset).unwrap_or(offset)
482 }<|>)
483 .collect();
484}",
485 r"
486pub fn handle_find_matching_brace() {
487 params.offsets
488 .map(|offset| world.analysis().matching_brace(&file, offset).unwrap_or(offset))
489 .collect();
490}",
491 );
492 }
493}
diff --git a/crates/ra_ide_api_light/src/lib.rs b/crates/ra_ide_api_light/src/lib.rs
index 40638eda8..e632108ce 100644
--- a/crates/ra_ide_api_light/src/lib.rs
+++ b/crates/ra_ide_api_light/src/lib.rs
@@ -11,6 +11,7 @@ mod line_index_utils;
11mod structure; 11mod structure;
12#[cfg(test)] 12#[cfg(test)]
13mod test_utils; 13mod test_utils;
14mod join_lines;
14mod typing; 15mod typing;
15mod diagnostics; 16mod diagnostics;
16 17
@@ -21,8 +22,10 @@ pub use self::{
21 line_index::{LineCol, LineIndex}, 22 line_index::{LineCol, LineIndex},
22 line_index_utils::translate_offset_with_edit, 23 line_index_utils::translate_offset_with_edit,
23 structure::{file_structure, StructureNode}, 24 structure::{file_structure, StructureNode},
24 typing::{join_lines, on_enter, on_dot_typed, on_eq_typed}, 25 diagnostics::diagnostics,
25 diagnostics::diagnostics 26 join_lines::join_lines,
27 typing::{on_enter, on_dot_typed, on_eq_typed},
28
26}; 29};
27use ra_text_edit::TextEditBuilder; 30use ra_text_edit::TextEditBuilder;
28use ra_syntax::{ 31use ra_syntax::{
diff --git a/crates/ra_ide_api_light/src/typing.rs b/crates/ra_ide_api_light/src/typing.rs
index d8177f245..5726209cc 100644
--- a/crates/ra_ide_api_light/src/typing.rs
+++ b/crates/ra_ide_api_light/src/typing.rs
@@ -1,62 +1,12 @@
1use std::mem;
2
3use itertools::Itertools;
4use ra_syntax::{ 1use ra_syntax::{
5 algo::{find_node_at_offset, find_covering_node, find_leaf_at_offset, LeafAtOffset}, 2 algo::{find_node_at_offset, find_leaf_at_offset, LeafAtOffset},
6 ast, 3 ast,
7 AstNode, Direction, SourceFile, SyntaxKind, 4 AstNode, Direction, SourceFile, SyntaxKind::*,
8 SyntaxKind::*, 5 SyntaxNode, TextUnit,
9 SyntaxNode, TextRange, TextUnit,
10}; 6};
11 7
12use crate::{LocalEdit, TextEditBuilder}; 8use crate::{LocalEdit, TextEditBuilder};
13 9
14pub fn join_lines(file: &SourceFile, range: TextRange) -> LocalEdit {
15 let range = if range.is_empty() {
16 let syntax = file.syntax();
17 let text = syntax.text().slice(range.start()..);
18 let pos = match text.find('\n') {
19 None => {
20 return LocalEdit {
21 label: "join lines".to_string(),
22 edit: TextEditBuilder::default().finish(),
23 cursor_position: None,
24 };
25 }
26 Some(pos) => pos,
27 };
28 TextRange::offset_len(range.start() + pos, TextUnit::of_char('\n'))
29 } else {
30 range
31 };
32
33 let node = find_covering_node(file.syntax(), range);
34 let mut edit = TextEditBuilder::default();
35 for node in node.descendants() {
36 let text = match node.leaf_text() {
37 Some(text) => text,
38 None => continue,
39 };
40 let range = match range.intersection(&node.range()) {
41 Some(range) => range,
42 None => continue,
43 } - node.range().start();
44 for (pos, _) in text[range].bytes().enumerate().filter(|&(_, b)| b == b'\n') {
45 let pos: TextUnit = (pos as u32).into();
46 let off = node.range().start() + range.start() + pos;
47 if !edit.invalidates_offset(off) {
48 remove_newline(&mut edit, node, text.as_str(), off);
49 }
50 }
51 }
52
53 LocalEdit {
54 label: "join lines".to_string(),
55 edit: edit.finish(),
56 cursor_position: None,
57 }
58}
59
60pub fn on_enter(file: &SourceFile, offset: TextUnit) -> Option<LocalEdit> { 10pub fn on_enter(file: &SourceFile, offset: TextUnit) -> Option<LocalEdit> {
61 let comment = find_leaf_at_offset(file.syntax(), offset) 11 let comment = find_leaf_at_offset(file.syntax(), offset)
62 .left_biased() 12 .left_biased()
@@ -184,441 +134,11 @@ fn last_line_indent_in_whitespace(ws: &str) -> &str {
184 ws.split('\n').last().unwrap_or("") 134 ws.split('\n').last().unwrap_or("")
185} 135}
186 136
187fn remove_newline(
188 edit: &mut TextEditBuilder,
189 node: &SyntaxNode,
190 node_text: &str,
191 offset: TextUnit,
192) {
193 if node.kind() != WHITESPACE || node_text.bytes().filter(|&b| b == b'\n').count() != 1 {
194 // The node is either the first or the last in the file
195 let suff = &node_text[TextRange::from_to(
196 offset - node.range().start() + TextUnit::of_char('\n'),
197 TextUnit::of_str(node_text),
198 )];
199 let spaces = suff.bytes().take_while(|&b| b == b' ').count();
200
201 edit.replace(
202 TextRange::offset_len(offset, ((spaces + 1) as u32).into()),
203 " ".to_string(),
204 );
205 return;
206 }
207
208 // Special case that turns something like:
209 //
210 // ```
211 // my_function({<|>
212 // <some-expr>
213 // })
214 // ```
215 //
216 // into `my_function(<some-expr>)`
217 if join_single_expr_block(edit, node).is_some() {
218 return;
219 }
220 // ditto for
221 //
222 // ```
223 // use foo::{<|>
224 // bar
225 // };
226 // ```
227 if join_single_use_tree(edit, node).is_some() {
228 return;
229 }
230
231 // The node is between two other nodes
232 let prev = node.prev_sibling().unwrap();
233 let next = node.next_sibling().unwrap();
234 if is_trailing_comma(prev.kind(), next.kind()) {
235 // Removes: trailing comma, newline (incl. surrounding whitespace)
236 edit.delete(TextRange::from_to(prev.range().start(), node.range().end()));
237 } else if prev.kind() == COMMA && next.kind() == R_CURLY {
238 // Removes: comma, newline (incl. surrounding whitespace)
239 let space = if let Some(left) = prev.prev_sibling() {
240 compute_ws(left, next)
241 } else {
242 " "
243 };
244 edit.replace(
245 TextRange::from_to(prev.range().start(), node.range().end()),
246 space.to_string(),
247 );
248 } else if let (Some(_), Some(next)) = (ast::Comment::cast(prev), ast::Comment::cast(next)) {
249 // Removes: newline (incl. surrounding whitespace), start of the next comment
250 edit.delete(TextRange::from_to(
251 node.range().start(),
252 next.syntax().range().start() + TextUnit::of_str(next.prefix()),
253 ));
254 } else {
255 // Remove newline but add a computed amount of whitespace characters
256 edit.replace(node.range(), compute_ws(prev, next).to_string());
257 }
258}
259
260fn is_trailing_comma(left: SyntaxKind, right: SyntaxKind) -> bool {
261 match (left, right) {
262 (COMMA, R_PAREN) | (COMMA, R_BRACK) => true,
263 _ => false,
264 }
265}
266
267fn join_single_expr_block(edit: &mut TextEditBuilder, node: &SyntaxNode) -> Option<()> {
268 let block = ast::Block::cast(node.parent()?)?;
269 let block_expr = ast::BlockExpr::cast(block.syntax().parent()?)?;
270 let expr = single_expr(block)?;
271 edit.replace(
272 block_expr.syntax().range(),
273 expr.syntax().text().to_string(),
274 );
275 Some(())
276}
277
278fn single_expr(block: &ast::Block) -> Option<&ast::Expr> {
279 let mut res = None;
280 for child in block.syntax().children() {
281 if let Some(expr) = ast::Expr::cast(child) {
282 if expr.syntax().text().contains('\n') {
283 return None;
284 }
285 if mem::replace(&mut res, Some(expr)).is_some() {
286 return None;
287 }
288 } else {
289 match child.kind() {
290 WHITESPACE | L_CURLY | R_CURLY => (),
291 _ => return None,
292 }
293 }
294 }
295 res
296}
297
298fn join_single_use_tree(edit: &mut TextEditBuilder, node: &SyntaxNode) -> Option<()> {
299 let use_tree_list = ast::UseTreeList::cast(node.parent()?)?;
300 let (tree,) = use_tree_list.use_trees().collect_tuple()?;
301 edit.replace(
302 use_tree_list.syntax().range(),
303 tree.syntax().text().to_string(),
304 );
305 Some(())
306}
307
308fn compute_ws(left: &SyntaxNode, right: &SyntaxNode) -> &'static str {
309 match left.kind() {
310 L_PAREN | L_BRACK => return "",
311 L_CURLY => {
312 if let USE_TREE = right.kind() {
313 return "";
314 }
315 }
316 _ => (),
317 }
318 match right.kind() {
319 R_PAREN | R_BRACK => return "",
320 R_CURLY => {
321 if let USE_TREE = left.kind() {
322 return "";
323 }
324 }
325 DOT => return "",
326 _ => (),
327 }
328 " "
329}
330
331#[cfg(test)] 137#[cfg(test)]
332mod tests { 138mod tests {
333 use super::*; 139 use crate::test_utils::{add_cursor, assert_eq_text, extract_offset};
334 use crate::test_utils::{
335 add_cursor, assert_eq_text, check_action, extract_offset, extract_range,
336};
337
338 fn check_join_lines(before: &str, after: &str) {
339 check_action(before, after, |file, offset| {
340 let range = TextRange::offset_len(offset, 0.into());
341 let res = join_lines(file, range);
342 Some(res)
343 })
344 }
345 140
346 #[test] 141 use super::*;
347 fn test_join_lines_comma() {
348 check_join_lines(
349 r"
350fn foo() {
351 <|>foo(1,
352 )
353}
354",
355 r"
356fn foo() {
357 <|>foo(1)
358}
359",
360 );
361 }
362
363 #[test]
364 fn test_join_lines_lambda_block() {
365 check_join_lines(
366 r"
367pub fn reparse(&self, edit: &AtomTextEdit) -> File {
368 <|>self.incremental_reparse(edit).unwrap_or_else(|| {
369 self.full_reparse(edit)
370 })
371}
372",
373 r"
374pub fn reparse(&self, edit: &AtomTextEdit) -> File {
375 <|>self.incremental_reparse(edit).unwrap_or_else(|| self.full_reparse(edit))
376}
377",
378 );
379 }
380
381 #[test]
382 fn test_join_lines_block() {
383 check_join_lines(
384 r"
385fn foo() {
386 foo(<|>{
387 92
388 })
389}",
390 r"
391fn foo() {
392 foo(<|>92)
393}",
394 );
395 }
396
397 #[test]
398 fn test_join_lines_use_items_left() {
399 // No space after the '{'
400 check_join_lines(
401 r"
402<|>use ra_syntax::{
403 TextUnit, TextRange,
404};",
405 r"
406<|>use ra_syntax::{TextUnit, TextRange,
407};",
408 );
409 }
410
411 #[test]
412 fn test_join_lines_use_items_right() {
413 // No space after the '}'
414 check_join_lines(
415 r"
416use ra_syntax::{
417<|> TextUnit, TextRange
418};",
419 r"
420use ra_syntax::{
421<|> TextUnit, TextRange};",
422 );
423 }
424
425 #[test]
426 fn test_join_lines_use_items_right_comma() {
427 // No space after the '}'
428 check_join_lines(
429 r"
430use ra_syntax::{
431<|> TextUnit, TextRange,
432};",
433 r"
434use ra_syntax::{
435<|> TextUnit, TextRange};",
436 );
437 }
438
439 #[test]
440 fn test_join_lines_use_tree() {
441 check_join_lines(
442 r"
443use ra_syntax::{
444 algo::<|>{
445 find_leaf_at_offset,
446 },
447 ast,
448};",
449 r"
450use ra_syntax::{
451 algo::<|>find_leaf_at_offset,
452 ast,
453};",
454 );
455 }
456
457 #[test]
458 fn test_join_lines_normal_comments() {
459 check_join_lines(
460 r"
461fn foo() {
462 // Hello<|>
463 // world!
464}
465",
466 r"
467fn foo() {
468 // Hello<|> world!
469}
470",
471 );
472 }
473
474 #[test]
475 fn test_join_lines_doc_comments() {
476 check_join_lines(
477 r"
478fn foo() {
479 /// Hello<|>
480 /// world!
481}
482",
483 r"
484fn foo() {
485 /// Hello<|> world!
486}
487",
488 );
489 }
490
491 #[test]
492 fn test_join_lines_mod_comments() {
493 check_join_lines(
494 r"
495fn foo() {
496 //! Hello<|>
497 //! world!
498}
499",
500 r"
501fn foo() {
502 //! Hello<|> world!
503}
504",
505 );
506 }
507
508 #[test]
509 fn test_join_lines_multiline_comments_1() {
510 check_join_lines(
511 r"
512fn foo() {
513 // Hello<|>
514 /* world! */
515}
516",
517 r"
518fn foo() {
519 // Hello<|> world! */
520}
521",
522 );
523 }
524
525 #[test]
526 fn test_join_lines_multiline_comments_2() {
527 check_join_lines(
528 r"
529fn foo() {
530 // The<|>
531 /* quick
532 brown
533 fox! */
534}
535",
536 r"
537fn foo() {
538 // The<|> quick
539 brown
540 fox! */
541}
542",
543 );
544 }
545
546 fn check_join_lines_sel(before: &str, after: &str) {
547 let (sel, before) = extract_range(before);
548 let file = SourceFile::parse(&before);
549 let result = join_lines(&file, sel);
550 let actual = result.edit.apply(&before);
551 assert_eq_text!(after, &actual);
552 }
553
554 #[test]
555 fn test_join_lines_selection_fn_args() {
556 check_join_lines_sel(
557 r"
558fn foo() {
559 <|>foo(1,
560 2,
561 3,
562 <|>)
563}
564 ",
565 r"
566fn foo() {
567 foo(1, 2, 3)
568}
569 ",
570 );
571 }
572
573 #[test]
574 fn test_join_lines_selection_struct() {
575 check_join_lines_sel(
576 r"
577struct Foo <|>{
578 f: u32,
579}<|>
580 ",
581 r"
582struct Foo { f: u32 }
583 ",
584 );
585 }
586
587 #[test]
588 fn test_join_lines_selection_dot_chain() {
589 check_join_lines_sel(
590 r"
591fn foo() {
592 join(<|>type_params.type_params()
593 .filter_map(|it| it.name())
594 .map(|it| it.text())<|>)
595}",
596 r"
597fn foo() {
598 join(type_params.type_params().filter_map(|it| it.name()).map(|it| it.text()))
599}",
600 );
601 }
602
603 #[test]
604 fn test_join_lines_selection_lambda_block_body() {
605 check_join_lines_sel(
606 r"
607pub fn handle_find_matching_brace() {
608 params.offsets
609 .map(|offset| <|>{
610 world.analysis().matching_brace(&file, offset).unwrap_or(offset)
611 }<|>)
612 .collect();
613}",
614 r"
615pub fn handle_find_matching_brace() {
616 params.offsets
617 .map(|offset| world.analysis().matching_brace(&file, offset).unwrap_or(offset))
618 .collect();
619}",
620 );
621 }
622 142
623 #[test] 143 #[test]
624 fn test_on_eq_typed() { 144 fn test_on_eq_typed() {