diff options
author | Lukas Wirth <[email protected]> | 2021-01-14 21:43:36 +0000 |
---|---|---|
committer | Lukas Wirth <[email protected]> | 2021-01-14 21:43:36 +0000 |
commit | d5095329a1c12e93653d8de4a93f0b4f5cad4c6e (patch) | |
tree | 1de73ddefe48cc9f82cb4f063eaddc069adf83bc /crates/ide_db/src | |
parent | e23bfafb32a235fdb60ba279ea68b5aa381c2110 (diff) |
Phase out SourceFileEdits in favour of a plain HashMap
Diffstat (limited to 'crates/ide_db/src')
-rw-r--r-- | crates/ide_db/src/source_change.rs | 50 |
1 files changed, 25 insertions, 25 deletions
diff --git a/crates/ide_db/src/source_change.rs b/crates/ide_db/src/source_change.rs index 516d7859a..b1f87731b 100644 --- a/crates/ide_db/src/source_change.rs +++ b/crates/ide_db/src/source_change.rs | |||
@@ -10,11 +10,12 @@ use std::{ | |||
10 | 10 | ||
11 | use base_db::{AnchoredPathBuf, FileId}; | 11 | use base_db::{AnchoredPathBuf, FileId}; |
12 | use rustc_hash::FxHashMap; | 12 | use rustc_hash::FxHashMap; |
13 | use stdx::assert_never; | ||
13 | use text_edit::TextEdit; | 14 | use text_edit::TextEdit; |
14 | 15 | ||
15 | #[derive(Default, Debug, Clone)] | 16 | #[derive(Default, Debug, Clone)] |
16 | pub struct SourceChange { | 17 | pub struct SourceChange { |
17 | pub source_file_edits: SourceFileEdits, | 18 | pub source_file_edits: FxHashMap<FileId, TextEdit>, |
18 | pub file_system_edits: Vec<FileSystemEdit>, | 19 | pub file_system_edits: Vec<FileSystemEdit>, |
19 | pub is_snippet: bool, | 20 | pub is_snippet: bool, |
20 | } | 21 | } |
@@ -23,51 +24,50 @@ impl SourceChange { | |||
23 | /// Creates a new SourceChange with the given label | 24 | /// Creates a new SourceChange with the given label |
24 | /// from the edits. | 25 | /// from the edits. |
25 | pub fn from_edits( | 26 | pub fn from_edits( |
26 | source_file_edits: SourceFileEdits, | 27 | source_file_edits: FxHashMap<FileId, TextEdit>, |
27 | file_system_edits: Vec<FileSystemEdit>, | 28 | file_system_edits: Vec<FileSystemEdit>, |
28 | ) -> Self { | 29 | ) -> Self { |
29 | SourceChange { source_file_edits, file_system_edits, is_snippet: false } | 30 | SourceChange { source_file_edits, file_system_edits, is_snippet: false } |
30 | } | 31 | } |
31 | } | ||
32 | |||
33 | #[derive(Default, Debug, Clone)] | ||
34 | pub struct SourceFileEdits { | ||
35 | pub edits: FxHashMap<FileId, TextEdit>, | ||
36 | } | ||
37 | 32 | ||
38 | impl SourceFileEdits { | ||
39 | pub fn from_text_edit(file_id: FileId, edit: TextEdit) -> Self { | 33 | pub fn from_text_edit(file_id: FileId, edit: TextEdit) -> Self { |
40 | SourceFileEdits { edits: FxHashMap::from_iter(iter::once((file_id, edit))) } | 34 | SourceChange { |
41 | } | 35 | source_file_edits: FxHashMap::from_iter(iter::once((file_id, edit))), |
42 | 36 | ..Default::default() | |
43 | pub fn len(&self) -> usize { | 37 | } |
44 | self.edits.len() | ||
45 | } | ||
46 | |||
47 | pub fn is_empty(&self) -> bool { | ||
48 | self.edits.is_empty() | ||
49 | } | 38 | } |
50 | 39 | ||
51 | pub fn insert(&mut self, file_id: FileId, edit: TextEdit) { | 40 | pub fn insert_source_edit(&mut self, file_id: FileId, edit: TextEdit) { |
52 | match self.edits.entry(file_id) { | 41 | match self.source_file_edits.entry(file_id) { |
53 | Entry::Occupied(mut entry) => { | 42 | Entry::Occupied(mut entry) => { |
54 | entry.get_mut().union(edit).expect("overlapping edits for same file"); | 43 | assert_never!( |
44 | entry.get_mut().union(edit).is_err(), | ||
45 | "overlapping edits for same file" | ||
46 | ); | ||
55 | } | 47 | } |
56 | Entry::Vacant(entry) => { | 48 | Entry::Vacant(entry) => { |
57 | entry.insert(edit); | 49 | entry.insert(edit); |
58 | } | 50 | } |
59 | } | 51 | } |
60 | } | 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 | } | 61 | } |
62 | 62 | ||
63 | impl Extend<(FileId, TextEdit)> for SourceFileEdits { | 63 | impl Extend<(FileId, TextEdit)> for SourceChange { |
64 | fn extend<T: IntoIterator<Item = (FileId, TextEdit)>>(&mut self, iter: T) { | 64 | fn extend<T: IntoIterator<Item = (FileId, TextEdit)>>(&mut self, iter: T) { |
65 | iter.into_iter().for_each(|(file_id, edit)| self.insert(file_id, edit)); | 65 | iter.into_iter().for_each(|(file_id, edit)| self.insert_source_edit(file_id, edit)); |
66 | } | 66 | } |
67 | } | 67 | } |
68 | 68 | ||
69 | impl From<SourceFileEdits> for SourceChange { | 69 | impl From<FxHashMap<FileId, TextEdit>> for SourceChange { |
70 | fn from(source_file_edits: SourceFileEdits) -> SourceChange { | 70 | fn from(source_file_edits: FxHashMap<FileId, TextEdit>) -> SourceChange { |
71 | SourceChange { source_file_edits, file_system_edits: Vec::new(), is_snippet: false } | 71 | SourceChange { source_file_edits, file_system_edits: Vec::new(), is_snippet: false } |
72 | } | 72 | } |
73 | } | 73 | } |