Skip to content

Commit

Permalink
Merge remote branch 'origin/master' into edge
Browse files Browse the repository at this point in the history
  • Loading branch information
automatic-merge committed May 27, 2024
2 parents c49b537 + 6356b60 commit ce68d69
Show file tree
Hide file tree
Showing 16 changed files with 988 additions and 79 deletions.
194 changes: 179 additions & 15 deletions source/gpr/lsp-gpr_documentation.adb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
------------------------------------------------------------------------------
-- Language Server Protocol --
-- --
-- Copyright (C) 2023-2024, AdaCore --
-- Copyright (C) 2023-2024, AdaCore --
-- --
-- This is free software; you can redistribute it and/or modify it under --
-- terms of the GNU General Public License as published by the Free Soft- --
Expand All @@ -17,10 +17,15 @@

with Ada.Characters.Conversions;
with Ada.Characters.Latin_1;
with Ada.Characters.Wide_Wide_Latin_1;

with GPR2.Project.Attribute;
with GPR2.Project.Typ;
with GPR2.Project.Variable;
with GPR2.Source_Reference;

with Gpr_Parser.Common;
with Gpr_Parser_Support.Slocs;

with GPR2.Project.Registry.Attribute.Description;
with GPR2.Project.Registry.Pack.Description;
Expand All @@ -32,12 +37,119 @@ with VSS.Strings.Conversions;

package body LSP.GPR_Documentation is

function Get_Documentation
(Ref : Gpr_Parser.Common.Token_Reference;
Style : GNATdoc.Comments.Options.Documentation_Style)
return VSS.Strings.Virtual_String;
-- Get variable/type declaration comment.

-----------------------
-- Get_Documentation --
-----------------------

function Get_Documentation
(Ref : Gpr_Parser.Common.Token_Reference;
Style : GNATdoc.Comments.Options.Documentation_Style)
return VSS.Strings.Virtual_String
is
Documentation : VSS.Strings.Virtual_String;
Add_LF : Boolean := False;
package Slocs renames Gpr_Parser_Support.Slocs;
use type Slocs.Line_Number;

Line : Slocs.Line_Number := Ref.Data.Sloc_Range.Start_Line;
Token : Gpr_Parser.Common.Token_Reference := Ref;

use type GNATdoc.Comments.Options.Documentation_Style;

function Next return Gpr_Parser.Common.Token_Reference is
(if Style = GNATdoc.Comments.Options.GNAT
then Token.Next
else Token.Previous);
-- Go to next or previous token depending on 'Style' value

function Valid_Comment return Boolean;
-- Return True if comment token is still part of 'Ref' comment.

-------------------
-- Valid_Comment --
-------------------

function Valid_Comment return Boolean is
Current_Line : constant Slocs.Line_Number :=
Token.Data.Sloc_Range.Start_Line;
Valid : Boolean := False;
begin
case Style is
when GNATdoc.Comments.Options.GNAT =>
if Current_Line <= Line + 1 then
Valid := True;
end if;
when GNATdoc.Comments.Options.Leading =>
if Current_Line >= Line - 1 then
Valid := True;
end if;
end case;
if Valid then
-- update Line to allow next/previous comment to still be valid
Line := Current_Line;
end if;
return Valid;
end Valid_Comment;

use type Gpr_Parser.Common.Token_Reference;
use type Gpr_Parser.Common.Token_Kind;
begin
Token := Next;
while Token /= Gpr_Parser.Common.No_Token loop
if Token.Data.Kind = Gpr_Parser.Common.Gpr_Comment
then
if Valid_Comment then
case Style is
when GNATdoc.Comments.Options.GNAT =>
if Add_LF then
Documentation.Append
(VSS.Strings.To_Virtual_String
(Ada.Characters.Wide_Wide_Latin_1.LF & Token.Text));
else
Documentation.Append
(VSS.Strings.To_Virtual_String
(Token.Text));
end if;
when GNATdoc.Comments.Options.Leading =>
if Add_LF then
Documentation.Prepend
(VSS.Strings.To_Virtual_String
(Token.Text & Ada.Characters.Wide_Wide_Latin_1.LF));
else
Documentation.Prepend
(VSS.Strings.To_Virtual_String
(Token.Text));
end if;
end case;
Add_LF := True;
else
exit;
end if;
end if;
Token := Next;
end loop;
return Documentation;
end Get_Documentation;

----------------------
-- Get_Tooltip_Text --
----------------------

