Skip to content

Commit c92073c

Browse files
author
Philippe Gil
committed
Merge branch 'topic/1186-gpr-packages-attributes-hover' into 'master'
Implements hover request for package & attributes. See merge request eng/ide/ada_language_server!1401
2 parents cfb1ec8 + 54a8b51 commit c92073c

File tree

10 files changed

+641
-0
lines changed

10 files changed

+641
-0
lines changed

source/gpr/lsp-gpr_documentation.adb

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
------------------------------------------------------------------------------
2+
-- Language Server Protocol --
3+
-- --
4+
-- Copyright (C) 2023, 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.Characters.Conversions;
19+
20+
with Gpr_Parser.Common;
21+
22+
with GPR2.Project.Registry.Attribute.Description;
23+
with GPR2.Project.Registry.Pack.Description;
24+
with VSS.Strings.Conversions;
25+
26+
package body LSP.GPR_Documentation is
27+
28+
procedure Get_Tooltip_Text
29+
(Self : LSP.GPR_Files.File;
30+
Position : LSP.Structures.Position;
31+
Tooltip_Text : out VSS.Strings.Virtual_String) is
32+
use Gpr_Parser.Common;
33+
34+
Token : constant Token_Reference := Self.Token (Position);
35+
36+
use GPR2.Project.Registry.Attribute.Description;
37+
use GPR2.Project.Registry.Pack.Description;
38+
use GPR2;
39+
use Ada.Characters.Conversions;
40+
begin
41+
42+
Tooltip_Text.Clear;
43+
44+
if Token.Data.Kind = Gpr_Identifier then
45+
case Token.Previous (Exclude_Trivia => True).Data.Kind is
46+
when Gpr_Package | Gpr_End =>
47+
Tooltip_Text.Append
48+
(VSS.Strings.Conversions.To_Virtual_String
49+
(Get_Package_Description (Self.Get_Package (Position))));
50+
51+
when Gpr_For =>
52+
Tooltip_Text.Append
53+
(VSS.Strings.Conversions.To_Virtual_String
54+
(Get_Attribute_Description ((
55+
Self.Get_Package (Position),
56+
+(Optional_Name_Type (To_String (Token.Text)))))));
57+
58+
when others =>
59+
null;
60+
end case;
61+
end if;
62+
end Get_Tooltip_Text;
63+
64+
end LSP.GPR_Documentation;

source/gpr/lsp-gpr_documentation.ads

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
------------------------------------------------------------------------------
2+
-- Language Server Protocol --
3+
-- --
4+
-- Copyright (C) 2023, 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+
-- Subprogram to obtain documentation for packages & attributes.
19+
20+
with LSP.GPR_Files;
21+
with LSP.Structures;
22+
23+
with VSS.Strings;
24+
25+
package LSP.GPR_Documentation is
26+
27+
procedure Get_Tooltip_Text
28+
(Self : LSP.GPR_Files.File;
29+
Position : LSP.Structures.Position;
30+
Tooltip_Text : out VSS.Strings.Virtual_String);
31+
-- Get all the information needed to produce tooltips (hover and completion
32+
-- requests)
33+
34+
end LSP.GPR_Documentation;

source/gpr/lsp-gpr_files.adb

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,34 @@ package body LSP.GPR_Files is
4747
package Convert is new VSS.Strings.Formatters.Generic_Integers (Unit_Index);
4848
package Conversions renames VSS.Strings.Conversions;
4949

