@@ -2959,9 +2959,13 @@ package body LSP.Ada_Handlers is
2959
2959
-- references to be renamed and is returned by this function.
2960
2960
2961
2961
Document : constant LSP.Ada_Documents.Document_Access :=
2962
- Get_Open_Document (Self, Value.textDocument.uri);
2962
+ Get_Open_Document (Self, Value.textDocument.uri);
2963
2963
2964
- Refs : Laltools.Refactor.Safe_Rename.Renamable_References;
2964
+ Safe_Renamer : Laltools.Refactor.Safe_Rename.Safe_Renamer;
2965
+ Algorithm : constant Laltools.Refactor.Safe_Rename.
2966
+ Problem_Finder_Algorithm_Kind :=
2967
+ Laltools.Refactor.Safe_Rename.Analyse_AST;
2968
+ Edits : Laltools.Refactor.Refactoring_Edits;
2965
2969
2966
2970
procedure Process_Context (C : Context_Access);
2967
2971
-- Process the rename request for the given context, and add the
@@ -2975,22 +2979,32 @@ package body LSP.Ada_Handlers is
2975
2979
Position : constant LSP.Messages.TextDocumentPositionParams :=
2976
2980
(Value.textDocument, Value.position);
2977
2981
2978
- Node : Ada_Node := C.Get_Node_At (Document, Position);
2979
- Name_Node : Name := Laltools.Common.Get_Node_As_Name (Node);
2982
+ Node : Ada_Node := C.Get_Node_At (Document, Position);
2983
+ Name_Node : Name := Laltools.Common.Get_Node_As_Name (Node);
2984
+ Definition : Defining_Name :=
2985
+ Laltools.Common.Resolve_Name_Precisely (Name_Node);
2980
2986
2981
2987
Empty : LSP.Messages.TextEdit_Vector;
2982
2988
2983
- procedure Process_Comments
2984
- (Node : Ada_Node;
2985
- Uri : LSP.Messages.DocumentUri);
2986
- -- Iterate over all comments and include them in the response when
2987
- -- they contain a renamed word.
2989
+ function Analysis_Units return Analysis_Unit_Array is
2990
+ (C.Analysis_Units);
2991
+ -- Callback needed to provide the analysis units to the safe rename
2992
+ -- tool;
2988
2993
2989
2994
procedure Process_Reference
2990
2995
(Unit : Libadalang.Analysis.Analysis_Unit;
2991
2996
Sloc_Range : Langkit_Support.Slocs.Source_Location_Range);
2992
2997
-- Add a reference to Response if it has not been added yet
2993
2998
2999
+ procedure Process_File_Rename
3000
+ (File_Rename : Laltools.Refactor.File_Rename);
3001
+ -- Add File_Rename to Response if it has not been added yet
3002
+
3003
+ procedure Process_Comments
3004
+ (Node : Ada_Node;
3005
+ Uri : LSP.Messages.DocumentUri);
3006
+ -- Iterate over all comments and include them in the response when
3007
+ -- they contain a renamed word.
2994
3008
-- ---------------------
2995
3009
-- Process_Reference --
2996
3010
-- ---------------------
@@ -3032,6 +3046,28 @@ package body LSP.Ada_Handlers is
3032
3046
end if ;
3033
3047
end Process_Reference ;
3034
3048
3049
+ -- -----------------------
3050
+ -- Process_File_Rename --
3051
+ -- -----------------------
3052
+
3053
+ procedure Process_File_Rename
3054
+ (File_Rename : Laltools.Refactor.File_Rename)
3055
+ is
3056
+ Rename : constant LSP.Messages.RenameFile :=
3057
+ LSP.Messages.RenameFile'
3058
+ (kind => LSP.Messages.rename,
3059
+ oldUri => LSP.Types.File_To_URI (File_Rename.Filepath),
3060
+ newUri => LSP.Types.File_To_URI (File_Rename.New_Name),
3061
+ options => <>,
3062
+ annotationId => <>);
3063
+
3064
+ begin
3065
+ Response.result.documentChanges.Append
3066
+ (LSP.Messages.Document_Change'
3067
+ (Kind => LSP.Messages.Rename_File,
3068
+ Rename_File => Rename));
3069
+ end Process_File_Rename ;
3070
+
3035
3071
-- ---------------------
3036
3072
-- Process_Comments --
3037
3073
-- ---------------------
@@ -3183,47 +3219,70 @@ package body LSP.Ada_Handlers is
3183
3219
end loop ;
3184
3220
end Process_Comments ;
3185
3221
3186
- use Laltools.Refactor.Safe_Rename ;
3222
+ use Laltools.Refactor;
3187
3223
3188
- Unit_Cursor : Unit_Slocs_Maps.Cursor;
3189
- Sloc_Cursor : Slocs_Maps.Cursor;
3224
+ Text_Edits_Cursor : Text_Edit_Ordered_Maps.Cursor;
3190
3225
3191
- use Unit_Slocs_Maps;
3192
- use Slocs_Maps;
3193
3226
begin
3227
+ if Definition.Is_Null then
3228
+ return ;
3229
+ end if ;
3230
+
3231
+ Safe_Renamer := Laltools.Refactor.Safe_Rename.Create_Safe_Renamer
3232
+ (Definition => Definition,
3233
+ New_Name => To_Unbounded_Text_Type (Value.newName),
3234
+ Algorithm => Algorithm);
3194
3235
3195
- Refs := Find_All_Renamable_References
3196
- (Node => Node,
3197
- New_Name => To_Unbounded_Text_Type (Value.newName),
3198
- Units => C.Analysis_Units,
3199
- Algorithm_Kind => Analyse_AST);
3236
+ Edits := Safe_Renamer.Refactor (Analysis_Units'Access );
3200
3237
3201
- -- Call to Find_All_Renamable_References above reparses all analysis
3202
- -- units, therefore, Node and Name_Node need to be recomputed.
3238
+ -- Call to Safe_Renamer.Refactor above might reparse all analysis
3239
+ -- units, dependingon the algorithm used, therefore, Node, Name_Node
3240
+ -- Definition need to be recomputed.
3203
3241
3204
3242
Node := C.Get_Node_At (Document, Position);
3205
3243
Name_Node := Laltools.Common.Get_Node_As_Name (Node);
3244
+ Definition := Laltools.Common.Resolve_Name_Precisely (Name_Node);
3245
+
3246
+ -- If problems were found, do not continue processing references
3206
3247
3207
- -- If problems were found, do not continue processing references.
3208
- if not Refs.Problems.Is_Empty then
3248
+ if not Edits.Diagnostics.Is_Empty then
3209
3249
return ;
3210
3250
end if ;
3211
3251
3212
- Unit_Cursor := Refs.References.First;
3213
- while Has_Element (Unit_Cursor) loop
3214
- Sloc_Cursor :=
3215
- Refs.References.Constant_Reference (Unit_Cursor).First;
3216
- while Has_Element (Sloc_Cursor) loop
3217
- for Sloc of
3218
- Refs.References.Constant_Reference (Unit_Cursor).
3219
- Constant_Reference (Sloc_Cursor)
3220
- loop
3221
- Process_Reference (Key (Unit_Cursor), Sloc);
3222
- end loop ;
3223
- Next (Sloc_Cursor);
3252
+ -- First process all references
3253
+
3254
+ Text_Edits_Cursor := Edits.Text_Edits.First;
3255
+
3256
+ while Text_Edit_Ordered_Maps.Has_Element (Text_Edits_Cursor) loop
3257
+ for Text_Edit of
3258
+ Text_Edit_Ordered_Maps.Element (Text_Edits_Cursor)
3259
+ loop
3260
+ Process_Reference
3261
+ (Unit =>
3262
+ C.LAL_Context.Get_From_File
3263
+ (Text_Edit_Ordered_Maps.Key (Text_Edits_Cursor)),
3264
+ Sloc_Range => Text_Edit.Location);
3224
3265
end loop ;
3225
- Next (Unit_Cursor);
3266
+
3267
+ Text_Edits_Cursor :=
3268
+ Text_Edit_Ordered_Maps.Next (Text_Edits_Cursor);
3226
3269
end loop ;
3270
+
3271
+ -- Then process all file renames
3272
+ -- Note: This is deactivated for now since file / folder operations
3273
+ -- are sent in the documentChanges field of a WorkspaceEdit.
3274
+ -- Previously, references and comments were processed and added to
3275
+ -- the changes field of a WorkspaceEdit.
3276
+ -- LSP 3.16:
3277
+ -- "If the client can handle versioned document edits and if
3278
+ -- documentChanges are present, the latter are preferred over
3279
+ -- changes."
3280
+ -- This means that activating the following Process_File_Rename will
3281
+ -- result in editors ignoring the previously processed references.
3282
+
3283
+ -- for File_Rename of Edits.File_Renames loop
3284
+ -- Process_File_Rename (File_Rename);
3285
+ -- end loop;
3227
3286
end Process_Context ;
3228
3287
3229
3288
begin
@@ -3233,15 +3292,15 @@ package body LSP.Ada_Handlers is
3233
3292
-- If problems were found, send an error message and do not proceed
3234
3293
-- with the renames.
3235
3294
3236
- if not Refs.Problems .Is_Empty then
3295
+ if not Edits.Diagnostics .Is_Empty then
3237
3296
return Response : LSP.Messages.Server_Responses.Rename_Response
3238
3297
(Is_Error => True)
3239
3298
do
3240
3299
declare
3241
3300
Error_Message : VSS.String_Vectors.Virtual_String_Vector;
3242
3301
3243
3302
begin
3244
- for Problem of Refs.Problems loop
3303
+ for Problem of Edits.Diagnostics loop
3245
3304
Error_Message.Append
3246
3305
(VSS.Strings.Conversions.To_Virtual_String
3247
3306
(Problem.Info));
0 commit comments