Skip to content

Commit 7512748

Browse files
Merge branch 'topic/eng/ide/ada_language_server#1159' into 'master'
Add with-clause for invisible completion See merge request eng/ide/ada_language_server!1569
2 parents bed3a36 + a95742d commit 7512748

File tree

26 files changed

+713
-9
lines changed

26 files changed

+713
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ The Ada Language Server now also supports the GPR language, via the
2828
such as navigation, outline and tooltips for GPR files. When this switch is
2929
present, the server will only support GPR files. To support both GPR and
3030
Ada/SPARK, you'll need to launch two instances of the server.
31-
You can refer to the [Supported LSP Server Requests](#supported-lsp-server-requests)
31+
You can refer to the [Supported LSP Server Requests](#supported-lsp-server-requests)
3232
section for more information.
3333

3434
We also provide [Visual Studio Code](https://code.visualstudio.com/)

doc/settings.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ Ada Language Server understands these settings:
6262
* [renameInComments](#renameincomments)
6363
* [namedNotationThreshold](#namednotationthreshold)
6464
* [foldComments](#foldcomments)
65+
* [useCompletionSnippets](#usecompletionsnippets)
66+
* [insertWithClauses](#insertwithclauses)
6567
* [followSymlinks](#followsymlinks)
6668
* [documentationStyle](#documentationstyle)
6769
* [onTypeFormatting.indentOnly](#ontypeformattingindentonly)
@@ -172,6 +174,15 @@ The value is a boolean.
172174
'useCompletionSnippets': true
173175
```
174176

177+
## insertWithClauses
178+
179+
Whether we should automatically insert missing with-clauses when
180+
accepting completion for invisible symbols.
181+
182+
```javascript
183+
'insertWithClauses': true
184+
```
185+
175186
## displayMethodAncestryOnNavigation
176187

177188
This setting controls the policy for displaying overriding and overridden

integration/vscode/ada/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,12 @@
391391
"default": false,
392392
"description": "Enable snippets in completion results (e.g. subprogram calls)."
393393
},
394+
"ada.insertWithClauses": {
395+
"scope": "window",
396+
"type": "boolean",
397+
"default": false,
398+
"description": "Enable insertion of missing with-clauses when accepting completion for invisible symbols."
399+
},
394400
"ada.renameInComments": {
395401
"scope": "window",
396402
"type": "boolean",

source/ada/lsp-ada_configurations.adb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,11 @@ package body LSP.Ada_Configurations is
277277
then
278278
Self.Use_Completion_Snippets := JSON (Index).Boolean_Value;
279279

280+
elsif Name = "insertWithClauses"
281+
and then JSON (Index).Kind = Boolean_Value
282+
then
283+
Self.Insert_With_Clauses := JSON (Index).Boolean_Value;
284+
280285
elsif Name = "logThreshold"
281286
and then JSON (Index).Kind = Number_Value
282287
and then JSON (Index).Number_Value.Kind = JSON_Integer

source/ada/lsp-ada_configurations.ads

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ package LSP.Ada_Configurations is
7878
-- True if we should use snippets for completion (e.g:
7979
-- subprogram calls).
8080

81+
function Insert_With_Clauses
82+
(Self : Configuration'Class) return Boolean;
83+
-- True if completion is allowed to insert automatically with-clauses for
84+
-- invisible symbols.
85+
8186
function Indent_Only (Self : Configuration'Class) return Boolean;
8287

8388
function Follow_Symlinks (Self : Configuration'Class) return Boolean;
@@ -130,6 +135,7 @@ private
130135
Use_Completion_Snippets : Boolean := True;
131136
Indent_Only : Boolean := True;
132137
Follow_Symlinks : Boolean := True;
138+
Insert_With_Clauses : Boolean := True;
133139

134140
Documentation_Style : GNATdoc.Comments.Options.Documentation_Style
135141
:= GNATdoc.Comments.Options.GNAT;
@@ -169,8 +175,14 @@ private
169175
(Self.Indexing_Enabled);
170176

171177
function Use_Completion_Snippets
172-
(Self : Configuration'Class) return Boolean is
173-
(Self.Use_Completion_Snippets);
178+
(Self : Configuration'Class) return Boolean
179+
is
180+
(Self.Use_Completion_Snippets);
181+
182+
function Insert_With_Clauses
183+
(Self : Configuration'Class) return Boolean
184+
is
185+
(Self.Insert_With_Clauses);
174186

175187
function Follow_Symlinks (Self : Configuration'Class) return Boolean is
176188
(Self.Follow_Symlinks);

source/ada/lsp-ada_documents.adb

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ with LSP.Ada_Contexts;
4343
with LSP.Ada_Documentation;
4444
with LSP.Ada_Documents.LAL_Diagnostics;
4545
with LSP.Ada_Handlers.Locations;
46+
with LSP.Ada_Handlers.Refactor.Auto_Import;
4647
with LSP.Ada_Id_Iterators;
4748
with LSP.Enumerations;
4849
with 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);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ package body LSP.Ada_Handlers.Refactor.Auto_Import is
170170
(title => <>,
171171
command => VSS.Strings.Conversions.
172172
To_Virtual_String (Command'External_Tag),
173-
arguments => Self.Write_Command)),
173+
arguments => Self.Write_Command_Args)),
174174
data => <>);
175175

176176
Commands_Vector.Append
@@ -232,7 +232,7 @@ package body LSP.Ada_Handlers.Refactor.Auto_Import is
232232
-- Write_Command --
233233
-------------------
234234

235-
function Write_Command
235+
function Write_Command_Args
236236
(Self : Command) return LSP.Structures.LSPAny_Vector
237237
is
238238
use VSS.JSON.Streams;
@@ -267,6 +267,6 @@ package body LSP.Ada_Handlers.Refactor.Auto_Import is
267267
Result.Append (JSON_Stream_Element'(Kind => End_Array));
268268

269269
return Result;
270-
end Write_Command;
270+
end Write_Command_Args;
271271

272272
end LSP.Ada_Handlers.Refactor.Auto_Import;

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ package LSP.Ada_Handlers.Refactor.Auto_Import is
4848
-- Initializes Command based on Suggestion and appends it to
4949
-- Commands_Vector.
5050

51+
function Write_Command_Args
52+
(Self : Command) return LSP.Structures.LSPAny_Vector;
53+
-- Write the given command's arguments as LPS any vector.
54+
-- These arguments are then passed to the server by the LSP client when
55+
-- executing the command via the executeCommand LSP request.
56+
5157
private
5258

5359
type Command is new LSP.Ada_Handlers.Refactor.Command with record
@@ -69,9 +75,6 @@ private
6975
return LSP.Server_Jobs.Job_Priority
7076
is (LSP.Server_Jobs.Low);
7177

72-
function Write_Command
73-
(Self : Command) return LSP.Structures.LSPAny_Vector;
74-
7578
function Command_To_Refactoring_Edits
7679
(Self : Command;
7780
Context : LSP.Ada_Contexts.Context;

source/server/lsp-text_documents-langkit_documents.adb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,19 @@ package body LSP.Text_Documents.Langkit_Documents is
103103
character => To_LSP_Column
104104
(End_Line_Text, A_Range.End_Column, False)));
105105

106+
---------------------
107+
-- To_LSP_Position --
108+
---------------------
109+
110+
function To_LSP_Position
111+
(Self : Langkit_Text_Document'Class;
112+
Sloc : Langkit_Support.Slocs.Source_Location)
113+
return LSP.Structures.Position
114+
is
115+
(line => To_LSP_Line (Sloc.Line),
116+
character => To_LSP_Column
117+
(Self.Line (Sloc.Line), Sloc.Column, False));
118+
106119
-------------------
107120
-- To_LSP_Column --
108121
-------------------

source/server/lsp-text_documents-langkit_documents.ads

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ package LSP.Text_Documents.Langkit_Documents is
7070
return LSP.Structures.A_Range;
7171
-- Convert Langkit Source_Location_Range to LSP's A_Range
7272

73+
function To_LSP_Position
74+
(Self : Langkit_Text_Document'Class;
75+
Sloc : Langkit_Support.Slocs.Source_Location)
76+
return LSP.Structures.Position;
77+
-- Convert a Langkit Source_Location to the corresponding LSP position.
78+
7379
private
7480

7581
type Langkit_Text_Document is abstract new Text_Document with null record;

0 commit comments

Comments
 (0)