Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
U610-067 Pretty printer options
Using the corresponding project that a file belongs to get the options.
  • Loading branch information
ogorodnik committed Jul 22, 2021
commit fde5f99a75c124e8810f1be48c97770eeda74c79
139 changes: 76 additions & 63 deletions source/ada/lsp-ada_contexts.adb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ with Laltools.Common;

with Langkit_Support.Slocs;

with Pp.Command_Lines;
with Utils.Command_Lines.Common;

package body LSP.Ada_Contexts is
Expand Down Expand Up @@ -586,8 +587,6 @@ package body LSP.Ada_Contexts is
is
procedure Update_Source_Files;
-- Update the value of Self.Source_Files
procedure Pretty_Printer_Setup;
-- Setup PP_Options object

-------------------------
-- Update_Source_Files --
Expand Down Expand Up @@ -638,61 +637,6 @@ package body LSP.Ada_Contexts is
end loop;
end Update_Source_Files;

--------------------------
-- Pretty_Printer_Setup --
--------------------------

procedure Pretty_Printer_Setup
is
use type GNAT.Strings.String_Access;
Options : GNAT.Strings.String_List_Access;
Validated : GNAT.Strings.String_List_Access;
Last : Integer;
Default : Boolean;
begin
Root.Switches
(In_Pkg => "Pretty_Printer",
File => GNATCOLL.VFS.No_File,
Language => "ada",
Value => Options,
Is_Default_Value => Default);

-- Initialize an gnatpp command line object
Last := Options'First - 1;
for Item of Options.all loop
if Item /= null
and then Item.all /= ""
then
Last := Last + 1;
end if;
end loop;

