Skip to content

Commit 1b326aa

Browse files
Merge branch 'topic/fallback_contexts' into 'master'
Use fallback context for non-source Ada files See merge request eng/ide/ada_language_server!1856
2 parents 1c529aa + 9114583 commit 1b326aa

File tree

12 files changed

+340
-174
lines changed

12 files changed

+340
-174
lines changed

source/ada/lsp-ada_context_sets.adb

+14-3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ package body LSP.Ada_Context_Sets is
4444

4545
Self.Map.Clear;
4646
Self.Total := 0;
47+
Self.Fallback_Context := null;
4748
end Cleanup;
4849

4950
------------------
@@ -70,16 +71,16 @@ package body LSP.Ada_Context_Sets is
7071
----------------------
7172

7273
function Get_Best_Context
73-
(Self : Context_Set'Class;
74-
URI : LSP.Structures.DocumentUri) return Context_Access is
74+
(Self : Context_Set'Class; URI : LSP.Structures.DocumentUri)
75+
return Context_Access is
7576
begin
7677
for Context of Self.Contexts loop
7778
if Context.Is_Part_Of_Project (URI) then
7879
return Context;
7980
end if;
8081
end loop;
8182

82-
return Self.Contexts.First_Element;
83+
return Self.Fallback_Context;
8384
end Get_Best_Context;
8485

8586
---------
@@ -113,6 +114,16 @@ package body LSP.Ada_Context_Sets is
113114
Self.Contexts.Prepend (Item);
114115
Self.Map.Insert (Item.Id, Item);
115116
Self.Total := Self.Total + Item.File_Count;
117+
118+
-- Free any previously set fallback context before
119+
-- setting the new one.
120+
if Item.Is_Fallback_Context then
121+
if Self.Fallback_Context /= null then
122+
Self.Fallback_Context.Free;
123+
Unchecked_Free (Self.Fallback_Context);
124+
end if;
125+
Self.Fallback_Context := Item;
126+
end if;
116127
end Prepend;
117128

118129
-------------------------

source/ada/lsp-ada_context_sets.ads

+16-8
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,12 @@ package LSP.Ada_Context_Sets is
4747
-- Reload each context in the set
4848

4949
function Get_Best_Context
50-
(Self : Context_Set'Class;
51-
URI : LSP.Structures.DocumentUri) return Context_Access;
50+
(Self : Context_Set'Class; URI : LSP.Structures.DocumentUri)
51+
return Context_Access;
5252
-- Return the first context in Contexts which contains a project
53-
-- which knows about file. Return the first context if no such
54-
-- context was found.
53+
-- which knows about file.
54+
-- If the file is not a known by any project context, return a
55+
-- fallback context not linked to any project subtree.
5556

5657
function Total_Source_Files (Self : Context_Set'Class) return Natural;
5758
-- Number of files in all contexts
@@ -95,12 +96,19 @@ private
9596
"=" => "=");
9697

9798
type Context_Set is tagged limited record
98-
Contexts : Context_Lists.List;
99+
Contexts : Context_Lists.List;
100+
-- The list of all contexts.
99101

100-
Map : Maps.Map;
101-
-- A map from Context.Id to Context access
102+
Fallback_Context : Context_Access;
103+
-- The fallback context used for files that do not belong
104+
-- to any project's subtree.
102105

103-
Total : Natural := 0;
106+
Map : Maps.Map;
107+
-- A map from Context.Id to Context access.
108+
109+
Total : Natural := 0;
110+
-- The total number of source files known by all the contexts stored
111+
-- in this set.
104112
end record;
105113

106114
end LSP.Ada_Context_Sets;

source/ada/lsp-ada_contexts.adb

+11-6
Original file line numberDiff line numberDiff line change
@@ -545,18 +545,25 @@ package body LSP.Ada_Contexts is
545545
Self.Charset := Ada.Strings.Unbounded.To_Unbounded_String
546546
("iso-8859-1");
547547
Self.Reader_Reference := Create_File_Reader_Reference (File_Reader);
548+
549+
-- Tab stop is set 1 to disable "visible character guessing" by LAL.
548550
Self.LAL_Context := Libadalang.Analysis.Create_Context
549551
(Unit_Provider => Self.Unit_Provider,
550552
File_Reader => Self.Reader_Reference,
551553
With_Trivia => True,
552554
Charset => Self.Get_Charset,
553555
Tab_Stop => 1);
554556
Self.Style := Style;
555-
556-
-- Tab stop is set 1 to disable "visible character guessing" by LAL.
557557
Self.Is_Fallback_Context := As_Fallback_Context;
558558
end Initialize;
559559

560+
-------------------------
561+
-- Is_Fallback_Context --
562+
-------------------------
563+
564+
function Is_Fallback_Context (Self : Context) return Boolean
565+
is (Self.Is_Fallback_Context);
566+
560567
------------------------
561568
-- Is_Part_Of_Project --
562569
------------------------
@@ -565,8 +572,7 @@ package body LSP.Ada_Contexts is
565572
(Self : Context;
566573
URI : LSP.Structures.DocumentUri) return Boolean is
567574
begin
568-
return Self.Is_Fallback_Context
569-
or else Self.Source_Files.Contains (Self.URI_To_File (URI));
575+
return Self.Is_Part_Of_Project (Self.URI_To_File (URI));
570576
end Is_Part_Of_Project;
571577

572578
------------------------
@@ -577,8 +583,7 @@ package body LSP.Ada_Contexts is
577583
(Self : Context;
578584
File : Virtual_File) return Boolean is
579585
begin
580-
return Self.Is_Fallback_Context
581-
or else Self.Source_Files.Contains (File);
586+
return Self.Source_Files.Contains (File);
582587
end Is_Part_Of_Project;
583588

584589
------------------

source/ada/lsp-ada_contexts.ads

+50-40
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,11 @@ package LSP.Ada_Contexts is
9191
procedure Find_All_References
9292
(Self : Context;
9393
Definition : Libadalang.Analysis.Defining_Name;
94-
Callback : not null access procedure
95-
(Base_Id : Libadalang.Analysis.Base_Id;
96-
Kind : Libadalang.Common.Ref_Result_Kind;
97-
Cancel : in out Boolean));
94+
Callback :
95+
not null access procedure
96+
(Base_Id : Libadalang.Analysis.Base_Id;
97+
Kind : Libadalang.Common.Ref_Result_Kind;
98+
Cancel : in out Boolean));
9899
-- Finds all references to a given defining name in all units of the
99100
-- context.
100101

@@ -124,10 +125,11 @@ package LSP.Ada_Contexts is
124125
procedure Find_All_Calls
125126
(Self : Context;
126127
Definition : Libadalang.Analysis.Defining_Name;
127-
Callback : not null access procedure
128-
(Base_Id : Libadalang.Analysis.Base_Id;
129-
Kind : Libadalang.Common.Ref_Result_Kind;
130-
Cancel : in out Boolean));
128+
Callback :
129+
not null access procedure
130+
(Base_Id : Libadalang.Analysis.Base_Id;
131+
Kind : Libadalang.Common.Ref_Result_Kind;
132+
Cancel : in out Boolean));
131133
-- Return all the enclosing entities that call Definition in all sources
132134
-- known to this project.
133135

