Skip to content

Commit 71a2c48

Browse files
V315-025: Use new LAL's API for preprocessing
Instead of our custom one.
1 parent 84d968c commit 71a2c48

File tree

4 files changed

+111
-11
lines changed

4 files changed

+111
-11
lines changed

source/ada/lsp-ada_handlers-file_readers.adb

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,18 @@
1515
-- of the license. --
1616
------------------------------------------------------------------------------
1717

18-
with VSS.Strings; use VSS.Strings;
19-
with LSP.Ada_Documents; use LSP.Ada_Documents;
20-
with LSP.Preprocessor; use LSP.Preprocessor;
18+
with GNAT.Strings; use GNAT.Strings;
19+
with GNATCOLL.Traces; use GNATCOLL.Traces;
20+
with VSS.Strings; use VSS.Strings;
21+
with VSS.Strings.Conversions;
22+
with LSP.Ada_Documents; use LSP.Ada_Documents;
23+
with Libadalang.Preprocessing; use Libadalang.Preprocessing;
24+
with Langkit_Support.File_Readers; use Langkit_Support.File_Readers;
2125

2226
package body LSP.Ada_Handlers.File_Readers is
2327

28+
Me : constant Trace_Handle := Create ("ALS.FILE_READERS");
29+
2430
----------
2531
-- Read --
2632
----------
@@ -34,22 +40,65 @@ package body LSP.Ada_Handlers.File_Readers is
3440
Diagnostics : in out
3541
Langkit_Support.Diagnostics.Diagnostics_Vectors.Vector)
3642
is
37-
Doc : Document_Access;
43+
Doc : Document_Access;
44+
Source : Preprocessed_Source := Preprocessed_Source'
45+
(Buffer => null, Last => 0);
46+
Buffer : GNAT.Strings.String_Access;
3847
begin
3948
-- First check if the file is an open document
4049
Doc := Self.Handler.Get_Open_Document
4150
(URI => LSP.Types.File_To_URI (Filename),
4251
Force => False);
4352

53+
-- Preprocess the document's contents if open, or the file contents if
54+
-- not.
4455
if Doc /= null then
45-
-- There is a document - we can get this and preprocess
46-
Contents := Preprocess_Buffer (Buffer => Doc.Text);
47-
56+
Buffer := new String'
57+
(VSS.Strings.Conversions.To_UTF_8_String (Doc.Text));
4858
else
49-
-- No open document: preprocess from the file
50-
Contents := Preprocess_File (Filename => Filename,
51-
Charset => Charset);
59+
Buffer := Create_From_UTF8 (Filename).Read_File;
60+
61+
-- Return an empty sring when failing to read the file (i.e: when the
62+
-- file has been deleted).
63+
if Buffer = null then
64+
Buffer := new String'("");
65+
end if;
66+
end if;
67+
68+
-- If we have preprocessing data, use LAL's API to preoprocess the file.
69+
-- Otherwise, just decode the contents of the document/file.
70+
71+
if Self.Preprocessing_Data /= No_Preprocessor_Data then
72+
Libadalang.Preprocessing.Preprocess
73+
(Data => Self.Preprocessing_Data,
74+
Filename => Filename,
75+
Input => Buffer.all,
76+
Contents => Source,
77+
Diagnostics => Diagnostics);
78+
79+
if Source.Buffer = null then
80+
-- Log the diagnostics when processing has failed
81+
for Diag of Diagnostics loop
82+
Me.Trace (Langkit_Support.Diagnostics.To_Pretty_String (Diag));
83+
end loop;
84+
end if;
5285
end if;
86+
87+
-- Decode the preprocessed buffer (or the initial contents when there is
88+
-- no preprocessing needed) in utf-8.
89+
90+
Decode_Buffer
91+
(Buffer => (if Source.Buffer /= null then
92+
Source.Buffer (1 .. Source.Last)
93+
else
94+
Buffer.all),
95+
Charset => "utf-8",
96+
Read_BOM => Read_BOM,
97+
Contents => Contents,
98+
Diagnostics => Diagnostics);
99+
100+
Free (Source);
101+
GNAT.Strings.Free (Buffer);
53102
end Read;
54103

