Skip to content

Commit 4fc0beb

Browse files
committed
Add a trivial textDocument/selectionRange request handler
Refs #1574
1 parent 1249619 commit 4fc0beb

10 files changed

+244
-0
lines changed

Diff for: liblsp_3_17/source/lsp-constants.ads

+4
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ package LSP.Constants is
131131
return LSP.Structures.Boolean_Or_DocumentRangeFormattingOptions_Optional
132132
is (Is_Set => True, Value => (Is_Boolean => True, Boolean => True));
133133

134+
function True
135+
return LSP.Structures.selectionRangeProvider_OfServerCapabilities_Optional
136+
is (Is_Set => True, Value => (LSP.Structures.Variant_1, True));
137+
134138
function Empty return LSP.Structures.Position
135139
is (line => 0, character => 0);
136140

Diff for: source/ada/lsp-ada_client_capabilities.adb

+1
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ package body LSP.Ada_Client_Capabilities is
422422
Result.hoverProvider := LSP.Constants.True;
423423
Result.implementationProvider := LSP.Constants.True;
424424
Result.referencesProvider := LSP.Constants.True;
425+
Result.selectionRangeProvider := LSP.Constants.True;
425426
Result.typeDefinitionProvider := LSP.Constants.True;
426427
Result.typeHierarchyProvider := LSP.Constants.True;
427428
Result.workspaceSymbolProvider := LSP.Constants.True;

Diff for: source/ada/lsp-ada_driver.adb

