use crate::protocol::display::{Line, Role};
use crate::protocol::vocabulary::{IntentLine, Mark, Plural};
use crate::report::{Block, Document};
use crate::{Intent, Mutation, Selection, Template};
use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, btree_map::Entry};
use vvv_core::{Edit, Query, RelPath};
use crate::change::Change;
use crate::graph::Graph;
use crate::{Candidate, EngineError, FileChange, Match, Planned, Workspace};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RewriteOf {
pub intent: RewriteIntent,
pub matches: Vec<crate::Match>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct RewriteIntent {
pub query: Query,
pub template: Template,
#[serde(default, skip_serializing_if = "Selection::is_all")]
pub selection: Selection,
}
impl RewriteIntent {
pub fn new(query: Query, template: impl Into<Template>) -> Self {
Self {
query,
template: template.into(),
selection: Selection::All,
}
}
pub fn selecting(mut self, selection: Selection) -> Self {
self.selection = selection;
self
}
pub fn plan(self, engine: &crate::Engine) -> Result<Planned<Rewrite>, EngineError> {
let _operation = engine.operation();
let mut graph = engine.graph()?;
self.plan_in(&mut graph, engine.workspace())
}
pub(crate) fn plan_in(
self,
graph: &mut crate::graph::Graph,
workspace: &crate::Workspace,
) -> Result<Planned<Rewrite>, EngineError> {
let matches = graph.search(&self.query)?.matches;
RewriteMatches::new(self, matches, graph)?.plan(workspace)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct Rewrite {
pub intent: RewriteIntent,
#[serde(flatten)]
pub state: crate::MutationState,
pub files: Vec<FileChange>,
}
impl Mutation for Rewrite {
fn into_mutation(self) -> crate::MutationAnswer {
crate::MutationAnswer::Rewrite(self)
}
fn applied(&mut self, id: u64) {
self.state = crate::MutationState::Applied { history_id: id };
}
}
impl RewriteOf {
pub fn plan(self, engine: &crate::Engine) -> Result<Planned<Rewrite>, EngineError> {
let _operation = engine.operation();
let mut graph = engine.graph()?;
self.plan_in(&mut graph, engine.workspace())
}
pub(crate) fn plan_in(
self,
graph: &mut crate::graph::Graph,
workspace: &crate::Workspace,
) -> Result<Planned<Rewrite>, EngineError> {
let matched = RewriteMatches::new(self.intent, self.matches, graph)?;
for file in matched.files.values() {
file.candidate
.validate_matches(&matched.intent.query, &file.matches)?;
}
matched.plan(workspace)
}
}
struct RewriteMatches {
intent: RewriteIntent,
files: BTreeMap<RelPath, FileMatches>,
}
struct FileMatches {
candidate: Candidate,
matches: Vec<Match>,
}
impl RewriteMatches {
fn new(intent: RewriteIntent, matches: Vec<Match>, graph: &Graph) -> Result<Self, EngineError> {
let mut files: BTreeMap<RelPath, FileMatches> = BTreeMap::new();
for m in intent.selection.narrow(matches)? {
let file = match files.entry(m.path.clone()) {
Entry::Occupied(entry) => entry.into_mut(),
Entry::Vacant(entry) => entry.insert(FileMatches {
candidate: graph.file(&m.path)?,
matches: Vec::new(),
}),
};
file.matches.push(m);
}
Ok(Self { intent, files })
}
fn plan(self, workspace: &Workspace) -> Result<Planned<Rewrite>, EngineError> {
let mut change = Change::new();
for file in self.files.values() {
file.edits(&self.intent, &mut change)?;
}
Planned::of(
workspace,
change,
crate::Intent::Rewrite(self.intent.clone()),
|_, files| Rewrite {
intent: self.intent,
state: crate::MutationState::Preview,
files,
},
)
}
}
impl FileMatches {
fn edits(&self, intent: &RewriteIntent, change: &mut Change) -> Result<(), EngineError> {
let file = self.candidate.file();
for m in &self.matches {
let replacement = intent.template.expand(m, file.source()).map_err(|source| {
EngineError::Template {
path: m.path.clone(),
line: m.start.line,
source,
}
})?;
change.edit(file.witness(), Edit::replace(m.span, replacement))?;
}
Ok(())
}
}
impl Document {
pub(crate) fn rewrite(result: &Rewrite) -> Self {
let mut report = Self::new();
report.title(IntentLine(&Intent::Rewrite(result.intent.clone())));
report.block_body(Block::Changes {
state: result.state,
files: result.files.clone(),
});
let edits = Self::edits_in(&result.files);
if edits == 0 {
report.block_note(Block::Summary(
Line::mark(Mark::Nothing).and(Role::Plain, " no matches"),
));
return report;
}
report.receipt(
result.state,
Line::mark(Mark::Rewrite)
.and(Role::Plain, format!(" {edits} "))
.and(Role::Dim, Plural(result.files.len(), "file").to_string()),
);
report
}
}