Skip to content

Commit 94b2e9d

Browse files
VB24-035: Factorize code for hover and completion tooltips
And try to use GNATdoc to extract the documentation and format the declarations when possible. Add a proper exception handler when needed.
1 parent 2c0f858 commit 94b2e9d

File tree

10 files changed

+204
-143
lines changed

10 files changed

+204
-143
lines changed

source/ada/lsp-ada_contexts.adb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,7 @@ package body LSP.Ada_Contexts is
646646
(Self : in out Context;
647647
File_Reader : File_Reader_Interface'Class;
648648
Follow_Symlinks : Boolean;
649+
Style : GNATdoc.Comments.Options.Documentation_Style;
649650
As_Fallback_Context : Boolean := False) is
650651
begin
651652
Self.Follow_Symlinks := Follow_Symlinks;
@@ -658,6 +659,8 @@ package body LSP.Ada_Contexts is
658659
With_Trivia => True,
659660
Charset => Self.Get_Charset,
660661
Tab_Stop => 1);
662+
Self.Style := Style;
663+
661664
-- Tab stop is set 1 to disable "visible character guessing" by LAL.
662665
Self.Is_Fallback_Context := As_Fallback_Context;
663666
end Initialize;

source/ada/lsp-ada_contexts.ads

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ with GNATCOLL.Projects;
2424
with GNATCOLL.Traces;
2525
with GNATCOLL.VFS;
2626

27+
with GNATdoc.Comments.Options;
28+
2729
with Langkit_Support.File_Readers; use Langkit_Support.File_Readers;
2830
with Laltools.Common;
2931

@@ -53,10 +55,13 @@ package LSP.Ada_Contexts is
5355
(Self : in out Context;
5456
File_Reader : File_Reader_Interface'Class;
5557
Follow_Symlinks : Boolean;
58+
Style : GNATdoc.Comments.Options.Documentation_Style;
5659
As_Fallback_Context : Boolean := False);
5760
-- Initialize the context, set Follow_Symlinks flag.
5861
-- As_Fallback_Context should be set when we are creating the "fallback"
5962
-- context based on the empty project.
63+
-- Style is used to extract the documentation of entities, for tooltips
64+
-- in particular.
6065

6166
procedure Load_Project
6267
(Self : in out Context;
@@ -212,6 +217,10 @@ package LSP.Ada_Contexts is
212217
Utils.Command_Lines.Command_Line;
213218
-- Return the command line for the Pretty Printer
214219

220+
function Get_Documentation_Style (Self : Context) return
221+
GNATdoc.Comments.Options.Documentation_Style;
222+
-- Get the documentation style used for this context.
223+
215224
function Analysis_Units
216225
(Self : Context) return Libadalang.Analysis.Analysis_Unit_Array;
217226
-- Return the analysis units for all Ada sources known to this context
@@ -348,6 +357,10 @@ private
348357
(Pp.Command_Lines.Descriptor'Access);
349358
-- Object to keep gnatpp options
350359

360+
Style : GNATdoc.Comments.Options.Documentation_Style :=
361+
GNATdoc.Comments.Options.GNAT;
362+
-- The context's documentation style.
363+
351364
Follow_Symlinks : Boolean := True;
352365
-- See LSP.Ada_Handlers for description
353366

@@ -369,4 +382,7 @@ private
369382
function Get_PP_Options (Self : Context) return
370383
Utils.Command_Lines.Command_Line is (Self.PP_Options);
371384

385+
function Get_Documentation_Style (Self : Context) return
386+
GNATdoc.Comments.Options.Documentation_Style is (Self.Style);
387+
372388
end LSP.Ada_Contexts;

source/ada/lsp-ada_documents.adb

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2513,28 +2513,39 @@ package body LSP.Ada_Documents is
25132513
Item : in out LSP.Messages.CompletionItem;
25142514
Compute_Doc_And_Details : Boolean)
25152515
is
2516-
use LSP.Messages;
25172516
begin
25182517
-- Compute the 'documentation' and 'detail' fields immediately if
25192518
-- requested (i.e: when the client does not support lazy computation
25202519
-- for these fields or if we are dealing with predefined types).
25212520
if Compute_Doc_And_Details or else LSP.Lal_Utils.Is_Synthetic (BD) then
2522-
Item.detail := (True, LSP.Lal_Utils.Compute_Completion_Detail (BD));
2523-
2524-
-- Property_Errors can occur when calling
2525-
-- Get_Documentation on unsupported docstrings, so
2526-
-- add an exception handler to catch them and recover.
2521+
declare
2522+
Loc_Text : VSS.Strings.Virtual_String;
2523+
Doc_Text : VSS.Strings.Virtual_String;
2524+
Decl_Text : VSS.Strings.Virtual_String;
25272525
begin
2526+
LSP.Lal_Utils.Get_Tooltip_Text
2527+
(BD => BD,
2528+
Trace => Context.Trace,
2529+
Style => Context.Get_Documentation_Style,
2530+
Loc_Text => Loc_Text,
2531+
Doc_Text => Doc_Text,
2532+
Decl_Text => Decl_Text);
2533+
2534+
Item.detail := (True, Decl_Text);
2535+
2536+
if not Doc_Text.Is_Empty then
2537+
Loc_Text.Append
2538+
(VSS.Strings.To_Virtual_String
2539+
((1 .. 2 => Ada.Characters.Wide_Wide_Latin_1.LF)));
2540+
2541+
Loc_Text.Append (Doc_Text);
2542+
end if;
2543+
25282544
Item.documentation :=
25292545
(Is_Set => True,
2530-
Value => String_Or_MarkupContent'
2546+
Value => LSP.Messages.String_Or_MarkupContent'
25312547
(Is_String => True,
2532-
String => LSP.Lal_Utils.Compute_Completion_Doc (BD)));
2533-
2534-
exception
2535-
when E : Libadalang.Common.Property_Error =>
2536-
LSP.Common.Log (Context.Trace, E);
2537-
Item.documentation := (others => <>);
2548+
String => Loc_Text));
25382549
end;
25392550
else
25402551
-- Set node's location to the 'data' field of the completion item, so

