Skip to content

Commit 820a6e9

Browse files
committed
Merge branch 'topic/ignore_files' into 'master'
Introduce `IDE.Ignored_Source_Files` project attribute See merge request eng/ide/ada_language_server!2112
2 parents e4b77d3 + 45e852e commit 820a6e9

File tree

12 files changed

+423
-91
lines changed

12 files changed

+423
-91
lines changed

source/ada/lsp-ada_driver.adb

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ with GNAT.OS_Lib;
2626
with GNAT.Strings;
2727

2828
pragma Warnings (Off, "is an internal GNAT unit");
29-
with Gnatformat.Configuration;
30-
with LSP.Ada_Handlers.Open_ALS_Log_File_Commands;
3129
with System.Soft_Links;
3230
with System.Secondary_Stack;
3331

@@ -42,35 +40,35 @@ with GNATCOLL.Traces; use GNATCOLL.Traces;
4240
with GNATCOLL.VFS; use GNATCOLL.VFS;
4341
with GNATCOLL.Utils;
4442

43+
with Gnatformat.Configuration;
44+
4545
with LSP.Ada_Commands;
46-
with LSP.Ada_Definition;
4746
with LSP.Ada_Declaration;
48-
with LSP.Ada_Document_Symbol;
47+
with LSP.Ada_Definition;
4948
with LSP.Ada_Did_Change_Configurations;
5049
with LSP.Ada_Did_Change_Document;
50+
with LSP.Ada_Document_Symbol;
5151
with LSP.Ada_Execute_Command;
5252
with LSP.Ada_Folding_Range;
53-
with LSP.Ada_Hover;
54-
with LSP.Ada_Prepare_Type_Hierarchy;
55-
with LSP.Ada_References;
56-
with LSP.Ada_Handlers;
5753
with LSP.Ada_Handlers.Executables_Commands;
54+
with LSP.Ada_Handlers.GPR_Dependencies_Commands;
5855
with LSP.Ada_Handlers.Mains_Commands;
5956
with LSP.Ada_Handlers.Named_Parameters_Commands;
6057
with LSP.Ada_Handlers.Object_Dir_Commands;
61-
with LSP.Ada_Handlers.Other_File_Commands;
58+
with LSP.Ada_Handlers.Open_ALS_Log_File_Commands;
6259
with LSP.Ada_Handlers.Open_Project_File_Commands;
60+
with LSP.Ada_Handlers.Other_File_Commands;
6361
with LSP.Ada_Handlers.Project_Attributes_Commands;
6462
with LSP.Ada_Handlers.Project_File_Commands;
6563
with LSP.Ada_Handlers.Project_Reload_Commands;
6664
with LSP.Ada_Handlers.Refactor.Add_Parameter;
65+
with LSP.Ada_Handlers.Refactor.Auto_Import;
6766
with LSP.Ada_Handlers.Refactor.Change_Parameter_Mode;
6867
with LSP.Ada_Handlers.Refactor.Change_Parameters_Default_Value;
6968
with LSP.Ada_Handlers.Refactor.Change_Parameters_Type;
7069
with LSP.Ada_Handlers.Refactor.Delete_Entity;
7170
with LSP.Ada_Handlers.Refactor.Extract_Subprogram;
7271
with LSP.Ada_Handlers.Refactor.Extract_Variable;
73-
with LSP.Ada_Handlers.Refactor.Auto_Import;
7472
with LSP.Ada_Handlers.Refactor.Inline_Variable;
7573
with LSP.Ada_Handlers.Refactor.Introduce_Parameter;
7674
with LSP.Ada_Handlers.Refactor.Move_Parameter;
@@ -82,10 +80,14 @@ with LSP.Ada_Handlers.Refactor.Sort_Dependencies;
8280
with LSP.Ada_Handlers.Refactor.Suppress_Seperate;
8381
with LSP.Ada_Handlers.Refactor.Swap_If_Else;
8482
with LSP.Ada_Handlers.Show_Dependencies_Commands;
85-
with LSP.Ada_Handlers.GPR_Dependencies_Commands;
8683
with LSP.Ada_Handlers.Source_Dirs_Commands;
8784
with LSP.Ada_Handlers.Suspend_Executions;
85+
with LSP.Ada_Handlers;
86+
with LSP.Ada_Hover;
8887
with LSP.Ada_Inline_Value;
88+
with LSP.Ada_Prepare_Type_Hierarchy;
89+
with LSP.Ada_Projects;
90+
with LSP.Ada_References;
8991
with LSP.Ada_Selection_Range;
9092
with LSP.Ada_Tokens_Full;
9193
with LSP.Ada_Tokens_Range;
@@ -95,9 +97,9 @@ with LSP.Default_Message_Handlers;
9597
with LSP.Env;
9698
with LSP.GNATCOLL_Trace_Streams;
9799
with LSP.GNATCOLL_Tracers;
98-
with LSP.GPR_Handlers;
99-
with LSP.GPR_External_Tools;
100100
with LSP.GPR_Did_Change_Document;
101+
with LSP.GPR_External_Tools;
102+
with LSP.GPR_Handlers;
101103
with LSP.Memory_Statistics;
102104
with LSP.Predefined_Completion;
103105
with LSP.Secure_Message_Loggers;
@@ -592,6 +594,7 @@ begin
592594
Ada.Text_IO.Set_Output (Ada.Text_IO.Standard_Error);
593595
-- Protect stdout from pollution by accidental Put_Line calls
594596