50+
function Is_Between
51+
(Position : LSP.Structures.Position;
52+
Span : LSP.Structures.A_Range)
53+
return Boolean
54+
is ((Position.line = Span.start.line
55+
and then Position.character >= Span.start.character)
56+
or else (Position.line = Span.an_end.line
57+
and then Position.character <= Span.an_end.character)
58+
or else (Position.line > Span.start.line
59+
and then Position.line < Span.an_end.line));
60+
-- Checks if Position is inside Span
61+
62+
-----------------
63+
-- Get_Package --
64+
-----------------
65+
66+
function Get_Package
67+
(Self : File; Position : LSP.Structures.Position) return GPR2.Package_Id
68+
is
69+
begin
70+
for P of Self.Packages loop
71+
if Is_Between (Position, P.Package_Range) then
72+
return P.Name;
73+
end if;
74+
end loop;
75+
return GPR2.Project_Level_Scope;
76+
end Get_Package;
77+
5078
--------
5179
-- Id --
5280
--------
@@ -315,6 +343,26 @@ package body LSP.GPR_Files is
315343
begin
316344
if Current_Package.Name /= Project_Level_Scope then
317345
Current_Package.Last := Last_Index;
346+
347+
-- Initialize Package_Range
348+
349+
declare
350+
Location_Range : Slocs.Source_Location_Range :=
351+
Gpr_Parser.Common.Sloc_Range
352+
(Gpr_Parser.Common.Data (Current_Package.First));
353+
begin
354+
Current_Package.Package_Range.start.line :=
355+
To_Position_Value (Location_Range.Start_Line);
356+
Current_Package.Package_Range.start.character :=
357+
To_Position_Value (Location_Range.Start_Column);
358+
Location_Range := Gpr_Parser.Common.Sloc_Range
359+
(Gpr_Parser.Common.Data (Current_Package.Last));
360+
Current_Package.Package_Range.an_end.line :=
361+
To_Position_Value (Location_Range.Start_Line);
362+
Current_Package.Package_Range.an_end.character :=
363+
To_Position_Value (Location_Range.Start_Column);
364+
end;
365+
318366
if not File.Packages.Contains (Current_Package.Name) then
319367
File.Packages.Insert (Current_Package.Name, Current_Package);
320368
end if;
@@ -981,4 +1029,21 @@ package body LSP.GPR_Files is
9811029
end if;
9821030
end Cleanup;
9831031

1032+
-----------
1033+
-- Token --
1034+
-----------
1035+
1036+
function Token
1037+
(Self : File;
1038+
Position : LSP.Structures.Position)
1039+
return Gpr_Parser.Common.Token_Reference is
1040+
Sloc : constant Source_Location :=
1041+
(To_Line_Number (Position.line),
1042+
To_Column_Number (Position.character));
1043+
1044+
begin
1045+
return Self.Unit.Lookup_Token (Sloc);
1046+
1047+
end Token;
1048+
9841049
end LSP.GPR_Files;

source/gpr/lsp-gpr_files.ads

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ with VSS.Strings;
4242
with VSS.Strings.Hash;
4343
with VSS.String_Vectors;
4444

45+
with LSP.Structures;
4546
with LSP.Tracers;
4647

4748
package LSP.GPR_Files is
@@ -104,6 +105,44 @@ package LSP.GPR_Files is
104105
Path : GPR2.Path_Name.Object) return File_Access;
105106
-- parse file if not yet parsed
106107

108+
function Get_Package
109+
(Self : File; Position : LSP.Structures.Position) return GPR2.Package_Id;
110+
-- return the Position's Package_Id. Returns Project_Level_Scope if
111+
-- Position not inside a package.
112+
113+
function Token
114+
(Self : File;
115+
Position : LSP.Structures.Position)
116+
return Gpr_Parser.Common.Token_Reference;
117+
-- Return File's Token at Position.
118+
119+
-------------------------------------------------
120+
-- GPR Parser / LSP Slocs-Position conversions --
121+
-------------------------------------------------
122+
123+
use Gpr_Parser_Support.Slocs;
124+
125+
function To_Position_Value
126+
(Line : Gpr_Parser_Support.Slocs.Line_Number) return Natural is
127+
(if Line = 0 then 0 else Natural (Line) - 1);
128+
129+
function To_Line_Number
130+
(Line : Natural) return Gpr_Parser_Support.Slocs.Line_Number is
131+
(Gpr_Parser_Support.Slocs.Line_Number (Line + 1));
132+
133+
function To_Position_Value
134+
(Column : Gpr_Parser_Support.Slocs.Column_Number) return Natural is
135+
(if Column = 0 then 0 else Natural (Column) - 1);
136+
137+
function To_Column_Number
138+
(Line : Natural) return Gpr_Parser_Support.Slocs.Column_Number is
139+
(Gpr_Parser_Support.Slocs.Column_Number (Line + 1));
140+
141+
function To_Position
142+
(Location : Gpr_Parser_Support.Slocs.Source_Location)
143+
return LSP.Structures.Position is
144+
(To_Position_Value (Location.Line), To_Position_Value (Location.Column));
145+
107146
private
108147

