1
1
//! Conversion of rust-analyzer specific types to lsp_types equivalents.
2
2
use std:: {
3
- collections :: HashMap ,
3
+ iter :: once ,
4
4
path:: { self , Path } ,
5
5
sync:: atomic:: { AtomicU32 , Ordering } ,
6
6
} ;
7
7
8
8
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 ,
14
14
} ;
15
15
use itertools:: Itertools ;
16
16
use serde_json:: to_value;
@@ -690,8 +690,8 @@ pub(crate) fn goto_definition_response(
690
690
}
691
691
}
692
692
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" )
695
695
}
696
696
697
697
pub ( crate ) fn snippet_text_document_edit (
@@ -702,26 +702,21 @@ pub(crate) fn snippet_text_document_edit(
702
702
) -> Result < lsp_ext:: SnippetTextDocumentEdit > {
703
703
let text_document = optional_versioned_text_document_identifier ( snap, file_id) ;
704
704
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
+ }
718
713
Ok ( lsp_ext:: SnippetTextDocumentEdit { text_document, edits } )
719
714
}
720
715
721
716
pub ( crate ) fn snippet_text_document_ops (
722
717
snap : & GlobalStateSnapshot ,
723
718
file_system_edit : FileSystemEdit ,
724
- ) -> Vec < lsp_ext:: SnippetDocumentChangeOperation > {
719
+ ) -> Cancelable < Vec < lsp_ext:: SnippetDocumentChangeOperation > > {
725
720
let mut ops = Vec :: new ( ) ;
726
721
match file_system_edit {
727
722
FileSystemEdit :: CreateFile { dst, initial_contents } => {
@@ -749,21 +744,19 @@ pub(crate) fn snippet_text_document_ops(
749
744
FileSystemEdit :: MoveFile { src, dst } => {
750
745
let old_uri = snap. file_id_to_url ( src) ;
751
746
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
+ ) ) )
764
757
}
765
758
}
766
- ops
759
+ Ok ( ops)
767
760
}
768
761
769
762
pub ( crate ) fn snippet_workspace_edit (
@@ -773,31 +766,33 @@ pub(crate) fn snippet_workspace_edit(
773
766
let mut document_changes: Vec < lsp_ext:: SnippetDocumentChangeOperation > = Vec :: new ( ) ;
774
767
775
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
770
document_changes. extend_from_slice ( & ops) ;
778
771
}
779
772
for ( file_id, edit) in source_change. source_file_edits {
780
773
let edit = snippet_text_document_edit ( & snap, source_change. is_snippet , file_id, edit) ?;
781
774
document_changes. push ( lsp_ext:: SnippetDocumentChangeOperation :: Edit ( edit) ) ;
782
775
}
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 {
797
777
changes : None ,
798
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
796
Ok ( workspace_edit)
802
797
}
803
798
0 commit comments