source/ada/lsp-ada_documents.ads

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
with Ada.Containers.Ordered_Maps;
2121
with Ada.Containers.Vectors;
2222
with VSS.String_Vectors;
23-
2423
with VSS.Strings;
2524
private with VSS.Strings.Markers;
2625

source/ada/lsp-ada_handlers.adb

Lines changed: 39 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,6 @@ with Libadalang.Doc_Utils;
9696
with Libadalang.Helpers;
9797
with Libadalang.Preprocessing;
9898

99-
with GNATdoc.Comments.Helpers;
100-
10199
with URIs;
102100

103101
package body LSP.Ada_Handlers is
@@ -669,8 +667,11 @@ package body LSP.Ada_Handlers is
669667
Self.Project_Environment.Set_Trusted_Mode (not Self.Follow_Symlinks);
670668
Self.Project_Tree := new Project_Tree;
671669

672-
C.Initialize (Reader, Self.Follow_Symlinks,
673-
As_Fallback_Context => True);
670+
C.Initialize
671+
(File_Reader => Reader,
672+
Follow_Symlinks => Self.Follow_Symlinks,
673+
Style => Self.Options.Documentation.Style,
674+
As_Fallback_Context => True);
674675

675676
-- Note: we would call Load_Implicit_Project here, but this has
676677
-- two problems:
@@ -3101,9 +3102,7 @@ package body LSP.Ada_Handlers is
31013102

31023103
Defining_Name_Node : Defining_Name;
31033104
Decl : Basic_Decl;
3104-
Decl_Lines : VSS.String_Vectors.Virtual_String_Vector;
31053105
Decl_Text : VSS.Strings.Virtual_String;
3106-
Comments_Lines : VSS.String_Vectors.Virtual_String_Vector;
31073106
Comments_Text : VSS.Strings.Virtual_String;
31083107
Location_Text : VSS.Strings.Virtual_String;
31093108

@@ -3112,12 +3111,6 @@ package body LSP.Ada_Handlers is
31123111
-- For the Hover request, we're only interested in the "best"
31133112
-- response value, not in the list of values for all contexts
31143113

3115-
Options : constant
3116-
GNATdoc.Comments.Options.Extractor_Options :=
3117-
(Style => Self.Options.Documentation.Style,
3118-
Pattern => <>,
3119-
Fallback => True);
3120-
31213114
begin
31223115
Self.Imprecise_Resolve_Name (C, Value, Defining_Name_Node);
31233116

@@ -3132,30 +3125,13 @@ package body LSP.Ada_Handlers is
31323125
return Response;
31333126
end if;
31343127