109148
use GPR2;
@@ -311,6 +350,9 @@ private
311350
Gpr_Parser.Common.No_Token;
312351
-- 'Last' is at the package's end or at the end of the renaming.
313352

353+
Package_Range : LSP.Structures.A_Range := ((0, 0), (0, 0));
354+
-- Useful to find to what package a LSP Position belongs.
355+
314356
Attributes : Attribute_Maps.Map;
315357
-- Attributes defined in the package
316358

source/gpr/lsp-gpr_handlers.adb

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ with GPR2.Source_Reference;
2828
with LSP.Constants;
2929
with LSP.Enumerations;
3030
with LSP.Generic_Cancel_Check;
31+
with LSP.GPR_Documentation;
3132
with LSP.GPR_File_Readers;
3233
with LSP.GPR_Files.Symbols;
3334
with LSP.Servers;
@@ -424,6 +425,60 @@ package body LSP.GPR_Handlers is
424425
Self.Sender.On_DocumentSymbol_Response (Id, Response);
425426
end On_DocumentSymbol_Request;
426427

428+
----------------------
429+
-- On_Hover_Request --
430+
----------------------
431+
432+
overriding procedure On_Hover_Request
433+
(Self : in out Message_Handler;
434+
Id : LSP.Structures.Integer_Or_Virtual_String;
435+
Value : LSP.Structures.HoverParams)
436+
is
437+
438+
Response : LSP.Structures.Hover_Or_Null;
439+
440+
procedure Compute_Response;
441+
442+
----------------------
443+
-- Compute_Response --
444+
----------------------
445+
446+
procedure Compute_Response is
447+
File : constant LSP.GPR_Files.File_Access :=
448+
LSP.GPR_Files.Parse
449+
(File_Provider => Self'Unchecked_Access,
450+
Path => Self.To_File
451+
(Value.textDocument.uri));
452+
Tooltip_Text : VSS.Strings.Virtual_String;
453+
begin
454+
455+
LSP.GPR_Documentation.Get_Tooltip_Text
456+
(Self => File.all,
457+
Position => Value.position,
458+
Tooltip_Text => Tooltip_Text);
459+
460+
if Tooltip_Text.Is_Empty then
461+
return;
462+
end if;
463+
464+
Response := (Is_Null => False, others => <>);
465+
Response.Value.contents := (Is_MarkupContent => False, others => <>);
466+
467+
-- Append the package/attribute description
468+
469+
Response.Value.contents.MarkedString_Vector.Append
470+
(LSP.Structures.MarkedString'
471+
(Is_Virtual_String => False,
472+
language => "plaintext",
473+
value => Tooltip_Text));
474+
475+
end Compute_Response;
476+
477+
begin
478+
Compute_Response;
479+
Self.Sender.On_Hover_Response (Id, Response);
480+
end On_Hover_Request;
481+
427482
---------------------------
428483
-- On_Initialize_Request --
429484
---------------------------

source/gpr/lsp-gpr_handlers.ads

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,11 @@ private
141141
(Self : in out Message_Handler;
142142
Value : LSP.Structures.DidOpenTextDocumentParams);
143143

144+
overriding procedure On_Hover_Request
145+
(Self : in out Message_Handler;
146+
Id : LSP.Structures.Integer_Or_Virtual_String;
147+
Value : LSP.Structures.HoverParams);
148+
144149
-----------------------------------------
145150
-- LSP.GPR_Documents.Document_Provider --
146151
-----------------------------------------

testsuite/gpr_lsp/hover/prj1.gpr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
project Prj1 is
2+
for Source_Dirs use ("src");
3+
package Compiler is
4+
for Switches ("Ada") use ();
5+
end Compiler;
6+
end Prj1;

testsuite/gpr_lsp/hover/prj2.gpr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
project Prj2 is
2+
for Source_Dirs use ("src");
3+
-- The list of source directories of the project.
4+
end Prj2;

0 commit comments

Comments
 (0)