Skip to content

Commit 4c87ca9

Browse files
Merge branch 'topic/predefined_tooltips' into 'master'
Compute tooltips for aspects, pragmas and attributes See merge request eng/ide/ada_language_server!1452
2 parents 8d15304 + d4d8303 commit 4c87ca9

File tree

7 files changed

+461
-23
lines changed

7 files changed

+461
-23
lines changed

source/ada/lsp-ada_handlers.adb

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ with LSP.Errors;
9494
with LSP.Formatters.Texts;
9595
with LSP.Generic_Cancel_Check;
9696
with LSP.GNATCOLL_Tracers.Handle;
97+
with LSP.Predefined_Completion;
9798
with LSP.Search;
9899
with LSP.Server_Notifications.DidChange;
99100
with LSP.Servers;
@@ -3028,24 +3029,48 @@ package body LSP.Ada_Handlers is
30283029

30293030
Decl_Text : VSS.Strings.Virtual_String;
30303031
Qualifier_Text : VSS.Strings.Virtual_String;
3031-
Comments_Text : VSS.Strings.Virtual_String;
3032+
Documentation_Text : VSS.Strings.Virtual_String;
30323033
Location_Text : VSS.Strings.Virtual_String;
30333034
Aspects_Text : VSS.Strings.Virtual_String;
30343035

30353036
begin
3036-
if Decl.Is_Null or else Self.Is_Canceled.all then
3037+
-- Return immediately if the request has been canceled
3038+
if Self.Is_Canceled.all then
30373039
return;
30383040
end if;
30393041

3040-
LSP.Ada_Documentation.Get_Tooltip_Text
3041-
(BD => Decl,
3042-
Style => Context.Get_Documentation_Style,
3043-
Declaration_Text => Decl_Text,
3044-
Qualifier_Text => Qualifier_Text,
3045-
Location_Text => Location_Text,
3046-
Documentation_Text => Comments_Text,
3047-
Aspects_Text => Aspects_Text);
3042+
if Decl.Is_Null then
3043+
3044+
-- There is no declaration for the hovered node: ask the predefined
3045+
-- entities' completion provider (attributes, pragmas, aspects) for
3046+
-- a tooltip text if it can.
3047+
declare
3048+
Node : constant Libadalang.Analysis.Ada_Node := Self.Get_Node_At
3049+
(Context.all, Value);
3050+
begin
3051+
if not Node.Is_Null
3052+
and then Node.Kind in Libadalang.Common.Ada_Identifier_Range
3053+
then
3054+
LSP.Predefined_Completion.Get_Tooltip_Text
3055+
(Node => Node.As_Identifier,
3056+
Declaration_Text => Decl_Text,
3057+
Documentation_Text => Documentation_Text);
3058+
end if;
3059+
end;
3060+
else
3061+
-- We have resolved the hovered node to its declaration: use
3062+
-- the default tooltip provider, based on GNATdoc.
3063+
LSP.Ada_Documentation.Get_Tooltip_Text
3064+
(BD => Decl,
3065+
Style => Context.Get_Documentation_Style,
3066+
Declaration_Text => Decl_Text,
3067+
Qualifier_Text => Qualifier_Text,
3068+
Location_Text => Location_Text,
3069+
Documentation_Text => Documentation_Text,
3070+
Aspects_Text => Aspects_Text);
3071+
end if;
30483072

3073+
-- Return if no provider has been able to compute text for a tooltip.
30493074
if Decl_Text.Is_Empty then
30503075
return;
30513076
end if;
@@ -3075,28 +3100,30 @@ package body LSP.Ada_Handlers is
30753100
-- In addition, append the project's name if we are dealing with an
30763101
-- aggregate project.
30773102

3078-
Location_Text := LSP.Utils.Node_Location_Image (Decl);
3103+
if not Decl.Is_Null then
3104+
Location_Text := LSP.Utils.Node_Location_Image (Decl);
30793105

3080-
if Self.Project_Tree.Root_Project.Kind in GPR2.Aggregate_Kind then
3081-
Location_Text.Append (VSS.Characters.Latin.Line_Feed);
3082-
Location_Text.Append ("As defined in project ");
3083-
Location_Text.Append (Context.Id);
3084-
Location_Text.Append (" (other projects skipped).");
3085-
end if;
3106+
if Self.Project_Tree.Root_Project.Kind in GPR2.Aggregate_Kind then
3107+
Location_Text.Append (VSS.Characters.Latin.Line_Feed);
3108+
Location_Text.Append ("As defined in project ");
3109+
Location_Text.Append (Context.Id);
3110+
Location_Text.Append (" (other projects skipped).");
3111+
end if;
30863112

