2020-06-27 02:31:50 -05:00
|
|
|
//! Applies structured search replace rules from the command line.
|
|
|
|
|
|
|
|
use crate::cli::{load_cargo::load_cargo, Result};
|
2020-06-30 00:55:20 -05:00
|
|
|
use ra_ssr::{MatchFinder, SsrPattern, SsrRule};
|
2020-06-27 02:31:50 -05:00
|
|
|
|
|
|
|
pub fn apply_ssr_rules(rules: Vec<SsrRule>) -> Result<()> {
|
|
|
|
use ra_db::SourceDatabaseExt;
|
|
|
|
let (host, vfs) = load_cargo(&std::env::current_dir()?, true, true)?;
|
|
|
|
let db = host.raw_database();
|
2020-07-22 00:00:28 -05:00
|
|
|
let mut match_finder = MatchFinder::at_first_file(db)?;
|
2020-06-27 02:31:50 -05:00
|
|
|
for rule in rules {
|
|
|
|
match_finder.add_rule(rule);
|
|
|
|
}
|
2020-07-22 01:23:43 -05:00
|
|
|
let edits = match_finder.edits();
|
2020-06-27 02:31:50 -05:00
|
|
|
for edit in edits {
|
|
|
|
if let Some(path) = vfs.file_path(edit.file_id).as_path() {
|
|
|
|
let mut contents = db.file_text(edit.file_id).to_string();
|
|
|
|
edit.edit.apply(&mut contents);
|
|
|
|
std::fs::write(path, contents)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-06-30 00:55:20 -05:00
|
|
|
|
|
|
|
/// Searches for `patterns`, printing debug information for any nodes whose text exactly matches
|
|
|
|
/// `debug_snippet`. This is intended for debugging and probably isn't in it's current form useful
|
|
|
|
/// for much else.
|
|
|
|
pub fn search_for_patterns(patterns: Vec<SsrPattern>, debug_snippet: Option<String>) -> Result<()> {
|
|
|
|
use ra_db::SourceDatabaseExt;
|
|
|
|
use ra_ide_db::symbol_index::SymbolsDatabase;
|
2020-07-22 01:23:43 -05:00
|
|
|
let (host, _vfs) = load_cargo(&std::env::current_dir()?, true, true)?;
|
2020-06-30 00:55:20 -05:00
|
|
|
let db = host.raw_database();
|
2020-07-22 00:00:28 -05:00
|
|
|
let mut match_finder = MatchFinder::at_first_file(db)?;
|
2020-06-30 00:55:20 -05:00
|
|
|
for pattern in patterns {
|
|
|
|
match_finder.add_search_pattern(pattern);
|
|
|
|
}
|
2020-07-22 01:23:43 -05:00
|
|
|
if let Some(debug_snippet) = &debug_snippet {
|
|
|
|
for &root in db.local_roots().iter() {
|
|
|
|
let sr = db.source_root(root);
|
|
|
|
for file_id in sr.iter() {
|
2020-06-30 00:55:20 -05:00
|
|
|
for debug_info in match_finder.debug_where_text_equal(file_id, debug_snippet) {
|
|
|
|
println!("{:#?}", debug_info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-22 01:23:43 -05:00
|
|
|
} else {
|
|
|
|
for m in match_finder.matches().flattened().matches {
|
|
|
|
// We could possibly at some point do something more useful than just printing
|
|
|
|
// the matched text. For now though, that's the easiest thing to do.
|
|
|
|
println!("{}", m.matched_text());
|
|
|
|
}
|
2020-06-30 00:55:20 -05:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|