Skip to content

Commit 0d358b7

Browse files
committed
Update to new LAL Refactor Auto Import API
1 parent f16bfec commit 0d358b7

File tree

6 files changed

+246
-414
lines changed

6 files changed

+246
-414
lines changed

source/ada/lsp-ada_driver.adb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ with LSP.Ada_Handlers.Refactor.Change_Parameter_Mode;
5151
with LSP.Ada_Handlers.Refactor.Change_Parameters_Default_Value;
5252
with LSP.Ada_Handlers.Refactor.Change_Parameters_Type;
5353
with LSP.Ada_Handlers.Refactor.Extract_Subprogram;
54-
with LSP.Ada_Handlers.Refactor.Imports_Commands;
54+
with LSP.Ada_Handlers.Refactor.Auto_Import;
5555
with LSP.Ada_Handlers.Refactor.Introduce_Parameter;
5656
with LSP.Ada_Handlers.Refactor.Move_Parameter;
5757
with LSP.Ada_Handlers.Refactor.Pull_Up_Declaration;
@@ -109,7 +109,7 @@ procedure LSP.Ada_Driver is
109109
LSP.Ada_Commands.Register
110110
(LSP.Ada_Handlers.Named_Parameters_Commands.Command'Tag);
111111
LSP.Ada_Commands.Register
112-
(LSP.Ada_Handlers.Refactor.Imports_Commands.Command'Tag);
112+
(LSP.Ada_Handlers.Refactor.Auto_Import.Command'Tag);
113113
LSP.Ada_Commands.Register
114114
(LSP.Ada_Handlers.Refactor.Suppress_Seperate.Command'Tag);
115115
LSP.Ada_Commands.Register

source/ada/lsp-ada_handlers-refactor-imports_commands.adb renamed to source/ada/lsp-ada_handlers-refactor-auto_import.adb

Lines changed: 52 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,19 @@
1515
-- of the license. --
1616
------------------------------------------------------------------------------
1717

18-
with Ada.Strings.Unbounded;
1918
with Ada.Strings.Wide_Wide_Unbounded;
2019

2120
with Langkit_Support.Text;
2221

2322
with Libadalang.Analysis;
24-
with Libadalang.Common;
25-
26-
with Laltools.Common;
2723

2824
with VSS.JSON.Streams;
25+
with VSS.Strings.Conversions;
2926

3027
with LSP.Enumerations;
3128
with LSP.Structures.LSPAny_Vectors; use LSP.Structures.LSPAny_Vectors;
3229

33-
package body LSP.Ada_Handlers.Refactor.Imports_Commands is
30+
package body LSP.Ada_Handlers.Refactor.Auto_Import is
3431

3532
----------------
3633
-- Initialize --
@@ -45,8 +42,11 @@ package body LSP.Ada_Handlers.Refactor.Imports_Commands is
4542
begin
4643
Self.Context := Context.Id;
4744
Self.Where := Where;
48-
Self.With_Clause := With_Clause;
49-
Self.Prefix := Prefix;
45+
Self.Suggestion :=
46+
(Import =>
47+
VSS.Strings.Conversions.To_Unbounded_Wide_Wide_String (With_Clause),
48+
Qualifier =>
49+
VSS.Strings.Conversions.To_Unbounded_Wide_Wide_String (Prefix));
5050
end Initialize;
5151

5252
------------
@@ -84,11 +84,15 @@ package body LSP.Ada_Handlers.Refactor.Imports_Commands is
8484
elsif Key = "where" then
8585
Self.Where := From_Any (C);
8686

87-
elsif Key = "with_clause" then
88-
Self.With_Clause := Element (C).String_Value;
87+
elsif Key = "import" then
88+
Self.Suggestion.Import :=
89+
VSS.Strings.Conversions.To_Unbounded_Wide_Wide_String
90+
(From_Any (C));
8991

90-
elsif Key = "prefix" then
91-
Self.Prefix := Element (C).String_Value;
92+
elsif Key = "qualifier" then
93+
Self.Suggestion.Qualifier :=
94+
VSS.Strings.Conversions.To_Unbounded_Wide_Wide_String
95+
(From_Any (C));
9296

9397
else
9498
Skip_Value (C);
@@ -105,16 +109,16 @@ package body LSP.Ada_Handlers.Refactor.Imports_Commands is
105109
-------------------------------------
106110

107111
procedure Append_Suggestion
108-
(Self : in out Command;
109-
Context : LSP.Ada_Context_Sets.Context_Access;
110-
Where : LSP.Structures.Location;
111-
Commands_Vector : in out LSP.Structures.Command_Or_CodeAction_Vector;
112-
Suggestion : LAL_Refactor.Refactor_Imports.Import_Suggestion)
112+
(Self : in out Command;
113+
Context : LSP.Ada_Context_Sets.Context_Access;
114+
Where : LSP.Structures.Location;
115+
Commands_Vector : in out LSP.Structures.Command_Or_CodeAction_Vector;
116+
Suggestion : LAL_Refactor.Auto_Import.Import_Type)
113117
is
114118
Item : LSP.Structures.CodeAction;
115119

116120
function Create_Suggestion_Title
117-
(Suggestion : LAL_Refactor.Refactor_Imports.Import_Suggestion)
121+
(Suggestion : LAL_Refactor.Auto_Import.Import_Type)
118122
return VSS.Strings.Virtual_String;
119123
-- Creates the suggestion text that will be shown by the client to
120124
-- to the developer. The text is costumized based on the need of
@@ -125,39 +129,17 @@ package body LSP.Ada_Handlers.Refactor.Imports_Commands is
125129
------------------------------
126130

127131
function Create_Suggestion_Title
128-
(Suggestion : LAL_Refactor.Refactor_Imports.Import_Suggestion)
132+
(Suggestion : LAL_Refactor.Auto_Import.Import_Type)
129133
return VSS.Strings.Virtual_String
130134
is
131-
Title : Ada.Strings.Wide_Wide_Unbounded.Unbounded_Wide_Wide_String :=
132-
Ada.Strings.Wide_Wide_Unbounded.Null_Unbounded_Wide_Wide_String;
133-
use type Ada.Strings.Wide_Wide_Unbounded.Unbounded_Wide_Wide_String;
135+
use Ada.Strings.Wide_Wide_Unbounded;
134136

137+
Title : constant Langkit_Support.Text.Unbounded_Text_Type :=
138+
"Qualify with " & Suggestion.Qualifier;
135139
begin
136-
if Suggestion.With_Clause_Text /= "" then
137-
if Suggestion.Prefix_Text /= "" then
138-
-- Add with clause and prefix
139-
Title :=
140-
Title
141-
& "Add 'with' clause for "
142-
& Suggestion.With_Clause_Text
143-
& " and prefix the object with "
144-
& Suggestion.Prefix_Text;
145-
146-
else
147-
-- Add with clause and leave the prefix as it is
148-
Title :=
149-
Title
150-
& "Add 'with' clause for "
151-
& Suggestion.With_Clause_Text;
152-
end if;
153-
else
154-
-- Only add prefix
155-
156-
Title := Title & "Prefix the object with "
157-
& Suggestion.Prefix_Text;
158-
end if;
159-
return VSS.Strings.To_Virtual_String
160-
(Langkit_Support.Text.To_Text (Title));
140+
return
141+
VSS.Strings.To_Virtual_String
142+
(Langkit_Support.Text.To_Text (Title));
161143
end Create_Suggestion_Title;
162144

163145
begin
@@ -167,15 +149,15 @@ package body LSP.Ada_Handlers.Refactor.Imports_Commands is
167149
Where.a_range.start),
168150
With_Clause =>
169151
VSS.Strings.Conversions.To_Virtual_String
170-
(Suggestion.With_Clause_Text),
152+
(Suggestion.Import),
171153
Prefix =>
172154
VSS.Strings.Conversions.To_Virtual_String
173-
(Suggestion.Prefix_Text));
155+
(Suggestion.Qualifier));
174156

175157
Item :=
176158
(title => Create_Suggestion_Title (Suggestion),
177159
kind => (Is_Set => True,
178-
Value => LSP.Enumerations.RefactorRewrite),
160+
Value => LSP.Enumerations.QuickFix),
179161
diagnostics => <>,
180162
disabled => (Is_Set => False),
181163
edit => (Is_Set => False),
@@ -203,119 +185,21 @@ package body LSP.Ada_Handlers.Refactor.Imports_Commands is
203185
Document : LSP.Ada_Documents.Document_Access)
204186
return LAL_Refactor.Refactoring_Edits
205187
is
206-
use Langkit_Support.Text;
207188
use Libadalang.Analysis;
208-
use Libadalang.Common;
209-
use Libadalang.Slocs;
210-
use LAL_Refactor;
211-
use VSS.Strings;
212-
use VSS.Strings.Conversions;
189+
use LAL_Refactor.Auto_Import;
213190

