diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-03-10 14:34:59 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2021-03-10 14:34:59 +0000 |
commit | 9dc13408f954719ae0b06d778ce6338bfcd5a63d (patch) | |
tree | 5c1e05f3b1afa70492d37384fd94b989d49d80c5 /crates/ide_ssr | |
parent | a9b1e5cde1cc6e470be60cf2c230b99a62c2d2c3 (diff) | |
parent | 09307be75b452bd7a10b3855b5d43083cca140a1 (diff) |
Merge #7874
7874: add apply ssr assist r=JoshMcguigan a=JoshMcguigan
This PR adds an `Apply SSR` assist which was briefly mentioned in #3186. It allows writing an ssr rule as a comment, and then applying that SSR via an assist. This workflow is much nicer than the default available via `coc-rust-analyzer` when iterating to find the proper replacement.
As currently implemented, this requires the ssr rule is written as a single line in the comment, and it doesn't require any kind of prefix. Anything which properly parses as a ssr rule will enable the assist. The benefit of requiring the ssr rule be on a single line is it allows for a workflow where the user has several rules written one after the other, possibly to be triggered in order, without having to try to parse multiple lines of text and determine where one rule ends and the next begins. The benefit of not requiring a prefix is less typing :laughing: - plus, I think the chance of something accidentally parsing as an ssr rule is minimal.
I think a reasonable extension of this would be to allow either any ssr rule that fits on a single line, or any comment block which in its entirety makes up a single ssr rule (parsing a comment block containing multiple ssr rules and running them all would break the use case that currently works where a user writes multiple ssr rules then runs them each one by one in arbitrary order).
I've marked this as a draft because for some reason I am strugging to make the unit tests pass. It does work when I compile rust-analyzer and test it in my editor though, so I'm not sure what is going on.
Co-authored-by: Josh Mcguigan <[email protected]>
Diffstat (limited to 'crates/ide_ssr')
-rw-r--r-- | crates/ide_ssr/src/from_comment.rs | 32 | ||||
-rw-r--r-- | crates/ide_ssr/src/lib.rs | 2 |
2 files changed, 34 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..e72c611a3 100644 --- a/crates/ide_ssr/src/lib.rs +++ b/crates/ide_ssr/src/lib.rs | |||
@@ -58,6 +58,7 @@ | |||
58 | // | VS Code | **Rust Analyzer: Structural Search Replace** | 58 | // | VS Code | **Rust Analyzer: Structural Search Replace** |
59 | // |=== | 59 | // |=== |
60 | 60 | ||
61 | mod from_comment; | ||
61 | mod matching; | 62 | mod matching; |
62 | mod nester; | 63 | mod nester; |
63 | mod parsing; | 64 | mod parsing; |
@@ -71,6 +72,7 @@ mod tests; | |||
71 | 72 | ||
72 | use crate::errors::bail; | 73 | use crate::errors::bail; |
73 | pub use crate::errors::SsrError; | 74 | pub use crate::errors::SsrError; |
75 | pub use crate::from_comment::ssr_from_comment; | ||
74 | pub use crate::matching::Match; | 76 | pub use crate::matching::Match; |
75 | use crate::matching::MatchFailureReason; | 77 | use crate::matching::MatchFailureReason; |
76 | use hir::Semantics; | 78 | use hir::Semantics; |