597+
LSP.Ada_Projects.Elaborate_GPR2;
595598
Gnatformat.Configuration.Elaborate_GPR2;
596599

597600
declare

source/ada/lsp-ada_handlers-file_readers.adb

Lines changed: 98 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ with GNAT.Strings; use GNAT.Strings;
2121
with GNATCOLL.Traces; use GNATCOLL.Traces;
2222
with GNATCOLL.VFS; use GNATCOLL.VFS;
2323

24+
with GPR2.Path_Name;
25+
with GPR2.Project.Attribute;
26+
2427
with VSS.Strings; use VSS.Strings;
2528
pragma Warnings
2629
(Off, "unit ""VSS.Strings.Character_Iterators"" is not referenced");
@@ -31,6 +34,8 @@ with VSS.Strings.Converters.Decoders;
3134
with VSS.Strings.Conversions;
3235

3336
with LSP.Ada_Documents; use LSP.Ada_Documents;
37+
with LSP.Ada_Projects;
38+
3439
with Libadalang.Preprocessing; use Libadalang.Preprocessing;
3540
with Langkit_Support.File_Readers; use Langkit_Support.File_Readers;
3641
with Langkit_Support.Slocs;
@@ -45,7 +50,7 @@ package body LSP.Ada_Handlers.File_Readers is
4550
Me : constant Trace_Handle := Create ("ALS.FILE_READERS");
4651