214-
Node : Ada_Node :=
215-
Document.Get_Node_At (Context, Self.Where.position);
191+
Name : constant Libadalang.Analysis.Name :=
192+
Document.Get_Node_At (Context, Self.Where.position).As_Name;
216193

217-
Edits : LAL_Refactor.Refactoring_Edits;
194+
function Units return Analysis_Unit_Array is ([]);
218195

219196
begin
220-
-- Add prefix
221-
222-
if not Self.Prefix.Is_Empty
223-
and then Node.Kind in Ada_Identifier
224-
then
225-
-- If this is a DottedName them remove the current prefix and replace
226-
-- it by the suggested one. Otherwise, just add the prepend the
227-
-- prefix
228-
229-
while Node.Parent.Kind in Ada_Dotted_Name_Range loop
230-
Node := Node.Parent;
231-
end loop;
197+
return
198+
Create_Auto_Importer
199+
(Name,
200+
Self.Suggestion)
201+
.Refactor (Units'Access);
232202

233-
if Node.Kind in Ada_Dotted_Name_Range then
234-
Node := Node.As_Dotted_Name.F_Suffix.As_Ada_Node;
235-
end if;
236-
237-
if Node.Parent.Kind = Ada_Dotted_Name then
238-
-- Node.Parent is the full Dotted Name: this includes the
239-
-- current prefixes and the identifier. Using this SLOC instead
240-
-- of only the current prefixes SLOC is better since this covers
241-
-- cases when the Dotted Name is splitted in multiple lines.
242-
243-
Safe_Insert
244-
(Edits => Edits.Text_Edits,
245-
File_Name => Node.Unit.Get_Filename,
246-
Edit =>
247-
Text_Edit'
248-
(Location =>
249-
Make_Range
250-
(Start_Sloc
251-
(Node.Parent.As_Dotted_Name.F_Prefix.Sloc_Range),
252-
Start_Sloc (Node.Sloc_Range)),
253-
Text =>
254-
Ada.Strings.Unbounded.To_Unbounded_String
255-
(To_UTF8 (To_Wide_Wide_String (Self.Prefix)))));
256-
257-
else
258-
Safe_Insert
259-
(Edits => Edits.Text_Edits,
260-
File_Name => Node.Unit.Get_Filename,
261-
Edit =>
262-
Text_Edit'
263-
(Location =>
264-
Make_Range
265-
(Start_Sloc (Node.Sloc_Range),
266-
Start_Sloc (Node.Sloc_Range)),
267-
Text =>
268-
Ada.Strings.Unbounded.To_Unbounded_String
269-
(To_UTF8 (To_Wide_Wide_String (Self.Prefix)))));
270-
end if;
271-
end if;
272-
273-
-- Add with clause
274-
275-
if not Self.With_Clause.Is_Empty then
276-
declare
277-
Last : Boolean;
278-
S : constant Libadalang.Slocs.Source_Location :=
279-
Laltools.Common.Get_Insert_With_Location
280-
(Node => Laltools.Common.Get_Compilation_Unit (Node),
281-
Pack_Name =>
282-
VSS.Strings.Conversions.To_Wide_Wide_String
283-
(Self.With_Clause),
284-
Last => Last);
285-
begin
286-
if S /= Libadalang.Slocs.No_Source_Location then
287-
if Last then
288-
Safe_Insert
289-
(Edits => Edits.Text_Edits,
290-
File_Name => Node.Unit.Get_Filename,
291-
Edit =>
292-
Text_Edit'
293-
(Location => Make_Range (S, S),
294-
Text =>
295-
Ada.Strings.Unbounded.To_Unbounded_String
296-
(To_UTF8 (To_Wide_Wide_String
297-
(Document.Line_Terminator
298-
& "with " & Self.With_Clause & ";")))));
299-
300-
else
301-
Safe_Insert
302-
(Edits => Edits.Text_Edits,
303-
File_Name => Node.Unit.Get_Filename,
304-
Edit =>
305-
Text_Edit'
306-
(Location => Make_Range (S, S),
307-
Text =>
308-
Ada.Strings.Unbounded.To_Unbounded_String
309-
(To_UTF8 (To_Wide_Wide_String
310-
("with " & Self.With_Clause & ";"
311-
& Document.Line_Terminator)))));
312-
end if;
313-
314-
end if;
315-
end;
316-
end if;
317-
318-
return Edits;
319203
end Command_To_Refactoring_Edits;
320204

321205
--------------
@@ -351,6 +235,7 @@ package body LSP.Ada_Handlers.Refactor.Imports_Commands is
351235
use VSS.JSON.Streams;
352236

353237
Result : LSP.Structures.LSPAny_Vector;
238+
354239
begin
355240
Result.Append (JSON_Stream_Element'(Kind => Start_Array));
356241
Result.Append (JSON_Stream_Element'(Kind => Start_Object));
@@ -363,18 +248,22 @@ package body LSP.Ada_Handlers.Refactor.Imports_Commands is
363248
Add_Key ("where", Result);
364249
To_Any (Self.Where, Result);
365250

366-
-- "with_clause"
367-
Add_Key ("with_clause", Result);
368-
To_Any (Self.With_Clause, Result);
251+
-- "import"
252+
Add_Key ("import", Result);
253+
To_Any
254+
(VSS.Strings.Conversions.To_Virtual_String (Self.Suggestion.Import),
255+
Result);
369256

370-
-- "prefix"
371-
Add_Key ("prefix", Result);
372-
To_Any (Self.Prefix, Result);
257+
-- "qualifier"
258+
Add_Key ("qualifier", Result);
259+
To_Any
260+
(VSS.Strings.Conversions.To_Virtual_String (Self.Suggestion.Qualifier),
261+
Result);
373262

374263
Result.Append (JSON_Stream_Element'(Kind => End_Object));
375264
Result.Append (JSON_Stream_Element'(Kind => End_Array));
376265

377266
return Result;
378267
end Write_Command;
379268

380-
end LSP.Ada_Handlers.Refactor.Imports_Commands;
269+
end LSP.Ada_Handlers.Refactor.Auto_Import;

source/ada/lsp-ada_handlers-refactor-imports_commands.ads renamed to source/ada/lsp-ada_handlers-refactor-auto_import.ads

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@
1818
-- Implementation of the command to refactor imports.
1919

2020
with LAL_Refactor;
21-
with LAL_Refactor.Refactor_Imports;
21+
with LAL_Refactor.Auto_Import;
2222

2323
with LSP.Ada_Contexts;
2424

25-
package LSP.Ada_Handlers.Refactor.Imports_Commands is
25+
package LSP.Ada_Handlers.Refactor.Auto_Import is
2626

2727
type Command is new LSP.Ada_Handlers.Refactor.Command with private;
2828

2929
overriding function Name (Self : Command) return String
3030
is
31-
("Imports Command");
31+
("Auto Import");
3232

3333
procedure Initialize
3434
(Self : in out Command'Class;
@@ -43,17 +43,16 @@ package LSP.Ada_Handlers.Refactor.Imports_Commands is
4343
Context : LSP.Ada_Context_Sets.Context_Access;
4444
Where : LSP.Structures.Location;
4545
Commands_Vector : in out LSP.Structures.Command_Or_CodeAction_Vector;
46-
Suggestion : LAL_Refactor.Refactor_Imports.Import_Suggestion);
46+
Suggestion : LAL_Refactor.Auto_Import.Import_Type);
4747
-- Initializes Command based on Suggestion and appends it to
4848
-- Commands_Vector.
4949

5050
private
5151

5252
type Command is new LSP.Ada_Handlers.Refactor.Command with record
53-
Context : VSS.Strings.Virtual_String;
54-
Where : LSP.Structures.TextDocumentPositionParams;
55-
With_Clause : VSS.Strings.Virtual_String;
56-
Prefix : VSS.Strings.Virtual_String;
53+
Context : VSS.Strings.Virtual_String;
54+
Where : LSP.Structures.TextDocumentPositionParams;
55+
Suggestion : LAL_Refactor.Auto_Import.Import_Type;
5756
end record;
5857

5958
overriding function Create
@@ -76,6 +75,6 @@ private
7675
-- Converts Self into LAL_Refactor.Refactoring_Edits that can be
7776
-- converted in a WorkspaceEdit.
7877

79-
for Command'External_Tag use "als-refactor-imports";
78+
for Command'External_Tag use "als-auto-import";
8079

81-
end LSP.Ada_Handlers.Refactor.Imports_Commands;
80+
end LSP.Ada_Handlers.Refactor.Auto_Import;

0 commit comments

Comments
 (0)