Skip to content

Commit b36b15e

Browse files
Revamp LAL_Refactor error reporting
We now publish diagnostics instead of just sending an error response when a refactoring fails. This allows users to better understand why the refactorings did not work. The code has also been factorized between all the refactorings. Tests have been adapated to new user-readable labels for each refactoring command. For eng/ide/ada_language_server#1149
1 parent 9f978c1 commit b36b15e

File tree

32 files changed

+453
-778
lines changed

32 files changed

+453
-778
lines changed

source/ada/lsp-ada_handlers-refactor-add_parameter.adb

Lines changed: 8 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,8 @@ with Libadalang.Analysis; use Libadalang.Analysis;
2424
with LAL_Refactor.Subprogram_Signature;
2525
use LAL_Refactor.Subprogram_Signature;
2626

27-
with LSP.Common;
28-
with LSP.Messages.Client_Requests;
29-
with LSP.Lal_Utils;
30-
3127
with VSS.Strings.Conversions;
28+
with LSP.Commands;
3229

3330
package body LSP.Ada_Handlers.Refactor.Add_Parameter is
3431

@@ -119,33 +116,27 @@ package body LSP.Ada_Handlers.Refactor.Add_Parameter is
119116
end return;
120117
end Create;
121118

122-
-------------
123-
-- Execute --
124-
-------------
119+
--------------
120+
-- Refactor --
121+
--------------
125122

126-
overriding procedure Execute
123+
overriding procedure Refactor
127124
(Self : Command;
128125
Handler : not null access LSP.Server_Notification_Receivers.
129126
Server_Notification_Receiver'Class;
130127
Client : not null access LSP.Client_Message_Receivers.
131128
Client_Message_Receiver'Class;
132-
Error : in out LSP.Errors.Optional_ResponseError)
129+
Edits : out LAL_Refactor.Refactoring_Edits)
133130
is
134131
use Langkit_Support.Slocs;
135132
use LAL_Refactor;
136-
use LSP.Messages;
137133
use LSP.Types;
138-
use VSS.Strings.Conversions;
139134

140135
Message_Handler : LSP.Ada_Handlers.Message_Handler renames
141136
LSP.Ada_Handlers.Message_Handler (Handler.all);
142137
Context : LSP.Ada_Contexts.Context renames
143138
Message_Handler.Contexts.Get (Self.Context_Id).all;
144139

