1use vvv_core::Edit;
6
7use crate::change::Change;
8use crate::command::{Command, Context};
9use crate::graph::Evidence;
10use crate::{
11 Confidence, EngineError, Match, Planned, References, ReferencesQuery, Rename, RenameIntent,
12};
13
14impl Command for RenameIntent {
19 type Output = Planned<Rename>;
20
21 fn run(self, cx: &mut Context<'_>) -> Result<Self::Output, EngineError> {
22 let intent = &self;
23 let graph = &mut *cx.graph;
24 let Evidence {
25 declarations,
26 occurrences,
27 ambiguous,
28 } = graph.references(&intent.references())?;
29
30 let default: Vec<Match> = occurrences
33 .iter()
34 .filter(|o| match o.confidence {
35 Confidence::Resolved => true,
36 Confidence::Unresolved => !ambiguous.contains(&o.m.language),
37 Confidence::Other => false,
38 })
39 .map(|o| o.m.clone())
40 .collect();
41 let chosen = if intent.selection.is_all() {
42 default
43 } else {
44 occurrences.iter().map(|o| o.m.clone()).collect()
45 };
46 let mut change = Change::new();
47 for m in intent.selection.narrow(chosen)? {
48 change.edit(&m.path, Edit::replace(m.span, &intent.to));
49 }
50 Planned::of(cx.workspace, change, |_, files| Rename {
51 intent: intent.clone(),
52 applied: false,
53 history_id: None,
54 declarations,
55 occurrences,
56 files,
57 })
58 }
59}
60
61impl Command for ReferencesQuery {
64 type Output = References;
65
66 fn run(self, cx: &mut Context<'_>) -> Result<Self::Output, EngineError> {
67 let query = &self;
68 let graph = &mut *cx.graph;
69 let evidence = graph.references(query)?;
70 Ok(References {
71 name: query.name.clone(),
72 declarations: evidence.declarations,
73 occurrences: evidence.occurrences,
74 })
75 }
76}