Skip to content

Commit 0c05a04

Browse files
reznikmmAnthonyLeonardoGracio
authored andcommitted
UC15-036 Replace GNATCOLL.Projects with GPR2
1 parent e990a51 commit 0c05a04

10 files changed

+496
-319
lines changed

source/ada/lsp-ada_contexts.adb

Lines changed: 119 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,15 @@ with Ada.Characters.Handling; use Ada.Characters.Handling;
1919

2020
with GNAT.Strings;
2121

22-
with GNATCOLL.Projects; use GNATCOLL.Projects;
2322
with GNATCOLL.Traces; use GNATCOLL.Traces;
2423
with GNATCOLL.VFS; use GNATCOLL.VFS;
2524

25+
with GPR2.Containers;
26+
with GPR2.Path_Name;
27+
with GPR2.Project.Attribute;
28+
with GPR2.Project.Attribute_Index;
29+
with GPR2.Project.Source;
30+
2631
with VSS.Strings.Conversions;
2732

2833
with URIs;
@@ -260,7 +265,7 @@ package body LSP.Ada_Contexts is
260265
File : GNATCOLL.VFS.Virtual_File;
261266
Reparse : Boolean := False) return Libadalang.Analysis.Analysis_Unit is
262267
begin
263-
if not Is_Ada_File (Self.Tree, File) then
268+
if not Is_Ada_File (Self.Tree.all, File) then
264269
return Libadalang.Analysis.No_Analysis_Unit;
265270
end if;
266271

@@ -682,10 +687,10 @@ package body LSP.Ada_Contexts is
682687
------------------
683688

684689
procedure Load_Project
685-
(Self : in out Context;
686-
Tree : not null GNATCOLL.Projects.Project_Tree_Access;
687-
Root : Project_Type;
688-
Charset : String)
690+
(Self : in out Context;
691+
Tree : GPR2.Project.Tree.Object;
692+
Root : GPR2.Project.View.Object;
693+
Charset : String)
689694
is
690695
procedure Update_Source_Files;
691696
-- Update the value of Self.Source_Files
@@ -697,44 +702,47 @@ package body LSP.Ada_Contexts is
697702
-------------------------
698703