145-
Apply : Client_Requests.Workspace_Apply_Edit_Request;
146-
Workspace_Edits : WorkspaceEdit renames Apply.params.edit;
147-
Label : Optional_Virtual_String renames Apply.params.label;
148-
149140
function Analysis_Units return Analysis_Unit_Array is
150141
(Context.Analysis_Units);
151142
-- Provides the Context Analysis_Unit_Array to the Mode_Changer
@@ -162,44 +153,9 @@ package body LSP.Ada_Handlers.Refactor.Add_Parameter is
162153
New_Parameter =>
163154
VSS.Strings.Conversions.To_Unbounded_UTF_8_String
164155
(Self.New_Parameter));
165-
Edits : constant LAL_Refactor.Refactoring_Edits :=
166-
Adder.Refactor (Analysis_Units'Access);
167-
168156
begin
169-
if Edits = No_Refactoring_Edits then
170-
Error :=
171-
(Is_Set => True,
172-
Value =>
173-
(code => LSP.Errors.UnknownErrorCode,
174-
message => VSS.Strings.Conversions.To_Virtual_String
175-
("Failed to execute the Add Parameter refactoring."),
176-
data => <>));
177-
178-
else
179-
Workspace_Edits :=
180-
LSP.Lal_Utils.To_Workspace_Edit
181-
(Edits => Edits,
182-
Resource_Operations => Message_Handler.Resource_Operations,
183-
Versioned_Documents => Message_Handler.Versioned_Documents,
184-
Document_Provider => Message_Handler'Access);
185-
Label :=
186-
(Is_Set => True,
187-
Value => To_Virtual_String (Command'External_Tag));
188-
189-
Client.On_Workspace_Apply_Edit_Request (Apply);
190-
end if;
191-
192-
exception
193-
when E : others =>
194-
LSP.Common.Log (Message_Handler.Trace, E);
195-
Error :=
196-
(Is_Set => True,
197-
Value =>
198-
(code => LSP.Errors.UnknownErrorCode,
199-
message => VSS.Strings.Conversions.To_Virtual_String
200-
("Failed to execute the Add Parameter refactoring."),
201-
data => <>));
202-
end Execute;
157+
Edits := Adder.Refactor (Analysis_Units'Access);
158+
end Refactor;
203159

204160
----------------
205161
-- Initialize --

source/ada/lsp-ada_handlers-refactor-add_parameter.ads

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,18 @@
2020
with Ada.Streams;
2121

2222
with LSP.Client_Message_Receivers;
23-
with LSP.Commands;
2423
with LSP.Messages;
25-
with LSP.Errors;
2624
with LSP.JSON_Streams;
2725

2826
with VSS.Strings;
2927

3028
package LSP.Ada_Handlers.Refactor.Add_Parameter is
3129

32-
type Command is new LSP.Commands.Command with private;
30+
type Command is new LSP.Ada_Handlers.Refactor.Command with private;
31+
32+
overriding function Name (Self : Command) return String
33+
is
34+
("Add Parameter");
3335

3436
procedure Append_Code_Action
3537
(Self : in out Command;
@@ -41,7 +43,7 @@ package LSP.Ada_Handlers.Refactor.Add_Parameter is
4143

4244
private
4345

44-
type Command is new LSP.Commands.Command with record
46+
type Command is new LSP.Ada_Handlers.Refactor.Command with record
4547
Context_Id : VSS.Strings.Virtual_String;
4648
Where : LSP.Messages.Location;
4749
New_Parameter : VSS.Strings.Virtual_String;
@@ -55,13 +57,13 @@ private
5557
-- Reads JS and creates a new Command
5658

5759
overriding
58-
procedure Execute
60+
procedure Refactor
5961
(Self : Command;
6062
Handler : not null access
6163
LSP.Server_Notification_Receivers.Server_Notification_Receiver'Class;
6264
Client : not null access
6365
LSP.Client_Message_Receivers.Client_Message_Receiver'Class;
64-
Error : in out LSP.Errors.Optional_ResponseError);
66+
Edits : out LAL_Refactor.Refactoring_Edits);
6567
-- Executes Self by computing the necessary refactorings
6668

6769
procedure Initialize

source/ada/lsp-ada_handlers-refactor-change_parameter_mode.adb

Lines changed: 15 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,11 @@ with Libadalang.Common; use Libadalang.Common;
2222

2323
with Laltools.Common; use Laltools.Common;
2424

25-
with LSP.Common;
2625
with LSP.Messages;
27-
with LSP.Messages.Client_Requests;
2826
with LSP.Lal_Utils;
2927

3028
with VSS.Strings.Conversions;
29+
with LSP.Commands;
3130

3231
package body LSP.Ada_Handlers.Refactor.Change_Parameter_Mode is
3332

@@ -234,23 +233,21 @@ package body LSP.Ada_Handlers.Refactor.Change_Parameter_Mode is
234233
end return;
235234
end Create;
236235

237-
-------------
238-
-- Execute --
239-
-------------
236+
--------------
237+
-- Refactor --
238+
--------------
240239

241240
overriding
242-
procedure Execute
241+
procedure Refactor
243242
(Self : Command;
244243
Handler : not null access LSP.Server_Notification_Receivers.
245244
Server_Notification_Receiver'Class;
246245
Client : not null access LSP.Client_Message_Receivers.
247246
Client_Message_Receiver'Class;
248-
Error : in out LSP.Errors.Optional_ResponseError)
247+
Edits : out LAL_Refactor.Refactoring_Edits)
249248
is
250249
use LAL_Refactor;
251-
use LSP.Messages;
252250
use LSP.Types;
253-
use VSS.Strings.Conversions;
254251

255252
Message_Handler : LSP.Ada_Handlers.Message_Handler renames
256253
LSP.Ada_Handlers.Message_Handler (Handler.all);
@@ -260,10 +257,6 @@ package body LSP.Ada_Handlers.Refactor.Change_Parameter_Mode is
260257
Document : constant LSP.Ada_Documents.Document_Access :=
261258
Message_Handler.Get_Open_Document (Self.Where.textDocument.uri);
262259

263-
Apply : Client_Requests.Workspace_Apply_Edit_Request;
264-
Workspace_Edits : WorkspaceEdit renames Apply.params.edit;
265-
Label : Optional_Virtual_String renames Apply.params.label;
266-
267260
Node : constant Ada_Node :=
268261
Document.Get_Node_At (Context, Self.Where.position);
269262

@@ -274,7 +267,6 @@ package body LSP.Ada_Handlers.Refactor.Change_Parameter_Mode is
274267
Last => Positive (Self.Last_Param_Index));
275268

276269
Changer : Mode_Changer;
277-
Edits : LAL_Refactor.Refactoring_Edits;
278270

279271
function Analysis_Units return Analysis_Unit_Array is
280272
(Context.Analysis_Units);
@@ -304,14 +296,14 @@ package body LSP.Ada_Handlers.Refactor.Change_Parameter_Mode is
304296

305297
begin
306298
if Target_Subp.Is_Null then
307-
Error :=
308-
(Is_Set => True,
309-
Value =>
310-
(code => LSP.Errors.InvalidRequest,
311-
message => VSS.Strings.To_Virtual_String
312-
("Could not execute Change Parameter Mode command. "
313-
& "The target subprogram could not be resolved precisely."),
314-
data => <>));
299+
Edits :=
300+
(Diagnostics =>
301+
[LAL_Refactor.Subprogram_Signature.Create
302+
(Subp => Node,
303+
Info => VSS.Strings.To_Virtual_String
304+
("The target subprogram could "
305+
& "not be resolved precisely."))],
306+
others => <>);
315307
return;
316308
end if;
317309

@@ -321,41 +313,7 @@ package body LSP.Ada_Handlers.Refactor.Change_Parameter_Mode is
321313
Value (Self.New_Mode));
322314

323315
Edits := Changer.Refactor (Analysis_Units'Access);
324-
325-
if Edits = No_Refactoring_Edits then
326-
Error :=
327-
(Is_Set => True,
328-
Value =>
329-
(code => LSP.Errors.UnknownErrorCode,
330-
message => VSS.Strings.Conversions.To_Virtual_String
331-
("Failed to execute the Change Parameter Mode refactoring."),
332-
data => <>));
333-
334-
else
335-
Workspace_Edits :=
336-
LSP.Lal_Utils.To_Workspace_Edit
337-
(Edits => Edits,
338-
Resource_Operations => Message_Handler.Resource_Operations,
339-
Versioned_Documents => Message_Handler.Versioned_Documents,
340-
Document_Provider => Message_Handler'Access);
341-
Label :=
342-
(Is_Set => True,
343-
Value => To_Virtual_String (Command'External_Tag));
344-
345-
Client.On_Workspace_Apply_Edit_Request (Apply);
346-
end if;
347-
348-
exception
349-
when E : others =>
350-
LSP.Common.Log (Message_Handler.Trace, E);
351-
Error :=
352-
(Is_Set => True,
353-
Value =>
354-
(code => LSP.Errors.UnknownErrorCode,
355-
message => VSS.Strings.Conversions.To_Virtual_String
356-
("Failed to execute the Change Parameter Mode refactoring."),
357-
data => <>));
358-
end Execute;
316+
end Refactor;
359317

360318
----------------
361319
-- Initialize --

source/ada/lsp-ada_handlers-refactor-change_parameter_mode.ads

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ with Ada.Streams;
2222
private with VSS.Strings;
2323

2424
with LSP.Client_Message_Receivers;
25-
with LSP.Commands;
26-
with LSP.Errors;
2725
with LSP.JSON_Streams;
2826

2927
with Libadalang.Analysis;
@@ -33,7 +31,11 @@ with LAL_Refactor.Subprogram_Signature; use LAL_Refactor.Subprogram_Signature;
3331

3432
package LSP.Ada_Handlers.Refactor.Change_Parameter_Mode is
3533

36-
type Command is new LSP.Commands.Command with private;
34+
type Command is new LSP.Ada_Handlers.Refactor.Command with private;
35+
36+
overriding function Name (Self : Command) return String
37+
is
38+
("Change Parameter Mode");
3739

3840
procedure Append_Code_Action
3941
(Self : in out Command;
@@ -46,7 +48,7 @@ package LSP.Ada_Handlers.Refactor.Change_Parameter_Mode is
4648

4749
private
4850

49-
type Command is new LSP.Commands.Command with record
51+
type Command is new LSP.Ada_Handlers.Refactor.Command with record
5052
Context : VSS.Strings.Virtual_String;
5153
Where : LSP.Messages.TextDocumentPositionParams;
5254
First_Param_Index : LSP.Types.LSP_Number;
@@ -61,13 +63,13 @@ private
6163
-- Reads JS and creates a new Command
6264

6365
overriding
64-
procedure Execute
66+
procedure Refactor
6567
(Self : Command;
6668
Handler : not null access
6769
LSP.Server_Notification_Receivers.Server_Notification_Receiver'Class;
6870
Client : not null access
6971
LSP.Client_Message_Receivers.Client_Message_Receiver'Class;
70-
Error : in out LSP.Errors.Optional_ResponseError);
72+
Edits : out LAL_Refactor.Refactoring_Edits);
7173
-- Executes Self by computing the necessary refactorings
7274

7375
procedure Initialize

0 commit comments

Comments
 (0)