diff options
Diffstat (limited to 'crates/ra_ide_api/src/join_lines.rs')
-rw-r--r-- | crates/ra_ide_api/src/join_lines.rs | 22 |
1 files changed, 11 insertions, 11 deletions
diff --git a/crates/ra_ide_api/src/join_lines.rs b/crates/ra_ide_api/src/join_lines.rs index 9b81ad9e4..fa998ebe1 100644 --- a/crates/ra_ide_api/src/join_lines.rs +++ b/crates/ra_ide_api/src/join_lines.rs | |||
@@ -28,14 +28,14 @@ pub fn join_lines(file: &SourceFile, range: TextRange) -> TextEdit { | |||
28 | }; | 28 | }; |
29 | let mut edit = TextEditBuilder::default(); | 29 | let mut edit = TextEditBuilder::default(); |
30 | for token in node.descendants_with_tokens().filter_map(|it| it.into_token()) { | 30 | for token in node.descendants_with_tokens().filter_map(|it| it.into_token()) { |
31 | let range = match range.intersection(&token.range()) { | 31 | let range = match range.intersection(&token.text_range()) { |
32 | Some(range) => range, | 32 | Some(range) => range, |
33 | None => continue, | 33 | None => continue, |
34 | } - token.range().start(); | 34 | } - token.text_range().start(); |
35 | let text = token.text(); | 35 | let text = token.text(); |
36 | for (pos, _) in text[range].bytes().enumerate().filter(|&(_, b)| b == b'\n') { | 36 | for (pos, _) in text[range].bytes().enumerate().filter(|&(_, b)| b == b'\n') { |
37 | let pos: TextUnit = (pos as u32).into(); | 37 | let pos: TextUnit = (pos as u32).into(); |
38 | let off = token.range().start() + range.start() + pos; | 38 | let off = token.text_range().start() + range.start() + pos; |
39 | if !edit.invalidates_offset(off) { | 39 | if !edit.invalidates_offset(off) { |
40 | remove_newline(&mut edit, &token, off); | 40 | remove_newline(&mut edit, &token, off); |
41 | } | 41 | } |
@@ -49,7 +49,7 @@ fn remove_newline(edit: &mut TextEditBuilder, token: &SyntaxToken, offset: TextU | |||
49 | if token.kind() != WHITESPACE || token.text().bytes().filter(|&b| b == b'\n').count() != 1 { | 49 | if token.kind() != WHITESPACE || token.text().bytes().filter(|&b| b == b'\n').count() != 1 { |
50 | // The node is either the first or the last in the file | 50 | // The node is either the first or the last in the file |
51 | let suff = &token.text()[TextRange::from_to( | 51 | let suff = &token.text()[TextRange::from_to( |
52 | offset - token.range().start() + TextUnit::of_char('\n'), | 52 | offset - token.text_range().start() + TextUnit::of_char('\n'), |
53 | TextUnit::of_str(token.text()), | 53 | TextUnit::of_str(token.text()), |
54 | )]; | 54 | )]; |
55 | let spaces = suff.bytes().take_while(|&b| b == b' ').count(); | 55 | let spaces = suff.bytes().take_while(|&b| b == b' ').count(); |
@@ -86,7 +86,7 @@ fn remove_newline(edit: &mut TextEditBuilder, token: &SyntaxToken, offset: TextU | |||
86 | let next = token.next_sibling_or_token().unwrap(); | 86 | let next = token.next_sibling_or_token().unwrap(); |
87 | if is_trailing_comma(prev.kind(), next.kind()) { | 87 | if is_trailing_comma(prev.kind(), next.kind()) { |
88 | // Removes: trailing comma, newline (incl. surrounding whitespace) | 88 | // Removes: trailing comma, newline (incl. surrounding whitespace) |
89 | edit.delete(TextRange::from_to(prev.range().start(), token.range().end())); | 89 | edit.delete(TextRange::from_to(prev.text_range().start(), token.text_range().end())); |
90 | } else if prev.kind() == T![,] && next.kind() == T!['}'] { | 90 | } else if prev.kind() == T![,] && next.kind() == T!['}'] { |
91 | // Removes: comma, newline (incl. surrounding whitespace) | 91 | // Removes: comma, newline (incl. surrounding whitespace) |
92 | let space = if let Some(left) = prev.prev_sibling_or_token() { | 92 | let space = if let Some(left) = prev.prev_sibling_or_token() { |
@@ -95,7 +95,7 @@ fn remove_newline(edit: &mut TextEditBuilder, token: &SyntaxToken, offset: TextU | |||
95 | " " | 95 | " " |
96 | }; | 96 | }; |
97 | edit.replace( | 97 | edit.replace( |
98 | TextRange::from_to(prev.range().start(), token.range().end()), | 98 | TextRange::from_to(prev.text_range().start(), token.text_range().end()), |
99 | space.to_string(), | 99 | space.to_string(), |
100 | ); | 100 | ); |
101 | } else if let (Some(_), Some(next)) = ( | 101 | } else if let (Some(_), Some(next)) = ( |
@@ -104,12 +104,12 @@ fn remove_newline(edit: &mut TextEditBuilder, token: &SyntaxToken, offset: TextU | |||
104 | ) { | 104 | ) { |
105 | // Removes: newline (incl. surrounding whitespace), start of the next comment | 105 | // Removes: newline (incl. surrounding whitespace), start of the next comment |
106 | edit.delete(TextRange::from_to( | 106 | edit.delete(TextRange::from_to( |
107 | token.range().start(), | 107 | token.text_range().start(), |
108 | next.syntax().range().start() + TextUnit::of_str(next.prefix()), | 108 | next.syntax().text_range().start() + TextUnit::of_str(next.prefix()), |
109 | )); | 109 | )); |
110 | } else { | 110 | } else { |
111 | // Remove newline but add a computed amount of whitespace characters | 111 | // Remove newline but add a computed amount of whitespace characters |
112 | edit.replace(token.range(), compute_ws(prev.kind(), next.kind()).to_string()); | 112 | edit.replace(token.text_range(), compute_ws(prev.kind(), next.kind()).to_string()); |
113 | } | 113 | } |
114 | } | 114 | } |
115 | 115 | ||
@@ -125,7 +125,7 @@ fn join_single_expr_block(edit: &mut TextEditBuilder, token: &SyntaxToken) -> Op | |||
125 | let block_expr = ast::BlockExpr::cast(block.syntax().parent()?)?; | 125 | let block_expr = ast::BlockExpr::cast(block.syntax().parent()?)?; |
126 | let expr = extract_trivial_expression(&block)?; | 126 | let expr = extract_trivial_expression(&block)?; |
127 | 127 | ||
128 | let block_range = block_expr.syntax().range(); | 128 | let block_range = block_expr.syntax().text_range(); |
129 | let mut buf = expr.syntax().text().to_string(); | 129 | let mut buf = expr.syntax().text().to_string(); |
130 | 130 | ||
131 | // Match block needs to have a comma after the block | 131 | // Match block needs to have a comma after the block |
@@ -143,7 +143,7 @@ fn join_single_expr_block(edit: &mut TextEditBuilder, token: &SyntaxToken) -> Op | |||
143 | fn join_single_use_tree(edit: &mut TextEditBuilder, token: &SyntaxToken) -> Option<()> { | 143 | fn join_single_use_tree(edit: &mut TextEditBuilder, token: &SyntaxToken) -> Option<()> { |
144 | let use_tree_list = ast::UseTreeList::cast(token.parent())?; | 144 | let use_tree_list = ast::UseTreeList::cast(token.parent())?; |
145 | let (tree,) = use_tree_list.use_trees().collect_tuple()?; | 145 | let (tree,) = use_tree_list.use_trees().collect_tuple()?; |
146 | edit.replace(use_tree_list.syntax().range(), tree.syntax().text().to_string()); | 146 | edit.replace(use_tree_list.syntax().text_range(), tree.syntax().text().to_string()); |
147 | Some(()) | 147 | Some(()) |
148 | } | 148 | } |
149 | 149 | ||