aboutsummaryrefslogtreecommitdiff
path: root/crates/ssr
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-01-15 09:43:08 +0000
committerGitHub <[email protected]>2021-01-15 09:43:08 +0000
commit054e2061521292a72748510f3f6cb7c8b1e8611b (patch)
treec1660a95eae4aaa57ea5546de0d81678d49f68f1 /crates/ssr
parentdc48de28d8460903dbfc9454c8cae0e17d62e9c1 (diff)
parentd5095329a1c12e93653d8de4a93f0b4f5cad4c6e (diff)
Merge #7272
7272: Group file source edits by FileId r=matklad a=Veykril Co-authored-by: Lukas Wirth <[email protected]>
Diffstat (limited to 'crates/ssr')
-rw-r--r--crates/ssr/src/lib.rs24
-rw-r--r--crates/ssr/src/matching.rs4
-rw-r--r--crates/ssr/src/tests.rs3
3 files changed, 18 insertions, 13 deletions
diff --git a/crates/ssr/src/lib.rs b/crates/ssr/src/lib.rs
index 747ce495d..a97fc8bca 100644
--- a/crates/ssr/src/lib.rs
+++ b/crates/ssr/src/lib.rs
@@ -75,10 +75,10 @@ pub use crate::matching::Match;
75use crate::matching::MatchFailureReason; 75use crate::matching::MatchFailureReason;
76use hir::Semantics; 76use hir::Semantics;
77use ide_db::base_db::{FileId, FilePosition, FileRange}; 77use ide_db::base_db::{FileId, FilePosition, FileRange};
78use ide_db::source_change::SourceFileEdit;
79use resolving::ResolvedRule; 78use resolving::ResolvedRule;
80use rustc_hash::FxHashMap; 79use rustc_hash::FxHashMap;
81use syntax::{ast, AstNode, SyntaxNode, TextRange}; 80use syntax::{ast, AstNode, SyntaxNode, TextRange};
81use text_edit::TextEdit;
82 82
83// A structured search replace rule. Create by calling `parse` on a str. 83// A structured search replace rule. Create by calling `parse` on a str.
84#[derive(Debug)] 84#[derive(Debug)]
@@ -159,7 +159,7 @@ impl<'db> MatchFinder<'db> {
159 } 159 }
160 160
161 /// Finds matches for all added rules and returns edits for all found matches. 161 /// Finds matches for all added rules and returns edits for all found matches.
162 pub fn edits(&self) -> Vec<SourceFileEdit> { 162 pub fn edits(&self) -> FxHashMap<FileId, TextEdit> {
163 use ide_db::base_db::SourceDatabaseExt; 163 use ide_db::base_db::SourceDatabaseExt;
164 let mut matches_by_file = FxHashMap::default(); 164 let mut matches_by_file = FxHashMap::default();
165 for m in self.matches().matches { 165 for m in self.matches().matches {
@@ -169,13 +169,19 @@ impl<'db> MatchFinder<'db> {
169 .matches 169 .matches
170 .push(m); 170 .push(m);
171 } 171 }
172 let mut edits = vec![]; 172 matches_by_file
173 for (file_id, matches) in matches_by_file { 173 .into_iter()
174 let edit = 174 .map(|(file_id, matches)| {
175 replacing::matches_to_edit(&matches, &self.sema.db.file_text(file_id), &self.rules); 175 (
176 edits.push(SourceFileEdit { file_id, edit }); 176 file_id,
177 } 177 replacing::matches_to_edit(
178 edits 178 &matches,
179 &self.sema.db.file_text(file_id),
180 &self.rules,
181 ),
182 )
183 })
184 .collect()
179 } 185 }
180 186
181 /// Adds a search pattern. For use if you intend to only call `find_matches_in_file`. If you 187 /// Adds a search pattern. For use if you intend to only call `find_matches_in_file`. If you
diff --git a/crates/ssr/src/matching.rs b/crates/ssr/src/matching.rs
index 6cf831431..42d313f91 100644
--- a/crates/ssr/src/matching.rs
+++ b/crates/ssr/src/matching.rs
@@ -810,9 +810,9 @@ mod tests {
810 810
811 let edits = match_finder.edits(); 811 let edits = match_finder.edits();
812 assert_eq!(edits.len(), 1); 812 assert_eq!(edits.len(), 1);
813 let edit = &edits[0]; 813 let edit = &edits[&position.file_id];
814 let mut after = input.to_string(); 814 let mut after = input.to_string();
815 edit.edit.apply(&mut after); 815 edit.apply(&mut after);
816 assert_eq!(after, "fn foo() {} fn bar() {} fn main() { bar(1+2); }"); 816 assert_eq!(after, "fn foo() {} fn bar() {} fn main() { bar(1+2); }");
817 } 817 }
818} 818}
diff --git a/crates/ssr/src/tests.rs b/crates/ssr/src/tests.rs
index d6918c22d..a3ea44f23 100644
--- a/crates/ssr/src/tests.rs
+++ b/crates/ssr/src/tests.rs
@@ -103,11 +103,10 @@ fn assert_ssr_transforms(rules: &[&str], input: &str, expected: Expect) {
103 if edits.is_empty() { 103 if edits.is_empty() {
104 panic!("No edits were made"); 104 panic!("No edits were made");
105 } 105 }
106 assert_eq!(edits[0].file_id, position.file_id);
107 // Note, db.file_text is not necessarily the same as `input`, since fixture parsing alters 106 // Note, db.file_text is not necessarily the same as `input`, since fixture parsing alters
108 // stuff. 107 // stuff.
109 let mut actual = db.file_text(position.file_id).to_string(); 108 let mut actual = db.file_text(position.file_id).to_string();
110 edits[0].edit.apply(&mut actual); 109 edits[&position.file_id].apply(&mut actual);
111 expected.assert_eq(&actual); 110 expected.assert_eq(&actual);
112} 111}
113 112