3087-
Response.Value.contents.MarkedString_Vector.Append
3088-
(LSP.Structures.MarkedString'
3089-
(Is_Virtual_String => True,
3090-
Virtual_String => Location_Text));
3113+
Response.Value.contents.MarkedString_Vector.Append
3114+
(LSP.Structures.MarkedString'
3115+
(Is_Virtual_String => True,
3116+
Virtual_String => Location_Text));
3117+
end if;
30913118

30923119
-- Append the comments associated with the basic declaration if any.
30933120

3094-
if not Comments_Text.Is_Empty then
3121+
if not Documentation_Text.Is_Empty then
30953122
Response.Value.contents.MarkedString_Vector.Append
30963123
(LSP.Structures.MarkedString'
30973124
(Is_Virtual_String => False,
30983125
language => "plaintext",
3099-
value => Comments_Text));
3126+
value => Documentation_Text));
31003127
end if;
31013128

31023129
-- Append text of aspects

source/ada/lsp-predefined_completion.adb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ with VSS.Strings.Conversions;
2828

2929
with LSP.Enumerations;
3030
with LSP.Predefined_Completion.Ada2012;
31+
with Libadalang.Common;
3132

3233
package body LSP.Predefined_Completion is
3334

@@ -277,4 +278,52 @@ package body LSP.Predefined_Completion is
277278
end;
278279
end Get_Output;
279280

281+
----------------------
282+
-- Get_Tooltip_Text --
283+
----------------------
284+
285+
procedure Get_Tooltip_Text
286+
(Node : Libadalang.Analysis.Identifier;
287+
Declaration_Text : out VSS.Strings.Virtual_String;
288+
Documentation_Text : out VSS.Strings.Virtual_String)
289+
is
290+
use Libadalang.Common;
291+
292+
Filtered_Items : CompletionItem_Vector;
293+
Prefix : VSS.Strings.Virtual_String;
294+
Parent : constant Libadalang.Analysis.Ada_Node :=
295+
(if Node.Is_Null then Libadalang.Analysis.No_Ada_Node
296+
else Node.Parent);
297+
Item : CompletionItem;
298+
begin
299+
-- Return immediately if the node is null of if its parent is null
300+
if Node.Is_Null or else Parent.Is_Null then
301+
return;
302+
end if;
303+
304+
-- Get the attribute/aspect/pragma completion item corresponding to the
305+
-- given node
306+
307+
Prefix := VSS.Strings.To_Virtual_String (Node.Text);
308+
309+
case Parent.Kind is
310+
when Ada_Attribute_Ref_Range =>
311+
Get_Attributes (Prefix => Prefix, Result => Filtered_Items);
312+
313+
when Ada_Aspect_Assoc_Range =>
314+
Get_Aspects (Prefix => Prefix, Result => Filtered_Items);
315+
316+
when Ada_Pragma_Node_Range =>
317+
Get_Pragmas (Prefix => Prefix, Result => Filtered_Items);
318+
319+
when others =>
320+
return;
321+
end case;
322+
323+
Item := Filtered_Items.First_Element;
324+
325+
Declaration_Text := Item.detail;
326+
Documentation_Text := Item.documentation.Value.Virtual_String;
327+
end Get_Tooltip_Text;
328+
280329
end LSP.Predefined_Completion;

source/ada/lsp-predefined_completion.ads

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ with GNATCOLL.Traces; use GNATCOLL.Traces;
2020
with VSS.Strings;
2121

2222
with LSP.Structures; use LSP.Structures;
23+
with Libadalang.Analysis;
2324

2425
package LSP.Predefined_Completion is
2526

@@ -43,4 +44,19 @@ package LSP.Predefined_Completion is
4344
Result : in out CompletionItem_Vector);
4445
-- Return completion for pragmas, filtering the results using Prefix.
4546

47+
procedure Get_Tooltip_Text
48+
(Node : Libadalang.Analysis.Identifier;
49+
Declaration_Text : out VSS.Strings.Virtual_String;
50+
Documentation_Text : out VSS.Strings.Virtual_String);
51+
-- Get all the information needed to produce tooltips (hover and completion
52+
-- requests) for the given node, if it's a predefined entity (e.g: aspect,
53+
-- pragma or attribute).
54+
--
55+
-- @param Node
56+
-- The predefined entity's node.
57+
-- @param Declaration_Text
58+
-- Contains the code corresponding to the predefined entity's declaration.
59+
-- @param Documentation_Text
60+
-- Contains the documentation associated to the predefined entity.
61+
4662
end LSP.Predefined_Completion;
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;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
with Ada.Text_IO; use Ada.Text_IO;
2+
3+
procedure Main
4+
with SPARK_Mode => Off
5+
is
6+
Foo : Integer := 30;
7+
pragma Convention (C, Foo);
8+
begin
9+
Put_Line (Foo'Image);
10+
end Main;

0 commit comments

Comments
 (0)