aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api_light/src/join_lines.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-01-10 14:50:49 +0000
committerAleksey Kladov <[email protected]>2019-01-10 14:50:49 +0000
commit3ca76c203921215cc9af68042ed8fbf4f50b5969 (patch)
tree52d77756f5d60161fa361e8772bbda78ccee3950 /crates/ra_ide_api_light/src/join_lines.rs
parentaca14c591fea40b2f803bbf5f02c1571732348fb (diff)
move join_lines to a separate module
Diffstat (limited to 'crates/ra_ide_api_light/src/join_lines.rs')
-rw-r--r--crates/ra_ide_api_light/src/join_lines.rs493
1 files changed, 493 insertions, 0 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}