15
15
-- of the license. --
16
16
-- ----------------------------------------------------------------------------
17
17
18
- with Ada.Strings.Unbounded ;
19
18
with Ada.Strings.Wide_Wide_Unbounded ;
20
19
21
20
with Langkit_Support.Text ;
22
21
23
22
with Libadalang.Analysis ;
24
- with Libadalang.Common ;
25
-
26
- with Laltools.Common ;
27
23
28
24
with VSS.JSON.Streams ;
25
+ with VSS.Strings.Conversions ;
29
26
30
27
with LSP.Enumerations ;
31
28
with LSP.Structures.LSPAny_Vectors ; use LSP.Structures.LSPAny_Vectors;
32
29
33
- package body LSP.Ada_Handlers.Refactor.Imports_Commands is
30
+ package body LSP.Ada_Handlers.Refactor.Auto_Import is
34
31
35
32
-- --------------
36
33
-- Initialize --
@@ -45,8 +42,11 @@ package body LSP.Ada_Handlers.Refactor.Imports_Commands is
45
42
begin
46
43
Self.Context := Context.Id;
47
44
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));
50
50
end Initialize ;
51
51
52
52
-- ----------
@@ -84,11 +84,15 @@ package body LSP.Ada_Handlers.Refactor.Imports_Commands is
84
84
elsif Key = " where" then
85
85
Self.Where := From_Any (C);
86
86
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));
89
91
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));
92
96
93
97
else
94
98
Skip_Value (C);
@@ -105,16 +109,16 @@ package body LSP.Ada_Handlers.Refactor.Imports_Commands is
105
109
-- -----------------------------------
106
110
107
111
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 )
113
117
is
114
118
Item : LSP.Structures.CodeAction;
115
119
116
120
function Create_Suggestion_Title
117
- (Suggestion : LAL_Refactor.Refactor_Imports.Import_Suggestion )
121
+ (Suggestion : LAL_Refactor.Auto_Import.Import_Type )
118
122
return VSS.Strings.Virtual_String;
119
123
-- Creates the suggestion text that will be shown by the client to
120
124
-- 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
125
129
-- ----------------------------
126
130
127
131
function Create_Suggestion_Title
128
- (Suggestion : LAL_Refactor.Refactor_Imports.Import_Suggestion )
132
+ (Suggestion : LAL_Refactor.Auto_Import.Import_Type )
129
133
return VSS.Strings.Virtual_String
130
134
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;
134
136
137
+ Title : constant Langkit_Support.Text.Unbounded_Text_Type :=
138
+ " Qualify with " & Suggestion.Qualifier;
135
139
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));
161
143
end Create_Suggestion_Title ;
162
144
163
145
begin
@@ -167,15 +149,15 @@ package body LSP.Ada_Handlers.Refactor.Imports_Commands is
167
149
Where.a_range.start),
168
150
With_Clause =>
169
151
VSS.Strings.Conversions.To_Virtual_String
170
- (Suggestion.With_Clause_Text ),
152
+ (Suggestion.Import ),
171
153
Prefix =>
172
154
VSS.Strings.Conversions.To_Virtual_String
173
- (Suggestion.Prefix_Text ));
155
+ (Suggestion.Qualifier ));
174
156
175
157
Item :=
176
158
(title => Create_Suggestion_Title (Suggestion),
177
159
kind => (Is_Set => True,
178
- Value => LSP.Enumerations.RefactorRewrite ),
160
+ Value => LSP.Enumerations.QuickFix ),
179
161
diagnostics => <>,
180
162
disabled => (Is_Set => False),
181
163
edit => (Is_Set => False),
@@ -203,119 +185,21 @@ package body LSP.Ada_Handlers.Refactor.Imports_Commands is
203
185
Document : LSP.Ada_Documents.Document_Access)
204
186
return LAL_Refactor.Refactoring_Edits
205
187
is
206
- use Langkit_Support.Text;
207
188
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;
213
190
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 ;
216
193
217
- Edits : LAL_Refactor.Refactoring_Edits ;
194
+ function Units return Analysis_Unit_Array is ([]) ;
218
195
219
196
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 );
232
202
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;
319
203
end Command_To_Refactoring_Edits ;
320
204
321
205
-- ------------
@@ -351,6 +235,7 @@ package body LSP.Ada_Handlers.Refactor.Imports_Commands is
351
235
use VSS.JSON.Streams;
352
236
353
237
Result : LSP.Structures.LSPAny_Vector;
238
+
354
239
begin
355
240
Result.Append (JSON_Stream_Element'(Kind => Start_Array));
356
241
Result.Append (JSON_Stream_Element'(Kind => Start_Object));
@@ -363,18 +248,22 @@ package body LSP.Ada_Handlers.Refactor.Imports_Commands is
363
248
Add_Key (" where" , Result);
364
249
To_Any (Self.Where, Result);
365
250
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);
369
256
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);
373
262
374
263
Result.Append (JSON_Stream_Element'(Kind => End_Object));
375
264
Result.Append (JSON_Stream_Element'(Kind => End_Array));
376
265
377
266
return Result;
378
267
end Write_Command ;
379
268
380
- end LSP.Ada_Handlers.Refactor.Imports_Commands ;
269
+ end LSP.Ada_Handlers.Refactor.Auto_Import ;
0 commit comments