@@ -922,9 +922,11 @@ package body LSP.Lal_Utils is
922922
923923 function To_Workspace_Edit
924924 (Edits : Laltools.Refactor.Refactoring_Edits;
925+ Resource_Operations : LSP.Messages.Optional_ResourceOperationKindSet :=
926+ LSP.Messages.Optional_ResourceOperationKindSet'(Is_Set => False);
925927 Versioned_Documents : Boolean := False;
926- Document_Provider : access LSP.Ada_Documents.Document_Provider'Class
927- := null ;
928+ Document_Provider : access LSP.Ada_Documents.Document_Provider'Class :=
929+ null ;
928930 Rename : Boolean := False)
929931 return LSP.Messages.WorkspaceEdit
930932 is
@@ -933,12 +935,27 @@ package body LSP.Lal_Utils is
933935 Text_Edits : LSP.Messages.TextEdit_Vector;
934936
935937 use Laltools.Refactor;
938+ use LSP.Messages;
936939
937940 Text_Edits_Cursor : Text_Edit_Ordered_Maps.Cursor
938941 := Edits.Text_Edits.First;
939942 File_Deletions_Cursor : Unbounded_String_Ordered_Sets.Cursor
940943 := Edits.File_Deletions.First;
941944
945+ Is_Create_Supported : constant Boolean :=
946+ Resource_Operations.Is_Set
947+ and then ResourceOperationKindSets.Contains
948+ (ResourceOperationKindSets.Set (Resource_Operations.Value), create);
949+ Is_Rename_Supported : constant Boolean :=
950+ Resource_Operations.Is_Set
951+ and then ResourceOperationKindSets.Contains
952+ (ResourceOperationKindSets.Set (Resource_Operations.Value),
953+ LSP.Messages.rename);
954+ Is_Delete_Supported : constant Boolean :=
955+ Resource_Operations.Is_Set
956+ and then ResourceOperationKindSets.Contains
957+ (ResourceOperationKindSets.Set (Resource_Operations.Value), delete);
958+
942959 begin
943960 return WE : LSP.Messages.WorkspaceEdit do
944961 -- Text edits
@@ -953,9 +970,14 @@ package body LSP.Lal_Utils is
953970 File_URI := LSP.Types.File_To_URI
954971 (Text_Edit_Ordered_Maps.Key (Text_Edits_Cursor));
955972
973+ -- If `workspace.workspaceEdit.documentChanges` client capability
974+ -- was true, then use `TextDocumentEdit[]` instead of
975+ -- `TextEdit[]`.
976+
956977 if Versioned_Documents then
957978 declare
958979 Annotaded_Edits : LSP.Messages.AnnotatedTextEdit_Vector;
980+
959981 begin
960982 Annotaded_Edits.Reserve_Capacity (Text_Edits.Capacity);
961983 for X of Text_Edits loop
@@ -979,31 +1001,50 @@ package body LSP.Lal_Utils is
9791001 Text_Edit_Ordered_Maps.Next (Text_Edits_Cursor);
9801002 end loop ;
9811003
982- -- File creations
983- -- TODO
984-
985- -- File deletions
986-
987- if not Rename then
988- while Unbounded_String_Ordered_Sets.Has_Element
989- (File_Deletions_Cursor)
990- loop
1004+ -- Resource operations are only supported if
1005+ -- `workspace.workspaceEdit.documentChanges` is True since they
1006+ -- must be sent in the `documentChanges` field.
1007+ -- `workspace.workspaceEdit.resourceOperations` client capability
1008+ -- must be checked in order to know which kind of operations are
1009+ -- supported.
9911010
992- File_URI := LSP.Types.File_To_URI
993- (Unbounded_String_Ordered_Sets.Element
994- (File_Deletions_Cursor));
1011+ -- File creations
9951012
1013+ if Versioned_Documents and then Is_Create_Supported then
1014+ for File_Creation of Edits.File_Creations loop
9961015 WE.documentChanges.Append
9971016 (LSP.Messages.Document_Change'(
998- (Kind => LSP.Messages.Delete_File,
999- Delete_File => LSP.Messages.DeleteFile'(
1000- uri => File_URI,
1001- others => <>
1002- ))));
1017+ (Kind => LSP.Messages.Create_File,
1018+ Create_File => LSP.Messages.CreateFile'
1019+ (uri => LSP.Types.File_To_URI (File_Creation.Filepath),
1020+ others => <>))));
10031021
1004- Unbounded_String_Ordered_Sets.Next (File_Deletions_Cursor);
1022+ declare
1023+ Annotaded_Edits : LSP.Messages.AnnotatedTextEdit_Vector;
1024+ Content : constant LSP.Messages.AnnotatedTextEdit :=
1025+ LSP.Messages.AnnotatedTextEdit'
1026+ (span => ((0 , 0 ), (0 , 0 )),
1027+ newText => To_LSP_String
1028+ (Ada.Strings.Unbounded.
1029+ To_String (File_Creation.Content)),
1030+ others => <>);
1031+
1032+ begin
1033+ Annotaded_Edits.Append (Content);
1034+
1035+ WE.documentChanges.Append
1036+ (LSP.Messages.Document_Change'(
1037+ (Kind => LSP.Messages.Text_Document_Edit,
1038+ Text_Document_Edit => LSP.Messages.TextDocumentEdit'
1039+ (edits => Annotaded_Edits,
1040+ others => <>))));
1041+ end ;
10051042 end loop ;
1006- else
1043+ end if ;
1044+
1045+ -- File deletions
1046+
1047+ if Versioned_Documents and then Is_Delete_Supported then
10071048 while Unbounded_String_Ordered_Sets.Has_Element
10081049 (File_Deletions_Cursor)
10091050 loop
@@ -1017,7 +1058,8 @@ package body LSP.Lal_Utils is
10171058 (Kind => LSP.Messages.Rename_File,
10181059 Rename_File => LSP.Messages.RenameFile'
10191060 (oldUri => File_URI,
1020- newUri => File_URI & " .bak" ,
1061+ newUri =>
1062+ (if Rename then File_URI & " .bak" else File_URI),
10211063 others => <>))));
10221064
10231065 Unbounded_String_Ordered_Sets.Next (File_Deletions_Cursor);
@@ -1026,15 +1068,17 @@ package body LSP.Lal_Utils is
10261068
10271069 -- File renames
10281070
1029- for File_Rename of Edits.File_Renames loop
1030- WE.documentChanges.Append
1031- (LSP.Messages.Document_Change'(
1032- (Kind => LSP.Messages.Rename_File,
1033- Rename_File => LSP.Messages.RenameFile'
1034- (oldUri => LSP.Types.File_To_URI (File_Rename.Filepath),
1035- newUri => LSP.Types.File_To_URI (File_Rename.New_Name),
1036- others => <>))));
1037- end loop ;
1071+ if Versioned_Documents and then Is_Rename_Supported then
1072+ for File_Rename of Edits.File_Renames loop
1073+ WE.documentChanges.Append
1074+ (LSP.Messages.Document_Change'(
1075+ (Kind => LSP.Messages.Rename_File,
1076+ Rename_File => LSP.Messages.RenameFile'
1077+ (oldUri => LSP.Types.File_To_URI (File_Rename.Filepath),
1078+ newUri => LSP.Types.File_To_URI (File_Rename.New_Name),
1079+ others => <>))));
1080+ end loop ;
1081+ end if ;
10381082 end return ;
10391083 end To_Workspace_Edit ;
10401084
0 commit comments