55104
end LSP.Ada_Handlers.File_Readers;

source/ada/lsp-ada_handlers-file_readers.ads

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,16 @@
2121

2222
with Langkit_Support.File_Readers;
2323
with Langkit_Support.Diagnostics;
24+
with Libadalang.Preprocessing;
2425

2526
package LSP.Ada_Handlers.File_Readers is
2627

2728
type LSP_Reader_Interface (Handler : access Message_Handler) is new
28-
Langkit_Support.File_Readers.File_Reader_Interface with null record;
29+
Langkit_Support.File_Readers.File_Reader_Interface with
30+
record
31+
Preprocessing_Data : Libadalang.Preprocessing.Preprocessor_Data :=
32+
Libadalang.Preprocessing.No_Preprocessor_Data;
33+
end record;
2934

3035
overriding procedure Read
3136
(Self : LSP_Reader_Interface;

source/ada/lsp-ada_handlers.adb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ with Libadalang.Analysis;
9494
with Libadalang.Common; use Libadalang.Common;
9595
with Libadalang.Doc_Utils;
9696
with Libadalang.Helpers;
97+
with Libadalang.Preprocessing;
9798

9899
with GNATdoc.Comments.Helpers;
99100

@@ -4682,7 +4683,48 @@ package body LSP.Ada_Handlers is
46824683
procedure Create_Context_For_Non_Aggregate (P : Project_Type) is
46834684
C : constant Context_Access := new Context (Self.Trace);
46844685
Reader : LSP.Ada_Handlers.File_Readers.LSP_Reader_Interface (Self);
4686+
4687+
Default_Config : Libadalang.Preprocessing.File_Config;
4688+
File_Configs : Libadalang.Preprocessing.File_Config_Maps.Map;
4689+
4690+
procedure Set_Line_Mode
4691+
(Config : in out Libadalang.Preprocessing.File_Config);
4692+
-- Used to force the preprocessing line mode to Blank_Lines, which
4693+
-- is needed to preserve the number of lines after preprocessing a
4694+
-- source file, otherwise LSP requests based on SLOCs will fail.
4695+
4696+
-------------------
4697+
-- Set_Line_Mode --
4698+
-------------------
4699+
4700+
procedure Set_Line_Mode
4701+
(Config : in out Libadalang.Preprocessing.File_Config) is
4702+
begin
4703+
if Config.Enabled then
4704+
Config.Line_Mode := Libadalang.Preprocessing.Blank_Lines;
4705+
end if;
4706+
end Set_Line_Mode;
4707+
46854708
begin
4709+
-- Extract the preprocessing options from the context's project
4710+
-- and create the file reader which will preprocess the files
4711+
-- accordingly.
4712+
4713+
Libadalang.Preprocessing.Extract_Preprocessor_Data_From_Project
4714+
(Tree => Self.Project_Tree.all,
4715+
Project => P,
4716+
Default_Config => Default_Config,
4717+
File_Configs => File_Configs);
4718+
4719+
Libadalang.Preprocessing.Iterate
4720+
(Default_Config => Default_Config,
4721+
File_Configs => File_Configs,
4722+
Process => Set_Line_Mode'Access);
4723+
4724+
Reader.Preprocessing_Data :=
4725+
Libadalang.Preprocessing.Create_Preprocessor_Data
4726+
(Default_Config, File_Configs);
4727+
46864728
C.Initialize (Reader, Self.Follow_Symlinks);
46874729
C.Load_Project (Tree => Self.Project_Tree,
46884730
Root => P,

testsuite/ada_lsp/preprocessor/p.gpr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
project p is
2+
3+
package Compiler is
4+
for Default_Switches ("Ada") use ("-gnateDdebug=true");
5+
end Compiler;
26
end p;

0 commit comments

Comments
 (0)