use vvv_core::Edit;
use crate::change::Change;
use crate::command::{Command, Context};
use crate::graph::Evidence;
use crate::{
Confidence, EngineError, Match, Planned, References, ReferencesQuery, Rename, RenameIntent,
};
impl Command for RenameIntent {
type Output = Planned<Rename>;
fn run(self, cx: &mut Context<'_>) -> Result<Self::Output, EngineError> {
let intent = &self;
let graph = &mut *cx.graph;
let Evidence {
declarations,
occurrences,
ambiguous,
} = graph.references(&intent.references())?;
let default: Vec<Match> = occurrences
.iter()
.filter(|o| match o.confidence {
Confidence::Resolved => true,
Confidence::Unresolved => !ambiguous.contains(&o.m.language),
Confidence::Other => false,
})
.map(|o| o.m.clone())
.collect();
let chosen = if intent.selection.is_all() {
default
} else {
occurrences.iter().map(|o| o.m.clone()).collect()
};
let mut change = Change::new();
for m in intent.selection.narrow(chosen)? {
change.edit(&m.path, Edit::replace(m.span, &intent.to));
}
Planned::of(cx.workspace, change, |_, files| Rename {
intent: intent.clone(),
applied: false,
history_id: None,
declarations,
occurrences,
files,
})
}
}
impl Command for ReferencesQuery {
type Output = References;
fn run(self, cx: &mut Context<'_>) -> Result<Self::Output, EngineError> {
let query = &self;
let graph = &mut *cx.graph;
let evidence = graph.references(query)?;
Ok(References {
name: query.name.clone(),
declarations: evidence.declarations,
occurrences: evidence.occurrences,
})
}
}