Skip to content

Commit ebe063e

Browse files
Allow navigation queries right after entities
For eng/ide/ada_language_server#1686
1 parent 3bbc319 commit ebe063e

File tree

6 files changed

+198
-194
lines changed

6 files changed

+198
-194
lines changed

source/ada/lsp-ada_documents.adb

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,10 +1202,70 @@ package body LSP.Ada_Documents is
12021202
Context : LSP.Ada_Contexts.Context;
12031203
Position : LSP.Structures.Position) return Libadalang.Analysis.Ada_Node
12041204
is
1205-
Unit : constant Libadalang.Analysis.Analysis_Unit := Self.Unit (Context);
1205+
use Langkit_Support.Slocs;
1206+
1207+
Unit : constant Libadalang.Analysis.Analysis_Unit :=
1208+
Self.Unit (Context);
1209+
1210+
function Get_Sloc return Langkit_Support.Slocs.Source_Location;
1211+
-- Return the source location corresponding to Position.
1212+
-- This function handles properly the case where the position is
1213+
-- at the end of an indentifier token and at the start of the next
1214+
-- token (e.g: 'An_Identifier^;' where '^' is the cursor's position):
1215+
-- in that case we want to retrieve the node corresponding to
1216+
-- the previous identifier token.
1217+
1218+
--------------
1219+
-- Get_Sloc --
1220+
--------------
1221+
1222+
function Get_Sloc return Langkit_Support.Slocs.Source_Location is
1223+
use type Langkit_Support.Slocs.Source_Location;
1224+
use Libadalang.Common;
1225+
1226+
Initial_Sloc : constant Langkit_Support.Slocs.Source_Location :=
1227+
Self.To_Source_Location (Position);
1228+
Token : constant Libadalang.Common.Token_Reference :=
1229+
Self.Get_Token_At (Context, Position);
1230+
Prev_Token : constant Libadalang.Common.Token_Reference :=
1231+
(if Token /= No_Token
1232+
then Libadalang.Common.Previous (Token)
1233+
else No_Token);
1234+
begin
1235+
-- No previous token, return the initial SLOC
1236+
if Prev_Token = No_Token then
1237+
return Initial_Sloc;
1238+
end if;
1239+
1240+
-- The current token is an identifier and its start SLOC and
1241+
-- the previous one's end SLOC are equal: consider that the query is
1242+
-- being done on the previous identifier token.
1243+
declare
1244+
Token_Data : constant Libadalang.Common.Token_Data_Type :=
1245+
Libadalang.Common.Data (Token);
1246+
Token_Range :
1247+
constant Libadalang.Slocs.Source_Location_Range :=
1248+
Token_Data.Sloc_Range;
1249+
Prev_Token_Data : constant Libadalang.Common.Token_Data_Type :=
1250+
Libadalang.Common.Data (Prev_Token);
1251+
Prev_Token_Range :
1252+
constant Libadalang.Slocs.Source_Location_Range :=
1253+
Prev_Token_Data.Sloc_Range;
1254+
begin
1255+
if Prev_Token_Data.Kind = Ada_Identifier
1256+
and then Initial_Sloc = Token_Range.Start_Sloc
1257+
and then Initial_Sloc = Prev_Token_Range.End_Sloc
1258+
then
1259+
return Prev_Token_Range.Start_Sloc;
1260+
else
1261+
return Initial_Sloc;
1262+
end if;
1263+
end;
1264+
end Get_Sloc;
1265+
12061266
begin
12071267
return (if Unit.Root.Is_Null then Libadalang.Analysis.No_Ada_Node
1208-
else Unit.Root.Lookup (Self.To_Source_Location (Position)));
1268+
else Unit.Root.Lookup (Get_Sloc));
12091269
end Get_Node_At;
12101270

12111271
--------------------------

testsuite/ada_lsp/callgraph.named_blocks/test.json

