Skip to content

Commit 493aaa1

Browse files
committed
Better visualise control flow for change_annotation_support"
1 parent b501b59 commit 493aaa1

File tree

1 file changed

+46
-51
lines changed

1 file changed

+46
-51
lines changed

crates/rust-analyzer/src/to_proto.rs

Lines changed: 46 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
//! Conversion of rust-analyzer specific types to lsp_types equivalents.
22
use std::{
3-
collections::HashMap,
3+
iter::once,
44
path::{self, Path},
55
sync::atomic::{AtomicU32, Ordering},
66
};
77

88
use ide::{
9-
Annotation, AnnotationKind, Assist, AssistKind, CallInfo, CompletionItem, CompletionItemKind,
10-
CompletionRelevance, Documentation, FileId, FileRange, FileSystemEdit, Fold, FoldKind,
11-
Highlight, HlMod, HlOperator, HlPunct, HlRange, HlTag, Indel, InlayHint, InlayKind,
12-
InsertTextFormat, Markup, NavigationTarget, ReferenceAccess, RenameError, Runnable, Severity,
13-
SourceChange, StructureNodeKind, SymbolKind, TextEdit, TextRange, TextSize,
9+
Annotation, AnnotationKind, Assist, AssistKind, CallInfo, Cancelable, CompletionItem,
10+
CompletionItemKind, CompletionRelevance, Documentation, FileId, FileRange, FileSystemEdit,
11+
Fold, FoldKind, Highlight, HlMod, HlOperator, HlPunct, HlRange, HlTag, Indel, InlayHint,
12+
InlayKind, InsertTextFormat, Markup, NavigationTarget, ReferenceAccess, RenameError, Runnable,
13+
Severity, SourceChange, StructureNodeKind, SymbolKind, TextEdit, TextRange, TextSize,
1414
};
1515
use itertools::Itertools;
1616
use serde_json::to_value;
@@ -690,8 +690,8 @@ pub(crate) fn goto_definition_response(
690690
}
691691
}
692692

693-
fn outside_workspace_annotation(snap: &GlobalStateSnapshot) -> Option<String> {
694-
snap.config.change_annotation_support().then(|| String::from("OutsideWorkspace"))
693+
fn outside_workspace_annotation_id() -> String {
694+
String::from("OutsideWorkspace")
695695
}
696696

697697
pub(crate) fn snippet_text_document_edit(
@@ -702,26 +702,21 @@ pub(crate) fn snippet_text_document_edit(
702702
) -> Result<lsp_ext::SnippetTextDocumentEdit> {
703703
let text_document = optional_versioned_text_document_identifier(snap, file_id);
704704
let line_index = snap.file_line_index(file_id)?;
705-
let outside_workspace_annotation = snap
706-
.analysis
707-
.is_library_file(file_id)?
708-
.then(|| outside_workspace_annotation(snap))
709-
.flatten();
710-
let edits = edit
711-
.into_iter()
712-
.map(|it| {
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();
705+
let mut edits: Vec<_> =
706+
edit.into_iter().map(|it| snippet_text_edit(&line_index, is_snippet, it)).collect();
707+
708+
if snap.analysis.is_library_file(file_id)? && snap.config.change_annotation_support() {
709+
for edit in &mut edits {
710+
edit.annotation_id = Some(outside_workspace_annotation_id())
711+
}
712+
}
718713
Ok(lsp_ext::SnippetTextDocumentEdit { text_document, edits })
719714
}
720715

721716
pub(crate) fn snippet_text_document_ops(
722717
snap: &GlobalStateSnapshot,
723718
file_system_edit: FileSystemEdit,
724-
) -> Vec<lsp_ext::SnippetDocumentChangeOperation> {
719+
) -> Cancelable<Vec<lsp_ext::SnippetDocumentChangeOperation>> {
725720
let mut ops = Vec::new();
726721
match file_system_edit {
727722
FileSystemEdit::CreateFile { dst, initial_contents } => {
@@ -749,21 +744,19 @@ pub(crate) fn snippet_text_document_ops(
749744
FileSystemEdit::MoveFile { src, dst } => {
750745
let old_uri = snap.file_id_to_url(src);
751746
let new_uri = snap.anchored_path(&dst);
752-
let rename_file = lsp_types::ResourceOp::Rename(lsp_types::RenameFile {
753-
old_uri,
754-
new_uri,
755-
options: None,
756-
annotation_id: snap
757-
.analysis
758-
.is_library_file(src)
759-
.unwrap()
760-
.then(|| outside_workspace_annotation(snap))
761-
.flatten(),
762-
});
763-
ops.push(lsp_ext::SnippetDocumentChangeOperation::Op(rename_file))
747+
let mut rename_file =
748+
lsp_types::RenameFile { old_uri, new_uri, options: None, annotation_id: None };
749+
if snap.analysis.is_library_file(src) == Ok(true)
750+
&& snap.config.change_annotation_support()
751+
{
752+
rename_file.annotation_id = Some(outside_workspace_annotation_id())
753+
}
754+
ops.push(lsp_ext::SnippetDocumentChangeOperation::Op(lsp_types::ResourceOp::Rename(
755+
rename_file,
756+
)))
764757
}
765758
}
766-
ops
759+
Ok(ops)
767760
}
768761

769762
pub(crate) fn snippet_workspace_edit(
@@ -773,31 +766,33 @@ pub(crate) fn snippet_workspace_edit(
773766
let mut document_changes: Vec<lsp_ext::SnippetDocumentChangeOperation> = Vec::new();
774767

775768
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)?;
777770
document_changes.extend_from_slice(&ops);
778771
}
779772
for (file_id, edit) in source_change.source_file_edits {
780773
let edit = snippet_text_document_edit(&snap, source_change.is_snippet, file_id, edit)?;
781774
document_changes.push(lsp_ext::SnippetDocumentChangeOperation::Edit(edit));
782775
}
783-
let change_annotations = outside_workspace_annotation(snap).map(|annotation| {
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 {
776+
let mut workspace_edit = lsp_ext::SnippetWorkspaceEdit {
797777
changes: None,
798778
document_changes: Some(document_changes),
799-
change_annotations,
779+
change_annotations: None,
800780
};
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+
}
801796
Ok(workspace_edit)
802797
}
803798

0 commit comments

Comments
 (0)