@@ -132,10 +132,113 @@ package body LSP.Ada_Documents is
132
132
-- Return a suitable sortText according to the completion item's
133
133
-- visibility and position in the completion list.
134
134
135
+ procedure Get_Missing_Unit_And_Qualifier
136
+ (Missing_Unit_Name : out VSS.Strings.Virtual_String;
137
+ Missing_Qualifier : out VSS.Strings.Virtual_String);
138
+ -- Get the missing unit name and qualifier (if needed) for invisible
139
+ -- completion items.
140
+
135
141
procedure Append_Auto_Import_Command ;
136
142
-- Append the needed command to add the missing with-clause/qualifier
137
143
-- when accepting an invisible completion item.
138
144
145
+ -- ----------------------------------
146
+ -- Get_Missing_unit_and_qualifier --
147
+ -- ----------------------------------
148
+
149
+ procedure Get_Missing_Unit_And_Qualifier
150
+ (Missing_Unit_Name : out VSS.Strings.Virtual_String;
151
+ Missing_Qualifier : out VSS.Strings.Virtual_String)
152
+ is
153
+ use Libadalang.Analysis;
154
+
155
+ Prefix : constant VSS.Strings.Virtual_String :=
156
+ VSS.Strings.Conversions.To_Virtual_String
157
+ (Langkit_Support.Text.To_UTF8 (Node.Text));
158
+
159
+ Dotted_Node : constant Ada_Node :=
160
+ (if Node.Kind in Libadalang.Common.Ada_Dotted_Name_Range then
161
+ Node
162
+ else
163
+ Node.Parent);
164
+
165
+ Is_Dotted_Name : constant Boolean :=
166
+ not Dotted_Node.Is_Null and then
167
+ Dotted_Node.Kind in Libadalang.Common.Ada_Dotted_Name_Range;
168
+ -- Check if we are completing a dotted name. We want to prepend the
169
+ -- right qualifier only if it's not the case.
170
+
171
+ Dotted_Node_Prefix : VSS.Strings.Virtual_String :=
172
+ (if Is_Dotted_Name then
173
+ VSS.Strings.Conversions.To_Virtual_String
174
+ (Langkit_Support.Text.To_UTF8
175
+ (Dotted_Node.As_Dotted_Name.F_Prefix.Text))
176
+ else
177
+ VSS.Strings.Empty_Virtual_String);
178
+ -- The prefix of the dotted name we are completion, or an empty
179
+ -- string if we are not completing a dotted name.
180
+
181
+ Missing_Unit_Root_Decl : constant Libadalang.Analysis.Basic_Decl :=
182
+ BD.P_Enclosing_Compilation_Unit.P_Decl;
183
+ -- The missing unit root declaration for this invisible symbol (e.g:
184
+ -- the "Ada.Text_IO" package declaration for the
185
+ -- "Ada.Text_IO.Put_Line" subprogram).
186
+
187
+ begin
188
+ Missing_Unit_Name := VSS.Strings.Conversions.To_Virtual_String
189
+ (Langkit_Support.Text.To_UTF8
190
+ (Missing_Unit_Root_Decl.P_Fully_Qualified_Name));
191
+
192
+ -- We are completing a dotted name but its prefix does not match
193
+ -- with the completion item's defining name's unit: this means we
194
+ -- are dealing with renames (e.g: 'GNAT.Strings.Strings_Access'
195
+ -- is a forward declaration of 'System.Strings.String_Access'). In
196
+ -- that case, use the prefix specified by the user instead of the
197
+ -- completion item's defining name's unit: the user explcitly wants
198
+ -- to use the renamed symbol instead of the base one.
199
+
200
+ if Is_Dotted_Name
201
+ and then not Missing_Unit_Name.Starts_With (Dotted_Node_Prefix)
202
+ then
203
+ declare
204
+ Dotted_Prefix_Parts : VSS.String_Vectors.
205
+ Virtual_String_Vector :=
206
+ Dotted_Node_Prefix.Split
207
+ (Separator => VSS.Characters.Latin.Full_Stop);
208
+ begin
209
+ -- Check if the unit specified as a prefix actually exists.
210
+ -- If not, it might be a renamed package
211
+ -- declaration/instantiation: in that case we want to add a
212
+ -- with-clause on the enclosing unit (e.g: the prefix before
213
+ -- the last '.').
214
+
215
+ while Get_From_Provider
216
+ (Context => Context.LAL_Context,
217
+ Name => Langkit_Support.Text.To_Text
218
+ (VSS.Strings.Conversions.To_UTF_8_String
219
+ (Dotted_Node_Prefix)),
220
+ Kind => Libadalang.Common.Unit_Specification).Root.Is_Null
221
+ loop
222
+ Dotted_Prefix_Parts.Delete_Last;
223
+ Dotted_Node_Prefix :=
224
+ Dotted_Prefix_Parts.Join (VSS.Characters.Latin.Full_Stop);
225
+ end loop ;
226
+
227
+ Missing_Unit_Name := Dotted_Node_Prefix;
228
+ end ;
229
+ end if ;
230
+
231
+ -- We should not add any qualifier if the user accepted the
232
+ -- completion item corresponding to the missing unit itself (e.g: if
233
+ -- the user selects "Ada.Text_IO" in the completion window, we do not
234
+ -- need to add any qualifier) or if he's completing a dotted name.
235
+ Missing_Qualifier :=
236
+ (if Is_Dotted_Name or else BD = Missing_Unit_Root_Decl then
237
+ VSS.Strings.Empty_Virtual_String
238
+ else
239
+ Missing_Unit_Name);
240
+ end Get_Missing_Unit_And_Qualifier ;
241
+
139
242
-- -----------------
140
243
-- Get_Sort_Text --
141
244
-- -----------------
@@ -166,43 +269,15 @@ package body LSP.Ada_Documents is
166
269
167
270
procedure Append_Auto_Import_Command is
168
271
use LSP.Ada_Handlers.Refactor;
169
- use Libadalang.Analysis;
170
272
171
273
Auto_Import_Command : Auto_Import.Command;
172
274
-- The auto-import command.
173
275
174
- Is_Dotted_Name : constant Boolean :=
175
- Node.Kind in Libadalang.Common.Ada_Dotted_Name_Range
176
- or else
177
- (not Node.Parent.Is_Null and then
178
- Node.Parent.Kind
179
- in Libadalang.Common.Ada_Dotted_Name_Range);
180
- -- Check if we are completing a dotted name. We want to prepend the
181
- -- right qualifier only if it's not the case.
182
-
183
- Missing_Unit_Root_Decl : constant Libadalang.Analysis.Basic_Decl :=
184
- BD.P_Enclosing_Compilation_Unit.P_Decl;
185
- -- The missing unit root declaration for this invisible symbol (e.g:
186
- -- the "Ada.Text_IO" package declaration for the
187
- -- "Ada.Text_IO.Put_Line" subprogram).
188
-
189
- Missing_Unit_Name : VSS.Strings.Virtual_String :=
190
- VSS.Strings.Conversions.To_Virtual_String
191
- (Langkit_Support.Text.To_UTF8
192
- (Missing_Unit_Root_Decl.P_Fully_Qualified_Name));
193
- -- Get the missing unit name.
194
-
195
- Missing_Qualifier : VSS.Strings.Virtual_String :=
196
- (if Is_Dotted_Name or else BD = Missing_Unit_Root_Decl then
197
- VSS.Strings.Empty_Virtual_String
198
- else
199
- Missing_Unit_Name);
200
- -- The missing qualifier. We should not add any qualifier if the
201
- -- user accepted the completion item corresponding to the missing
202
- -- unit itself (e.g: if the user selects "Ada.Text_IO" in the
203
- -- completion window, we do not need to add any qualifier) or if
204
- -- he's completing a dotted name.
276
+ Missing_Unit_Name : VSS.Strings.Virtual_String;
277
+ Missing_Qualifier : VSS.Strings.Virtual_String;
205
278
begin
279
+ Get_Missing_Unit_And_Qualifier (Missing_Unit_Name, Missing_Qualifier);
280
+
206
281
Auto_Import_Command.Initialize
207
282
(Context => Context,
208
283
Where =>
0 commit comments