procedure Get_Tooltip_Text
(Self : LSP.GPR_Files.File_Access;
URI : LSP.Structures.DocumentUri;
Document_Provider : LSP.GPR_Documents.Document_Provider_Access;
Position : LSP.Structures.Position;
Tooltip_Text : out VSS.Strings.Virtual_String) is
(Self : LSP.GPR_Files.File_Access;
URI : LSP.Structures.DocumentUri;
Document_Provider : LSP.GPR_Documents.Document_Provider_Access;
Position : LSP.Structures.Position;
Style : GNATdoc.Comments.Options.Documentation_Style;
Declaration_Text : out VSS.Strings.Virtual_String;
Documentation_Text : out VSS.Strings.Virtual_String;
Location_Text : out VSS.Strings.Virtual_String) is
use Gpr_Parser.Common;

package LKD renames LSP.Text_Documents.Langkit_Documents;
Expand Down Expand Up @@ -69,6 +181,7 @@ package body LSP.GPR_Documentation is
begin
if Reference.Is_Variable_Reference
or else Reference.Is_Attribute_Reference
or else Reference.Is_Type_Reference
then
declare
Document : constant LSP.GPR_Documents.Document_Access :=
Expand All @@ -85,9 +198,27 @@ package body LSP.GPR_Documentation is
Reference => Reference);
begin
if Variable.Is_Defined then
Tooltip_Text.Append
Declaration_Text.Append
(VSS.Strings.Conversions.To_Virtual_String
(GPR2.Project.Variable.Image (Variable)));
Location_Text.Append
(VSS.Strings.Conversions.To_Virtual_String
(GPR2.Source_Reference.Format
(GPR2.Source_Reference.Object
(Variable))));
if Variable.Has_Type then
declare
Typ : constant GPR2.Project.Typ.Object :=
Variable.Typ;
begin
if Typ.Is_Defined then
Declaration_Text.Prepend
(VSS.Strings.Conversions.To_Virtual_String
(GPR2.Project.Typ.Image (Typ)
& Ada.Characters.Latin_1.CR));
end if;
end;
end if;
end if;
end;
elsif Reference.Is_Attribute_Reference then
Expand All @@ -98,12 +229,39 @@ package body LSP.GPR_Documentation is
Reference => Reference);
begin
if Attribute.Is_Defined then
Tooltip_Text.Append
Declaration_Text.Append
(VSS.Strings.Conversions.To_Virtual_String
(GPR2.Project.Attribute.Image (Attribute)
& Ada.Characters.Latin_1.CR));
Location_Text.Append
(VSS.Strings.Conversions.To_Virtual_String
(GPR2.Source_Reference.Format
(GPR2.Source_Reference.Object
(Attribute))));
end if;

end;
elsif Reference.Is_Type_Reference then
declare
Typ : constant GPR2.Project.Typ.Object :=
Document.Get_Type

(Root_File => Self,
Reference => Reference);
begin
if Typ.Is_Defined then
Declaration_Text.Append
(VSS.Strings.Conversions.To_Virtual_String
(GPR2.Project.Typ.Image (Typ)
& Ada.Characters.Latin_1.CR));
Location_Text.Append
(VSS.Strings.Conversions.To_Virtual_String
(GPR2.Source_Reference.Format
(GPR2.Source_Reference.Object
(Typ))));
end if;
end;

end if;
end if;
end;
Expand All @@ -112,8 +270,6 @@ package body LSP.GPR_Documentation is

begin

Tooltip_Text.Clear;

if Token /= No_Token and then Token.Data.Kind = Gpr_Identifier then
declare
Reference : constant FR.Reference :=
Expand All @@ -127,14 +283,14 @@ package body LSP.GPR_Documentation is
if Previous /= No_Token then
case Previous.Data.Kind is
when Gpr_Package | Gpr_End =>
Tooltip_Text.Append
Documentation_Text.Append
(VSS.Strings.Conversions.To_Virtual_String
(Get_Package_Description
(Self.Get_Package (Position))));

