hir_expand/
change.rs

1//! Defines a unit of change that can applied to the database to get the next
2//! state. Changes are transactional.
3use base_db::{CrateGraphBuilder, FileChange, SourceRoot, salsa::Durability};
4use span::FileId;
5use triomphe::Arc;
6
7use crate::{db::ExpandDatabase, proc_macro::ProcMacrosBuilder};
8
9#[derive(Debug, Default)]
10pub struct ChangeWithProcMacros {
11    pub source_change: FileChange,
12    pub proc_macros: Option<ProcMacrosBuilder>,
13}
14
15impl ChangeWithProcMacros {
16    pub fn apply(self, db: &mut impl ExpandDatabase) {
17        let crates_id_map = self.source_change.apply(db);
18        if let Some(proc_macros) = self.proc_macros {
19            let proc_macros = proc_macros.build(
20                crates_id_map
21                    .as_ref()
22                    .expect("cannot set proc macros without setting the crate graph too"),
23            );
24            db.set_proc_macros_with_durability(Arc::new(proc_macros), Durability::HIGH);
25        }
26    }
27
28    pub fn change_file(&mut self, file_id: FileId, new_text: Option<String>) {
29        self.source_change.change_file(file_id, new_text)
30    }
31
32    pub fn set_crate_graph(&mut self, graph: CrateGraphBuilder) {
33        self.source_change.set_crate_graph(graph);
34    }
35
36    pub fn set_proc_macros(&mut self, proc_macros: ProcMacrosBuilder) {
37        self.proc_macros = Some(proc_macros);
38    }
39
40    pub fn set_roots(&mut self, roots: Vec<SourceRoot>) {
41        self.source_change.set_roots(roots)
42    }
43}