+10
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ with LSP.Ada_Handlers.Refactor.Suppress_Seperate;
7878
with LSP.Ada_Handlers.Show_Dependencies_Commands;
7979
with LSP.Ada_Handlers.Source_Dirs_Commands;
8080
with LSP.Ada_Handlers.Suspend_Executions;
81+
with LSP.Ada_Selection_Range;
8182
with LSP.Ada_Tokens_Full;
8283
with LSP.Ada_Tokens_Range;
8384
with LSP.Ada_Type_Hierarchy_Subtypes;
@@ -111,6 +112,7 @@ with LSP.Server_Requests.Hover;
111112
with LSP.Server_Requests.Initialize;
112113
with LSP.Server_Requests.PrepareTypeHierarchy;
113114
with LSP.Server_Requests.References;
115+
with LSP.Server_Requests.SelectionRange;
114116
with LSP.Server_Requests.Subtypes;
115117
with LSP.Server_Requests.Supertypes;
116118
with LSP.Server_Requests.Tokens_Full;
@@ -299,6 +301,10 @@ procedure LSP.Ada_Driver is
299301
LSP.Ada_Folding_Range.Ada_Folding_Range_Handler
300302
(Ada_Handler'Unchecked_Access);
301303

304+
Ada_Selection_Range_Handler : aliased
305+
LSP.Ada_Selection_Range.Ada_Selection_Range_Handler
306+
(Ada_Handler'Unchecked_Access);
307+
302308
Ada_Tokens_Full_Handler : aliased
303309
LSP.Ada_Tokens_Full.Ada_Tokens_Full_Handler
304310
(Ada_Handler'Unchecked_Access);
@@ -681,6 +687,10 @@ begin
681687
(LSP.Server_Requests.FoldingRange.Request'Tag,
682688
Ada_Folding_Range_Handler'Unchecked_Access);
683689

690+
Server.Register_Handler
691+
(LSP.Server_Requests.SelectionRange.Request'Tag,
692+
Ada_Selection_Range_Handler'Unchecked_Access);
693+
684694
Server.Register_Handler
685695
(LSP.Server_Requests.Tokens_Full.Request'Tag,
686696
Ada_Tokens_Full_Handler'Unchecked_Access);

Diff for: source/ada/lsp-ada_handlers-locations.adb

+31
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,37 @@ package body LSP.Ada_Handlers.Locations is
321321
return To_LSP_Range (Unit, Sloc);
322322
end To_LSP_Range;
323323

324+
------------------
325+
-- To_LSP_Range --
326+
------------------
327+
328+
function To_LSP_Range
329+
(Self : in out Message_Handler'Class;
330+
Node : Libadalang.Analysis.Ada_Node'Class)
331+
return LSP.Structures.A_Range
332+
is
333+
use type LSP.Ada_Documents.Document_Access;
334+
335+
URI : constant LSP.Structures.DocumentUri :=
336+
(VSS.Strings.Conversions.To_Virtual_String
337+
(URIs.Conversions.From_File (Node.Unit.Get_Filename))
338+
with null record);
339+
340+
Sloc : constant Langkit_Support.Slocs.Source_Location_Range :=
341+
Node.Sloc_Range;
342+
343+
Doc : constant LSP.Ada_Documents.Document_Access :=
344+
Self.Get_Open_Document (URI);
345+
346+
begin
347+
if Doc /= null then
348+
return Doc.To_A_Range (Sloc);
349+
350+
else
351+
return To_LSP_Range (Node.Unit, Sloc);
352+
end if;
353+
end To_LSP_Range;
354+
324355
---------------------
325356
-- To_LSP_Location --
326357
---------------------

Diff for: source/ada/lsp-ada_handlers-locations.ads

+5
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ package LSP.Ada_Handlers.Locations is
5151
Token : Libadalang.Common.Token_Reference)
5252
return LSP.Structures.A_Range;
5353

54+
function To_LSP_Range
55+
(Self : in out Message_Handler'Class;
56+
Node : Libadalang.Analysis.Ada_Node'Class)
57+
return LSP.Structures.A_Range;
58+
5459
function Get_Node_At
5560
(Self : in out Message_Handler'Class;
5661
Context : LSP.Ada_Contexts.Context;

Diff for: source/ada/lsp-ada_handlers.adb

+6
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,12 @@ package body LSP.Ada_Handlers is
148148
return LSP.Structures.Location is
149149
(LSP.Ada_Handlers.Locations.To_LSP_Location (Self, Node));
150150

151+
overriding function To_LSP_Range
152+
(Self : in out Message_Handler;
153+
Node : Libadalang.Analysis.Ada_Node'Class)
154+
return LSP.Structures.A_Range is
155+
(LSP.Ada_Handlers.Locations.To_LSP_Range (Self, Node));
156+
151157
overriding function To_LSP_Range
152158
(Self : in out Message_Handler;
153159
Unit : Libadalang.Analysis.Analysis_Unit;

Diff for: source/ada/lsp-ada_handlers.ads

+5
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,11 @@ private
509509
Token : Libadalang.Common.Token_Reference)
510510
return LSP.Structures.A_Range;
511511

512+
overriding function To_LSP_Range
513+
(Self : in out Message_Handler;
514+
Node : Libadalang.Analysis.Ada_Node'Class)
515+
return LSP.Structures.A_Range;
516+
512517
overriding procedure Append_Location
513518
(Self : in out Message_Handler;
514519
Result : in out LSP.Structures.Location_Vector;

Diff for: source/ada/lsp-ada_job_contexts.ads

+5
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ package LSP.Ada_Job_Contexts is
132132
Node : Libadalang.Analysis.Ada_Node'Class)
133133
return LSP.Structures.Location is abstract;
134134

135+
function To_LSP_Range
136+
(Self : in out Ada_Job_Context;
137+
Node : Libadalang.Analysis.Ada_Node'Class)
138+
return LSP.Structures.A_Range is abstract;
139+
135140
function To_LSP_Range
136141
(Self : in out Ada_Job_Context;
137142
Unit : Libadalang.Analysis.Analysis_Unit;

Diff for: source/ada/lsp-ada_selection_range.adb

+139
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;
19+
with Libadalang.Analysis;
20+
with LSP.Ada_Context_Sets;
21+
with LSP.Ada_Request_Jobs;
22+
with LSP.Client_Message_Receivers;
23+
with LSP.Server_Requests.SelectionRange;
24+
with LSP.Structures;
25+
26+
package body LSP.Ada_Selection_Range is
27+
28+
type Ada_Selection_Range_Job
29+
(Parent : not null access constant Ada_Selection_Range_Handler) is limited
30+
new LSP.Ada_Request_Jobs.Ada_Request_Job
31+
(Priority => LSP.Server_Jobs.High)
32+
with null record;
33+
34+
overriding procedure Execute_Ada_Request
35+
(Self : in out Ada_Selection_Range_Job;
36+
Client :
37+
in out LSP.Client_Message_Receivers.Client_Message_Receiver'Class;
38+
Status : out LSP.Server_Jobs.Execution_Status);
39+
40+
type Ada_Selection_Range_Job_Access is access all Ada_Selection_Range_Job;
41+
42+
function Process
43+
(Self : Ada_Selection_Range_Handler'Class;
44+
Node : Libadalang.Analysis.Ada_Node)
45+
return LSP.Structures.SelectionRange;
46+
47+
----------------
48+
-- Create_Job --
49+
----------------
50+
51+
overriding function Create_Job
52+
(Self : Ada_Selection_Range_Handler;
53+
Message : LSP.Server_Messages.Server_Message_Access)
54+
return LSP.Server_Jobs.Server_Job_Access
55+
is
56+
Result : constant Ada_Selection_Range_Job_Access :=
57+
new Ada_Selection_Range_Job'
58+
(Parent => Self'Unchecked_Access,
59+
Request => LSP.Ada_Request_Jobs.Request_Access (Message));
60+
begin
61+
return LSP.Server_Jobs.Server_Job_Access (Result);
62+
end Create_Job;
63+
64+
-------------------------
65+
-- Execute_Ada_Request --
66+
-------------------------
67+
68+
overriding procedure Execute_Ada_Request
69+
(Self : in out Ada_Selection_Range_Job;
70+
Client :
71+
in out LSP.Client_Message_Receivers.Client_Message_Receiver'Class;
72+
Status : out LSP.Server_Jobs.Execution_Status)
73+
is
74+
Message : LSP.Server_Requests.SelectionRange.Request
75+
renames LSP.Server_Requests.SelectionRange.Request (Self.Message.all);
76+
77+
Value : LSP.Structures.SelectionRangeParams renames Message.Params;
78+
79+
Response : LSP.Structures.SelectionRange_Vector_Or_Null;
80+
81+
Context : constant LSP.Ada_Context_Sets.Context_Access :=
82+
Self.Parent.Context.Get_Best_Context
83+
(Value.textDocument.uri);
84+
85+
begin
86+
Status := LSP.Server_Jobs.Done;
87+
88+
for Item of Value.positions loop
89+
declare
90+
Pos : constant LSP.Structures.TextDocumentPositionParams :=
91+
(Value.textDocument, Item);
92+
Node : constant Libadalang.Analysis.Ada_Node :=
93+
Self.Parent.Context.Get_Node_At (Context.all, Pos);
94+
begin
95+
Response.Append (Self.Parent.Process (Node));
96+
end;
97+
end loop;
98+
99+
Client.On_SelectionRange_Response (Message.Id, Response);
100+
end Execute_Ada_Request;
101+
102+
-------------
103+
-- Process --
104+
-------------
105+
106+
function Process
107+
(Self : Ada_Selection_Range_Handler'Class;
108+
Node : Libadalang.Analysis.Ada_Node)
109+
return LSP.Structures.SelectionRange
110+
is
111+
Result : LSP.Structures.SelectionRange;
112+
First : Boolean := True;
113+
List : constant Libadalang.Analysis.Ada_Node_Array :=
114+
(if Node.Is_Null then [] else Node.Parents);
115+
begin
116+
for Item of reverse List loop
117+
declare
118+
Next : LSP.Structures.SelectionRange;
119+
begin
120+
Next.a_range := Self.Context.To_LSP_Range (Item);
121+
122+
if First then
123+
First := False;
124+
else
125+
Next.parent.Set (Result);
126+
end if;
127+
128+
Result := Next;
129+
end;
130+
end loop;
131+
132+
if First then
133+
Result.a_range := ((0, 0), (0, 0));
134+
end if;
135+
136+
return Result;
137+
end Process;
138+
139+
end LSP.Ada_Selection_Range;

Diff for: source/ada/lsp-ada_selection_range.ads

+38
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 a job for textDocument/selectionRange
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_Selection_Range is
27+
28+
type Ada_Selection_Range_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_Selection_Range_Handler;
35+
Message : LSP.Server_Messages.Server_Message_Access)
36+
return LSP.Server_Jobs.Server_Job_Access;
37+
38+
end LSP.Ada_Selection_Range;

0 commit comments

Comments
 (0)