699704
procedure Update_Source_Files is
700-
All_Sources : File_Array_Access :=
701-
Root.Source_Files (Recursive => True);
702-
All_Ada_Sources : File_Array (1 .. All_Sources'Length);
703-
Free_Index : Natural := All_Ada_Sources'First;
704-
begin
705-
-- Iterate through all sources, returning only those that have Ada
706-
-- as language.
707-
for J in All_Sources'Range loop
708-
if Is_Ada_File (Self.Tree, All_Sources (J)) then
709-
All_Ada_Sources (Free_Index) := All_Sources (J);
710-
Free_Index := Free_Index + 1;
705+
706+
procedure Insert_Source (Source : GPR2.Project.Source.Object);
707+
-- Insert Source in Self.Source_Files
708+
709+
-------------------
710+
-- Insert_Source --
711+
-------------------
712+
713+
procedure Insert_Source (Source : GPR2.Project.Source.Object) is
714+
Path : constant Virtual_File := Source.Path_Name.Virtual_File;
715+
begin
716+
if not Self.Source_Files.Contains (Path) then
717+
Self.Source_Files.Include (Path);
711718
end if;
712-
end loop;
719+
end Insert_Source;
713720

714-
Unchecked_Free (All_Sources);
721+
begin
715722
Self.Source_Files.Clear;
716723

717-
for Index in 1 .. Free_Index - 1 loop
718-
Self.Source_Files.Include (All_Ada_Sources (Index));
719-
end loop;
724+
Tree.For_Each_Source
725+
(View => Root,
726+
Action => Insert_Source'Access,
727+
Language => GPR2.Ada_Language,
728+
Externally_Built => False);
720729

721730
Self.Source_Dirs.Clear;
722-
Self.External_Source_Dirs.Clear;
723731

724-
for Dir of Source_Dirs
725-
(Project => Root,
726-
Recursive => True,
727-
Include_Externally_Built => False)
732+
for Dir of Tree.Source_Directories
733+
(View => Root,
734+
Externally_Built => False)
728735
loop
729-
Self.Source_Dirs.Include (Dir);
736+
Self.Source_Dirs.Include (Dir.Virtual_File);
730737
end loop;
731738

732-
for Dir of Source_Dirs
733-
(Project => Root,
734-
Recursive => True,
735-
Include_Externally_Built => True)
739+
Self.External_Source_Dirs.Clear;
740+
741+
for Dir of Tree.Source_Directories
742+
(View => Root,
743+
Externally_Built => True)
736744
loop
737-
Self.External_Source_Dirs.Include (Dir);
745+
Self.External_Source_Dirs.Include (Dir.Virtual_File);
738746
end loop;
739747
end Update_Source_Files;
740748

@@ -744,39 +752,45 @@ package body LSP.Ada_Contexts is
744752

745753
procedure Pretty_Printer_Setup
746754
is
747-
use type GNAT.Strings.String_Access;
748-
Options : GNAT.Strings.String_List_Access;
749755
Validated : GNAT.Strings.String_List_Access;
750-
Last : Integer;
751-
Default : Boolean;
756+
Index : Integer := 0;
757+
Attribute : GPR2.Project.Attribute.Object;
758+
Values : GPR2.Containers.Value_List;
752759
begin
753-
Root.Switches
754-
(In_Pkg => "Pretty_Printer",
755-
File => GNATCOLL.VFS.No_File,
756-
Language => "ada",
757-
Value => Options,
758-
Is_Default_Value => Default);
759760

760761
-- Initialize an gnatpp command line object
761-
Last := Options'First - 1;
762-
for Item of Options.all loop
763-
if Item /= null
764-
and then Item.all /= ""
765-
then
766-
Last := Last + 1;
767-
end if;
768-
end loop;
769762

770-
Validated := new GNAT.Strings.String_List (Options'First .. Last);
771-
Last := Options'First - 1;
772-
for Item of Options.all loop
773-
if Item /= null
774-
and then Item.all /= ""
775-
then
776-
Last := Last + 1;
777-
Validated (Last) := new String'(Item.all);
763+
if Root.Check_Attribute
764+
(Name => LSP.Common.Pretty_Printer.Switches,
765+
Index => LSP.Common.Ada_Index,
766+
Result => Attribute)
767+
then
768+
769+
-- Fill 'Values' with non empty value
770+
771+
for Value of Attribute.Values loop
772+
declare
773+
Text : constant String := Value.Text;
774+
begin
775+
if Text /= "" then
776+
Values.Append (Text);
777+
Index := Index + 1;
778+
end if;
779+
end;
780+
end loop;
781+
782+
Validated := new GNAT.Strings.String_List (1 .. Index);
783+
784+
if Index > 0 then
785+
Index := Validated'First;
786+
for Text of Values loop
787+
Validated (Index) := new String'(Text);
788+
Index := Index + 1;
789+
end loop;
778790
end if;
779-
end loop;
791+
else
792+
Validated := new GNAT.Strings.String_List (1 .. 0);
793+
end if;
780794

781795
Utils.Command_Lines.Parse
782796
(Validated,
@@ -786,24 +800,21 @@ package body LSP.Ada_Contexts is
786800
Collect_File_Names => False,
787801
Ignore_Errors => True);
788802

789-
GNAT.Strings.Free (Options);
790803
GNAT.Strings.Free (Validated);
791804

792805
-- Set UTF-8 encoding
793806
Utils.Command_Lines.Common.Set_WCEM (Self.PP_Options, "8");
794807
end Pretty_Printer_Setup;
795808

796809
begin
797-
Self.Id := VSS.Strings.Conversions.To_Virtual_String (Root.Name);
798-
Self.Tree := Tree;
810+
Self.Id := VSS.Strings.Conversions.To_Virtual_String
811+
(String (Root.Name));
812+
Self.Tree := Tree.Reference;
799813
Self.Charset := Ada.Strings.Unbounded.To_Unbounded_String (Charset);
800814

801815
Self.Unit_Provider :=
802816
Libadalang.Project_Provider.Create_Project_Unit_Provider
803-
(Tree => Tree,
804-
Project => Root,
805-
Env => Get_Environment (Root),
806-
Is_Project_Owner => False);
817+
(Tree => Tree, Project => Root);
807818

808819
Self.Event_Handler := Libadalang.Analysis.Create_Event_Handler_Reference
809820
(LSP_Context_Event_Handler_Type'(Trace => Self.Trace));
@@ -1163,12 +1174,49 @@ package body LSP.Ada_Contexts is
11631174

11641175
function Project_Attribute_Value
11651176
(Self : Context;
1166-
Attribute : Attribute_Pkg_String;
1177+
Attribute : GPR2.Q_Attribute_Id;
11671178
Index : String := "";
11681179
Default : String := "";
11691180
Use_Extended : Boolean := False) return String
1170-
is (if Self.Tree = null then Default
1171-
else Root_Project (Self.Tree.all).
1172-
Attribute_Value (Attribute, Index, Default, Use_Extended));
1181+
is
1182+
Attribute_Index : constant GPR2.Project.Attribute_Index.Object :=
1183+
(if Index = ""
1184+
then GPR2.Project.Attribute_Index.Undefined
1185+
else GPR2.Project.Attribute_Index.Create (Index));
1186+
1187+
Attribute_Value : GPR2.Project.Attribute.Object;
1188+
1189+
begin
1190+
if Self.Tree.Root_Project.Check_Attribute
1191+
(Name => Attribute,
1192+
Index => Attribute_Index,
1193+
Result => Attribute_Value)
1194+
then
1195+
return Attribute_Value.Value.Text;
1196+
elsif Use_Extended and then Self.Tree.Root_Project.Is_Extending then
1197+
-- Look at Extended project list as attribute not found in
1198+
-- Root_Project and Use_Extended requested.
1199+
1200+
declare
1201+
Extended_Root : GPR2.Project.View.Object :=
1202+
Self.Tree.Root_Project.Extended_Root;
1203+
begin
1204+
while Extended_Root.Is_Defined loop
1205+
if Extended_Root.Check_Attribute
1206+
(Name => Attribute,
1207+
Index => Attribute_Index,
1208+
Result => Attribute_Value)
1209+
then
1210+
return Attribute_Value.Value.Text;
1211+
elsif Extended_Root.Is_Extending then
1212+
Extended_Root := Extended_Root.Extended_Root;
1213+
else
1214+
Extended_Root := GPR2.Project.View.Undefined;
1215+
end if;
1216+
end loop;
1217+
end;
1218+
end if;
1219+
return Default;
1220+
end Project_Attribute_Value;
11731221

11741222
end LSP.Ada_Contexts;

source/ada/lsp-ada_contexts.ads

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@
2020
with Ada.Strings.Unbounded;
2121
with Ada.Strings.UTF_Encoding;
2222

23-
with GNATCOLL.Projects;
2423
with GNATCOLL.Traces;
2524
with GNATCOLL.VFS;
2625

2726
with GNATdoc.Comments.Options;
2827

28+
with GPR2.Project.Tree;
29+
with GPR2.Project.View;
30+
2931
with Langkit_Support.File_Readers; use Langkit_Support.File_Readers;
3032
with Laltools.Common;
3133

@@ -64,10 +66,10 @@ package LSP.Ada_Contexts is
6466
-- in particular.
6567

6668
procedure Load_Project
67-
(Self : in out Context;
68-
Tree : not null GNATCOLL.Projects.Project_Tree_Access;
69-
Root : GNATCOLL.Projects.Project_Type;
70-
Charset : String);
69+
(Self : in out Context;
70+
Tree : GPR2.Project.Tree.Object;
71+
Root : GPR2.Project.View.Object;
72+
Charset : String);
7173
-- Use the given project tree, and root project within this project
7274
-- tree, as project for this context. Root must be a non-aggregate
7375
-- project tree representing the root of a hierarchy inside Tree.
@@ -309,7 +311,7 @@ package LSP.Ada_Contexts is
309311

310312
function Project_Attribute_Value
311313
(Self : Context;
312-
Attribute : GNATCOLL.Projects.Attribute_Pkg_String;
314+
Attribute : GPR2.Q_Attribute_Id;
313315
Index : String := "";
314316
Default : String := "";
315317
Use_Extended : Boolean := False) return String;
@@ -338,7 +340,7 @@ private
338340
-- Indicate that this is a "fallback" context, ie the context
339341
-- holding any file, in the case no valid project was loaded.
340342

341-
Tree : GNATCOLL.Projects.Project_Tree_Access;
343+
Tree : access GPR2.Project.Tree.Object;
342344
-- The loaded project tree: we need to keep a reference to this
343345
-- in order to figure out which files are Ada and which are not.
344346
-- Do not deallocate: this is owned by the Message_Handler.

source/ada/lsp-ada_handlers-other_file_commands.adb

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717

1818
with Ada.Strings.UTF_Encoding;
1919

20+
with GNATCOLL.Tribooleans;
21+
22+
with GPR2.Project.Source;
23+
2024
with LSP.Messages.Client_Requests;
2125

2226
with VSS.Strings.Conversions;
@@ -74,8 +78,50 @@ package body LSP.Ada_Handlers.Other_File_Commands is
7478
File : constant GNATCOLL.VFS.Virtual_File :=
7579
Message_Handler.To_File (Self.URI);
7680

77-
Other_File : constant GNATCOLL.VFS.Virtual_File :=
78-
Message_Handler.Project_Tree.Other_File (File);
81+
function Other_File return GNATCOLL.VFS.Virtual_File;
82+
83+
----------------
84+
-- Other_File --
85+
----------------
86+
87+
function Other_File return GNATCOLL.VFS.Virtual_File is
88+
F : constant GPR2.Path_Name.Object := GPR2.Path_Name.Create (File);
89+
begin
90+
for V in Message_Handler.Project_Tree.Iterate
91+
(Status => (GPR2.Project.S_Externally_Built =>
92+
GNATCOLL.Tribooleans.Indeterminate))
93+
loop
94+
declare
95+
Source : constant GPR2.Project.Source.Object :=
96+
GPR2.Project.Tree.Element (V).Source (F);
97+
Other_Part : GPR2.Project.Source.Source_Part;
98+
begin
99+
if Source.Is_Defined then
100+
Other_Part := Source.Other_Part_Unchecked (GPR2.No_Index);
101+
if Other_Part.Source.Is_Defined then
102+
return Other_Part.Source.Path_Name.Virtual_File;
103+
end if;
104+
end if;
105+
end;
106+
end loop;
107+
108+
if Message_Handler.Project_Tree.Has_Runtime_Project then
109+
declare
110+
Source : constant GPR2.Project.Source.Object :=
111+
Message_Handler.Project_Tree.Runtime_Project.
112+
Source (F);
113+
Other_Part : GPR2.Project.Source.Source_Part;
114+
begin
115+
if Source.Is_Defined then
116+
Other_Part := Source.Other_Part_Unchecked (GPR2.No_Index);
117+
if Other_Part.Source.Is_Defined then
118+
return Other_Part.Source.Path_Name.Virtual_File;
119+
end if;
120+
end if;
121+
end;
122+
end if;
123+
return File;
124+
end Other_File;
79125

80126
URI : constant LSP.Messages.DocumentUri :=
81127
Message_Handler.From_File (Other_File);

0 commit comments

Comments
 (0)