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
2226package 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
55104end LSP.Ada_Handlers.File_Readers ;
0 commit comments