Skip to content

Commit dda4da1

Browse files
committed
Add supertypes request
Refs #360
1 parent ae3f519 commit dda4da1

File tree

3 files changed

+187
-0
lines changed

3 files changed

+187
-0
lines changed

source/ada/lsp-ada_driver.adb

+10
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ with LSP.Ada_Handlers.Source_Dirs_Commands;
7575
with LSP.Ada_Handlers.Suspend_Executions;
7676
with LSP.Ada_Tokens_Full;
7777
with LSP.Ada_Tokens_Range;
78+
with LSP.Ada_Type_Hierarchy_Supertypes;
7879
with LSP.Default_Message_Handlers;
7980
with LSP.GNATCOLL_Trace_Streams;
8081
with LSP.GNATCOLL_Tracers;
@@ -100,6 +101,7 @@ with LSP.Server_Requests.Hover;
100101
with LSP.Server_Requests.Initialize;
101102
with LSP.Server_Requests.PrepareTypeHierarchy;
102103
with LSP.Server_Requests.References;
104+
with LSP.Server_Requests.Supertypes;
103105
with LSP.Server_Requests.Tokens_Full;
104106
with LSP.Server_Requests.Tokens_Range;
105107
with LSP.Servers;
@@ -245,6 +247,10 @@ procedure LSP.Ada_Driver is
245247
LSP.Ada_Prepare_Type_Hierarchy.Ada_Prepare_Type_Hierarchy_Handler
246248
(Ada_Handler'Unchecked_Access);
247249

250+
Ada_Type_Hierarchy_Supertypes_Handler : aliased
251+
LSP.Ada_Type_Hierarchy_Supertypes.Ada_Type_Hierarchy_Supertype_Handler
252+
(Ada_Handler'Unchecked_Access);
253+
248254
Ada_Fence_Message_Handler : aliased
249255
LSP.Default_Message_Handlers.Default_Message_Handler;
250256
-- A shared handler with Fense priority
@@ -540,6 +546,10 @@ begin
540546
(LSP.Server_Requests.PrepareTypeHierarchy.Request'Tag,
541547
Ada_Prepare_Type_Hierarchy_Handler'Unchecked_Access);
542548

549+
Server.Register_Handler
550+
(LSP.Server_Requests.Supertypes.Request'Tag,
551+
Ada_Type_Hierarchy_Supertypes_Handler'Unchecked_Access);
552+
543553
Server.Register_Handler
544554
(LSP.Server_Requests.References.Request'Tag,
545555
Ada_References_Handler'Unchecked_Access);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
------------------------------------------------------------------------------
2+
-- Language Server Protocol --
3+
-- --
4+
-- Copyright (C) 2024, AdaCore --
5+
-- --
6+
-- This is free software; you can redistribute it and/or modify it under --
7+
-- terms of the GNU General Public License as published by the Free Soft- --
8+
-- ware Foundation; either version 3, or (at your option) any later ver- --
9+
-- sion. This software is distributed in the hope that it will be useful, --
10+
-- but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- --
11+
-- TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public --
12+
-- License for more details. You should have received a copy of the GNU --
13+
-- General Public License distributed with this software; see file --
14+
-- COPYING3. If not, go to http://www.gnu.org/licenses for a complete copy --
15+
-- of the license. --
16+
------------------------------------------------------------------------------
17+
18+
with Libadalang.Analysis;
19+
with Libadalang.Common;
20+
21+
with VSS.Strings;
22+
23+
with LSP.Ada_Context_Sets;
24+
with LSP.Ada_Request_Jobs;
25+
with LSP.Client_Message_Receivers;
26+
with LSP.Server_Request_Jobs;
27+
with LSP.Server_Requests.Supertypes;
28+
with LSP.Structures;
29+
with LSP.Utils;
30+
31+
package body LSP.Ada_Type_Hierarchy_Supertypes is
32+
33+
type Ada_Type_Hierarchy_Supertype_Job
34+
(Parent : not null access constant Ada_Type_Hierarchy_Supertype_Handler) is
35+
limited new LSP.Ada_Request_Jobs.Ada_Request_Job
36+
(Priority => LSP.Server_Jobs.Low)
37+
with null record;
38+
39+
overriding procedure Execute_Ada_Request
40+
(Self : in out Ada_Type_Hierarchy_Supertype_Job;
41+
Client :
42+
in out LSP.Client_Message_Receivers.Client_Message_Receiver'Class;
43+
Status : out LSP.Server_Jobs.Execution_Status);
44+
-- Execute Supertypes request
45+
46+
-------------------------
47+
-- Execute_Ada_Request --
48+
-------------------------
49+
50+
overriding procedure Execute_Ada_Request
51+
(Self : in out Ada_Type_Hierarchy_Supertype_Job;
52+
Client :
53+
in out LSP.Client_Message_Receivers.Client_Message_Receiver'Class;
54+
Status : out LSP.Server_Jobs.Execution_Status)
55+
is
56+
use type LSP.Structures.A_Range;
57+
use type LSP.Structures.DocumentUri;
58+
59+
Message : LSP.Server_Requests.Supertypes.Request
60+
renames LSP.Server_Requests.Supertypes.Request (Self.Message.all);
61+
62+
Context : constant LSP.Ada_Context_Sets.Context_Access :=
63+
Self.Parent.Context.Get_Best_Context (Message.Params.item.uri);
64+
65+
Response : LSP.Structures.TypeHierarchyItem_Vector_Or_Null;
66+
67+
Node : constant Libadalang.Analysis.Ada_Node :=
68+
Self.Parent.Context.Get_Node_At
69+
(Context.all,
70+
(LSP.Structures.TextDocumentPositionParams'
71+
((uri => Message.Params.item.uri),
72+
Message.Params.item.selectionRange.start)));
73+
-- We expect here an identifier in the type declaration
74+
75+
Decl : constant Libadalang.Analysis.Basic_Decl :=
76+
(if not Node.Is_Null
77+
and then Node.Kind in Libadalang.Common.Ada_Name
78+
and then Node.As_Name.P_Is_Defining
79+
then Node.As_Name.P_Enclosing_Defining_Name.P_Basic_Decl
80+
else Libadalang.Analysis.No_Basic_Decl);
81+
82+
Loc : LSP.Structures.Location;
83+
Item : LSP.Structures.TypeHierarchyItem;
84+
Name : Libadalang.Analysis.Defining_Name;
85+
begin
86+
-- Iterate over all type completion parts and find parent types for each
87+
-- part.
88+
if not Decl.Is_Null then
89+
for Part of Decl.P_All_Parts
90+
when Part.Kind in Libadalang.Common.Ada_Base_Type_Decl
91+
loop
92+
for Tipe of Part.As_Base_Type_Decl.P_Base_Types (Part) loop
93+
Name := Tipe.P_Defining_Name.P_Canonical_Part;
94+
95+
Loc := Self.Parent.Context.To_LSP_Location (Name.P_Basic_Decl);
96+
97+
if not
98+
(for some X of Response =>
99+
X.uri = Loc.uri and X.a_range = Loc.a_range)
100+
then
101+
Item :=
102+
(name => VSS.Strings.To_Virtual_String
103+
(Name.Text),
104+
kind => LSP.Utils.Get_Decl_Kind
105+
(Name.P_Basic_Decl),
106+
tags => <>,
107+
detail => LSP.Utils.Node_Location_Image
108+
(Name),
109+
uri => Loc.uri,
110+
a_range => Loc.a_range,
111+
selectionRange => Self.Parent.Context.To_LSP_Location
112+
(Name).a_range,
113+
data => <>);
114+
115+
Response.Append (Item);
116+
end if;
117+
end loop;
118+
end loop;
119+
end if;
120+
121+
Client.On_Supertypes_Response (Message.Id, Response);
122+
Status := LSP.Server_Jobs.Done;
123+
end Execute_Ada_Request;
124+
125+
----------------
126+
-- Create_Job --
127+
----------------
128+
129+
overriding function Create_Job
130+
(Self : Ada_Type_Hierarchy_Supertype_Handler;
131+
Message : LSP.Server_Messages.Server_Message_Access)
132+
return LSP.Server_Jobs.Server_Job_Access is
133+
begin
134+
return new Ada_Type_Hierarchy_Supertype_Job'
135+
(Parent => Self'Unchecked_Access,
136+
Request => LSP.Server_Request_Jobs.Request_Access (Message));
137+
end Create_Job;
138+
139+
end LSP.Ada_Type_Hierarchy_Supertypes;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
------------------------------------------------------------------------------
2+
-- Language Server Protocol --
3+
-- --
4+
-- Copyright (C) 2024, AdaCore --
5+
-- --
6+
-- This is free software; you can redistribute it and/or modify it under --
7+
-- terms of the GNU General Public License as published by the Free Soft- --
8+
-- ware Foundation; either version 3, or (at your option) any later ver- --
9+
-- sion. This software is distributed in the hope that it will be useful, --
10+
-- but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- --
11+
-- TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public --
12+
-- License for more details. You should have received a copy of the GNU --
13+
-- General Public License distributed with this software; see file --
14+
-- COPYING3. If not, go to http://www.gnu.org/licenses for a complete copy --
15+
-- of the license. --
16+
------------------------------------------------------------------------------
17+
18+
-- This package provides handler and job types for typeHierarchy/supertypes
19+
-- requests.
20+
21+
with LSP.Ada_Job_Contexts;
22+
with LSP.Server_Jobs;
23+
with LSP.Server_Message_Handlers;
24+
with LSP.Server_Messages;
25+
26+
package LSP.Ada_Type_Hierarchy_Supertypes is
27+
28+
type Ada_Type_Hierarchy_Supertype_Handler
29+
(Context : not null access LSP.Ada_Job_Contexts.Ada_Job_Context'Class) is
30+
limited new LSP.Server_Message_Handlers.Server_Message_Handler
31+
with null record;
32+
33+
overriding function Create_Job
34+
(Self : Ada_Type_Hierarchy_Supertype_Handler;
35+
Message : LSP.Server_Messages.Server_Message_Access)
36+
return LSP.Server_Jobs.Server_Job_Access;
37+
38+
end LSP.Ada_Type_Hierarchy_Supertypes;

0 commit comments

Comments
 (0)