diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-07-19 12:15:55 +0100 |
---|---|---|
committer | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-07-19 12:15:55 +0100 |
commit | f209843e31af7f0e0212aa28ffec2efad2a70c6f (patch) | |
tree | 548227da78a3bea644f57714d075410c0bdf7469 /crates/ra_ide_api/src/extend_selection.rs | |
parent | 58d4983ba5745975446d60f2886d96f8d2adf0f2 (diff) | |
parent | d4a66166c002f0a49e41d856a49cb5685ac93202 (diff) |
Merge #1545
1545: migrate ra_syntax to the new rowan API r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_ide_api/src/extend_selection.rs')
-rw-r--r-- | crates/ra_ide_api/src/extend_selection.rs | 28 |
1 files changed, 14 insertions, 14 deletions
diff --git a/crates/ra_ide_api/src/extend_selection.rs b/crates/ra_ide_api/src/extend_selection.rs index 491b15702..8c49960f5 100644 --- a/crates/ra_ide_api/src/extend_selection.rs +++ b/crates/ra_ide_api/src/extend_selection.rs | |||
@@ -42,7 +42,7 @@ fn try_extend_selection(root: &SyntaxNode, range: TextRange) -> Option<TextRange | |||
42 | TokenAtOffset::None => return None, | 42 | TokenAtOffset::None => return None, |
43 | TokenAtOffset::Single(l) => { | 43 | TokenAtOffset::Single(l) => { |
44 | if string_kinds.contains(&l.kind()) { | 44 | if string_kinds.contains(&l.kind()) { |
45 | extend_single_word_in_comment_or_string(l, offset).unwrap_or_else(|| l.range()) | 45 | extend_single_word_in_comment_or_string(&l, offset).unwrap_or_else(|| l.range()) |
46 | } else { | 46 | } else { |
47 | l.range() | 47 | l.range() |
48 | } | 48 | } |
@@ -56,7 +56,7 @@ fn try_extend_selection(root: &SyntaxNode, range: TextRange) -> Option<TextRange | |||
56 | if token.range() != range { | 56 | if token.range() != range { |
57 | return Some(token.range()); | 57 | return Some(token.range()); |
58 | } | 58 | } |
59 | if let Some(comment) = ast::Comment::cast(token) { | 59 | if let Some(comment) = ast::Comment::cast(token.clone()) { |
60 | if let Some(range) = extend_comments(comment) { | 60 | if let Some(range) = extend_comments(comment) { |
61 | return Some(range); | 61 | return Some(range); |
62 | } | 62 | } |
@@ -73,7 +73,7 @@ fn try_extend_selection(root: &SyntaxNode, range: TextRange) -> Option<TextRange | |||
73 | let node = node.ancestors().take_while(|n| n.range() == node.range()).last().unwrap(); | 73 | let node = node.ancestors().take_while(|n| n.range() == node.range()).last().unwrap(); |
74 | 74 | ||
75 | if node.parent().map(|n| list_kinds.contains(&n.kind())) == Some(true) { | 75 | if node.parent().map(|n| list_kinds.contains(&n.kind())) == Some(true) { |
76 | if let Some(range) = extend_list_item(node) { | 76 | if let Some(range) = extend_list_item(&node) { |
77 | return Some(range); | 77 | return Some(range); |
78 | } | 78 | } |
79 | } | 79 | } |
@@ -82,7 +82,7 @@ fn try_extend_selection(root: &SyntaxNode, range: TextRange) -> Option<TextRange | |||
82 | } | 82 | } |
83 | 83 | ||
84 | fn extend_single_word_in_comment_or_string( | 84 | fn extend_single_word_in_comment_or_string( |
85 | leaf: SyntaxToken, | 85 | leaf: &SyntaxToken, |
86 | offset: TextUnit, | 86 | offset: TextUnit, |
87 | ) -> Option<TextRange> { | 87 | ) -> Option<TextRange> { |
88 | let text: &str = leaf.text(); | 88 | let text: &str = leaf.text(); |
@@ -131,9 +131,9 @@ fn extend_ws(root: &SyntaxNode, ws: SyntaxToken, offset: TextUnit) -> TextRange | |||
131 | ws.range() | 131 | ws.range() |
132 | } | 132 | } |
133 | 133 | ||
134 | fn pick_best<'a>(l: SyntaxToken<'a>, r: SyntaxToken<'a>) -> SyntaxToken<'a> { | 134 | fn pick_best<'a>(l: SyntaxToken, r: SyntaxToken) -> SyntaxToken { |
135 | return if priority(r) > priority(l) { r } else { l }; | 135 | return if priority(&r) > priority(&l) { r } else { l }; |
136 | fn priority(n: SyntaxToken) -> usize { | 136 | fn priority(n: &SyntaxToken) -> usize { |
137 | match n.kind() { | 137 | match n.kind() { |
138 | WHITESPACE => 0, | 138 | WHITESPACE => 0, |
139 | IDENT | T![self] | T![super] | T![crate] | LIFETIME => 2, | 139 | IDENT | T![self] | T![super] | T![crate] | LIFETIME => 2, |
@@ -156,7 +156,7 @@ fn extend_list_item(node: &SyntaxNode) -> Option<TextRange> { | |||
156 | SyntaxElement::Token(it) => is_single_line_ws(it), | 156 | SyntaxElement::Token(it) => is_single_line_ws(it), |
157 | }) | 157 | }) |
158 | .next() | 158 | .next() |
159 | .and_then(|it| it.as_token()) | 159 | .and_then(|it| it.as_token().cloned()) |
160 | .filter(|node| node.kind() == T![,]) | 160 | .filter(|node| node.kind() == T![,]) |
161 | } | 161 | } |
162 | 162 | ||
@@ -167,7 +167,7 @@ fn extend_list_item(node: &SyntaxNode) -> Option<TextRange> { | |||
167 | // Include any following whitespace when comma if after list item. | 167 | // Include any following whitespace when comma if after list item. |
168 | let final_node = comma_node | 168 | let final_node = comma_node |
169 | .next_sibling_or_token() | 169 | .next_sibling_or_token() |
170 | .and_then(|it| it.as_token()) | 170 | .and_then(|it| it.as_token().cloned()) |
171 | .filter(|node| is_single_line_ws(node)) | 171 | .filter(|node| is_single_line_ws(node)) |
172 | .unwrap_or(comma_node); | 172 | .unwrap_or(comma_node); |
173 | 173 | ||
@@ -178,8 +178,8 @@ fn extend_list_item(node: &SyntaxNode) -> Option<TextRange> { | |||
178 | } | 178 | } |
179 | 179 | ||
180 | fn extend_comments(comment: ast::Comment) -> Option<TextRange> { | 180 | fn extend_comments(comment: ast::Comment) -> Option<TextRange> { |
181 | let prev = adj_comments(comment, Direction::Prev); | 181 | let prev = adj_comments(&comment, Direction::Prev); |
182 | let next = adj_comments(comment, Direction::Next); | 182 | let next = adj_comments(&comment, Direction::Next); |
183 | if prev != next { | 183 | if prev != next { |
184 | Some(TextRange::from_to(prev.syntax().range().start(), next.syntax().range().end())) | 184 | Some(TextRange::from_to(prev.syntax().range().start(), next.syntax().range().end())) |
185 | } else { | 185 | } else { |
@@ -187,14 +187,14 @@ fn extend_comments(comment: ast::Comment) -> Option<TextRange> { | |||
187 | } | 187 | } |
188 | } | 188 | } |
189 | 189 | ||
190 | fn adj_comments(comment: ast::Comment, dir: Direction) -> ast::Comment { | 190 | fn adj_comments(comment: &ast::Comment, dir: Direction) -> ast::Comment { |
191 | let mut res = comment; | 191 | let mut res = comment.clone(); |
192 | for element in comment.syntax().siblings_with_tokens(dir) { | 192 | for element in comment.syntax().siblings_with_tokens(dir) { |
193 | let token = match element.as_token() { | 193 | let token = match element.as_token() { |
194 | None => break, | 194 | None => break, |
195 | Some(token) => token, | 195 | Some(token) => token, |
196 | }; | 196 | }; |
197 | if let Some(c) = ast::Comment::cast(token) { | 197 | if let Some(c) = ast::Comment::cast(token.clone()) { |
198 | res = c | 198 | res = c |
199 | } else if token.kind() != WHITESPACE || token.text().contains("\n\n") { | 199 | } else if token.kind() != WHITESPACE || token.text().contains("\n\n") { |
200 | break; | 200 | break; |