aboutsummaryrefslogtreecommitdiff
path: root/crates/rust-analyzer/src/cli/ssr.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/rust-analyzer/src/cli/ssr.rs')
-rw-r--r--crates/rust-analyzer/src/cli/ssr.rs40
1 files changed, 39 insertions, 1 deletions
diff --git a/crates/rust-analyzer/src/cli/ssr.rs b/crates/rust-analyzer/src/cli/ssr.rs
index a5265ac15..4fb829ea5 100644
--- a/crates/rust-analyzer/src/cli/ssr.rs
+++ b/crates/rust-analyzer/src/cli/ssr.rs
@@ -2,7 +2,7 @@
2 2
3use crate::cli::{load_cargo::load_cargo, Result}; 3use crate::cli::{load_cargo::load_cargo, Result};
4use ra_ide::SourceFileEdit; 4use ra_ide::SourceFileEdit;
5use ra_ssr::{MatchFinder, SsrRule}; 5use ra_ssr::{MatchFinder, SsrPattern, SsrRule};
6 6
7pub fn apply_ssr_rules(rules: Vec<SsrRule>) -> Result<()> { 7pub fn apply_ssr_rules(rules: Vec<SsrRule>) -> Result<()> {
8 use ra_db::SourceDatabaseExt; 8 use ra_db::SourceDatabaseExt;
@@ -31,3 +31,41 @@ pub fn apply_ssr_rules(rules: Vec<SsrRule>) -> Result<()> {
31 } 31 }
32 Ok(()) 32 Ok(())
33} 33}
34
35/// Searches for `patterns`, printing debug information for any nodes whose text exactly matches
36/// `debug_snippet`. This is intended for debugging and probably isn't in it's current form useful
37/// for much else.
38pub fn search_for_patterns(patterns: Vec<SsrPattern>, debug_snippet: Option<String>) -> Result<()> {
39 use ra_db::SourceDatabaseExt;
40 use ra_ide_db::symbol_index::SymbolsDatabase;
41 let (host, vfs) = load_cargo(&std::env::current_dir()?, true, true)?;
42 let db = host.raw_database();
43 let mut match_finder = MatchFinder::new(db);
44 for pattern in patterns {
45 match_finder.add_search_pattern(pattern);
46 }
47 for &root in db.local_roots().iter() {
48 let sr = db.source_root(root);
49 for file_id in sr.iter() {
50 if let Some(debug_snippet) = &debug_snippet {
51 for debug_info in match_finder.debug_where_text_equal(file_id, debug_snippet) {
52 println!("{:#?}", debug_info);
53 }
54 } else {
55 let matches = match_finder.find_matches_in_file(file_id);
56 if !matches.matches.is_empty() {
57 let matches = matches.flattened().matches;
58 if let Some(path) = vfs.file_path(file_id).as_path() {
59 println!("{} matches in '{}'", matches.len(), path.to_string_lossy());
60 }
61 // We could possibly at some point do something more useful than just printing
62 // the matched text. For now though, that's the easiest thing to do.
63 for m in matches {
64 println!("{}", m.matched_text());
65 }
66 }
67 }
68 }
69 }
70 Ok(())
71}