@@ -147,52 +149,63 @@ package LSP.Ada_Contexts is
147149
(Self : Context;
148150
Definition : Libadalang.Analysis.Defining_Name;
149151
Imprecise_Results : out Boolean;
150-
Callback : not null access procedure
151-
(Base_Id : Libadalang.Analysis.Base_Id;
152-
Kind : Libadalang.Common.Ref_Result_Kind;
153-
Cancel : in out Boolean));
152+
Callback :
153+
not null access procedure
154+
(Base_Id : Libadalang.Analysis.Base_Id;
155+
Kind : Libadalang.Common.Ref_Result_Kind;
156+
Cancel : in out Boolean));
154157
-- Get all the references to a given defining name in all units for
155158
-- renaming purposes: for instance, when called on a tagged type primitive
156159
-- definition, references to the base subprograms it inherits and to the
157160
-- overriding ones are also returned.
158161

159-
function Is_Part_Of_Project
160-
(Self : Context;
161-
URI : LSP.Structures.DocumentUri) return Boolean;
162-
-- Check if given file belongs to the project loaded in the Context
162+
function Is_Fallback_Context (Self : Context) return Boolean;
163+
-- Return true if the given context is used as a fallback (i.e: used for files
164+
-- that do not belong to any known project subtree).
163165

164166
function Is_Part_Of_Project
165-
(Self : Context;
166-
File : GNATCOLL.VFS.Virtual_File) return Boolean;
167-
-- Check if given file belongs to the project loaded in the Context
167+
(Self : Context; URI : LSP.Structures.DocumentUri) return Boolean;
168+
-- Check if the file designated by the given URI belongs to the project
169+
-- loaded in the Context.
170+
-- This returns False for fallback contexts, since fallback contexts
171+
-- are not linked to any project subtree.
168172

