diff options
author | adamrk <[email protected]> | 2020-02-25 22:35:16 +0000 |
---|---|---|
committer | adamrk <[email protected]> | 2020-02-25 22:35:16 +0000 |
commit | 94603d984808f140931fe517d500bc61d871ea24 (patch) | |
tree | 32c9adddcb3609f93afa67d11780e68d64107e14 | |
parent | 4f1d90e73bb47b4da3df62b05a84ebd4297ed78d (diff) |
save comments as ra_syntax::ast::Comment
-rw-r--r-- | crates/ra_ide/src/ssr.rs | 16 |
1 files changed, 6 insertions, 10 deletions
diff --git a/crates/ra_ide/src/ssr.rs b/crates/ra_ide/src/ssr.rs index 83c212494..74fb6bab6 100644 --- a/crates/ra_ide/src/ssr.rs +++ b/crates/ra_ide/src/ssr.rs | |||
@@ -3,7 +3,8 @@ | |||
3 | use crate::source_change::SourceFileEdit; | 3 | use crate::source_change::SourceFileEdit; |
4 | use ra_ide_db::RootDatabase; | 4 | use ra_ide_db::RootDatabase; |
5 | use ra_syntax::ast::make::expr_from_text; | 5 | use ra_syntax::ast::make::expr_from_text; |
6 | use ra_syntax::{AstNode, SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken}; | 6 | use ra_syntax::ast::{AstToken, Comment}; |
7 | use ra_syntax::{AstNode, SyntaxElement, SyntaxNode}; | ||
7 | use ra_text_edit::{TextEdit, TextEditBuilder}; | 8 | use ra_text_edit::{TextEdit, TextEditBuilder}; |
8 | use rustc_hash::FxHashMap; | 9 | use rustc_hash::FxHashMap; |
9 | use std::collections::HashMap; | 10 | use std::collections::HashMap; |
@@ -70,7 +71,7 @@ type Binding = HashMap<Var, SyntaxNode>; | |||
70 | struct Match { | 71 | struct Match { |
71 | place: SyntaxNode, | 72 | place: SyntaxNode, |
72 | binding: Binding, | 73 | binding: Binding, |
73 | ignored_comments: Vec<SyntaxToken>, | 74 | ignored_comments: Vec<Comment>, |
74 | } | 75 | } |
75 | 76 | ||
76 | #[derive(Debug)] | 77 | #[derive(Debug)] |
@@ -199,12 +200,7 @@ fn find(pattern: &SsrPattern, code: &SyntaxNode) -> SsrMatches { | |||
199 | let mut code_children = | 200 | let mut code_children = |
200 | code.children_with_tokens().filter(|element| !element.kind().is_trivia()); | 201 | code.children_with_tokens().filter(|element| !element.kind().is_trivia()); |
201 | let new_ignored_comments = code.children_with_tokens().filter_map(|element| { | 202 | let new_ignored_comments = code.children_with_tokens().filter_map(|element| { |
202 | if let SyntaxElement::Token(token) = element { | 203 | element.as_token().and_then(|token| Comment::cast(token.clone())) |
203 | if token.kind() == SyntaxKind::COMMENT { | ||
204 | return Some(token.clone()); | ||
205 | } | ||
206 | } | ||
207 | None | ||
208 | }); | 204 | }); |
209 | match_.ignored_comments.extend(new_ignored_comments); | 205 | match_.ignored_comments.extend(new_ignored_comments); |
210 | let match_from_children = pattern_children | 206 | let match_from_children = pattern_children |
@@ -254,7 +250,7 @@ fn replace(matches: &SsrMatches, template: &SsrTemplate) -> TextEdit { | |||
254 | 250 | ||
255 | fn render_replace( | 251 | fn render_replace( |
256 | binding: &Binding, | 252 | binding: &Binding, |
257 | ignored_comments: &Vec<SyntaxToken>, | 253 | ignored_comments: &Vec<Comment>, |
258 | template: &SsrTemplate, | 254 | template: &SsrTemplate, |
259 | ) -> String { | 255 | ) -> String { |
260 | let mut builder = TextEditBuilder::default(); | 256 | let mut builder = TextEditBuilder::default(); |
@@ -264,7 +260,7 @@ fn render_replace( | |||
264 | } | 260 | } |
265 | } | 261 | } |
266 | for comment in ignored_comments { | 262 | for comment in ignored_comments { |
267 | builder.insert(template.template.text_range().end(), comment.to_string()) | 263 | builder.insert(template.template.text_range().end(), comment.syntax().to_string()) |
268 | } | 264 | } |
269 | builder.finish().apply(&template.template.text().to_string()) | 265 | builder.finish().apply(&template.template.text().to_string()) |
270 | } | 266 | } |