diff options
author | David Lattimore <[email protected]> | 2020-07-22 07:23:43 +0100 |
---|---|---|
committer | David Lattimore <[email protected]> | 2020-07-24 12:34:00 +0100 |
commit | a45682ed96f18f962ac403419b4d143d59ba5283 (patch) | |
tree | 5f2178b2957b2e7cfddac94ec1748edb3dd19f10 /crates/ra_ssr/src/lib.rs | |
parent | 13f901f636846e330699a4414059591ec2e67cd1 (diff) |
Move iteration over all files into the SSR crate
The methods `edits_for_file` and `find_matches_in_file` are replaced with just `edits` and `matches`. This simplifies the API a bit, but more importantly it makes it possible in a subsequent commit for SSR to decide to not search all files.
Diffstat (limited to 'crates/ra_ssr/src/lib.rs')
-rw-r--r-- | crates/ra_ssr/src/lib.rs | 48 |
1 files changed, 27 insertions, 21 deletions
diff --git a/crates/ra_ssr/src/lib.rs b/crates/ra_ssr/src/lib.rs index dac73c07c..7b6409806 100644 --- a/crates/ra_ssr/src/lib.rs +++ b/crates/ra_ssr/src/lib.rs | |||
@@ -17,8 +17,9 @@ pub use crate::matching::Match; | |||
17 | use crate::matching::MatchFailureReason; | 17 | use crate::matching::MatchFailureReason; |
18 | use hir::Semantics; | 18 | use hir::Semantics; |
19 | use ra_db::{FileId, FileRange}; | 19 | use ra_db::{FileId, FileRange}; |
20 | use ra_ide_db::source_change::SourceFileEdit; | ||
20 | use ra_syntax::{ast, AstNode, SyntaxNode, TextRange}; | 21 | use ra_syntax::{ast, AstNode, SyntaxNode, TextRange}; |
21 | use ra_text_edit::TextEdit; | 22 | use rustc_hash::FxHashMap; |
22 | 23 | ||
23 | // A structured search replace rule. Create by calling `parse` on a str. | 24 | // A structured search replace rule. Create by calling `parse` on a str. |
24 | #[derive(Debug)] | 25 | #[derive(Debug)] |
@@ -60,32 +61,37 @@ impl<'db> MatchFinder<'db> { | |||
60 | self.add_parsed_rules(rule.parsed_rules); | 61 | self.add_parsed_rules(rule.parsed_rules); |
61 | } | 62 | } |
62 | 63 | ||
64 | /// Finds matches for all added rules and returns edits for all found matches. | ||
65 | pub fn edits(&self) -> Vec<SourceFileEdit> { | ||
66 | use ra_db::SourceDatabaseExt; | ||
67 | let mut matches_by_file = FxHashMap::default(); | ||
68 | for m in self.matches().matches { | ||
69 | matches_by_file | ||
70 | .entry(m.range.file_id) | ||
71 | .or_insert_with(|| SsrMatches::default()) | ||
72 | .matches | ||
73 | .push(m); | ||
74 | } | ||
75 | let mut edits = vec![]; | ||
76 | for (file_id, matches) in matches_by_file { | ||
77 | let edit = | ||
78 | replacing::matches_to_edit(&matches, &self.sema.db.file_text(file_id), &self.rules); | ||
79 | edits.push(SourceFileEdit { file_id, edit }); | ||
80 | } | ||
81 | edits | ||
82 | } | ||
83 | |||
63 | /// Adds a search pattern. For use if you intend to only call `find_matches_in_file`. If you | 84 | /// Adds a search pattern. For use if you intend to only call `find_matches_in_file`. If you |
64 | /// intend to do replacement, use `add_rule` instead. | 85 | /// intend to do replacement, use `add_rule` instead. |
65 | pub fn add_search_pattern(&mut self, pattern: SsrPattern) { | 86 | pub fn add_search_pattern(&mut self, pattern: SsrPattern) { |
66 | self.add_parsed_rules(pattern.parsed_rules); | 87 | self.add_parsed_rules(pattern.parsed_rules); |
67 | } | 88 | } |
68 | 89 | ||
69 | pub fn edits_for_file(&self, file_id: FileId) -> Option<TextEdit> { | 90 | /// Returns matches for all added rules. |
70 | let matches = self.find_matches_in_file(file_id); | 91 | pub fn matches(&self) -> SsrMatches { |
71 | if matches.matches.is_empty() { | 92 | let mut matches = Vec::new(); |
72 | None | 93 | self.find_all_matches(&mut matches); |
73 | } else { | 94 | SsrMatches { matches } |
74 | use ra_db::SourceDatabaseExt; | ||
75 | Some(replacing::matches_to_edit( | ||
76 | &matches, | ||
77 | &self.sema.db.file_text(file_id), | ||
78 | &self.rules, | ||
79 | )) | ||
80 | } | ||
81 | } | ||
82 | |||
83 | pub fn find_matches_in_file(&self, file_id: FileId) -> SsrMatches { | ||
84 | let file = self.sema.parse(file_id); | ||
85 | let code = file.syntax(); | ||
86 | let mut matches = SsrMatches::default(); | ||
87 | self.slow_scan_node(code, &None, &mut matches.matches); | ||
88 | matches | ||
89 | } | 95 | } |
90 | 96 | ||
91 | /// Finds all nodes in `file_id` whose text is exactly equal to `snippet` and attempts to match | 97 | /// Finds all nodes in `file_id` whose text is exactly equal to `snippet` and attempts to match |