169-
function List_Files (Self : Context'CLass)
170-
return LSP.Ada_File_Sets.File_Sets.Set_Iterator_Interfaces
171-
.Reversible_Iterator'Class;
173+
function Is_Part_Of_Project
174+
(Self : Context; File : GNATCOLL.VFS.Virtual_File) return Boolean;
175+
-- Check if given file belongs to the project loaded in the Context.
176+
-- This returns False for fallback contexts, since fallback contexts
177+
-- are not linked to any project subtree.
178+
179+
function List_Files
180+
(Self : Context'CLass)
181+
return LSP
182+
.Ada_File_Sets
183+
.File_Sets
184+
.Set_Iterator_Interfaces
185+
.Reversible_Iterator'Class;
172186
-- Return the list of files known to this context.
173187

174188
function File_Count (Self : Context) return Natural;
175189
-- Return number of files known to this context.
176190

177-
function Get_PP_Options (Self : Context) return
178-
Utils.Command_Lines.Command_Line;
191+
function Get_PP_Options
192+
(Self : Context) return Utils.Command_Lines.Command_Line;
179193
-- Return the command line for the Pretty Printer
180194

181195
function Get_Format_Options
182196
(Self : Context) return Gnatformat.Configuration.Format_Options_Type;
183197
-- Return the formatting options for Gnatformat
184198

185-
function Get_Documentation_Style (Self : Context) return
186-
GNATdoc.Comments.Options.Documentation_Style;
199+
function Get_Documentation_Style
200+
(Self : Context) return GNATdoc.Comments.Options.Documentation_Style;
187201
-- Get the documentation style used for this context.
188202

189203
function Analysis_Units
190204
(Self : Context) return Libadalang.Analysis.Analysis_Unit_Array;
191205
-- Return the analysis units for all Ada sources known to this context
192206

193207
function List_Source_Directories
194-
(Self : Context;
195-
Include_Externally_Built : Boolean := False)
208+
(Self : Context; Include_Externally_Built : Boolean := False)
196209
return LSP.Ada_File_Sets.File_Sets.Set;
197210
-- List the source directories, including externally built projects' source
198211
-- directories when Include_Externally_Built is set to True.
@@ -220,23 +233,19 @@ package LSP.Ada_Contexts is
220233
-- increase the speed of semantic requests.
221234

222235
procedure Include_File
223-
(Self : in out Context;
224-
File : GNATCOLL.VFS.Virtual_File);
236+
(Self : in out Context; File : GNATCOLL.VFS.Virtual_File);
225237
-- Includes File in Self's source files
226238

227239
procedure Exclude_File
228-
(Self : in out Context;
229-
File : GNATCOLL.VFS.Virtual_File);
240+
(Self : in out Context; File : GNATCOLL.VFS.Virtual_File);
230241
-- Excludes File from Self's source files
231242

232243
procedure Index_Document
233-
(Self : in out Context;
234-
Document : in out LSP.Ada_Documents.Document);
244+
(Self : in out Context; Document : in out LSP.Ada_Documents.Document);
235245
-- Index/reindex the given document in this context
236246

237247
procedure Flush_Document
238-
(Self : in out Context;
239-
File : GNATCOLL.VFS.Virtual_File);
248+
(Self : in out Context; File : GNATCOLL.VFS.Virtual_File);
240249
-- Revert a document to the state of the file discarding any changes
241250

242251
function LAL_Context
@@ -247,10 +256,11 @@ package LSP.Ada_Contexts is
247256
(Self : Context;
248257
Pattern : LSP.Search.Search_Pattern'Class;
249258
Only_Public : Boolean;
250-
Callback : not null access procedure
251-
(File : GNATCOLL.VFS.Virtual_File;
252-
Name : Libadalang.Analysis.Defining_Name;
253-
Stop : in out Boolean);
259+
Callback :
260+
not null access procedure
261+
(File : GNATCOLL.VFS.Virtual_File;
262+
Name : Libadalang.Analysis.Defining_Name;
263+
Stop : in out Boolean);
254264
Unit_Prefix : VSS.Strings.Virtual_String :=
255265
VSS.Strings.Empty_Virtual_String);
256266
-- Find symbols that match the given Pattern in all files of the context and

0 commit comments

Comments
 (0)