when Gpr_For =>
Append_Value (Reference);
Tooltip_Text.Append
Documentation_Text.Append
(VSS.Strings.Conversions.To_Virtual_String
(Get_Attribute_Description ((
Self.Get_Package (Position),
Expand All @@ -143,24 +299,32 @@ package body LSP.GPR_Documentation is
when others =>
Append_Value (Reference);
if Reference.Is_Package_Reference then
Tooltip_Text.Append
Documentation_Text.Append
(VSS.Strings.Conversions.To_Virtual_String
(Get_Package_Description
(Reference.Referenced_Package)));
else
elsif Reference.Is_Attribute_Reference then
declare
Attribute : constant FR.Attribute_Definition :=
Reference.Referenced_Attribute;

use type FR.Attribute_Definition;
begin
if Attribute /= FR.No_Attribute_Definition then
Tooltip_Text.Append
Documentation_Text.Append
(VSS.Strings.Conversions.To_Virtual_String
(Get_Attribute_Description
(Attribute.Name)));
end if;
end;
elsif Reference.Is_Variable_Reference
or else Reference.Is_Type_Reference
then
Documentation_Text.Append
(Get_Documentation
(LSP.GPR_Files.References.Token_Reference
(Self, Position),
Style));
end if;
end case;
end if;
Expand Down
15 changes: 10 additions & 5 deletions source/gpr/lsp-gpr_documentation.ads
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

-- Subprogram to obtain documentation for packages & attributes.

with GNATdoc.Comments.Options;

with LSP.GPR_Documents;
with LSP.GPR_Files;
with LSP.Structures;
Expand All @@ -26,11 +28,14 @@ with VSS.Strings;
package LSP.GPR_Documentation is

procedure Get_Tooltip_Text
(Self : LSP.GPR_Files.File_Access;
URI : LSP.Structures.DocumentUri;
Document_Provider : LSP.GPR_Documents.Document_Provider_Access;
Position : LSP.Structures.Position;
Tooltip_Text : out VSS.Strings.Virtual_String);
(Self : LSP.GPR_Files.File_Access;
URI : LSP.Structures.DocumentUri;
Document_Provider : LSP.GPR_Documents.Document_Provider_Access;
Position : LSP.Structures.Position;
Style : GNATdoc.Comments.Options.Documentation_Style;
Declaration_Text : out VSS.Strings.Virtual_String;
Documentation_Text : out VSS.Strings.Virtual_String;
Location_Text : out VSS.Strings.Virtual_String);
-- Get all the information needed to produce tooltips (hover and completion
-- requests)

Expand Down
69 changes: 69 additions & 0 deletions source/gpr/lsp-gpr_documents.adb
Original file line number Diff line number Diff line change
Expand Up @@ -368,4 +368,73 @@ package body LSP.GPR_Documents is
return GPR2.Project.Attribute.Undefined;
end Get_Attribute;

--------------
-- Get_Type --
--------------

function Get_Type
(Self : Document'Class;
Root_File : LSP.GPR_Files.File_Access;
Reference : LSP.GPR_Files.References.Reference)
return GPR2.Project.Typ.Object is

function Typ
(View : GPR2.Project.View.Object) return GPR2.Project.Typ.Object;

---------
-- Typ --
---------

function Typ
(View : GPR2.Project.View.Object) return GPR2.Project.Typ.Object
is
Name : constant GPR2.Optional_Name_Type :=
GPR2.Optional_Name_Type
(VSS.Strings.Conversions.To_UTF_8_String
(LSP.GPR_Files.Image (Reference.Referenced_Type)));

begin
if View.Has_Types (Name) then
return View.Typ (Name);
else
return GPR2.Project.Typ.Undefined;
end if;
end Typ;

begin
if LSP.GPR_Files.References.Is_Type_Reference (Reference)
and then not Self.Tree.Log_Messages.Has_Error
then
declare
File : constant LSP.GPR_Files.File_Access :=
LSP.GPR_Files.References.Referenced_File
(File => Root_File,
Reference => Reference);
Path : constant GPR2.Path_Name.Object :=
LSP.GPR_Files.Path (File.all);
Root : constant GPR2.Project.View.Object := Self.Tree.Root_Project;
begin
if Root.Path_Name = Path then
return Typ (Root);
end if;
if Root.Is_Extended then
declare
Extending : constant GPR2.Project.View.Object :=
Root.Extending;
begin
if Extending.Path_Name = Path then
return Typ (Extending);
end if;
end;
end if;
for Import of Root.Imports loop
if Import.Path_Name = Path then
return Typ (Import);
end if;
end loop;
end;
end if;
return GPR2.Project.Typ.Undefined;
end Get_Type;

end LSP.GPR_Documents;
Loading

0 comments on commit ce68d69

Please sign in to comment.