]> git.lizzy.rs Git - rust.git/blob - crates/ide_db/src/source_change.rs
Merge #11157
[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 `Change`.
5
6 use std::{collections::hash_map::Entry, iter};
7
8 use base_db::{AnchoredPathBuf, FileId};
9 use rustc_hash::FxHashMap;
10 use stdx::never;
11 use text_edit::TextEdit;
12
13 #[derive(Default, Debug, Clone)]
14 pub struct SourceChange {
15     pub source_file_edits: FxHashMap<FileId, TextEdit>,
16     pub file_system_edits: Vec<FileSystemEdit>,
17     pub is_snippet: bool,
18 }
19
20 impl SourceChange {
21     /// Creates a new SourceChange with the given label
22     /// from the edits.
23     pub fn from_edits(
24         source_file_edits: FxHashMap<FileId, TextEdit>,
25         file_system_edits: Vec<FileSystemEdit>,
26     ) -> Self {
27         SourceChange { source_file_edits, file_system_edits, is_snippet: false }
28     }
29
30     pub fn from_text_edit(file_id: FileId, edit: TextEdit) -> Self {
31         SourceChange {
32             source_file_edits: iter::once((file_id, edit)).collect(),
33             ..Default::default()
34         }
35     }
36
37     /// Inserts a [`TextEdit`] for the given [`FileId`]. This properly handles merging existing
38     /// edits for a file if some already exist.
39     pub fn insert_source_edit(&mut self, file_id: FileId, edit: TextEdit) {
40         match self.source_file_edits.entry(file_id) {
41             Entry::Occupied(mut entry) => {
42                 never!(entry.get_mut().union(edit).is_err(), "overlapping edits for same file");
43             }
44             Entry::Vacant(entry) => {
45                 entry.insert(edit);
46             }
47         }
48     }
49
50     pub fn push_file_system_edit(&mut self, edit: FileSystemEdit) {
51         self.file_system_edits.push(edit);
52     }
53
54     pub fn get_source_edit(&self, file_id: FileId) -> Option<&TextEdit> {
55         self.source_file_edits.get(&file_id)
56     }
57
58     pub fn merge(mut self, other: SourceChange) -> SourceChange {
59         self.extend(other.source_file_edits);
60         self.extend(other.file_system_edits);
61         self.is_snippet |= other.is_snippet;
62         self
63     }
64 }
65
66 impl Extend<(FileId, TextEdit)> for SourceChange {
67     fn extend<T: IntoIterator<Item = (FileId, TextEdit)>>(&mut self, iter: T) {
68         iter.into_iter().for_each(|(file_id, edit)| self.insert_source_edit(file_id, edit));
69     }
70 }
71
72 impl Extend<FileSystemEdit> for SourceChange {
73     fn extend<T: IntoIterator<Item = FileSystemEdit>>(&mut self, iter: T) {
74         iter.into_iter().for_each(|edit| self.push_file_system_edit(edit));
75     }
76 }
77
78 impl From<FxHashMap<FileId, TextEdit>> for SourceChange {
79     fn from(source_file_edits: FxHashMap<FileId, TextEdit>) -> SourceChange {
80         SourceChange { source_file_edits, file_system_edits: Vec::new(), is_snippet: false }
81     }
82 }
83
84 #[derive(Debug, Clone)]
85 pub enum FileSystemEdit {
86     CreateFile { dst: AnchoredPathBuf, initial_contents: String },
87     MoveFile { src: FileId, dst: AnchoredPathBuf },
88 }
89
90 impl From<FileSystemEdit> for SourceChange {
91     fn from(edit: FileSystemEdit) -> SourceChange {
92         SourceChange {
93             source_file_edits: Default::default(),
94             file_system_edits: vec![edit],
95             is_snippet: false,
96         }
97     }
98 }