Skip to content

Commit 4f4806b

Browse files
U921-010: Avoid duplicates in completion
by using full sloc image comparison. This is needed in particular for inherited subprograms coming from generic packages that are instantiated multiple times: from the LAL perspective these nodes are different, which is not what we want for completion. An automatic test has been added.
1 parent 624af8d commit 4f4806b

File tree

9 files changed

+352
-13
lines changed

9 files changed

+352
-13
lines changed

source/ada/lsp-ada_completions.adb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
------------------------------------------------------------------------------
1717

1818
with Ada.Containers.Hashed_Sets;
19+
with Langkit_Support;
1920

2021
with VSS.Strings;
2122

@@ -25,15 +26,16 @@ with LSP.Lal_Utils;
2526

2627
package body LSP.Ada_Completions is
2728

28-
--------------
29-
-- Is_Equal --
30-
--------------
29+
------------------------
30+
-- Is_Full_Sloc_Equal --
31+
------------------------
3132

32-
function Is_Equal (Left, Right : Libadalang.Analysis.Defining_Name)
33-
return Boolean is
33+
function Is_Full_Sloc_Equal
34+
(Left, Right : Libadalang.Analysis.Defining_Name) return Boolean is
3435
begin
35-
return Libadalang.Analysis."=" (Left, Right);
36-
end Is_Equal;
36+
return Libadalang.Analysis.Full_Sloc_Image
37+
(Left) = Libadalang.Analysis.Full_Sloc_Image (Right);
38+
end Is_Full_Sloc_Equal;
3739

3840
-----------------------
3941
-- Write_Completions --

source/ada/lsp-ada_completions.ads

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
-- This package provides types to support completions in Ada Language server.
1919

2020
with Ada.Containers.Hashed_Maps;
21+
with Ada.Strings.Hash_Case_Insensitive;
2122

2223
with Langkit_Support.Slocs;
24+
with Langkit_Support.Text;
2325
with Libadalang.Analysis;
2426
with Libadalang.Common;
2527

@@ -32,12 +34,14 @@ package LSP.Ada_Completions is
3234

3335
function Hash (Name : Libadalang.Analysis.Defining_Name)
3436
return Ada.Containers.Hash_Type is
35-
(Name.As_Ada_Node.Hash);
37+
(Ada.Strings.Hash_Case_Insensitive (
38+
Langkit_Support.Text.To_UTF8 (Name.Full_Sloc_Image)));
3639

37-
function Is_Equal (Left, Right : Libadalang.Analysis.Defining_Name)
38-
return Boolean;
39-
-- This custom Is_Equal function is here as a temporary workaround.
40-
-- The ticket for the corresponding compiler bug is T806-020.
40+
function Is_Full_Sloc_Equal
41+
(Left, Right : Libadalang.Analysis.Defining_Name) return Boolean;
42+
-- Compare the two nodes using full sloc image (filename + sloc). Needed
43+
-- for completion, since LAL can return several times the same declaration
44+
-- and specially subprograms from generic instantiations.
4145

4246
type Name_Information is record
4347
Is_Dot_Call : Boolean;
@@ -59,7 +63,7 @@ package LSP.Ada_Completions is
5963
(Key_Type => Libadalang.Analysis.Defining_Name,
6064
Element_Type => Name_Information,
6165
Hash => Hash,
62-
Equivalent_Keys => Is_Equal,
66+
Equivalent_Keys => Is_Full_Sloc_Equal,
6367
"=" => "=");
6468

6569
type Completion_Provider is abstract tagged limited null record;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
main
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
project Default is
2+
3+
for Main use ("main.adb") & project'Main;
4+
5+
end Default;
6+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
with Parents; use Parents;
2+
3+
generic
4+
A : Integer;
5+
package Generic_Pack is
6+
7+
type Child is new Parent with null record;
8+
9+
end Generic_Pack;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
with Generic_Pack;
2+
with Parents; use Parents;
3+
4+
procedure Main is
5+
package A is new Generic_Pack (3);
6+
use A;
7+
8+
begin
9+
Prim
10+
end Main;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package Parents is
2+
3+
type Parent is tagged null record;
4+
5+
procedure Primitive (Obj : Parent) is null;
6+
7+
end Parents;

0 commit comments

Comments
 (0)