Validated := new GNAT.Strings.String_List (Options'First .. Last);
Last := Options'First - 1;
for Item of Options.all loop
if Item /= null
and then Item.all /= ""
then
Last := Last + 1;
Validated (Last) := new String'(Item.all);
end if;
end loop;

Utils.Command_Lines.Parse
(Validated,
Self.PP_Options,
Phase => Utils.Command_Lines.Cmd_Line_1,
Callback => null,
Collect_File_Names => False,
Ignore_Errors => True);

GNAT.Strings.Free (Options);
GNAT.Strings.Free (Validated);

-- Set UTF-8 encoding
Utils.Command_Lines.Common.Set_WCEM (Self.PP_Options, "8");
end Pretty_Printer_Setup;

begin
Self.Id := VSS.Strings.Conversions.To_Virtual_String (Root.Name);

Expand All @@ -707,7 +651,6 @@ package body LSP.Ada_Contexts is

Self.Reload;
Update_Source_Files;
Pretty_Printer_Setup;
end Load_Project;

------------
Expand All @@ -730,27 +673,38 @@ package body LSP.Ada_Contexts is
procedure Format
(Self : in out Context;
Document : LSP.Ada_Documents.Document_Access;
Project : GNATCOLL.Projects.Project_Type;
Span : LSP.Messages.Span;
Options : LSP.Messages.FormattingOptions;
Edit : out LSP.Messages.TextEdit_Vector;
Success : out Boolean)
is
File : constant Virtual_File :=
Create_From_UTF8 (Self.URI_To_File (Document.URI));

PP_Options : Utils.Command_Lines.Command_Line
(Pp.Command_Lines.Descriptor'Access);

begin
Self.Get_PP_Options (Project, File, PP_Options);

Pp.Command_Lines.Pp_Nat_Switches.Set_Arg
(Self.PP_Options,
(PP_Options,
Pp.Command_Lines.Indentation,
Natural (Options.tabSize));

Pp.Command_Lines.Pp_Flag_Switches.Set_Arg
(Self.PP_Options,
(PP_Options,
Pp.Command_Lines.No_Tab,
Options.insertSpaces);

Success := Document.Formatting
(Context => Self,
Span => Span,
Cmd => Self.PP_Options,
Cmd => PP_Options,
Edit => Edit);

Utils.Command_Lines.Clear (PP_Options);
end Format;

----------
Expand All @@ -761,8 +715,6 @@ package body LSP.Ada_Contexts is
begin
Self.Source_Files.Clear;
Self.Source_Dirs.Clear;
-- Destroy GnatPP command line
Utils.Command_Lines.Clear (Self.PP_Options);
end Free;

-----------------
Expand Down Expand Up @@ -814,6 +766,67 @@ package body LSP.Ada_Contexts is
end if;
end Get_Node_At;

--------------------
-- Get_PP_Options --
--------------------

procedure Get_PP_Options
(Self : Context;
Project : GNATCOLL.Projects.Project_Type;
File : GNATCOLL.VFS.Virtual_File;
PP_Options : in out Utils.Command_Lines.Command_Line)
is
use type GNAT.Strings.String_Access;

Options : GNAT.Strings.String_List_Access;
Validated : GNAT.Strings.String_List_Access;
Last : Integer;
Default : Boolean;

begin
Project.Switches
(In_Pkg => "Pretty_Printer",
File => File,
Language => "ada",
Value => Options,
Is_Default_Value => Default);

-- Initialize an gnatpp command line object
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Initialize the gnatpp command line"

Last := Options'First - 1;
for Item of Options.all loop
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lacking comments. Put a comment to say that we are counting valid options here for instance, people don't want to analyze each loop in your code

if Item /= null
and then Item.all /= ""
then
Last := Last + 1;
end if;
end loop;

Validated := new GNAT.Strings.String_List (Options'First .. Last);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Validated_Options" would be even better

Last := Options'First - 1;
for Item of Options.all loop
if Item /= null
and then Item.all /= ""
then
Last := Last + 1;
Validated (Last) := new String'(Item.all);
end if;
end loop;

Utils.Command_Lines.Parse
(Validated,
PP_Options,
Phase => Utils.Command_Lines.Cmd_Line_1,
Callback => null,
Collect_File_Names => False,
Ignore_Errors => True);

GNAT.Strings.Free (Options);
GNAT.Strings.Free (Validated);

-- Set UTF-8 encoding
Utils.Command_Lines.Common.Set_WCEM (PP_Options, "8");
end Get_PP_Options;

--------
-- Id --
--------
Expand Down
12 changes: 7 additions & 5 deletions source/ada/lsp-ada_contexts.ads
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ with Libadalang.Analysis;
with Libadalang.Common;

with Utils.Command_Lines;
with Pp.Command_Lines;

with VSS.Strings;

Expand Down Expand Up @@ -92,6 +91,7 @@ package LSP.Ada_Contexts is
procedure Format
(Self : in out Context;
Document : LSP.Ada_Documents.Document_Access;
Project : GNATCOLL.Projects.Project_Type;
Span : LSP.Messages.Span;
Options : LSP.Messages.FormattingOptions;
Edit : out LSP.Messages.TextEdit_Vector;
Expand Down Expand Up @@ -257,17 +257,19 @@ private
-- All the source dirs in the loaded project, not including
-- the externally built projects

PP_Options : Utils.Command_Lines.Command_Line
(Pp.Command_Lines.Descriptor'Access);
-- Object to keep gnatpp options

Follow_Symlinks : Boolean := True;
-- See LSP.Ada_Handlers for description

Reader_Reference : Langkit_Support.File_Readers.File_Reader_Reference;
-- A reference to the file reader created for this context
end record;

procedure Get_PP_Options
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment

(Self : Context;
Project : GNATCOLL.Projects.Project_Type;
File : GNATCOLL.VFS.Virtual_File;
PP_Options : in out Utils.Command_Lines.Command_Line);

function LAL_Context
(Self : Context) return Libadalang.Analysis.Analysis_Context is
(Self.LAL_Context);
Expand Down
61 changes: 61 additions & 0 deletions source/ada/lsp-ada_handlers.adb
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,13 @@ package body LSP.Ada_Handlers is
URI : LSP.Types.LSP_String) return LSP.Types.LSP_String;
-- Turn URI into path

function Get_Project
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LSP.Ada_Documents seems a better package to put that function (might be reused somewhere else in the future)

(Self : access Message_Handler;
Uri : LSP.Messages.DocumentUri)
return GNATCOLL.Projects.Project_Type;
-- Retrieve the project that the file belongs to. Root project is returned
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should say what it does for aggregate projects (that's the purpose of your function)

-- if the file is not a source of any project.

-----------------------
-- Contexts_For_File --
-----------------------
Expand Down Expand Up @@ -3732,6 +3739,58 @@ package body LSP.Ada_Handlers is
return Response;
end On_Completion_Request;

-----------------
-- Get_Project --
-----------------

function Get_Project
(Self : access Message_Handler;
Uri : LSP.Messages.DocumentUri)
return GNATCOLL.Projects.Project_Type
is
use GNATCOLL.Projects;
File : constant Virtual_File := Self.To_File (Uri);
begin
if Self.Project_Tree.Root_Project.Is_Aggregate_Project then
declare
Aggregated : Project_Array_Access :=
Self.Project_Tree.Root_Project.Aggregated_Projects;
Iter : Project_Iterator;
Result : Project_Type;
begin
Result := Self.Project_Tree.Root_Project;

Main_Loop : for P of Aggregated.all loop
Iter := Start (P);
while Current (Iter) /= No_Project loop
declare
Sources : File_Array_Access :=
Current (Iter).Source_Files;
begin
for J in Sources'Range loop
if Sources (J) = File then
Result := Current (Iter);
Unchecked_Free (Sources);
exit Main_Loop;
end if;
end loop;
Unchecked_Free (Sources);
end;

Next (Iter);
end loop;
end loop Main_Loop;

Unchecked_Free (Aggregated);
return Result;
end;

else
return Project
(Info (Self.Project_Tree.all, File), Root_If_Not_Found => True);
end if;
end Get_Project;

---------------------------
-- On_Formatting_Request --
---------------------------
Expand Down Expand Up @@ -3765,6 +3824,7 @@ package body LSP.Ada_Handlers is

Context.Format
(Document,
Self.Get_Project (Request.params.textDocument.uri),
LSP.Messages.Empty_Span,
Request.params.options,
Response.result,
Expand Down Expand Up @@ -4152,6 +4212,7 @@ package body LSP.Ada_Handlers is

Context.Format
(Document,
Self.Get_Project (Request.params.textDocument.uri),
Request.params.span,
Request.params.options,
Response.result,
Expand Down