Lines changed: 2 additions & 192 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
},
77
{
88
"start": {
9-
"cmd": [
10-
"${ALS}"
11-
]
9+
"cmd": ["${ALS}"]
1210
}
1311
},
1412
{
@@ -34,10 +32,7 @@
3432
"dynamicRegistration": true,
3533
"completionItem": {
3634
"snippetSupport": true,
37-
"documentationFormat": [
38-
"plaintext",
39-
"markdown"
40-
]
35+
"documentationFormat": ["plaintext", "markdown"]
4136
}
4237
},
4338
"hover": {},
@@ -295,159 +290,6 @@
295290
"wait": []
296291
}
297292
},
298-
{
299-
"send": {
300-
"request": {
301-
"jsonrpc": "2.0",
302-
"id": "ada-4",
303-
"method": "textDocument/foldingRange",
304-
"params": {
305-
"textDocument": {
306-
"uri": "$URI{p1.adb}"
307-
}
308-
}
309-
},
310-
"wait": [
311-
{
312-
"id": "ada-4",
313-
"result": [
314-
{
315-
"startLine": 0,
316-
"endLine": 13,
317-
"kind": "region"
318-
},
319-
{
320-
"startLine": 4,
321-
"endLine": 9,
322-
"kind": "region"
323-
}
324-
]
325-
}
326-
]
327-
}
328-
},
329-
{
330-
"send": {
331-
"request": {
332-
"jsonrpc": "2.0",
333-
"id": "ada-5",
334-
"method": "textDocument/codeAction",
335-
"params": {
336-
"textDocument": {
337-
"uri": "$URI{p1.adb}"
338-
},
339-
"range": {
340-
"start": {
341-
"line": 7,
342-
"character": 11
343-
},
344-
"end": {
345-
"line": 7,
346-
"character": 11
347-
}
348-
},
349-
"context": {
350-
"diagnostics": []
351-
}
352-
}
353-
},
354-
"wait": [
355-
{
356-
"id": "ada-5",
357-
"result": null
358-
}
359-
]
360-
}
361-
},
362-
{
363-
"send": {
364-
"request": {
365-
"jsonrpc": "2.0",
366-
"id": "ada-6",
367-
"method": "textDocument/documentHighlight",
368-
"params": {
369-
"textDocument": {
370-
"uri": "$URI{p1.adb}"
371-
},
372-
"position": {
373-
"line": 7,
374-
"character": 10
375-
}
376-
}
377-
},
378-
"wait": [
379-
{
380-
"id": "ada-6",
381-
"result": null
382-
}
383-
]
384-
}
385-
},
386-
{
387-
"send": {
388-
"request": {
389-
"jsonrpc": "2.0",
390-
"id": "ada-7",
391-
"method": "textDocument/hover",
392-
"params": {
393-
"textDocument": {
394-
"uri": "$URI{p1.adb}"
395-
},
396-
"position": {
397-
"line": 7,
398-
"character": 9
399-
}
400-
}
401-
},
402-
"wait": [
403-
{
404-
"id": "ada-7",
405-
"result": {
406-
"contents": [
407-
{
408-
"language": "ada",
409-
"value": "procedure A"
410-
},
411-
"at p1.adb (3:4)"
412-
]
413-
}
414-
}
415-
]
416-
}
417-
},
418-
{
419-
"send": {
420-
"request": {
421-
"jsonrpc": "2.0",
422-
"id": "ada-8",
423-
"method": "textDocument/codeAction",
424-
"params": {
425-
"textDocument": {
426-
"uri": "$URI{p1.adb}"
427-
},
428-
"range": {
429-
"start": {
430-
"line": 7,
431-
"character": 11
432-
},
433-
"end": {
434-
"line": 7,
435-
"character": 11
436-
}
437-
},
438-
"context": {
439-
"diagnostics": []
440-
}
441-
}
442-
},
443-
"wait": [
444-
{
445-
"id": "ada-8",
446-
"result": null
447-
}
448-
]
449-
}
450-
},
451293
{
452294
"send": {
453295
"request": {
@@ -581,38 +423,6 @@
581423
]
582424
}
583425
},
584-
{
585-
"send": {
586-
"request": {
587-
"jsonrpc": "2.0",
588-
"id": "ada-11",
589-
"method": "textDocument/codeAction",
590-
"params": {
591-
"textDocument": {
592-
"uri": "$URI{p1.adb}"
593-
},
594-
"range": {
595-
"start": {
596-
"line": 7,
597-
"character": 10
598-
},
599-
"end": {
600-
"line": 7,
601-
"character": 11
602-
}
603-
},
604-
"context": {
605-
"diagnostics": []
606-
}
607-
}
608-
},
609-
"wait": [
610-
{
611-
"id": "ada-11"
612-
}
613-
]
614-
}
615-
},
616426
{
617427
"send": {
618428
"request": {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
with Ada.Text_IO;
2+
procedure Aaa is
3+
begin
4+
Ada.Text_IO.New_Line;
5+
Aaa;
6+
end Aaa;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
project Default is
2+
end Default;

0 commit comments

Comments
 (0)