Group file source edits by FileId

This commit is contained in:
Lukas Wirth 2021-01-14 18:35:22 +01:00
parent f88f3d6885
commit f51457a643
15 changed files with 188 additions and 180 deletions

View file

@ -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<SourceFileEdit> {
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

View file

@ -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); }");
}
}

View file

@ -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);
}