Skip to main content

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, SourceDatabase, SourceRoot};
4use span::FileId;
5
6use crate::proc_macro::ProcMacrosBuilder;
7
8#[derive(Debug, Default)]
9pub struct ChangeWithProcMacros {
10    pub source_change: FileChange,
11    pub proc_macros: Option<ProcMacrosBuilder>,
12}
13
14impl ChangeWithProcMacros {
15    pub fn apply(self, db: &mut impl SourceDatabase) {
16        let crates_id_map = self.source_change.apply(db);
17        if let Some(proc_macros) = self.proc_macros {
18            proc_macros.build_in(
19                db,
20                crates_id_map
21                    .as_ref()
22                    .expect("cannot set proc macros without setting the crate graph too"),
23            );
24        }
25    }
26
27    pub fn change_file(&mut self, file_id: FileId, new_text: Option<String>) {
28        self.source_change.change_file(file_id, new_text)
29    }
30
31    pub fn set_crate_graph(&mut self, graph: CrateGraphBuilder) {
32        self.source_change.set_crate_graph(graph);
33    }
34
35    pub fn set_proc_macros(&mut self, proc_macros: ProcMacrosBuilder) {
36        self.proc_macros = Some(proc_macros);
37    }
38
39    pub fn set_roots(&mut self, roots: Vec<SourceRoot>) {
40        self.source_change.set_roots(roots)
41    }
42}