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