aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_db/src/source_change.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide_db/src/source_change.rs')
-rw-r--r--crates/ide_db/src/source_change.rs56
1 files changed, 43 insertions, 13 deletions
diff --git a/crates/ide_db/src/source_change.rs b/crates/ide_db/src/source_change.rs
index 10c0abdac..b1f87731b 100644
--- a/crates/ide_db/src/source_change.rs
+++ b/crates/ide_db/src/source_change.rs
@@ -3,12 +3,19 @@
3//! 3//!
4//! It can be viewed as a dual for `AnalysisChange`. 4//! It can be viewed as a dual for `AnalysisChange`.
5 5
6use std::{
7 collections::hash_map::Entry,
8 iter::{self, FromIterator},
9};
10
6use base_db::{AnchoredPathBuf, FileId}; 11use base_db::{AnchoredPathBuf, FileId};
12use rustc_hash::FxHashMap;
13use stdx::assert_never;
7use text_edit::TextEdit; 14use text_edit::TextEdit;
8 15
9#[derive(Default, Debug, Clone)] 16#[derive(Default, Debug, Clone)]
10pub struct SourceChange { 17pub struct SourceChange {
11 pub source_file_edits: Vec<SourceFileEdit>, 18 pub source_file_edits: FxHashMap<FileId, TextEdit>,
12 pub file_system_edits: Vec<FileSystemEdit>, 19 pub file_system_edits: Vec<FileSystemEdit>,
13 pub is_snippet: bool, 20 pub is_snippet: bool,
14} 21}
@@ -17,27 +24,50 @@ impl SourceChange {
17 /// Creates a new SourceChange with the given label 24 /// Creates a new SourceChange with the given label
18 /// from the edits. 25 /// from the edits.
19 pub fn from_edits( 26 pub fn from_edits(
20 source_file_edits: Vec<SourceFileEdit>, 27 source_file_edits: FxHashMap<FileId, TextEdit>,
21 file_system_edits: Vec<FileSystemEdit>, 28 file_system_edits: Vec<FileSystemEdit>,
22 ) -> Self { 29 ) -> Self {
23 SourceChange { source_file_edits, file_system_edits, is_snippet: false } 30 SourceChange { source_file_edits, file_system_edits, is_snippet: false }
24 } 31 }
25}
26 32
27#[derive(Debug, Clone)] 33 pub fn from_text_edit(file_id: FileId, edit: TextEdit) -> Self {
28pub struct SourceFileEdit { 34 SourceChange {
29 pub file_id: FileId, 35 source_file_edits: FxHashMap::from_iter(iter::once((file_id, edit))),
30 pub edit: TextEdit, 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 }
31} 61}
32 62
33impl From<SourceFileEdit> for SourceChange { 63impl Extend<(FileId, TextEdit)> for SourceChange {
34 fn from(edit: SourceFileEdit) -> SourceChange { 64 fn extend<T: IntoIterator<Item = (FileId, TextEdit)>>(&mut self, iter: T) {
35 vec![edit].into() 65 iter.into_iter().for_each(|(file_id, edit)| self.insert_source_edit(file_id, edit));
36 } 66 }
37} 67}
38 68
39impl From<Vec<SourceFileEdit>> for SourceChange { 69impl From<FxHashMap<FileId, TextEdit>> for SourceChange {
40 fn from(source_file_edits: Vec<SourceFileEdit>) -> SourceChange { 70 fn from(source_file_edits: FxHashMap<FileId, TextEdit>) -> SourceChange {
41 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 }
42 } 72 }
43} 73}
@@ -51,7 +81,7 @@ pub enum FileSystemEdit {
51impl From<FileSystemEdit> for SourceChange { 81impl From<FileSystemEdit> for SourceChange {
52 fn from(edit: FileSystemEdit) -> SourceChange { 82 fn from(edit: FileSystemEdit) -> SourceChange {
53 SourceChange { 83 SourceChange {
54 source_file_edits: Vec::new(), 84 source_file_edits: Default::default(),
55 file_system_edits: vec![edit], 85 file_system_edits: vec![edit],
56 is_snippet: false, 86 is_snippet: false,
57 } 87 }