aboutsummaryrefslogtreecommitdiff
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
parent414576fb3059c8431f32a5bbe8fa117d4f3d83b7 (diff)
Add initial_contents field for CreateFile
-rw-r--r--crates/assists/src/assist_context.rs5
-rw-r--r--crates/assists/src/tests.rs52
-rw-r--r--crates/ide/src/diagnostics/fixes.rs1
-rw-r--r--crates/ide_db/src/source_change.rs2
-rw-r--r--crates/rust-analyzer/src/to_proto.rs37
5 files changed, 47 insertions, 50 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
diff --git a/crates/ide/src/diagnostics/fixes.rs b/crates/ide/src/diagnostics/fixes.rs
index e8b896623..d79f5c170 100644
--- a/crates/ide/src/diagnostics/fixes.rs
+++ b/crates/ide/src/diagnostics/fixes.rs
@@ -40,6 +40,7 @@ impl DiagnosticWithFix for UnresolvedModule {
40 anchor: self.file.original_file(sema.db), 40 anchor: self.file.original_file(sema.db),
41 path: self.candidate.clone(), 41 path: self.candidate.clone(),
42 }, 42 },
43 initial_contents: "".to_string(),
43 } 44 }
44 .into(), 45 .into(),
45 unresolved_module.syntax().text_range(), 46 unresolved_module.syntax().text_range(),
diff --git a/crates/ide_db/src/source_change.rs b/crates/ide_db/src/source_change.rs
index e87d98dad..10c0abdac 100644
--- a/crates/ide_db/src/source_change.rs
+++ b/crates/ide_db/src/source_change.rs
@@ -44,7 +44,7 @@ impl From<Vec<SourceFileEdit>> for SourceChange {
44 44
45#[derive(Debug, Clone)] 45#[derive(Debug, Clone)]
46pub enum FileSystemEdit { 46pub enum FileSystemEdit {
47 CreateFile { dst: AnchoredPathBuf }, 47 CreateFile { dst: AnchoredPathBuf, initial_contents: String },
48 MoveFile { src: FileId, dst: AnchoredPathBuf }, 48 MoveFile { src: FileId, dst: AnchoredPathBuf },
49} 49}
50 50
diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs
index e0561b5a7..5a1ae96aa 100644
--- a/crates/rust-analyzer/src/to_proto.rs
+++ b/crates/rust-analyzer/src/to_proto.rs
@@ -634,30 +634,47 @@ pub(crate) fn snippet_text_document_edit(
634 Ok(lsp_ext::SnippetTextDocumentEdit { text_document, edits }) 634 Ok(lsp_ext::SnippetTextDocumentEdit { text_document, edits })
635} 635}
636 636
637pub(crate) fn resource_op( 637pub(crate) fn snippet_text_document_ops(
638 snap: &GlobalStateSnapshot, 638 snap: &GlobalStateSnapshot,
639 file_system_edit: FileSystemEdit, 639 file_system_edit: FileSystemEdit,
640) -> lsp_types::ResourceOp { 640) -> Vec<lsp_ext::SnippetDocumentChangeOperation> {
641 let mut ops = Vec::new();
641 match file_system_edit { 642 match file_system_edit {
642 FileSystemEdit::CreateFile { dst } => { 643 FileSystemEdit::CreateFile { dst, initial_contents } => {
643 let uri = snap.anchored_path(&dst); 644 let uri = snap.anchored_path(&dst);
644 lsp_types::ResourceOp::Create(lsp_types::CreateFile { 645 let create_file = lsp_types::ResourceOp::Create(lsp_types::CreateFile {
645 uri, 646 uri: uri.clone(),
646 options: None, 647 options: None,
647 annotation_id: None, 648 annotation_id: None,
648 }) 649 });
650 ops.push(lsp_ext::SnippetDocumentChangeOperation::Op(create_file));
651 if !initial_contents.is_empty() {
652 let text_document =
653 lsp_types::OptionalVersionedTextDocumentIdentifier { uri, version: None };
654 let range = range(&LineIndex::new(""), TextRange::empty(TextSize::from(0)));
655 let text_edit = lsp_ext::SnippetTextEdit {
656 range,
657 new_text: initial_contents,
658 insert_text_format: Some(lsp_types::InsertTextFormat::PlainText),
659 };
660 let edit_file =
661 lsp_ext::SnippetTextDocumentEdit { text_document, edits: vec![text_edit] };
662 ops.push(lsp_ext::SnippetDocumentChangeOperation::Edit(edit_file));
663 }
649 } 664 }
650 FileSystemEdit::MoveFile { src, dst } => { 665 FileSystemEdit::MoveFile { src, dst } => {
651 let old_uri = snap.file_id_to_url(src); 666 let old_uri = snap.file_id_to_url(src);
652 let new_uri = snap.anchored_path(&dst); 667 let new_uri = snap.anchored_path(&dst);
653 lsp_types::ResourceOp::Rename(lsp_types::RenameFile { 668 let rename_file = lsp_types::ResourceOp::Rename(lsp_types::RenameFile {
654 old_uri, 669 old_uri,
655 new_uri, 670 new_uri,
656 options: None, 671 options: None,
657 annotation_id: None, 672 annotation_id: None,
658 }) 673 });
674 ops.push(lsp_ext::SnippetDocumentChangeOperation::Op(rename_file))
659 } 675 }
660 } 676 }
677 ops
661} 678}
662 679
663pub(crate) fn snippet_workspace_edit( 680pub(crate) fn snippet_workspace_edit(
@@ -666,8 +683,8 @@ pub(crate) fn snippet_workspace_edit(
666) -> Result<lsp_ext::SnippetWorkspaceEdit> { 683) -> Result<lsp_ext::SnippetWorkspaceEdit> {
667 let mut document_changes: Vec<lsp_ext::SnippetDocumentChangeOperation> = Vec::new(); 684 let mut document_changes: Vec<lsp_ext::SnippetDocumentChangeOperation> = Vec::new();
668 for op in source_change.file_system_edits { 685 for op in source_change.file_system_edits {
669 let op = resource_op(&snap, op); 686 let ops = snippet_text_document_ops(snap, op);
670 document_changes.push(lsp_ext::SnippetDocumentChangeOperation::Op(op)); 687 document_changes.extend_from_slice(&ops);
671 } 688 }
672 for edit in source_change.source_file_edits { 689 for edit in source_change.source_file_edits {
673 let edit = snippet_text_document_edit(&snap, source_change.is_snippet, edit)?; 690 let edit = snippet_text_document_edit(&snap, source_change.is_snippet, edit)?;