Skip to main content

vvv_engine/
rename.rs

1//! `rename` and `references`: what both commands gather is the graph's
2//! [`Evidence`](crate::graph::Evidence) for a name; `rename` then plans an
3//! edit per chosen occurrence.
4
5use 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
14/// Plan a rename. Nothing is written; see [`Apply`](crate::Apply).
15///
16/// Occurrences are gathered only from the languages in which a matching
17/// declaration exists, so a Rust `foo` never touches a TypeScript `foo`.
18impl 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        // What `Selection::All` means: never an occurrence of another
31        // declaration; unresolved ones only when nothing else could be meant.
32        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
61/// The declarations called `name` and every token spelling it, judged:
62/// what `rename` acts on, answered without a plan.
63impl 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}