aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src/ssr.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide/src/ssr.rs')
-rw-r--r--crates/ra_ide/src/ssr.rs72
1 files changed, 0 insertions, 72 deletions
diff --git a/crates/ra_ide/src/ssr.rs b/crates/ra_ide/src/ssr.rs
deleted file mode 100644
index 97b82b70e..000000000
--- a/crates/ra_ide/src/ssr.rs
+++ /dev/null
@@ -1,72 +0,0 @@
1use base_db::{FilePosition, FileRange};
2use ra_ide_db::RootDatabase;
3
4use crate::SourceFileEdit;
5use ra_ssr::{MatchFinder, SsrError, SsrRule};
6
7// Feature: Structural Search and Replace
8//
9// Search and replace with named wildcards that will match any expression, type, path, pattern or item.
10// The syntax for a structural search replace command is `<search_pattern> ==>> <replace_pattern>`.
11// A `$<name>` placeholder in the search pattern will match any AST node and `$<name>` will reference it in the replacement.
12// Within a macro call, a placeholder will match up until whatever token follows the placeholder.
13//
14// All paths in both the search pattern and the replacement template must resolve in the context
15// in which this command is invoked. Paths in the search pattern will then match the code if they
16// resolve to the same item, even if they're written differently. For example if we invoke the
17// command in the module `foo` with a pattern of `Bar`, then code in the parent module that refers
18// to `foo::Bar` will match.
19//
20// Paths in the replacement template will be rendered appropriately for the context in which the
21// replacement occurs. For example if our replacement template is `foo::Bar` and we match some
22// code in the `foo` module, we'll insert just `Bar`.
23//
24// Inherent method calls should generally be written in UFCS form. e.g. `foo::Bar::baz($s, $a)` will
25// match `$s.baz($a)`, provided the method call `baz` resolves to the method `foo::Bar::baz`.
26//
27// The scope of the search / replace will be restricted to the current selection if any, otherwise
28// it will apply to the whole workspace.
29//
30// Placeholders may be given constraints by writing them as `${<name>:<constraint1>:<constraint2>...}`.
31//
32// Supported constraints:
33//
34// |===
35// | Constraint | Restricts placeholder
36//
37// | kind(literal) | Is a literal (e.g. `42` or `"forty two"`)
38// | not(a) | Negates the constraint `a`
39// |===
40//
41// Available via the command `rust-analyzer.ssr`.
42//
43// ```rust
44// // Using structural search replace command [foo($a, $b) ==>> ($a).foo($b)]
45//
46// // BEFORE
47// String::from(foo(y + 5, z))
48//
49// // AFTER
50// String::from((y + 5).foo(z))
51// ```
52//
53// |===
54// | Editor | Action Name
55//
56// | VS Code | **Rust Analyzer: Structural Search Replace**
57// |===
58pub fn parse_search_replace(
59 rule: &str,
60 parse_only: bool,
61 db: &RootDatabase,
62 resolve_context: FilePosition,
63 selections: Vec<FileRange>,
64) -> Result<Vec<SourceFileEdit>, SsrError> {
65 let rule: SsrRule = rule.parse()?;
66 let mut match_finder = MatchFinder::in_context(db, resolve_context, selections);
67 match_finder.add_rule(rule)?;
68 if parse_only {
69 return Ok(Vec::new());
70 }
71 Ok(match_finder.edits())
72}