aboutsummaryrefslogtreecommitdiff
path: root/crates/ssr
diff options
context:
space:
mode:
authorLukas Wirth <[email protected]>2021-01-14 17:35:22 +0000
committerLukas Wirth <[email protected]>2021-01-14 17:35:22 +0000
commitf51457a643b768794092f73add6dda4aecd400a1 (patch)
tree8deeda89c7709aa69918d24ee4ecd18ea0e179db /crates/ssr
parentf88f3d688507508ae9528101e13e1c62902467a3 (diff)
Group file source edits by FileId
Diffstat (limited to 'crates/ssr')
-rw-r--r--crates/ssr/src/lib.rs28
-rw-r--r--crates/ssr/src/matching.rs4
-rw-r--r--crates/ssr/src/tests.rs3
3 files changed, 22 insertions, 13 deletions
diff --git a/crates/ssr/src/lib.rs b/crates/ssr/src/lib.rs
index 747ce495d..d99ebefb1 100644
--- a/crates/ssr/src/lib.rs
+++ b/crates/ssr/src/lib.rs
@@ -74,8 +74,10 @@ pub use crate::errors::SsrError;
74pub use crate::matching::Match; 74pub 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::{
78use ide_db::source_change::SourceFileEdit; 78 base_db::{FileId, FilePosition, FileRange},
79 source_change::SourceFileEdits,
80};
79use resolving::ResolvedRule; 81use resolving::ResolvedRule;
80use rustc_hash::FxHashMap; 82use rustc_hash::FxHashMap;
81use syntax::{ast, AstNode, SyntaxNode, TextRange}; 83use syntax::{ast, AstNode, SyntaxNode, TextRange};
@@ -159,7 +161,7 @@ impl<'db> MatchFinder<'db> {
159 } 161 }
160 162
161 /// Finds matches for all added rules and returns edits for all found matches. 163 /// Finds matches for all added rules and returns edits for all found matches.
162 pub fn edits(&self) -> Vec<SourceFileEdit> { 164 pub fn edits(&self) -> SourceFileEdits {
163 use ide_db::base_db::SourceDatabaseExt; 165 use ide_db::base_db::SourceDatabaseExt;
164 let mut matches_by_file = FxHashMap::default(); 166 let mut matches_by_file = FxHashMap::default();
165 for m in self.matches().matches { 167 for m in self.matches().matches {
@@ -169,13 +171,21 @@ impl<'db> MatchFinder<'db> {
169 .matches 171 .matches
170 .push(m); 172 .push(m);
171 } 173 }
172 let mut edits = vec![]; 174 SourceFileEdits {
173 for (file_id, matches) in matches_by_file { 175 edits: matches_by_file
174 let edit = 176 .into_iter()
175 replacing::matches_to_edit(&matches, &self.sema.db.file_text(file_id), &self.rules); 177 .map(|(file_id, matches)| {
176 edits.push(SourceFileEdit { file_id, edit }); 178 (
179 file_id,
180 replacing::matches_to_edit(
181 &matches,
182 &self.sema.db.file_text(file_id),
183 &self.rules,
184 ),
185 )
186 })
187 .collect(),
177 } 188 }
178 edits
179 } 189 }
180 190
181 /// Adds a search pattern. For use if you intend to only call `find_matches_in_file`. If you 191 /// 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..5888bf8f8 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.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..8ba783526 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.edits[&position.file_id].apply(&mut actual);
111 expected.assert_eq(&actual); 110 expected.assert_eq(&actual);
112} 111}
113 112