Skip to content

Commit 095ae49

Browse files
committed
U126-028 Don't keep Ada_Node in the global name index
to prevent STALE_REFERENCE_ERROR. Replace Ada_Node with node's Source_Location and find corresponding node at the query time (it could be costly however). Close #633
1 parent ee91f1b commit 095ae49

File tree

3 files changed

+52
-8
lines changed

3 files changed

+52
-8
lines changed

source/ada/lsp-ada_contexts.adb

+36-2
Original file line numberDiff line numberDiff line change
@@ -505,9 +505,43 @@ package body LSP.Ada_Contexts is
505505
Callback : not null access procedure
506506
(URI : LSP.Messages.DocumentUri;
507507
Name : Libadalang.Analysis.Defining_Name;
508-
Stop : in out Boolean)) is
508+
Stop : in out Boolean))
509+
is
510+
procedure Adapter
511+
(URI : LSP.Messages.DocumentUri;
512+
Loc : Langkit_Support.Slocs.Source_Location;
513+
Stop : in out Boolean);
514+
-- Find a Defining_Name at the given location Loc in a unit of URI and
515+
-- pass it to Callback procedure call.
516+
517+
-------------
518+
-- Adapter --
519+
-------------
520+
521+
procedure Adapter
522+
(URI : LSP.Messages.DocumentUri;
523+
Loc : Langkit_Support.Slocs.Source_Location;
524+
Stop : in out Boolean)
525+
is
526+
Unit : constant Libadalang.Analysis.Analysis_Unit :=
527+
LSP.Preprocessor.Get_From_File
528+
(Self.LAL_Context,
529+
LSP.Types.To_UTF_8_String (Self.URI_To_File (URI)),
530+
Charset => Self.Get_Charset);
531+
532+
Name : constant Libadalang.Analysis.Name :=
533+
Laltools.Common.Get_Node_As_Name (Unit.Root.Lookup (Loc));
534+
535+
Def_Name : constant Libadalang.Analysis.Defining_Name :=
536+
Laltools.Common.Get_Name_As_Defining (Name);
537+
begin
538+
if not Def_Name.Is_Null then
539+
Callback (URI, Def_Name, Stop);
540+
end if;
541+
end Adapter;
542+
509543
begin
510-
Self.Source_Files.Get_Any_Symbol_Completion (Prefix, Callback);
544+
Self.Source_Files.Get_Any_Symbol_Completion (Prefix, Adapter'Access);
511545
end Get_Any_Symbol_Completion;
512546

513547
-----------------

source/ada/lsp-ada_file_sets.adb

+13-4
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ package body LSP.Ada_File_Sets is
160160
Prefix : VSS.Strings.Virtual_String;
161161
Callback : not null access procedure
162162
(URI : LSP.Messages.DocumentUri;
163-
Name : Libadalang.Analysis.Defining_Name;
163+
Loc : Langkit_Support.Slocs.Source_Location;
164164
Stop : in out Boolean))
165165
is
166166
Stop : Boolean := False;
@@ -176,7 +176,7 @@ package body LSP.Ada_File_Sets is
176176
exit Each_Prefix when not Value.Starts_With (Prefix);
177177

178178
for Item of Self.All_Symbols (Cursor) loop
179-
Callback (Item.URI, Item.Name, Stop);
179+
Callback (Item.URI, Item.Loc, Stop);
180180
exit Each_Prefix when Stop;
181181
end loop;
182182

@@ -258,8 +258,17 @@ package body LSP.Ada_File_Sets is
258258

259259
while It.Next (Node) loop
260260
declare
261+
Token : constant Libadalang.Common.Token_Reference :=
262+
Node.Token_End;
263+
264+
Sloc_Range : constant Langkit_Support.Slocs.Source_Location_Range
265+
:= Libadalang.Common.Sloc_Range (Libadalang.Common.Data (Token));
266+
267+
Start_Sloc : constant Langkit_Support.Slocs.Source_Location :=
268+
Langkit_Support.Slocs.Start_Sloc (Sloc_Range);
269+
261270
Text : constant Wide_Wide_String :=
262-
Libadalang.Common.Text (Node.Token_End);
271+
Libadalang.Common.Text (Token);
263272

264273
Canonical : constant Symbolization_Result :=
265274
Libadalang.Sources.Canonicalize (Text);
@@ -274,7 +283,7 @@ package body LSP.Ada_File_Sets is
274283
Cursor,
275284
Inserted);
276285

277-
Self.All_Symbols (Cursor).Append ((URI, Node.As_Defining_Name));
286+
Self.All_Symbols (Cursor).Append ((URI, Start_Sloc));
278287
end if;
279288
end;
280289
end loop;

source/ada/lsp-ada_file_sets.ads

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ with Ada.Containers.Vectors;
2525
with GNATCOLL.VFS;
2626

2727
with Libadalang.Analysis;
28+
with Langkit_Support.Slocs;
2829

2930
with VSS.Strings;
3031

@@ -68,7 +69,7 @@ package LSP.Ada_File_Sets is
6869
Prefix : VSS.Strings.Virtual_String;
6970
Callback : not null access procedure
7071
(URI : LSP.Messages.DocumentUri;
71-
Name : Libadalang.Analysis.Defining_Name;
72+
Loc : Langkit_Support.Slocs.Source_Location;
7273
Stop : in out Boolean));
7374
-- Find symbols starting with given Prefix in all files of the set and
7475
-- call Callback for each. Name could contain a stale reference if the File
@@ -77,7 +78,7 @@ package LSP.Ada_File_Sets is
7778
private
7879
type Name_Information is record
7980
URI : LSP.Messages.DocumentUri;
80-
Name : Libadalang.Analysis.Defining_Name;
81+
Loc : Langkit_Support.Slocs.Source_Location;
8182
end record;
8283

8384
package Name_Vectors is new Ada.Containers.Vectors

0 commit comments

Comments
 (0)