From f51457a643b768794092f73add6dda4aecd400a1 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Thu, 14 Jan 2021 18:35:22 +0100 Subject: Group file source edits by FileId --- crates/ssr/src/lib.rs | 28 +++++++++++++++++++--------- crates/ssr/src/matching.rs | 4 ++-- crates/ssr/src/tests.rs | 3 +-- 3 files changed, 22 insertions(+), 13 deletions(-) (limited to 'crates/ssr') 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; pub use crate::matching::Match; use crate::matching::MatchFailureReason; use hir::Semantics; -use ide_db::base_db::{FileId, FilePosition, FileRange}; -use ide_db::source_change::SourceFileEdit; +use ide_db::{ + base_db::{FileId, FilePosition, FileRange}, + source_change::SourceFileEdits, +}; use resolving::ResolvedRule; use rustc_hash::FxHashMap; use syntax::{ast, AstNode, SyntaxNode, TextRange}; @@ -159,7 +161,7 @@ impl<'db> MatchFinder<'db> { } /// Finds matches for all added rules and returns edits for all found matches. - pub fn edits(&self) -> Vec { + pub fn edits(&self) -> SourceFileEdits { use ide_db::base_db::SourceDatabaseExt; let mut matches_by_file = FxHashMap::default(); for m in self.matches().matches { @@ -169,13 +171,21 @@ impl<'db> MatchFinder<'db> { .matches .push(m); } - let mut edits = vec![]; - for (file_id, matches) in matches_by_file { - let edit = - replacing::matches_to_edit(&matches, &self.sema.db.file_text(file_id), &self.rules); - edits.push(SourceFileEdit { file_id, edit }); + SourceFileEdits { + edits: matches_by_file + .into_iter() + .map(|(file_id, matches)| { + ( + file_id, + replacing::matches_to_edit( + &matches, + &self.sema.db.file_text(file_id), + &self.rules, + ), + ) + }) + .collect(), } - edits } /// 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 { let edits = match_finder.edits(); assert_eq!(edits.len(), 1); - let edit = &edits[0]; + let edit = &edits.edits[&position.file_id]; let mut after = input.to_string(); - edit.edit.apply(&mut after); + edit.apply(&mut after); assert_eq!(after, "fn foo() {} fn bar() {} fn main() { bar(1+2); }"); } } 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) { if edits.is_empty() { panic!("No edits were made"); } - assert_eq!(edits[0].file_id, position.file_id); // Note, db.file_text is not necessarily the same as `input`, since fixture parsing alters // stuff. let mut actual = db.file_text(position.file_id).to_string(); - edits[0].edit.apply(&mut actual); + edits.edits[&position.file_id].apply(&mut actual); expected.assert_eq(&actual); } -- cgit v1.2.3 From d5095329a1c12e93653d8de4a93f0b4f5cad4c6e Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Thu, 14 Jan 2021 22:43:36 +0100 Subject: Phase out SourceFileEdits in favour of a plain HashMap --- crates/ssr/src/lib.rs | 36 ++++++++++++++++-------------------- crates/ssr/src/matching.rs | 2 +- crates/ssr/src/tests.rs | 2 +- 3 files changed, 18 insertions(+), 22 deletions(-) (limited to 'crates/ssr') diff --git a/crates/ssr/src/lib.rs b/crates/ssr/src/lib.rs index d99ebefb1..a97fc8bca 100644 --- a/crates/ssr/src/lib.rs +++ b/crates/ssr/src/lib.rs @@ -74,13 +74,11 @@ pub use crate::errors::SsrError; pub use crate::matching::Match; use crate::matching::MatchFailureReason; use hir::Semantics; -use ide_db::{ - base_db::{FileId, FilePosition, FileRange}, - source_change::SourceFileEdits, -}; +use ide_db::base_db::{FileId, FilePosition, FileRange}; use resolving::ResolvedRule; use rustc_hash::FxHashMap; use syntax::{ast, AstNode, SyntaxNode, TextRange}; +use text_edit::TextEdit; // A structured search replace rule. Create by calling `parse` on a str. #[derive(Debug)] @@ -161,7 +159,7 @@ impl<'db> MatchFinder<'db> { } /// Finds matches for all added rules and returns edits for all found matches. - pub fn edits(&self) -> SourceFileEdits { + pub fn edits(&self) -> FxHashMap { use ide_db::base_db::SourceDatabaseExt; let mut matches_by_file = FxHashMap::default(); for m in self.matches().matches { @@ -171,21 +169,19 @@ impl<'db> MatchFinder<'db> { .matches .push(m); } - SourceFileEdits { - edits: matches_by_file - .into_iter() - .map(|(file_id, matches)| { - ( - file_id, - replacing::matches_to_edit( - &matches, - &self.sema.db.file_text(file_id), - &self.rules, - ), - ) - }) - .collect(), - } + matches_by_file + .into_iter() + .map(|(file_id, matches)| { + ( + file_id, + replacing::matches_to_edit( + &matches, + &self.sema.db.file_text(file_id), + &self.rules, + ), + ) + }) + .collect() } /// 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 5888bf8f8..42d313f91 100644 --- a/crates/ssr/src/matching.rs +++ b/crates/ssr/src/matching.rs @@ -810,7 +810,7 @@ mod tests { let edits = match_finder.edits(); assert_eq!(edits.len(), 1); - let edit = &edits.edits[&position.file_id]; + let edit = &edits[&position.file_id]; let mut after = input.to_string(); edit.apply(&mut after); assert_eq!(after, "fn foo() {} fn bar() {} fn main() { bar(1+2); }"); diff --git a/crates/ssr/src/tests.rs b/crates/ssr/src/tests.rs index 8ba783526..a3ea44f23 100644 --- a/crates/ssr/src/tests.rs +++ b/crates/ssr/src/tests.rs @@ -106,7 +106,7 @@ fn assert_ssr_transforms(rules: &[&str], input: &str, expected: Expect) { // Note, db.file_text is not necessarily the same as `input`, since fixture parsing alters // stuff. let mut actual = db.file_text(position.file_id).to_string(); - edits.edits[&position.file_id].apply(&mut actual); + edits[&position.file_id].apply(&mut actual); expected.assert_eq(&actual); } -- cgit v1.2.3