aboutsummaryrefslogtreecommitdiff
path: root/crates/rust-analyzer/src/to_proto.rs
diff options
context:
space:
mode:
authorLukas Wirth <[email protected]>2021-04-18 23:14:21 +0100
committerLukas Wirth <[email protected]>2021-04-18 23:14:55 +0100
commit493aaa140325f3b8fa40de3de58b34e4b96c5d13 (patch)
treec8004af23670749b8ccc8877116ba8d8bde56467 /crates/rust-analyzer/src/to_proto.rs
parentb501b59eda3cd1f6641c3887212ee3a6bdb97f6b (diff)
Better visualise control flow for change_annotation_support"
Diffstat (limited to 'crates/rust-analyzer/src/to_proto.rs')
-rw-r--r--crates/rust-analyzer/src/to_proto.rs97
1 files changed, 46 insertions, 51 deletions
diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs
index 13de11df1..fe4d0733d 100644
--- a/crates/rust-analyzer/src/to_proto.rs
+++ b/crates/rust-analyzer/src/to_proto.rs
@@ -1,16 +1,16 @@
1//! Conversion of rust-analyzer specific types to lsp_types equivalents. 1//! Conversion of rust-analyzer specific types to lsp_types equivalents.
2use std::{ 2use std::{
3 collections::HashMap, 3 iter::once,
4 path::{self, Path}, 4 path::{self, Path},
5 sync::atomic::{AtomicU32, Ordering}, 5 sync::atomic::{AtomicU32, Ordering},
6}; 6};
7 7
8use ide::{ 8use ide::{
9 Annotation, AnnotationKind, Assist, AssistKind, CallInfo, CompletionItem, CompletionItemKind, 9 Annotation, AnnotationKind, Assist, AssistKind, CallInfo, Cancelable, CompletionItem,
10 CompletionRelevance, Documentation, FileId, FileRange, FileSystemEdit, Fold, FoldKind, 10 CompletionItemKind, CompletionRelevance, Documentation, FileId, FileRange, FileSystemEdit,
11 Highlight, HlMod, HlOperator, HlPunct, HlRange, HlTag, Indel, InlayHint, InlayKind, 11 Fold, FoldKind, Highlight, HlMod, HlOperator, HlPunct, HlRange, HlTag, Indel, InlayHint,
12 InsertTextFormat, Markup, NavigationTarget, ReferenceAccess, RenameError, Runnable, Severity, 12 InlayKind, InsertTextFormat, Markup, NavigationTarget, ReferenceAccess, RenameError, Runnable,
13 SourceChange, StructureNodeKind, SymbolKind, TextEdit, TextRange, TextSize, 13 Severity, SourceChange, StructureNodeKind, SymbolKind, TextEdit, TextRange, TextSize,
14}; 14};
15use itertools::Itertools; 15use itertools::Itertools;
16use serde_json::to_value; 16use serde_json::to_value;
@@ -690,8 +690,8 @@ pub(crate) fn goto_definition_response(
690 } 690 }
691} 691}
692 692
693fn outside_workspace_annotation(snap: &GlobalStateSnapshot) -> Option<String> { 693fn outside_workspace_annotation_id() -> String {
694 snap.config.change_annotation_support().then(|| String::from("OutsideWorkspace")) 694 String::from("OutsideWorkspace")
695} 695}
696 696
697pub(crate) fn snippet_text_document_edit( 697pub(crate) fn snippet_text_document_edit(
@@ -702,26 +702,21 @@ pub(crate) fn snippet_text_document_edit(
702) -> Result<lsp_ext::SnippetTextDocumentEdit> { 702) -> Result<lsp_ext::SnippetTextDocumentEdit> {
703 let text_document = optional_versioned_text_document_identifier(snap, file_id); 703 let text_document = optional_versioned_text_document_identifier(snap, file_id);
704 let line_index = snap.file_line_index(file_id)?; 704 let line_index = snap.file_line_index(file_id)?;
705 let outside_workspace_annotation = snap 705 let mut edits: Vec<_> =
706 .analysis 706 edit.into_iter().map(|it| snippet_text_edit(&line_index, is_snippet, it)).collect();
707 .is_library_file(file_id)? 707
708 .then(|| outside_workspace_annotation(snap)) 708 if snap.analysis.is_library_file(file_id)? && snap.config.change_annotation_support() {
709 .flatten(); 709 for edit in &mut edits {
710 let edits = edit 710 edit.annotation_id = Some(outside_workspace_annotation_id())
711 .into_iter() 711 }
712 .map(|it| { 712 }
713 let mut edit = snippet_text_edit(&line_index, is_snippet, it);
714 edit.annotation_id = outside_workspace_annotation.clone();
715 edit
716 })
717 .collect();
718 Ok(lsp_ext::SnippetTextDocumentEdit { text_document, edits }) 713 Ok(lsp_ext::SnippetTextDocumentEdit { text_document, edits })
719} 714}
720 715
721pub(crate) fn snippet_text_document_ops( 716pub(crate) fn snippet_text_document_ops(
722 snap: &GlobalStateSnapshot, 717 snap: &GlobalStateSnapshot,
723 file_system_edit: FileSystemEdit, 718 file_system_edit: FileSystemEdit,
724) -> Vec<lsp_ext::SnippetDocumentChangeOperation> { 719) -> Cancelable<Vec<lsp_ext::SnippetDocumentChangeOperation>> {
725 let mut ops = Vec::new(); 720 let mut ops = Vec::new();
726 match file_system_edit { 721 match file_system_edit {
727 FileSystemEdit::CreateFile { dst, initial_contents } => { 722 FileSystemEdit::CreateFile { dst, initial_contents } => {
@@ -749,21 +744,19 @@ pub(crate) fn snippet_text_document_ops(
749 FileSystemEdit::MoveFile { src, dst } => { 744 FileSystemEdit::MoveFile { src, dst } => {
750 let old_uri = snap.file_id_to_url(src); 745 let old_uri = snap.file_id_to_url(src);
751 let new_uri = snap.anchored_path(&dst); 746 let new_uri = snap.anchored_path(&dst);
752 let rename_file = lsp_types::ResourceOp::Rename(lsp_types::RenameFile { 747 let mut rename_file =
753 old_uri, 748 lsp_types::RenameFile { old_uri, new_uri, options: None, annotation_id: None };
754 new_uri, 749 if snap.analysis.is_library_file(src) == Ok(true)
755 options: None, 750 && snap.config.change_annotation_support()
756 annotation_id: snap 751 {
757 .analysis 752 rename_file.annotation_id = Some(outside_workspace_annotation_id())
758 .is_library_file(src) 753 }
759 .unwrap() 754 ops.push(lsp_ext::SnippetDocumentChangeOperation::Op(lsp_types::ResourceOp::Rename(
760 .then(|| outside_workspace_annotation(snap)) 755 rename_file,
761 .flatten(), 756 )))
762 });
763 ops.push(lsp_ext::SnippetDocumentChangeOperation::Op(rename_file))
764 } 757 }
765 } 758 }
766 ops 759 Ok(ops)
767} 760}
768 761
769pub(crate) fn snippet_workspace_edit( 762pub(crate) fn snippet_workspace_edit(
@@ -773,31 +766,33 @@ pub(crate) fn snippet_workspace_edit(
773 let mut document_changes: Vec<lsp_ext::SnippetDocumentChangeOperation> = Vec::new(); 766 let mut document_changes: Vec<lsp_ext::SnippetDocumentChangeOperation> = Vec::new();
774 767
775 for op in source_change.file_system_edits { 768 for op in source_change.file_system_edits {
776 let ops = snippet_text_document_ops(snap, op); 769 let ops = snippet_text_document_ops(snap, op)?;
777 document_changes.extend_from_slice(&ops); 770 document_changes.extend_from_slice(&ops);
778 } 771 }
779 for (file_id, edit) in source_change.source_file_edits { 772 for (file_id, edit) in source_change.source_file_edits {
780 let edit = snippet_text_document_edit(&snap, source_change.is_snippet, file_id, edit)?; 773 let edit = snippet_text_document_edit(&snap, source_change.is_snippet, file_id, edit)?;
781 document_changes.push(lsp_ext::SnippetDocumentChangeOperation::Edit(edit)); 774 document_changes.push(lsp_ext::SnippetDocumentChangeOperation::Edit(edit));
782 } 775 }
783 let change_annotations = outside_workspace_annotation(snap).map(|annotation| { 776 let mut workspace_edit = lsp_ext::SnippetWorkspaceEdit {
784 use std::iter::FromIterator;
785 HashMap::from_iter(Some((
786 annotation,
787 lsp_types::ChangeAnnotation {
788 label: String::from("Edit outside of the workspace"),
789 needs_confirmation: Some(true),
790 description: Some(String::from(
791 "This edit lies outside of the workspace and may affect dependencies",
792 )),
793 },
794 )))
795 });
796 let workspace_edit = lsp_ext::SnippetWorkspaceEdit {
797 changes: None, 777 changes: None,
798 document_changes: Some(document_changes), 778 document_changes: Some(document_changes),
799 change_annotations, 779 change_annotations: None,
800 }; 780 };
781 if snap.config.change_annotation_support() {
782 workspace_edit.change_annotations = Some(
783 once((
784 outside_workspace_annotation_id(),
785 lsp_types::ChangeAnnotation {
786 label: String::from("Edit outside of the workspace"),
787 needs_confirmation: Some(true),
788 description: Some(String::from(
789 "This edit lies outside of the workspace and may affect dependencies",
790 )),
791 },
792 ))
793 .collect(),
794 )
795 }
801 Ok(workspace_edit) 796 Ok(workspace_edit)
802} 797}
803 798