aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src/ssr.rs
blob: 9f8e540c023e107a47f587cce12a367643fa2595 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use ra_db::SourceDatabaseExt;
use ra_ide_db::{symbol_index::SymbolsDatabase, RootDatabase};

use crate::SourceFileEdit;
use ra_ssr::{MatchFinder, SsrError, SsrRule};

// Feature: Structural Search and Replace
//
// Search and replace with named wildcards that will match any expression, type, path, pattern or item.
// The syntax for a structural search replace command is `<search_pattern> ==>> <replace_pattern>`.
// A `$<name>` placeholder in the search pattern will match any AST node and `$<name>` will reference it in the replacement.
// Within a macro call, a placeholder will match up until whatever token follows the placeholder.
// Available via the command `rust-analyzer.ssr`.
//
// ```rust
// // Using structural search replace command [foo($a, $b) ==>> ($a).foo($b)]
//
// // BEFORE
// String::from(foo(y + 5, z))
//
// // AFTER
// String::from((y + 5).foo(z))
// ```
//
// |===
// | Editor  | Action Name
//
// | VS Code | **Rust Analyzer: Structural Search Replace**
// |===
pub fn parse_search_replace(
    rule: &str,
    parse_only: bool,
    db: &RootDatabase,
) -> Result<Vec<SourceFileEdit>, SsrError> {
    let mut edits = vec![];
    let rule: SsrRule = rule.parse()?;
    if parse_only {
        return Ok(edits);
    }
    let mut match_finder = MatchFinder::new(db);
    match_finder.add_rule(rule);
    for &root in db.local_roots().iter() {
        let sr = db.source_root(root);
        for file_id in sr.iter() {
            if let Some(edit) = match_finder.edits_for_file(file_id) {
                edits.push(SourceFileEdit { file_id, edit });
            }
        }
    }
    Ok(edits)
}