Skip to content

Commit 8689231

Browse files
committed
Add subtypes request
Refs #360
1 parent dda4da1 commit 8689231

File tree

3 files changed

+223
-0
lines changed

3 files changed

+223
-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_Subtypes;
7879
with LSP.Ada_Type_Hierarchy_Supertypes;
7980
with LSP.Default_Message_Handlers;
8081
with LSP.GNATCOLL_Trace_Streams;
@@ -101,6 +102,7 @@ with LSP.Server_Requests.Hover;
101102
with LSP.Server_Requests.Initialize;
102103
with LSP.Server_Requests.PrepareTypeHierarchy;
103104
with LSP.Server_Requests.References;
105+
with LSP.Server_Requests.Subtypes;
104106
with LSP.Server_Requests.Supertypes;
105107
with LSP.Server_Requests.Tokens_Full;
106108
with LSP.Server_Requests.Tokens_Range;
@@ -247,6 +249,10 @@ procedure LSP.Ada_Driver is
247249
LSP.Ada_Prepare_Type_Hierarchy.Ada_Prepare_Type_Hierarchy_Handler
248250
(Ada_Handler'Unchecked_Access);
249251

252+
Ada_Type_Hierarchy_Subtypes_Handler : aliased
253+
LSP.Ada_Type_Hierarchy_Subtypes.Ada_Type_Hierarchy_Subtype_Handler
254+
(Ada_Handler'Unchecked_Access);
255+
250256
Ada_Type_Hierarchy_Supertypes_Handler : aliased
251257
LSP.Ada_Type_Hierarchy_Supertypes.Ada_Type_Hierarchy_Supertype_Handler
252258
(Ada_Handler'Unchecked_Access);
@@ -546,6 +552,10 @@ begin
546552
(LSP.Server_Requests.PrepareTypeHierarchy.Request'Tag,
547553
Ada_Prepare_Type_Hierarchy_Handler'Unchecked_Access);
548554

555+
Server.Register_Handler
556+
(LSP.Server_Requests.Subtypes.Request'Tag,
557+
Ada_Type_Hierarchy_Subtypes_Handler'Unchecked_Access);
558+
549559
Server.Register_Handler
550560
(LSP.Server_Requests.Supertypes.Request'Tag,
551561
Ada_Type_Hierarchy_Supertypes_Handler'Unchecked_Access);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
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 Ada.Unchecked_Deallocation;
19+
20+
with Libadalang.Analysis;
21+
with Libadalang.Common;
22+
23+
with VSS.Strings;
24+
25+
with LSP.Ada_Context_Sets;
26+
with LSP.Ada_File_Sets;
27+
with LSP.Ada_Request_Jobs;
28+
with LSP.Client_Message_Receivers;
29+
with LSP.Locations;
30+
with LSP.Server_Requests.Subtypes;
31+
with LSP.Structures;
32+
with LSP.Utils;
33+
34+
package body LSP.Ada_Type_Hierarchy_Subtypes is
35+
36+
subtype Reversible_Iterator is LSP.Ada_File_Sets.File_Sets
37+
.Set_Iterator_Interfaces.Reversible_Iterator'Class;
38+
39+
type Iterator_Access is access Reversible_Iterator;
40+
41+
procedure Free is new Ada.Unchecked_Deallocation
42+
(Reversible_Iterator, Iterator_Access);
43+
44+
type Ada_References_Job
45+
(Parent : not null access constant Ada_Type_Hierarchy_Subtype_Handler) is
46+
limited new LSP.Ada_Request_Jobs.Ada_Request_Job
47+
(Priority => LSP.Server_Jobs.Low) with
48+
record
49+
Response : LSP.Structures.TypeHierarchyItem_Vector_Or_Null;
50+
Filter : LSP.Locations.File_Span_Sets.Set;
51+
Context : LSP.Ada_Context_Sets.Context_Access;
52+
Iterator : Iterator_Access;
53+
Cursor : LSP.Ada_File_Sets.File_Sets.Cursor;
54+
Decl : Libadalang.Analysis.Basic_Decl;
55+
end record;
56+
57+
type Ada_References_Job_Access is access all Ada_References_Job;
58+
59+
overriding procedure Execute_Ada_Request
60+
(Self : in out Ada_References_Job;
61+
Client :
62+
in out LSP.Client_Message_Receivers.Client_Message_Receiver'Class;
63+
Status : out LSP.Server_Jobs.Execution_Status);
64+
65+
----------------
66+
-- Create_Job --
67+
----------------
68+
69+
overriding function Create_Job
70+
(Self : Ada_Type_Hierarchy_Subtype_Handler;
71+
Message : LSP.Server_Messages.Server_Message_Access)
72+
return LSP.Server_Jobs.Server_Job_Access
73+
is
74+
Value : LSP.Server_Requests.Subtypes.Request
75+
renames LSP.Server_Requests.Subtypes.Request
76+
(Message.all);
77+
78+
Context : constant LSP.Ada_Context_Sets.Context_Access :=
79+
Self.Context.Get_Best_Context (Value.Params.item.uri);
80+
81+
Node : constant Libadalang.Analysis.Ada_Node :=
82+
Self.Context.Get_Node_At
83+
(Context.all,
84+
(LSP.Structures.TextDocumentPositionParams'
85+
((uri => Value.Params.item.uri),
86+
Value.Params.item.selectionRange.start)));
87+
-- We expect here an identifier in the type declaration
88+
89+
Decl : constant Libadalang.Analysis.Basic_Decl :=
90+
(if not Node.Is_Null
91+
and then Node.Kind in Libadalang.Common.Ada_Name
92+
and then Node.As_Name.P_Is_Defining
93+
then Node.As_Name.P_Enclosing_Defining_Name.P_Basic_Decl
94+
else Libadalang.Analysis.No_Basic_Decl);
95+
96+
Result : constant Ada_References_Job_Access :=
97+
new Ada_References_Job'
98+
(Parent => Self'Unchecked_Access,
99+
Request => LSP.Ada_Request_Jobs.Request_Access (Message),
100+
Context => Context,
101+
Iterator => new Reversible_Iterator'(Context.List_Files),
102+
Decl => Decl,
103+
others => <>);
104+
begin
105+
Result.Cursor := Result.Iterator.First;
106+
107+
return LSP.Server_Jobs.Server_Job_Access (Result);
108+
end Create_Job;
109+
110+
-------------------------
111+
-- Execute_Ada_Request --
112+
-------------------------
113+
114+
overriding procedure Execute_Ada_Request
115+
(Self : in out Ada_References_Job;
116+
Client :
117+
in out LSP.Client_Message_Receivers.Client_Message_Receiver'Class;
118+
Status : out LSP.Server_Jobs.Execution_Status)
119+
is
120+
121+
Message : LSP.Server_Requests.Subtypes.Request
122+
renames LSP.Server_Requests.Subtypes.Request (Self.Message.all);
123+
124+
Ignore : Boolean;
125+
Unit : Libadalang.Analysis.Analysis_Unit;
126+
Loc : LSP.Structures.Location;
127+
Item : LSP.Structures.TypeHierarchyItem;
128+
Name : Libadalang.Analysis.Defining_Name;
129+
begin
130+
if LSP.Ada_File_Sets.File_Sets.Has_Element (Self.Cursor) then
131+
Unit := Self.Context.Get_AU
132+
(LSP.Ada_File_Sets.File_Sets.Element (Self.Cursor));
133+
134+
for Part of Self.Decl.P_All_Parts
135+
when Part.Kind in Libadalang.Common.Ada_Base_Type_Decl
136+
loop
137+
for Tipe of Part.As_Base_Type_Decl.P_Find_Derived_Types
138+
(Root => Unit.Root,
139+
Origin => Self.Decl)
140+
loop
141+
Name := Tipe.P_Defining_Name.P_Canonical_Part;
142+
143+
Loc := Self.Parent.Context.To_LSP_Location (Name.P_Basic_Decl);
144+
145+
if not Self.Filter.Contains (Loc) then
146+
Item :=
147+
(name => VSS.Strings.To_Virtual_String
148+
(Name.Text),
149+
kind => LSP.Utils.Get_Decl_Kind
150+
(Name.P_Basic_Decl),
151+
tags => <>,
152+
detail => LSP.Utils.Node_Location_Image
153+
(Name),
154+
uri => Loc.uri,
155+
a_range => Loc.a_range,
156+
selectionRange => Self.Parent.Context.To_LSP_Location
157+
(Name).a_range,
158+
data => <>);
159+
160+
Self.Filter.Insert (Loc);
161+
Self.Response.Append (Item);
162+
end if;
163+
end loop;
164+
end loop;
165+
166+
Self.Cursor := Self.Iterator.Next (Self.Cursor);
167+
Status := LSP.Server_Jobs.Continue;
168+
else
169+
Free (Self.Iterator);
170+
Client.On_Subtypes_Response (Message.Id, Self.Response);
171+
Status := LSP.Server_Jobs.Done;
172+
end if;
173+
end Execute_Ada_Request;
174+
175+
end LSP.Ada_Type_Hierarchy_Subtypes;
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/subtypes
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_Subtypes is
27+
28+
type Ada_Type_Hierarchy_Subtype_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_Subtype_Handler;
35+
Message : LSP.Server_Messages.Server_Message_Access)
36+
return LSP.Server_Jobs.Server_Job_Access;
37+
38+
end LSP.Ada_Type_Hierarchy_Subtypes;

0 commit comments

Comments
 (0)