]> git.lizzy.rs Git - rust.git/blob - crates/ide_db/src/source_change.rs
b1f87731bfdf4389b38d3d6eb27b9f6e5288d83f
[rust.git] / crates / ide_db / src / source_change.rs
1 //! This modules defines type to represent changes to the source code, that flow
2 //! from the server to the client.
3 //!
4 //! It can be viewed as a dual for `AnalysisChange`.
5
6 use std::{
7     collections::hash_map::Entry,
8     iter::{self, FromIterator},
9 };
10
11 use base_db::{AnchoredPathBuf, FileId};
12 use rustc_hash::FxHashMap;
13 use stdx::assert_never;
14 use text_edit::TextEdit;
15
16 #[derive(Default, Debug, Clone)]
17 pub struct SourceChange {
18     pub source_file_edits: FxHashMap<FileId, TextEdit>,
19     pub file_system_edits: Vec<FileSystemEdit>,
20     pub is_snippet: bool,
21 }
22
23 impl SourceChange {
24     /// Creates a new SourceChange with the given label
25     /// from the edits.
26     pub fn from_edits(
27         source_file_edits: FxHashMap<FileId, TextEdit>,
28         file_system_edits: Vec<FileSystemEdit>,
29     ) -> Self {
30         SourceChange { source_file_edits, file_system_edits, is_snippet: false }
31     }
32
33     pub fn from_text_edit(file_id: FileId, edit: TextEdit) -> Self {
34         SourceChange {
35             source_file_edits: FxHashMap::from_iter(iter::once((file_id, edit))),
36             ..Default::default()
37         }
38     }
39
40     pub fn insert_source_edit(&mut self, file_id: FileId, edit: TextEdit) {
41         match self.source_file_edits.entry(file_id) {
42             Entry::Occupied(mut entry) => {
43                 assert_never!(
44                     entry.get_mut().union(edit).is_err(),
45                     "overlapping edits for same file"
46                 );
47             }
48             Entry::Vacant(entry) => {
49                 entry.insert(edit);
50             }
51         }
52     }
53
54     pub fn push_file_system_edit(&mut self, edit: FileSystemEdit) {
55         self.file_system_edits.push(edit);
56     }
57
58     pub fn get_source_edit(&self, file_id: FileId) -> Option<&TextEdit> {
59         self.source_file_edits.get(&file_id)
60     }
61 }
62
63 impl Extend<(FileId, TextEdit)> for SourceChange {
64     fn extend<T: IntoIterator<Item = (FileId, TextEdit)>>(&mut self, iter: T) {
65         iter.into_iter().for_each(|(file_id, edit)| self.insert_source_edit(file_id, edit));
66     }
67 }
68
69 impl From<FxHashMap<FileId, TextEdit>> for SourceChange {
70     fn from(source_file_edits: FxHashMap<FileId, TextEdit>) -> SourceChange {
71         SourceChange { source_file_edits, file_system_edits: Vec::new(), is_snippet: false }
72     }
73 }
74
75 #[derive(Debug, Clone)]
76 pub enum FileSystemEdit {
77     CreateFile { dst: AnchoredPathBuf, initial_contents: String },
78     MoveFile { src: FileId, dst: AnchoredPathBuf },
79 }
80
81 impl From<FileSystemEdit> for SourceChange {
82     fn from(edit: FileSystemEdit) -> SourceChange {
83         SourceChange {
84             source_file_edits: Default::default(),
85             file_system_edits: vec![edit],
86             is_snippet: false,
87         }
88     }
89 }