aboutsummaryrefslogtreecommitdiff
path: root/crates/assists/src
diff options
context:
space:
mode:
authorDaiki Ihara <[email protected]>2020-12-17 13:09:55 +0000
committerDaiki Ihara <[email protected]>2020-12-21 06:55:40 +0000
commit7a2eebde22b1013b73706a11c4a89e969c36ed2b (patch)
treeee9e1e41fb6821745b9747bfa347500945251b09 /crates/assists/src
parent414576fb3059c8431f32a5bbe8fa117d4f3d83b7 (diff)
Add initial_contents field for CreateFile
Diffstat (limited to 'crates/assists/src')
-rw-r--r--crates/assists/src/assist_context.rs5
-rw-r--r--crates/assists/src/tests.rs52
2 files changed, 18 insertions, 39 deletions
diff --git a/crates/assists/src/assist_context.rs b/crates/assists/src/assist_context.rs
index cd22cf17d..80cf9aba1 100644
--- a/crates/assists/src/assist_context.rs
+++ b/crates/assists/src/assist_context.rs
@@ -285,10 +285,9 @@ impl AssistBuilder {
285 } 285 }
286 } 286 }
287 pub(crate) fn create_file(&mut self, dst: AnchoredPathBuf, content: impl Into<String>) { 287 pub(crate) fn create_file(&mut self, dst: AnchoredPathBuf, content: impl Into<String>) {
288 let file_system_edit = FileSystemEdit::CreateFile { dst: dst.clone() }; 288 let file_system_edit =
289 FileSystemEdit::CreateFile { dst: dst.clone(), initial_contents: content.into() };
289 self.file_system_edits.push(file_system_edit); 290 self.file_system_edits.push(file_system_edit);
290 self.edit_file(dst.anchor);
291 self.insert(TextSize::from(0), content)
292 } 291 }
293 292
294 fn finish(mut self) -> SourceChange { 293 fn finish(mut self) -> SourceChange {
diff --git a/crates/assists/src/tests.rs b/crates/assists/src/tests.rs
index 9002040ce..d18e566e6 100644
--- a/crates/assists/src/tests.rs
+++ b/crates/assists/src/tests.rs
@@ -107,47 +107,27 @@ fn check(handler: Handler, before: &str, expected: ExpectedResult, assist_label:
107 && source_change.file_system_edits.len() == 0; 107 && source_change.file_system_edits.len() == 0;
108 source_change.source_file_edits.sort_by_key(|it| it.file_id); 108 source_change.source_file_edits.sort_by_key(|it| it.file_id);
109 109
110 let mut created_file_ids = Vec::new();
111 let mut buf = String::new(); 110 let mut buf = String::new();
112 for file_system_edit in source_change.file_system_edits.clone() { 111 for source_file_edit in source_change.source_file_edits {
113 match file_system_edit { 112 let mut text = db.file_text(source_file_edit.file_id).as_ref().to_owned();
114 FileSystemEdit::CreateFile { dst } => { 113 source_file_edit.edit.apply(&mut text);
115 created_file_ids.push(dst.anchor); 114 if !skip_header {
116 } 115 let sr = db.file_source_root(source_file_edit.file_id);
117 _ => (), 116 let sr = db.source_root(sr);
117 let path = sr.path_for_file(&source_file_edit.file_id).unwrap();
118 format_to!(buf, "//- {}\n", path)
118 } 119 }
120 buf.push_str(&text);
119 } 121 }
120 122
121 for source_file_edit in source_change.source_file_edits { 123 for file_system_edit in source_change.file_system_edits.clone() {
122 if created_file_ids.contains(&source_file_edit.file_id) { 124 match file_system_edit {
123 let target_dst = source_change 125 FileSystemEdit::CreateFile { dst, initial_contents } => {
124 .file_system_edits 126 let target_dst = dst.path;
125 .iter() 127 format_to!(buf, "//- {}\n", target_dst);
126 .find_map(|f| match f { 128 buf.push_str(&initial_contents);
127 FileSystemEdit::CreateFile { dst } => {
128 if dst.anchor == source_file_edit.file_id {
129 Some(&dst.path)
130 } else {
131 None
132 }
133 }
134 _ => None,
135 })
136 .unwrap();
137 format_to!(buf, "//- {}\n", target_dst);
138 let mut text = String::new();
139 source_file_edit.edit.apply(&mut text);
140 buf.push_str(&text);
141 } else {
142 let mut text = db.file_text(source_file_edit.file_id).as_ref().to_owned();
143 source_file_edit.edit.apply(&mut text);
144 if !skip_header {
145 let sr = db.file_source_root(source_file_edit.file_id);
146 let sr = db.source_root(sr);
147 let path = sr.path_for_file(&source_file_edit.file_id).unwrap();
148 format_to!(buf, "//- {}\n", path)
149 } 129 }
150 buf.push_str(&text); 130 _ => (),
151 } 131 }
152 } 132 }
153 133