4752
procedure Read_And_Decode
48-
(Filename : String;
53+
(Filename : Virtual_File;
4954
Charset : VSS.Strings.Virtual_String;
5055
Decoded : out VSS.Strings.Virtual_String;
5156
Error : out VSS.Strings.Virtual_String);
@@ -64,12 +69,96 @@ package body LSP.Ada_Handlers.File_Readers is
6469
-- present
6570
-- Default flags for the text decoder.
6671

72+
----------------
73+
-- Initialize --
74+
----------------
75+
76+
procedure Initialize
77+
(Self : in out LSP_File_Reader'Class;
78+
Tree : GPR2.Project.Tree.Object;
79+
View : GPR2.Project.View.Object)
80+
is
81+
procedure Set_Line_Mode
82+
(Config : in out Libadalang.Preprocessing.File_Config);
83+
-- Used to force the preprocessing line mode to Blank_Lines, which
84+
-- is needed to preserve the number of lines after preprocessing a
85+
-- source file, otherwise LSP requests based on SLOCs will fail.
86+
87+
-------------------
88+
-- Set_Line_Mode --
89+
-------------------
90+
91+
procedure Set_Line_Mode
92+
(Config : in out Libadalang.Preprocessing.File_Config) is
93+
begin
94+
if Config.Enabled then
95+
Config.Line_Mode := Libadalang.Preprocessing.Blank_Lines;
96+
end if;
97+
end Set_Line_Mode;
98+
99+
procedure Read_Excluded_Source_Files;
100+
-- Read IDE.Excluded_Source_Files value from the project if any
101+
102+
--------------------------------
103+
-- Read_Excluded_Source_Files --
104+
--------------------------------
105+
106+
procedure Read_Excluded_Source_Files is
107+
Attribute : GPR2.Project.Attribute.Object;
108+
begin
109+
if View.Check_Attribute
110+
(Name => LSP.Ada_Projects.IDE.Excluded_Source_Files,
111+
Result => Attribute)
112+
then
113+
for Value of Attribute.Values loop
114+
declare
115+
Name : constant GPR2.Filename_Optional :=
116+
GPR2.Filename_Optional (Value.Text);
117+
118+
Source : constant GPR2.Build.Source.Object :=
119+
(if GPR2.Is_Simple_Name (Name)
120+
then Tree.Root_Project.Visible_Source (Name)
121+
else GPR2.Build.Source.Undefined);
122+
begin
123+
if Source.Is_Defined then
124+
Self.Excluded_Files.Insert
125+
(Source.Path_Name.Virtual_File);
126+
end if;
127+
end;
128+
end loop;
129+
end if;
130+
end Read_Excluded_Source_Files;
131+
132+
Default_Config : Libadalang.Preprocessing.File_Config;
133+
File_Configs : Libadalang.Preprocessing.File_Config_Maps.Map;
134+
135+
begin
136+
Self.Preprocessing_Data := Libadalang.Preprocessing.No_Preprocessor_Data;
137+
138+
Read_Excluded_Source_Files; -- Read IDE.Excluded_Source_Files attr
139+
140+
Libadalang.Preprocessing.Extract_Preprocessor_Data_From_Project
141+
(Tree => Tree,
142+
Project => View,
143+
Default_Config => Default_Config,
144+
File_Configs => File_Configs);
145+
146+
Libadalang.Preprocessing.Iterate
147+
(Default_Config => Default_Config,
148+
File_Configs => File_Configs,
149+
Process => Set_Line_Mode'Access);
150+
151+
Self.Preprocessing_Data :=
152+
Libadalang.Preprocessing.Create_Preprocessor_Data
153+
(Default_Config, File_Configs);
154+
end Initialize;
155+
67156
---------------------
68157
-- Read_And_Decode --
69158
---------------------
70159

71160
procedure Read_And_Decode
72-
(Filename : String;
161+
(Filename : Virtual_File;
73162
Charset : VSS.Strings.Virtual_String;
74163
Decoded : out VSS.Strings.Virtual_String;
75164
Error : out VSS.Strings.Virtual_String)
@@ -80,7 +169,7 @@ package body LSP.Ada_Handlers.File_Readers is
80169
begin
81170
-- Read the file (this call uses MMAP)
82171

83-
Raw := Create_From_UTF8 (Filename).Read_File;
172+
Raw := Filename.Read_File;
84173

85174
if Raw = null then
86175
Decoded.Clear;
@@ -134,6 +223,7 @@ package body LSP.Ada_Handlers.File_Readers is
134223
URI : constant URIs.URI_String := URIs.Conversions.From_File (Filename);
135224

136225
Doc : Document_Access;
226+
File : constant Virtual_File := Create_From_UTF8 (Filename);
137227
Text : VSS.Strings.Virtual_String;
138228
Error : VSS.Strings.Virtual_String;
139229

@@ -150,9 +240,13 @@ package body LSP.Ada_Handlers.File_Readers is
150240
if Doc /= null then
151241
Text := Doc.Text;
152242

243+
elsif Self.Excluded_Files.Contains (File) then
244+
Contents :=
245+
Langkit_Support.File_Readers.Create_Decoded_File_Contents ("");
246+
153247
else
154248
Read_And_Decode
155-
(Filename => Filename,
249+
(Filename => File,
156250
Charset => VSS.Strings.Conversions.To_Virtual_String (Charset),
157251
Decoded => Text,
158252
Error => Error);

source/ada/lsp-ada_handlers-file_readers.ads

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,54 @@
1616
------------------------------------------------------------------------------
1717

1818
-- This package provides a Langkit File_Reader which is able to
19-
-- - read files from open documents
20-
-- - preprocess code on the fly
19+
-- - read files from open documents
20+
-- - return empty text if file is excluded
21+
-- - preprocess code on the fly
22+
23+
with GPR2.Project.View;
2124

2225
with Langkit_Support.File_Readers;
2326
with Langkit_Support.Diagnostics;
2427
with Libadalang.Preprocessing;
2528

2629
package LSP.Ada_Handlers.File_Readers is
2730

28-
type LSP_File_Reader (Handler : access Message_Handler'Class) is new
29-
Langkit_Support.File_Readers.File_Reader_Interface with
30-
record
31+
type LSP_File_Reader (Handler : access Message_Handler'Class) is
32+
new Langkit_Support.File_Readers.File_Reader_Interface with private;
33+
34+
procedure Initialize
35+
(Self : in out LSP_File_Reader'Class;
36+
Tree : GPR2.Project.Tree.Object;
37+
View : GPR2.Project.View.Object);
38+
-- Extract the preprocessing options from the context's project
39+
-- and create the file reader which will preprocess the files
40+
-- accordingly.
41+
-- Extract IDE.Excluded_Source_Files list from the project and keep it
42+
-- to ignore such files when reading.
43+
44+
private
45+
46+
type LSP_File_Reader (Handler : access Message_Handler'Class) is
47+
new Langkit_Support.File_Readers.File_Reader_Interface
48+
with record
3149
Preprocessing_Data : Libadalang.Preprocessing.Preprocessor_Data :=
32-
Libadalang.Preprocessing.No_Preprocessor_Data;
50+
Libadalang.Preprocessing.No_Preprocessor_Data;
51+
52+
Excluded_Files : LSP.Ada_File_Sets.File_Sets.Set;
53+
-- Set of files from IDE.Excluded_Source_Files project attribute
3354
end record;
3455

35-
overriding procedure Read
56+
overriding
57+
procedure Read
3658
(Self : LSP_File_Reader;
3759
Filename : String;
3860
Charset : String;
3961
Read_BOM : Boolean;
4062
Contents : out Langkit_Support.File_Readers.Decoded_File_Contents;
41-
Diagnostics : in out
42-
Langkit_Support.Diagnostics.Diagnostics_Vectors.Vector);
63+
Diagnostics :
64+
in out Langkit_Support.Diagnostics.Diagnostics_Vectors.Vector);
4365

44-
overriding procedure Release
45-
(Self : in out LSP_File_Reader) is null;
66+
overriding
67+
procedure Release (Self : in out LSP_File_Reader) is null;
4668

4769
end LSP.Ada_Handlers.File_Readers;

source/ada/lsp-ada_handlers-project_loading.adb

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ pragma Warnings (Off, "unit ""GPR2.Build.Source.Sets"" is not referenced");
3535
with GPR2.Build.Source.Sets;
3636

3737
with Langkit_Support.Errors;
38-
with Libadalang.Preprocessing;
3938
with Libadalang.Project_Provider;
4039
with LSP.Ada_Contexts;
4140
with LSP.Ada_Context_Sets;
@@ -533,47 +532,11 @@ package body LSP.Ada_Handlers.Project_Loading is
533532
LSP.Ada_Handlers.File_Readers.LSP_File_Reader
534533
(Self'Unchecked_Access);
535534

536-
Default_Config : Libadalang.Preprocessing.File_Config;
537-
File_Configs : Libadalang.Preprocessing.File_Config_Maps.Map;
538-
539-
procedure Set_Line_Mode
540-
(Config : in out Libadalang.Preprocessing.File_Config);
541-
-- Used to force the preprocessing line mode to Blank_Lines, which
542-
-- is needed to preserve the number of lines after preprocessing a
543-
-- source file, otherwise LSP requests based on SLOCs will fail.
544-
545-
-------------------
546-
-- Set_Line_Mode --
547-
-------------------
548-
549-
procedure Set_Line_Mode
550-
(Config : in out Libadalang.Preprocessing.File_Config) is
551-
begin
552-
if Config.Enabled then
553-
Config.Line_Mode := Libadalang.Preprocessing.Blank_Lines;
554-
end if;
555-
end Set_Line_Mode;
556-
557535
begin
558-
-- Extract the preprocessing options from the context's project
559-
-- and create the file reader which will preprocess the files
560-
-- accordingly.
561-
562536
begin
563-
Libadalang.Preprocessing.Extract_Preprocessor_Data_From_Project
564-
(Tree => Self.Project_Tree,
565-
Project => View,
566-
Default_Config => Default_Config,
567-
File_Configs => File_Configs);
568-
569-
Libadalang.Preprocessing.Iterate
570-
(Default_Config => Default_Config,
571-
File_Configs => File_Configs,
572-
Process => Set_Line_Mode'Access);
573-
574-
Reader.Preprocessing_Data :=
575-
Libadalang.Preprocessing.Create_Preprocessor_Data
576-
(Default_Config, File_Configs);
537+
Reader.Initialize
538+
(Tree => Self.Project_Tree,
539+
View => View);
577540
exception
578541
-- Fallback to a degraded mode when failing to parse preprocessing
579542
-- options.
@@ -592,8 +555,6 @@ package body LSP.Ada_Handlers.Project_Loading is
592555
& (C.Id)
593556
& ", fallback to a degraded mode without support for "
594557
& "preprocessing directives");
595-
Reader.Preprocessing_Data :=
596-
Libadalang.Preprocessing.No_Preprocessor_Data;
597558
end;
598559

599560
C.Initialize

0 commit comments

Comments
 (0)