@@ -2959,9 +2959,13 @@ package body LSP.Ada_Handlers is
29592959 -- references to be renamed and is returned by this function.
29602960
29612961 Document : constant LSP.Ada_Documents.Document_Access :=
2962- Get_Open_Document (Self, Value.textDocument.uri);
2962+ Get_Open_Document (Self, Value.textDocument.uri);
29632963
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;
29652969
29662970 procedure Process_Context (C : Context_Access);
29672971 -- Process the rename request for the given context, and add the
@@ -2975,22 +2979,32 @@ package body LSP.Ada_Handlers is
29752979 Position : constant LSP.Messages.TextDocumentPositionParams :=
29762980 (Value.textDocument, Value.position);
29772981
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);
29802986
29812987 Empty : LSP.Messages.TextEdit_Vector;
29822988
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;
29882993
29892994 procedure Process_Reference
29902995 (Unit : Libadalang.Analysis.Analysis_Unit;
29912996 Sloc_Range : Langkit_Support.Slocs.Source_Location_Range);
29922997 -- Add a reference to Response if it has not been added yet
29932998
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.
29943008 -- ---------------------
29953009 -- Process_Reference --
29963010 -- ---------------------
@@ -3032,6 +3046,28 @@ package body LSP.Ada_Handlers is
30323046 end if ;
30333047 end Process_Reference ;
30343048
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+
30353071 -- ---------------------
30363072 -- Process_Comments --
30373073 -- ---------------------
@@ -3183,47 +3219,70 @@ package body LSP.Ada_Handlers is
31833219 end loop ;
31843220 end Process_Comments ;
31853221
3186- use Laltools.Refactor.Safe_Rename ;
3222+ use Laltools.Refactor;
31873223
3188- Unit_Cursor : Unit_Slocs_Maps.Cursor;
3189- Sloc_Cursor : Slocs_Maps.Cursor;
3224+ Text_Edits_Cursor : Text_Edit_Ordered_Maps.Cursor;
31903225
3191- use Unit_Slocs_Maps;
3192- use Slocs_Maps;
31933226 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);
31943235
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 );
32003237
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.
32033241
32043242 Node := C.Get_Node_At (Document, Position);
32053243 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
32063247
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
32093249 return ;
32103250 end if ;
32113251
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);
32243265 end loop ;
3225- Next (Unit_Cursor);
3266+
3267+ Text_Edits_Cursor :=
3268+ Text_Edit_Ordered_Maps.Next (Text_Edits_Cursor);
32263269 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;
32273286 end Process_Context ;
32283287
32293288 begin
@@ -3233,15 +3292,15 @@ package body LSP.Ada_Handlers is
32333292 -- If problems were found, send an error message and do not proceed
32343293 -- with the renames.
32353294
3236- if not Refs.Problems .Is_Empty then
3295+ if not Edits.Diagnostics .Is_Empty then
32373296 return Response : LSP.Messages.Server_Responses.Rename_Response
32383297 (Is_Error => True)
32393298 do
32403299 declare
32413300 Error_Message : VSS.String_Vectors.Virtual_String_Vector;
32423301
32433302 begin
3244- for Problem of Refs.Problems loop
3303+ for Problem of Edits.Diagnostics loop
32453304 Error_Message.Append
32463305 (VSS.Strings.Conversions.To_Virtual_String
32473306 (Problem.Info));
0 commit comments