@@ -43,6 +43,7 @@ with LSP.Ada_Contexts;
4343with LSP.Ada_Documentation ;
4444with LSP.Ada_Documents.LAL_Diagnostics ;
4545with LSP.Ada_Handlers.Locations ;
46+ with LSP.Ada_Handlers.Refactor.Auto_Import ;
4647with LSP.Ada_Id_Iterators ;
4748with LSP.Enumerations ;
4849with LSP.Formatters.File_Names ;
@@ -131,6 +132,10 @@ package body LSP.Ada_Documents is
131132 -- Return a suitable sortText according to the completion item's
132133 -- visibility and position in the completion list.
133134
135+ procedure Append_Auto_Import_Command ;
136+ -- Append the needed command to add the missing with-clause/qualifier
137+ -- when accepting an invisible completion item.
138+
134139 -- -----------------
135140 -- Get_Sort_Text --
136141 -- -----------------
@@ -155,6 +160,57 @@ package body LSP.Ada_Documents is
155160 end return ;
156161 end Get_Sort_Text ;
157162
163+ -- ------------------------------
164+ -- Append_Auto_Import_Command --
165+ -- ------------------------------
166+
167+ procedure Append_Auto_Import_Command is
168+ use LSP.Ada_Handlers.Refactor;
169+
170+ Auto_Import_Command : Auto_Import.Command;
171+ -- The auto-import command.
172+
173+ Is_Dotted_Name : constant Boolean :=
174+ Node.Kind in Libadalang.Common.Ada_Dotted_Name_Range
175+ or else
176+ (not Node.Parent.Is_Null and then
177+ Node.Parent.Kind
178+ in Libadalang.Common.Ada_Dotted_Name_Range);
179+ -- Check if we are completing a dotted name. We want to prepend the
180+ -- right qualifier only if it's not the case.
181+
182+ Missing_Unit_Name : VSS.Strings.Virtual_String :=
183+ VSS.Strings.Conversions.To_Virtual_String
184+ (Langkit_Support.Text.To_UTF8
185+ (BD.P_Enclosing_Compilation_Unit.P_Decl.
186+ P_Fully_Qualified_Name));
187+ -- Get the missing unit name.
188+
189+ Missing_Qualifier : VSS.Strings.Virtual_String :=
190+ (if not Is_Dotted_Name then
191+ Missing_Unit_Name
192+ else
193+ VSS.Strings.Empty_Virtual_String);
194+ -- The missing qualifier. We should not add any qualifier if it's
195+ -- already present (i.e: when completing a dotted name).
196+ begin
197+ Auto_Import_Command.Initialize
198+ (Context => Context,
199+ Where =>
200+ ((uri => Document.URI),
201+ Document.To_LSP_Position (Sloc)),
202+ With_Clause => Missing_Unit_Name,
203+ Prefix => Missing_Qualifier);
204+
205+ Item.command :=
206+ (Is_Set => True,
207+ Value =>
208+ (title => <>,
209+ command => VSS.Strings.Conversions.
210+ To_Virtual_String (Auto_Import.Command'External_Tag),
211+ arguments => Auto_Import_Command.Write_Command_Args));
212+ end Append_Auto_Import_Command ;
213+
158214 begin
159215 Item.label := Label;
160216 Item.kind := (True, To_Completion_Kind (LSP.Utils.Get_Decl_Kind (BD)));
@@ -163,6 +219,12 @@ package body LSP.Ada_Documents is
163219 Item.insertText := Label;
164220 Item.label.Append (" (invisible)" );
165221 Item.filterText := Label;
222+
223+ -- If the corresponding setting is enabled, append a command to
224+ -- insert the missing with-clause/qualifier.
225+ if Handler.Get_Configuration.Insert_With_Clauses then
226+ Append_Auto_Import_Command;
227+ end if ;
166228 end if ;
167229
168230 Item.sortText := Get_Sort_Text (Label);
0 commit comments