3135-
-- Extract documentation with GNATdoc when supported.
3136-
3137-
GNATdoc.Comments.Helpers.Get_Plain_Text_Documentation
3138-
(Defining_Name_Node, Options, Decl_Lines, Comments_Lines);
3139-
3140-
Decl_Text := Decl_Lines.Join_Lines (VSS.Strings.LF, False);
3141-
Comments_Text := Comments_Lines.Join_Lines (VSS.Strings.LF, False);
3142-
3143-
-- Obtain documentation when GNATdoc support is missing.
3144-
3145-
if Comments_Text.Is_Empty then
3146-
Comments_Text :=
3147-
VSS.Strings.To_Virtual_String
3148-
(Libadalang.Doc_Utils.Get_Documentation (Decl).Doc.To_String);
3149-
end if;
3150-
3151-
if Decl_Text.Is_Empty
3152-
or else not Decl.P_Subp_Spec_Or_Null.Is_Null
3153-
then
3154-
-- For subprograms additional information is added, use old code to
3155-
-- obtain it yet.
3156-
3157-
Decl_Text := Get_Hover_Text (Decl, Decl_Lines);
3158-
end if;
3128+
LSP.Lal_Utils.Get_Tooltip_Text
3129+
(BD => Decl,
3130+
Trace => Self.Trace,
3131+
Style => C.Get_Documentation_Style,
3132+
Loc_Text => Location_Text,
3133+
Doc_Text => Comments_Text,
3134+
Decl_Text => Decl_Text);
31593135

31603136
if Decl_Text.Is_Empty then
31613137
return Response;
@@ -4666,10 +4642,13 @@ package body LSP.Ada_Handlers is
46664642
Libadalang.Preprocessing.Create_Preprocessor_Data
46674643
(Default_Config, File_Configs);
46684644

4669-
C.Initialize (Reader, Self.Follow_Symlinks);
4645+
C.Initialize
4646+
(Reader,
4647+
Style => Self.Options.Documentation.Style,
4648+
Follow_Symlinks => Self.Follow_Symlinks);
46704649

46714650
C.Load_Project
4672-
(Self.Project_Tree,
4651+
(Tree => Self.Project_Tree,
46734652
Root => P,
46744653
Charset => VSS.Strings.Conversions.To_UTF_8_String (Charset));
46754654

@@ -5555,25 +5534,35 @@ package body LSP.Ada_Handlers is
55555534
-- Compute the completion item's details
55565535
if not Node.Is_Null then
55575536
declare
5558-
BD : constant Libadalang.Analysis.Basic_Decl :=
5537+
BD : constant Libadalang.Analysis.Basic_Decl :=
55595538
Node.As_Basic_Decl;
5539+
Loc_Text : VSS.Strings.Virtual_String;
5540+
Doc_Text : VSS.Strings.Virtual_String;
5541+
Decl_Text : VSS.Strings.Virtual_String;
55605542
begin
5561-
Item.detail := (True, Compute_Completion_Detail (BD));
5562-
5563-
-- Property_Errors can occur when calling
5564-
-- Get_Documentation on unsupported docstrings, so
5565-
-- add an exception handler to catch them and recover.
5543+
LSP.Lal_Utils.Get_Tooltip_Text
5544+
(BD => BD,
5545+
Trace => C.Trace,
5546+
Style => Self.Options.Documentation.Style,
5547+
Loc_Text => Loc_Text,
5548+
Doc_Text => Doc_Text,
5549+
Decl_Text => Decl_Text);
5550+
5551+
Item.detail := (True, Decl_Text);
5552+
5553+
if not Doc_Text.Is_Empty then
5554+
Loc_Text.Append
5555+
(VSS.Strings.To_Virtual_String
5556+
((1 .. 2 => Ada.Characters.Wide_Wide_Latin_1.LF)));
5557+
5558+
Loc_Text.Append (Doc_Text);
5559+
end if;
55665560

55675561
Item.documentation :=
55685562
(Is_Set => True,
55695563
Value => LSP.Messages.String_Or_MarkupContent'
55705564
(Is_String => True,
5571-
String => LSP.Lal_Utils.Compute_Completion_Doc (BD)));
5572-
5573-
exception
5574-
when E : Libadalang.Common.Property_Error =>
5575-
LSP.Common.Log (C.Trace, E);
5576-
Item.documentation := (others => <>);
5565+
String => Loc_Text));
55775566
end;
55785567

55795568
Response.result := Item;

0 commit comments

Comments
 (0)