Skip to content

Commit 55717df

Browse files
committed
Add to definition response
destinations for `begin` and `private` keywords for package, subprogram, task and protected objects to make navigation easy.
1 parent 077b33b commit 55717df

File tree

14 files changed

+730
-409
lines changed

14 files changed

+730
-409
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ section below it for the last release. -->
55
## \<next>
66

77
* Added a `Ada: Report Issue` command that opens the [VS Code Issue Reporter](https://code.visualstudio.com/docs/supporting/FAQ#_report-an-issue-with-a-vs-code-extension) with an extension-specific template
8+
* Add `begin` and `private` destinations to `texDocument/definition` response triggered
9+
respectively on subprograms and packages (e.g: ctrl-click on a subprogram name will
10+
jump to `begin` in the subprogram body, while ctrl-clicking on a package spec name will
11+
jump to its private part, if any).
812

913
## 26.0.202507021
1014

source/ada/lsp-ada_definition.adb

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ package body LSP.Ada_Definition is
105105
procedure Append_Overrides (Decl : Libadalang.Analysis.Basic_Decl);
106106
-- Append overloaded subprograms for given declaration
107107

108+
procedure Append_Prev_Token (Node : Libadalang.Analysis.Ada_Node'Class);
109+
-- Find a token before Node and append its location to Result
110+
108111
Message : LSP.Server_Requests.Definition.Request
109112
renames LSP.Server_Requests.Definition.Request (Self.Message.all);
110113

@@ -199,6 +202,28 @@ package body LSP.Ada_Definition is
199202
end if;
200203
end Append_Overrides;
201204

205+
-----------------------
206+
-- Append_Prev_Token --
207+
-----------------------
208+
209+
procedure Append_Prev_Token
210+
(Node : Libadalang.Analysis.Ada_Node'Class) is
211+
begin
212+
if not Node.Is_Null then
213+
declare
214+
Prev : constant Libadalang.Common.Token_Reference :=
215+
Libadalang.Common.Previous (Node.Token_Start, True);
216+
217+
begin
218+
Self.Parent.Context.Append_Location
219+
(Result => Self.Response,
220+
Filter => Self.Filter,
221+
Unit => Node.Unit,
222+
Token => Prev);
223+
end;
224+
end if;
225+
end Append_Prev_Token;
226+
202227
begin
203228
if Self.Contexts.Is_Empty then
204229
-- No more contexts to process, sort and return collected results
@@ -299,6 +324,49 @@ package body LSP.Ada_Definition is
299324
then
300325
Decl_For_Find_Overrides := Declaration;
301326
end if;
327+
328+
-- Append private part, begin destinations to result
329+
for Part of Definition.P_Basic_Decl.P_All_Parts loop
330+
case Part.Kind is
331+
332+
when Libadalang.Common.Ada_Package_Body_Range =>
333+
Append_Prev_Token
334+
(Part.As_Package_Body.F_Stmts);
335+
336+
when Libadalang.Common.Ada_Subp_Body_Range =>
337+
Append_Prev_Token
338+
(Part.As_Subp_Body.F_Stmts);
339+
340+
when Libadalang.Common.Ada_Task_Body_Range =>
341+
Append_Prev_Token
342+
(Part.As_Task_Body.F_Stmts);
343+
344+
when Libadalang.Common.Ada_Base_Package_Decl =>
345+
Append_Prev_Token
346+
(Part.As_Base_Package_Decl.F_Private_Part);
347+
348+
when Libadalang.Common.Ada_Protected_Type_Decl_Range =>
349+
Append_Prev_Token
350+
(Part.As_Protected_Type_Decl.F_Definition.F_Private_Part);
351+
352+
when Libadalang.Common.Ada_Single_Protected_Decl_Range =>
353+
Append_Prev_Token
354+
(Part.As_Single_Protected_Decl.F_Definition.
355+
F_Private_Part);
356+
357+
when Libadalang.Common.Ada_Task_Type_Decl_Range =>
358+
Append_Prev_Token
359+
(Part.As_Task_Type_Decl.F_Definition.F_Private_Part);
360+
361+
when Libadalang.Common.Ada_Single_Task_Decl_Range =>
362+
Append_Prev_Token
363+
(Part.As_Single_Task_Decl.F_Task_Type.F_Definition.
364+
F_Private_Part);
365+
366+
when others =>
367+
null;
368+
end case;
369+
end loop;
302370
end if;
303371

304372
Append_Overrides (Decl_For_Find_Overrides);

source/ada/lsp-ada_handlers-locations.adb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,39 @@ package body LSP.Ada_Handlers.Locations is
4040
-- Append_Location --
4141
---------------------
4242

43+
procedure Append_Location
44+
(Self : in out Message_Handler;
45+
Result : in out LSP.Structures.Location_Vector;
46+
Filter : in out LSP.Locations.File_Span_Sets.Set;
47+
Unit : Libadalang.Analysis.Analysis_Unit;
48+
Token : Libadalang.Common.Token_Reference)
49+
is
50+
use type Libadalang.Common.Token_Reference;
51+
begin
52+
if Token /= Libadalang.Common.No_Token then
53+
declare
54+
URI : constant LSP.Structures.DocumentUri :=
55+
(VSS.Strings.Conversions.To_Virtual_String
56+
(URIs.Conversions.From_File (Unit.Get_Filename))
57+
with null record);
58+
59+
Value : constant LSP.Structures.Location :=
60+
(uri => URI,
61+
a_range => Locations.To_LSP_Range (Self, Unit, Token),
62+
alsKind => LSP.Constants.Empty);
63+
begin
64+
if not Filter.Contains (Value) then
65+
Result.Append (Value);
66+
Filter.Insert (Value);
67+
end if;
68+
end;
69+
end if;
70+
end Append_Location;
71+
72+
---------------------
73+
-- Append_Location --
74+
---------------------
75+
4376
procedure Append_Location
4477
(Self : in out Message_Handler;
4578
Result : in out LSP.Structures.Location_Vector;

source/ada/lsp-ada_handlers-locations.ads

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ package LSP.Ada_Handlers.Locations is
7272
(Token : Libadalang.Common.Token_Reference)
7373
return LSP.Structures.Position;
7474

75+
procedure Append_Location
76+
(Self : in out Message_Handler;
77+
Result : in out LSP.Structures.Location_Vector;
78+
Filter : in out LSP.Locations.File_Span_Sets.Set;
79+
Unit : Libadalang.Analysis.Analysis_Unit;
80+
Token : Libadalang.Common.Token_Reference);
81+
-- Append the location corresponding to the given token to the Result.
82+
7583
procedure Append_Location
7684
(Self : in out Message_Handler;
7785
Result : in out LSP.Structures.Location_Vector;

source/ada/lsp-ada_handlers.adb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,14 @@ package body LSP.Ada_Handlers is
184184
Kinds : AlsReferenceKind_Array := LSP.Constants.Empty)
185185
renames LSP.Ada_Handlers.Locations.Append_Location;
186186

187+
overriding procedure Append_Location
188+
(Self : in out Message_Handler;
189+
Result : in out LSP.Structures.Location_Vector;
190+
Filter : in out LSP.Locations.File_Span_Sets.Set;
191+
Unit : Libadalang.Analysis.Analysis_Unit;
192+
Token : Libadalang.Common.Token_Reference)
193+
renames LSP.Ada_Handlers.Locations.Append_Location;
194+
187195
function Project_Predefined_Units
188196
(Self : in out Message_Handler; Context : LSP.Ada_Contexts.Context)
189197
return Libadalang.Analysis.Analysis_Unit_Array;

source/ada/lsp-ada_handlers.ads

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,13 @@ private
571571
Node : Libadalang.Analysis.Ada_Node'Class;
572572
Kinds : LSP.Structures.AlsReferenceKind_Set := LSP.Constants.Empty);
573573

574+
overriding procedure Append_Location
575+
(Self : in out Message_Handler;
576+
Result : in out LSP.Structures.Location_Vector;
577+
Filter : in out LSP.Locations.File_Span_Sets.Set;
578+
Unit : Libadalang.Analysis.Analysis_Unit;
579+
Token : Libadalang.Common.Token_Reference);
580+
574581
overriding procedure Trace_Exception
575582
(Self : Message_Handler;
576583
Error : Ada.Exceptions.Exception_Occurrence;

source/ada/lsp-ada_job_contexts.ads

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,15 @@ package LSP.Ada_Job_Contexts is
187187
-- Append given Node location to the Result.
188188
-- Do nothing if the item inside of an synthetic file (like __standard).
189189

190+
procedure Append_Location
191+
(Self : in out Ada_Job_Context;
192+
Result : in out LSP.Structures.Location_Vector;
193+
Filter : in out LSP.Locations.File_Span_Sets.Set;
194+
Unit : Libadalang.Analysis.Analysis_Unit;
195+
Token : Libadalang.Common.Token_Reference)
196+
is abstract;
197+
-- Append given token location to the Result.
198+
190199
procedure Trace_Exception
191200
(Self : Ada_Job_Context;
192201
Error : Ada.Exceptions.Exception_Occurrence;

testsuite/ada_lsp/D803-003.xrefs.generics_go_to_body/test.json

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
[
22
{
3-
"comment": [
4-
"test that textDocument/definition works well with generics"
5-
]
3+
"comment": ["test that textDocument/definition works well with generics"]
64
},
75
{
86
"start": {
9-
"cmd": [
10-
"${ALS}"
11-
]
7+
"cmd": ["${ALS}"]
128
}
139
},
1410
{
@@ -39,12 +35,7 @@
3935
"referencesProvider": true,
4036
"textDocumentSync": 2,
4137
"completionProvider": {
42-
"triggerCharacters": [
43-
".",
44-
",",
45-
"'",
46-
"("
47-
],
38+
"triggerCharacters": [".", ",", "'", "("],
4839
"resolveProvider": true
4940
},
5041
"documentSymbolProvider": true
@@ -130,8 +121,7 @@
130121
"jsonrpc": "2.0",
131122
"method": "textDocument/didOpen"
132123
},
133-
"wait": [
134-
]
124+
"wait": []
135125
}
136126
},
137127
{
@@ -153,20 +143,19 @@
153143
"wait": [
154144
{
155145
"id": 2,
156-
"result":
157-
{
158-
"range": {
159-
"start": {
160-
"line": 2,
161-
"character": 13
162-
},
163-
"end": {
164-
"line": 2,
165-
"character": 16
166-
}
146+
"result": {
147+
"range": {
148+
"start": {
149+
"line": 2,
150+
"character": 13
167151
},
168-
"uri": "$URI{gen.ads}"
169-
}
152+
"end": {
153+
"line": 2,
154+
"character": 16
155+
}
156+
},
157+
"uri": "$URI{gen.ads}"
158+
}
170159
}
171160
]
172161
}
@@ -190,7 +179,7 @@
190179
"wait": [
191180
{
192181
"id": 3,
193-
"result":
182+
"result": [
194183
{
195184
"range": {
196185
"start": {
@@ -203,7 +192,21 @@
203192
}
204193
},
205194
"uri": "$URI{gen.adb}"
195+
},
196+
{
197+
"range": {
198+
"start": {
199+
"line": 2,
200+
"character": 3
201+
},
202+
"end": {
203+
"line": 2,
204+
"character": 8
205+
}
206+
},
207+
"uri": "$URI{gen.adb}"
206208
}
209+
]
207210
}
208211
]
209212
}
@@ -227,20 +230,19 @@
227230
"wait": [
228231
{
229232
"id": 4,
230-
"result":
231-
{
232-
"range": {
233-
"start": {
234-
"line": 5,
235-
"character": 13
236-
},
237-
"end": {
238-
"line": 5,
239-
"character": 14
240-
}
233+
"result": {
234+
"range": {
235+
"start": {
236+
"line": 5,
237+
"character": 13
241238
},
242-
"uri": "$URI{main.adb}"
243-
}
239+
"end": {
240+
"line": 5,
241+
"character": 14
242+
}
243+
},
244+
"uri": "$URI{main.adb}"
245+
}
244246
}
245247
]
246248
}

0 commit comments

Comments
 (0)