diff options
Diffstat (limited to 'crates/ra_ide_db/src/source_change.rs')
-rw-r--r-- | crates/ra_ide_db/src/source_change.rs | 101 |
1 files changed, 22 insertions, 79 deletions
diff --git a/crates/ra_ide_db/src/source_change.rs b/crates/ra_ide_db/src/source_change.rs index c64165f3a..e713f4b7e 100644 --- a/crates/ra_ide_db/src/source_change.rs +++ b/crates/ra_ide_db/src/source_change.rs | |||
@@ -3,94 +3,35 @@ | |||
3 | //! | 3 | //! |
4 | //! It can be viewed as a dual for `AnalysisChange`. | 4 | //! It can be viewed as a dual for `AnalysisChange`. |
5 | 5 | ||
6 | use ra_db::{FileId, FilePosition, RelativePathBuf, SourceRootId}; | 6 | use ra_db::{FileId, RelativePathBuf, SourceRootId}; |
7 | use ra_text_edit::{TextEdit, TextSize}; | 7 | use ra_text_edit::TextEdit; |
8 | 8 | ||
9 | #[derive(Debug, Clone)] | 9 | #[derive(Debug, Clone)] |
10 | pub struct SourceChange { | 10 | pub struct SourceChange { |
11 | /// For display in the undo log in the editor | ||
12 | pub label: String, | ||
13 | pub source_file_edits: Vec<SourceFileEdit>, | 11 | pub source_file_edits: Vec<SourceFileEdit>, |
14 | pub file_system_edits: Vec<FileSystemEdit>, | 12 | pub file_system_edits: Vec<FileSystemEdit>, |
15 | pub cursor_position: Option<FilePosition>, | ||
16 | pub is_snippet: bool, | 13 | pub is_snippet: bool, |
17 | } | 14 | } |
18 | 15 | ||
19 | impl SourceChange { | 16 | impl SourceChange { |
20 | /// Creates a new SourceChange with the given label | 17 | /// Creates a new SourceChange with the given label |
21 | /// from the edits. | 18 | /// from the edits. |
22 | pub fn from_edits<L: Into<String>>( | 19 | pub fn from_edits( |
23 | label: L, | ||
24 | source_file_edits: Vec<SourceFileEdit>, | 20 | source_file_edits: Vec<SourceFileEdit>, |
25 | file_system_edits: Vec<FileSystemEdit>, | 21 | file_system_edits: Vec<FileSystemEdit>, |
26 | ) -> Self { | 22 | ) -> Self { |
27 | SourceChange { | 23 | SourceChange { source_file_edits, file_system_edits, is_snippet: false } |
28 | label: label.into(), | ||
29 | source_file_edits, | ||
30 | file_system_edits, | ||
31 | cursor_position: None, | ||
32 | is_snippet: false, | ||
33 | } | ||
34 | } | 24 | } |
35 | 25 | ||
36 | /// Creates a new SourceChange with the given label, | 26 | /// Creates a new SourceChange with the given label, |
37 | /// containing only the given `SourceFileEdits`. | 27 | /// containing only the given `SourceFileEdits`. |
38 | pub fn source_file_edits<L: Into<String>>(label: L, edits: Vec<SourceFileEdit>) -> Self { | 28 | pub fn source_file_edits(edits: Vec<SourceFileEdit>) -> Self { |
39 | let label = label.into(); | 29 | SourceChange { source_file_edits: edits, file_system_edits: vec![], is_snippet: false } |
40 | assert!(label.starts_with(char::is_uppercase)); | ||
41 | SourceChange { | ||
42 | label: label, | ||
43 | source_file_edits: edits, | ||
44 | file_system_edits: vec![], | ||
45 | cursor_position: None, | ||
46 | is_snippet: false, | ||
47 | } | ||
48 | } | ||
49 | |||
50 | /// Creates a new SourceChange with the given label, | ||
51 | /// containing only the given `FileSystemEdits`. | ||
52 | pub(crate) fn file_system_edits<L: Into<String>>(label: L, edits: Vec<FileSystemEdit>) -> Self { | ||
53 | SourceChange { | ||
54 | label: label.into(), | ||
55 | source_file_edits: vec![], | ||
56 | file_system_edits: edits, | ||
57 | cursor_position: None, | ||
58 | is_snippet: false, | ||
59 | } | ||
60 | } | ||
61 | |||
62 | /// Creates a new SourceChange with the given label, | ||
63 | /// containing only a single `SourceFileEdit`. | ||
64 | pub fn source_file_edit<L: Into<String>>(label: L, edit: SourceFileEdit) -> Self { | ||
65 | SourceChange::source_file_edits(label, vec![edit]) | ||
66 | } | ||
67 | |||
68 | /// Creates a new SourceChange with the given label | ||
69 | /// from the given `FileId` and `TextEdit` | ||
70 | pub fn source_file_edit_from<L: Into<String>>( | ||
71 | label: L, | ||
72 | file_id: FileId, | ||
73 | edit: TextEdit, | ||
74 | ) -> Self { | ||
75 | SourceChange::source_file_edit(label, SourceFileEdit { file_id, edit }) | ||
76 | } | 30 | } |
77 | |||
78 | /// Creates a new SourceChange with the given label | 31 | /// Creates a new SourceChange with the given label |
79 | /// from the given `FileId` and `TextEdit` | 32 | /// from the given `FileId` and `TextEdit` |
80 | pub fn file_system_edit<L: Into<String>>(label: L, edit: FileSystemEdit) -> Self { | 33 | pub fn source_file_edit_from(file_id: FileId, edit: TextEdit) -> Self { |
81 | SourceChange::file_system_edits(label, vec![edit]) | 34 | SourceFileEdit { file_id, edit }.into() |
82 | } | ||
83 | |||
84 | /// Sets the cursor position to the given `FilePosition` | ||
85 | pub fn with_cursor(mut self, cursor_position: FilePosition) -> Self { | ||
86 | self.cursor_position = Some(cursor_position); | ||
87 | self | ||
88 | } | ||
89 | |||
90 | /// Sets the cursor position to the given `FilePosition` | ||
91 | pub fn with_cursor_opt(mut self, cursor_position: Option<FilePosition>) -> Self { | ||
92 | self.cursor_position = cursor_position; | ||
93 | self | ||
94 | } | 35 | } |
95 | } | 36 | } |
96 | 37 | ||
@@ -100,25 +41,27 @@ pub struct SourceFileEdit { | |||
100 | pub edit: TextEdit, | 41 | pub edit: TextEdit, |
101 | } | 42 | } |
102 | 43 | ||
44 | impl From<SourceFileEdit> for SourceChange { | ||
45 | fn from(edit: SourceFileEdit) -> SourceChange { | ||
46 | SourceChange { | ||
47 | source_file_edits: vec![edit], | ||
48 | file_system_edits: Vec::new(), | ||
49 | is_snippet: false, | ||
50 | } | ||
51 | } | ||
52 | } | ||
53 | |||
103 | #[derive(Debug, Clone)] | 54 | #[derive(Debug, Clone)] |
104 | pub enum FileSystemEdit { | 55 | pub enum FileSystemEdit { |
105 | CreateFile { source_root: SourceRootId, path: RelativePathBuf }, | 56 | CreateFile { source_root: SourceRootId, path: RelativePathBuf }, |
106 | MoveFile { src: FileId, dst_source_root: SourceRootId, dst_path: RelativePathBuf }, | 57 | MoveFile { src: FileId, dst_source_root: SourceRootId, dst_path: RelativePathBuf }, |
107 | } | 58 | } |
108 | 59 | ||
109 | pub struct SingleFileChange { | 60 | impl From<FileSystemEdit> for SourceChange { |
110 | pub label: String, | 61 | fn from(edit: FileSystemEdit) -> SourceChange { |
111 | pub edit: TextEdit, | ||
112 | pub cursor_position: Option<TextSize>, | ||
113 | } | ||
114 | |||
115 | impl SingleFileChange { | ||
116 | pub fn into_source_change(self, file_id: FileId) -> SourceChange { | ||
117 | SourceChange { | 62 | SourceChange { |
118 | label: self.label, | 63 | source_file_edits: Vec::new(), |
119 | source_file_edits: vec![SourceFileEdit { file_id, edit: self.edit }], | 64 | file_system_edits: vec![edit], |
120 | file_system_edits: Vec::new(), | ||
121 | cursor_position: self.cursor_position.map(|offset| FilePosition { file_id, offset }), | ||
122 | is_snippet: false, | 65 | is_snippet: false, |
123 | } | 66 | } |
124 | } | 67 | } |