diff options
Diffstat (limited to 'crates/ide_ssr/src')
-rw-r--r-- | crates/ide_ssr/src/from_comment.rs | 32 | ||||
-rw-r--r-- | crates/ide_ssr/src/lib.rs | 11 |
2 files changed, 43 insertions, 0 deletions
diff --git a/crates/ide_ssr/src/from_comment.rs b/crates/ide_ssr/src/from_comment.rs new file mode 100644 index 000000000..f1b312284 --- /dev/null +++ b/crates/ide_ssr/src/from_comment.rs | |||
@@ -0,0 +1,32 @@ | |||
1 | //! This module allows building an SSR MatchFinder by parsing the SSR rule | ||
2 | //! from a comment. | ||
3 | |||
4 | use ide_db::{ | ||
5 | base_db::{FilePosition, FileRange, SourceDatabase}, | ||
6 | RootDatabase, | ||
7 | }; | ||
8 | use syntax::{ | ||
9 | ast::{self, AstNode, AstToken}, | ||
10 | TextRange, | ||
11 | }; | ||
12 | |||
13 | use crate::MatchFinder; | ||
14 | |||
15 | /// Attempts to build an SSR MatchFinder from a comment at the given file | ||
16 | /// range. If successful, returns the MatchFinder and a TextRange covering | ||
17 | /// comment. | ||
18 | pub fn ssr_from_comment(db: &RootDatabase, frange: FileRange) -> Option<(MatchFinder, TextRange)> { | ||
19 | let comment = { | ||
20 | let file = db.parse(frange.file_id); | ||
21 | file.tree().syntax().token_at_offset(frange.range.start()).find_map(ast::Comment::cast) | ||
22 | }?; | ||
23 | let comment_text_without_prefix = comment.text().strip_prefix(comment.prefix()).unwrap(); | ||
24 | let ssr_rule = comment_text_without_prefix.parse().ok()?; | ||
25 | |||
26 | let lookup_context = FilePosition { file_id: frange.file_id, offset: frange.range.start() }; | ||
27 | |||
28 | let mut match_finder = MatchFinder::in_context(db, lookup_context, vec![]); | ||
29 | match_finder.add_rule(ssr_rule).ok()?; | ||
30 | |||
31 | Some((match_finder, comment.syntax().text_range())) | ||
32 | } | ||
diff --git a/crates/ide_ssr/src/lib.rs b/crates/ide_ssr/src/lib.rs index a97fc8bca..00585f448 100644 --- a/crates/ide_ssr/src/lib.rs +++ b/crates/ide_ssr/src/lib.rs | |||
@@ -57,7 +57,17 @@ | |||
57 | // | 57 | // |
58 | // | VS Code | **Rust Analyzer: Structural Search Replace** | 58 | // | VS Code | **Rust Analyzer: Structural Search Replace** |
59 | // |=== | 59 | // |=== |
60 | // | ||
61 | // Also available as an assist, by writing a comment containing the structural | ||
62 | // search and replace rule. You will only see the assist if the comment can | ||
63 | // be parsed as a valid structural search and replace rule. | ||
64 | // | ||
65 | // ```rust | ||
66 | // // Place the cursor on the line below to see the assist 💡. | ||
67 | // // foo($a, $b) ==>> ($a).foo($b) | ||
68 | // ``` | ||
60 | 69 | ||
70 | mod from_comment; | ||
61 | mod matching; | 71 | mod matching; |
62 | mod nester; | 72 | mod nester; |
63 | mod parsing; | 73 | mod parsing; |
@@ -71,6 +81,7 @@ mod tests; | |||
71 | 81 | ||
72 | use crate::errors::bail; | 82 | use crate::errors::bail; |
73 | pub use crate::errors::SsrError; | 83 | pub use crate::errors::SsrError; |
84 | pub use crate::from_comment::ssr_from_comment; | ||
74 | pub use crate::matching::Match; | 85 | pub use crate::matching::Match; |
75 | use crate::matching::MatchFailureReason; | 86 | use crate::matching::MatchFailureReason; |
76 | use hir::Semantics; | 87 | use hir::Semantics; |