Skip to content

Commit 42f6ca9

Browse files
author
automatic-merge
committed
Merge remote branch 'origin/master' into edge
2 parents d6bd568 + 7b9e69e commit 42f6ca9

File tree

8 files changed

+508
-695
lines changed

8 files changed

+508
-695
lines changed

Makefile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ ROOTDIR=$(shell pwd)
33

44
# ALS version
55
VERSION ?=
6+
7+
# ALS build date
8+
BUILD_DATE ?=
9+
610
# Location of home dir for tests
711
export ALS_HOME=$(ROOTDIR)/testsuite
812

@@ -91,7 +95,7 @@ all: coverage-instrument
9195
$(GPRBUILD) -P gnat/tester.gpr -p $(BUILD_FLAGS)
9296
$(GPRBUILD) -d -ws -c -u -P gnat/lsp_server.gpr -p $(BUILD_FLAGS) s-memory.adb
9397
$(GPRBUILD) -P gnat/lsp_server.gpr -p $(COVERAGE_BUILD_FLAGS) \
94-
-XVERSION=$(VERSION)
98+
-XVERSION=$(VERSION) -XBUILD_DATE=$(BUILD_DATE)
9599
$(GPRBUILD) -P gnat/codec_test.gpr -p $(COVERAGE_BUILD_FLAGS)
96100
$(GPRBUILD) -P gnat/lsp_client.gpr -p $(COVERAGE_BUILD_FLAGS) \
97101
-XVERSION=$(VERSION)
@@ -111,7 +115,7 @@ ifneq ($(COVERAGE),)
111115
# Remove artifacts from previous instrumentations, so that stale units that
112116
# are not overriden by new ones don't get in our way.
113117
rm -rf .obj/*/gnatcov-instr
114-
$(COVERAGE_INSTR) -XVERSION=$(VERSION) \
118+
$(COVERAGE_INSTR) -XVERSION=$(VERSION) XBUILD_DATE=$(BUILD_DATE) \
115119
-Pgnat/lsp_server.gpr --projects lsp_server --projects lsp
116120
$(COVERAGE_INSTR) -Pgnat/tester.gpr --projects lsp
117121
$(COVERAGE_INSTR) -Pgnat/codec_test.gpr --projects lsp

gnat/lsp_server.gpr

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ project LSP_Server is
2929

3030
VERSION := external ("VERSION", "latest");
3131

32+
BUILD_DATE := external ("BUILD_DATE", "unknown");
33+
3234
type Library_Kind is ("static", "static-pic", "relocatable");
3335
Library_Type : Library_Kind := external("ALS_LIBRARY_TYPE",
3436
external("LIBRARY_TYPE", "relocatable"));
@@ -50,7 +52,8 @@ project LSP_Server is
5052
package Compiler is
5153
for Default_Switches ("Ada") use LSP.Compiler'Default_Switches ("Ada");
5254
for Switches ("lsp-ada_driver.adb") use
53-
LSP.Compiler'Default_Switches ("Ada") & ("-gnateDVERSION=""" & VERSION & """");
55+
LSP.Compiler'Default_Switches ("Ada") &
56+
("-gnateDVERSION=""" & VERSION & """", "-gnateDBUILD_DATE=""" & BUILD_DATE & """");
5457
for Switches ("s-memory.adb") use ("-g", "-O2", "-gnatpg");
5558
for Local_Configuration_Pragmas use "gnat.adc";
5659
end Compiler;

source/ada/lsp-ada_driver.adb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ with LSP.Ada_Handlers;
4444
with LSP.Ada_Handlers.Named_Parameters_Commands;
4545
with LSP.Ada_Handlers.Other_File_Commands;
4646
with LSP.Ada_Handlers.Project_Reload_Commands;
47-
with LSP.Ada_Handlers.Refactor.Imports_Commands;
47+
with LSP.Ada_Handlers.Refactor.Auto_Import;
4848
with LSP.Ada_Handlers.Refactor.Add_Parameter;
4949
with LSP.Ada_Handlers.Refactor.Remove_Parameter;
5050
with LSP.Ada_Handlers.Refactor.Move_Parameter;
@@ -148,7 +148,7 @@ procedure LSP.Ada_Driver is
148148
LSP.Commands.Register
149149
(LSP.Ada_Handlers.Named_Parameters_Commands.Command'Tag);
150150
LSP.Commands.Register
151-
(LSP.Ada_Handlers.Refactor.Imports_Commands.Command'Tag);
151+
(LSP.Ada_Handlers.Refactor.Auto_Import.Command'Tag);
152152
LSP.Commands.Register
153153
(LSP.Ada_Handlers.Refactor.Suppress_Seperate.Command'Tag);
154154
LSP.Commands.Register
@@ -243,7 +243,8 @@ begin
243243
VSS.Command_Line.Process; -- Will exit if errors or help requested.
244244

245245
if VSS.Command_Line.Is_Specified (Version_Option) then
246-
Ada.Text_IO.Put_Line ("ALS version: " & $VERSION);
246+
Ada.Text_IO.Put_Line
247+
("ALS version: " & $VERSION & " (" & $BUILD_DATE & ")");
247248
GNAT.OS_Lib.OS_Exit (0);
248249
end if;
249250

@@ -316,7 +317,7 @@ begin
316317
end;
317318
end if;
318319

319-
Server_Trace.Trace ("ALS version: " & $VERSION);
320+
Server_Trace.Trace ("ALS version: " & $VERSION & " (" & $BUILD_DATE & ")");
320321

321322
Server_Trace.Trace ("Initializing server ...");
322323

Lines changed: 57 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
------------------------------------------------------------------------------
22
-- Language Server Protocol --
33
-- --
4-
-- Copyright (C) 2020-2022, AdaCore --
4+
-- Copyright (C) 2020-2023, AdaCore --
55
-- --
66
-- This is free software; you can redistribute it and/or modify it under --
77
-- terms of the GNU General Public License as published by the Free Soft- --
@@ -16,22 +16,18 @@
1616
------------------------------------------------------------------------------
1717

1818
with Ada.Strings.UTF_Encoding;
19-
with Ada.Strings.Unbounded;
2019
with Ada.Strings.Wide_Wide_Unbounded;
2120

2221
with Langkit_Support.Text;
2322

2423
with Libadalang.Analysis;
25-
with Libadalang.Common;
26-
27-
with Laltools.Common;
2824

25+
with LSP.Commands;
2926
with LSP.Messages;
3027

3128
with VSS.Strings.Conversions;
32-
with LSP.Commands;
3329

34-
package body LSP.Ada_Handlers.Refactor.Imports_Commands is
30+
package body LSP.Ada_Handlers.Refactor.Auto_Import is
3531

3632
----------------
3733
-- Initialize --
@@ -46,8 +42,11 @@ package body LSP.Ada_Handlers.Refactor.Imports_Commands is
4642
begin
4743
Self.Context := Context.Id;
4844
Self.Where := Where;
49-
Self.With_Clause := With_Clause;
50-
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));
5150
end Initialize;
5251

5352
------------
@@ -74,10 +73,25 @@ package body LSP.Ada_Handlers.Refactor.Imports_Commands is
7473
LSP.Types.Read_String (JS, V.Context);
7574
elsif Key = "where" then
7675
LSP.Messages.TextDocumentPositionParams'Read (JS, V.Where);
77-
elsif Key = "with_clause" then
78-
LSP.Types.Read_String (JS, V.With_Clause);
79-
elsif Key = "prefix" then
80-
LSP.Types.Read_String (JS, V.Prefix);
76+
elsif Key = "import" then
77+
declare
78+
Import : VSS.Strings.Virtual_String;
79+
begin
80+
LSP.Types.Read_String (JS, Import);
81+
V.Suggestion.Import :=
82+
VSS.Strings.Conversions.To_Unbounded_Wide_Wide_String
83+
(Import);
84+
end;
85+
86+
elsif Key = "qualifier" then
87+
declare
88+
Qualififer : VSS.Strings.Virtual_String;
89+
begin
90+
LSP.Types.Read_String (JS, Qualififer);
91+
V.Suggestion.Qualifier :=
92+
VSS.Strings.Conversions.To_Unbounded_Wide_Wide_String
93+
(Qualififer);
94+
end;
8195
else
8296
JS.Skip_Value;
8397
end if;
@@ -96,13 +110,13 @@ package body LSP.Ada_Handlers.Refactor.Imports_Commands is
96110
Context : Context_Access;
97111
Where : LSP.Messages.Location;
98112
Commands_Vector : in out LSP.Messages.CodeAction_Vector;
99-
Suggestion : LAL_Refactor.Refactor_Imports.Import_Suggestion)
113+
Suggestion : LAL_Refactor.Auto_Import.Import_Type)
100114
is
101115
Pointer : LSP.Commands.Command_Pointer;
102116
Item : LSP.Messages.CodeAction;
103117

104118
function Create_Suggestion_Title
105-
(Suggestion : LAL_Refactor.Refactor_Imports.Import_Suggestion)
119+
(Suggestion : LAL_Refactor.Auto_Import.Import_Type)
106120
return VSS.Strings.Virtual_String;
107121
-- Creates the suggestion text that will be shown by the client to
108122
-- to the developer. The text is costumized based on the need of
@@ -112,42 +126,17 @@ package body LSP.Ada_Handlers.Refactor.Imports_Commands is
112126
-- Create_Suggestions_Title --
113127
------------------------------
114128
function Create_Suggestion_Title
115-
(Suggestion : LAL_Refactor.Refactor_Imports.Import_Suggestion)
129+
(Suggestion : LAL_Refactor.Auto_Import.Import_Type)
116130
return VSS.Strings.Virtual_String
117131
is
118-
Title : Ada.Strings.Wide_Wide_Unbounded.
119-
Unbounded_Wide_Wide_String
120-
:= Ada.Strings.Wide_Wide_Unbounded.
121-
Null_Unbounded_Wide_Wide_String;
122-
use type Ada.Strings.Wide_Wide_Unbounded.
123-
Unbounded_Wide_Wide_String;
132+
use Ada.Strings.Wide_Wide_Unbounded;
124133

134+
Title : constant Langkit_Support.Text.Unbounded_Text_Type :=
135+
"Qualify with " & Suggestion.Qualifier;
125136
begin
126-
if Suggestion.With_Clause_Text /= "" then
127-
if Suggestion.Prefix_Text /= "" then
128-
-- Add with clause and prefix
129-
Title :=
130-
Title
131-
& "Add 'with' clause for "
132-
& Suggestion.With_Clause_Text
133-
& " and prefix the object with "
134-
& Suggestion.Prefix_Text;
135-
136-
else
137-
-- Add with clause and leave the prefix as it is
138-
Title :=
139-
Title
140-
& "Add 'with' clause for "
141-
& Suggestion.With_Clause_Text;
142-
end if;
143-
else
144-
-- Only add prefix
145-
146-
Title := Title & "Prefix the object with "
147-
& Suggestion.Prefix_Text;
148-
end if;
149-
return VSS.Strings.To_Virtual_String
150-
(Langkit_Support.Text.To_Text (Title));
137+
return
138+
VSS.Strings.To_Virtual_String
139+
(Langkit_Support.Text.To_Text (Title));
151140
end Create_Suggestion_Title;
152141

153142
begin
@@ -157,15 +146,15 @@ package body LSP.Ada_Handlers.Refactor.Imports_Commands is
157146
Where.span.first),
158147
With_Clause =>
159148
VSS.Strings.Conversions.To_Virtual_String
160-
(Suggestion.With_Clause_Text),
149+
(Suggestion.Import),
161150
Prefix =>
162151
VSS.Strings.Conversions.To_Virtual_String
163-
(Suggestion.Prefix_Text));
152+
(Suggestion.Qualifier));
164153
Pointer.Set (Self);
165154
Item :=
166155
(title => Create_Suggestion_Title (Suggestion),
167156
kind => (Is_Set => True,
168-
Value => LSP.Messages.RefactorRewrite),
157+
Value => LSP.Messages.QuickFix),
169158
diagnostics => (Is_Set => False),
170159
disabled => (Is_Set => False),
171160
edit => (Is_Set => False),
@@ -188,119 +177,20 @@ package body LSP.Ada_Handlers.Refactor.Imports_Commands is
188177
Document : LSP.Ada_Documents.Document_Access)
189178
return LAL_Refactor.Refactoring_Edits
190179
is
191-
use Langkit_Support.Text;
192180
use Libadalang.Analysis;
193-
use Libadalang.Common;
194-
use Libadalang.Slocs;
195-
use LAL_Refactor;
196-
use VSS.Strings;
197-
use VSS.Strings.Conversions;
181+
use LAL_Refactor.Auto_Import;
198182

199-
Node : Ada_Node :=
200-
Document.Get_Node_At (Context, Self.Where.position);
183+
Name : constant Libadalang.Analysis.Name :=
184+
Document.Get_Node_At (Context, Self.Where.position).As_Name;
201185

202-
Edits : LAL_Refactor.Refactoring_Edits;
186+
function Units return Analysis_Unit_Array is ([]);
203187

204188
begin
205-
-- Add prefix
206-
207-
if not Self.Prefix.Is_Empty
208-
and then Node.Kind in Ada_Identifier
209-
then
210-
-- If this is a DottedName them remove the current prefix and replace
211-
-- it by the suggested one. Otherwise, just add the prepend the
212-
-- prefix
213-
214-
while Node.Parent.Kind in Ada_Dotted_Name_Range loop
215-
Node := Node.Parent;
216-
end loop;
217-
218-
if Node.Kind in Ada_Dotted_Name_Range then
219-
Node := Node.As_Dotted_Name.F_Suffix.As_Ada_Node;
220-
end if;
221-
222-
if Node.Parent.Kind = Ada_Dotted_Name then
223-
-- Node.Parent is the full Dotted Name: this includes the
224-
-- current prefixes and the identifier. Using this SLOC instead
225-
-- of only the current prefixes SLOC is better since this covers
226-
-- cases when the Dotted Name is splitted in multiple lines.
227-
228-
Safe_Insert
229-
(Edits => Edits.Text_Edits,
230-
File_Name => Node.Unit.Get_Filename,
231-
Edit =>
232-
Text_Edit'
233-
(Location =>
234-
Make_Range
235-
(Start_Sloc
236-
(Node.Parent.As_Dotted_Name.F_Prefix.Sloc_Range),
237-
Start_Sloc (Node.Sloc_Range)),
238-
Text =>
239-
Ada.Strings.Unbounded.To_Unbounded_String
240-
(To_UTF8 (To_Wide_Wide_String (Self.Prefix)))));
241-
242-
else
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 (Node.Sloc_Range),
251-
Start_Sloc (Node.Sloc_Range)),
252-
Text =>
253-
Ada.Strings.Unbounded.To_Unbounded_String
254-
(To_UTF8 (To_Wide_Wide_String (Self.Prefix)))));
255-
end if;
256-
end if;
257-
258-
-- Add with clause
259-
260-
if not Self.With_Clause.Is_Empty then
261-
declare
262-
Last : Boolean;
263-
S : constant Libadalang.Slocs.Source_Location :=
264-
Laltools.Common.Get_Insert_With_Location
265-
(Node => Laltools.Common.Get_Compilation_Unit (Node),
266-
Pack_Name =>
267-
VSS.Strings.Conversions.To_Wide_Wide_String
268-
(Self.With_Clause),
269-
Last => Last);
270-
begin
271-
if S /= Libadalang.Slocs.No_Source_Location then
272-
if Last then
273-
Safe_Insert
274-
(Edits => Edits.Text_Edits,
275-
File_Name => Node.Unit.Get_Filename,
276-
Edit =>
277-
Text_Edit'
278-
(Location => Make_Range (S, S),
279-
Text =>
280-
Ada.Strings.Unbounded.To_Unbounded_String
281-
(To_UTF8 (To_Wide_Wide_String
282-
(Document.Line_Terminator
283-
& "with " & Self.With_Clause & ";")))));
284-
285-
else
286-
Safe_Insert
287-
(Edits => Edits.Text_Edits,
288-
File_Name => Node.Unit.Get_Filename,
289-
Edit =>
290-
Text_Edit'
291-
(Location => Make_Range (S, S),
292-
Text =>
293-
Ada.Strings.Unbounded.To_Unbounded_String
294-
(To_UTF8 (To_Wide_Wide_String
295-
("with " & Self.With_Clause & ";"
296-
& Document.Line_Terminator)))));
297-
end if;
298-
299-
end if;
300-
end;
301-
end if;
302-
303-
return Edits;
189+
return
190+
Create_Auto_Importer
191+
(Name,
192+
Self.Suggestion)
193+
.Refactor (Units'Access);
304194
end Command_To_Refactoring_Edits;
305195

306196
--------------
@@ -345,11 +235,15 @@ package body LSP.Ada_Handlers.Refactor.Imports_Commands is
345235
LSP.Types.Write_String (S, V.Context);
346236
JS.Key ("where");
347237
LSP.Messages.TextDocumentPositionParams'Write (S, V.Where);
348-
JS.Key ("with_clause");
349-
LSP.Types.Write_String (S, V.With_Clause);
350-
JS.Key ("prefix");
351-
LSP.Types.Write_String (S, V.Prefix);
238+
JS.Key ("import");
239+
LSP.Types.Write_String
240+
(S,
241+
VSS.Strings.Conversions.To_Virtual_String (V.Suggestion.Import));
242+
JS.Key ("qualifier");
243+
LSP.Types.Write_String
244+
(S,
245+
VSS.Strings.Conversions.To_Virtual_String (V.Suggestion.Qualifier));
352246
JS.End_Object;
353247
end Write_Command;
354248

355-
end LSP.Ada_Handlers.Refactor.Imports_Commands;
249+
end LSP.Ada_Handlers.Refactor.Auto_Import;

0 commit comments

Comments
 (0)