Skip to content

Commit 6bf9674

Browse files
V701-011: Fix precedence of pretty printer options
Options explicitly set in the project file should have precedence over LSP options set by the client.
1 parent 52fc04d commit 6bf9674

File tree

6 files changed

+643
-26
lines changed

6 files changed

+643
-26
lines changed

source/ada/lsp-ada_contexts.adb

Lines changed: 74 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ with Utils.Command_Lines.Common;
3939

4040
package body LSP.Ada_Contexts is
4141

42-
Indexing_Trace : constant Trace_Handle := Create ("ALS.INDEXING", Off);
42+
Indexing_Trace : constant Trace_Handle := Create ("ALS.INDEXING", Off);
43+
44+
Formatting_Trace : constant Trace_Handle := Create ("ALS.FORMATTING", On);
4345

4446
use type Libadalang.Analysis.Analysis_Unit;
4547

@@ -68,8 +70,16 @@ package body LSP.Ada_Contexts is
6870
function URI_To_File
6971
(Self : Context;
7072
URI : LSP.Types.LSP_URI)
71-
return GNATCOLL.VFS.Virtual_File is
72-
(GNATCOLL.VFS.Create_From_UTF8 (Self.URI_To_File (URI)));
73+
return GNATCOLL.VFS.Virtual_File
74+
is
75+
(GNATCOLL.VFS.Create_From_UTF8 (Self.URI_To_File (URI)));
76+
77+
procedure Update_Pp_Formatting_Options
78+
(Pp_Options : in out Utils.Command_Lines.Command_Line;
79+
LSP_Options : LSP.Messages.FormattingOptions);
80+
-- Update the gnatpp formatting options using the LSP ones.
81+
-- Options that are explicitly specified in the .gpr file take precedence
82+
-- over LSP options.
7383

7484
-------------------------
7585
-- Append_Declarations --
@@ -763,6 +773,55 @@ package body LSP.Ada_Contexts is
763773
-- Tab stop is set 1 to disable "visible character guessing" by LAL.
764774
end Reload;
765775

776+
----------------------------------
777+
-- Update_Pp_Formatting_Options --
778+
----------------------------------
779+
780+
procedure Update_Pp_Formatting_Options
781+
(Pp_Options : in out Utils.Command_Lines.Command_Line;
782+
LSP_Options : LSP.Messages.FormattingOptions)
783+
is
784+
Pp_Indentation : constant Natural :=
785+
Pp.Command_Lines.Pp_Nat_Switches.Arg
786+
(Pp_Options, Pp.Command_Lines.Indentation);
787+
Pp_No_Tab : constant Boolean :=
788+
Pp.Command_Lines.Pp_Flag_Switches.Arg
789+
(Pp_Options, Pp.Command_Lines.No_Tab);
790+
begin
791+
-- Check if intentation and 'no tab' policy options have been explictly
792+
-- set in the project.
793+
-- If it's not the case, use the LSP options.
794+
795+
if not Pp.Command_Lines.Pp_Nat_Switches.Explicit
796+
(Pp_Options, Pp.Command_Lines.Indentation)
797+
then
798+
Pp.Command_Lines.Pp_Nat_Switches.Set_Arg
799+
(Pp_Options,
800+
Pp.Command_Lines.Indentation,
801+
Natural (LSP_Options.tabSize));
802+
803+
elsif Pp_Indentation /= Natural (LSP_Options.tabSize) then
804+
Formatting_Trace.Trace
805+
("Project file defines an indentation "
806+
& "of" & Pp_Indentation'Img & ", while LSP defines an "
807+
& "indentation of" & LSP_Options.tabSize'Img & ".");
808+
end if;
809+
810+
if not Pp.Command_Lines.Pp_Flag_Switches.Explicit
811+
(Pp_Options, Pp.Command_Lines.No_Tab)
812+
then
813+
Pp.Command_Lines.Pp_Flag_Switches.Set_Arg
814+
(Pp_Options,
815+
Pp.Command_Lines.No_Tab,
816+
LSP_Options.insertSpaces);
817+
818+
elsif Pp_No_Tab /= LSP_Options.insertSpaces then
819+
Formatting_Trace.Trace
820+
("Project file no tab policy is set to " & Pp_No_Tab'Img
821+
& ", while LSP is set to " & LSP_Options.insertSpaces'Img);
822+
end if;
823+
end Update_Pp_Formatting_Options;
824+
766825
------------
767826
-- Format --
768827
------------
@@ -774,18 +833,14 @@ package body LSP.Ada_Contexts is
774833
Options : LSP.Messages.FormattingOptions;
775834
Edit : out LSP.Messages.TextEdit_Vector;
776835
Success : out Boolean;
777-
Messages : out VSS.String_Vectors.Virtual_String_Vector)
778-
is
836+
Messages : out VSS.String_Vectors.Virtual_String_Vector) is
779837
begin
780-
Pp.Command_Lines.Pp_Nat_Switches.Set_Arg
781-
(Self.PP_Options,
782-
Pp.Command_Lines.Indentation,
783-
Natural (Options.tabSize));
838+
-- Take into account the options set by the request only if the
839+
-- corresponding GPR switches are not explicitly set.
784840

785-
Pp.Command_Lines.Pp_Flag_Switches.Set_Arg
786-
(Self.PP_Options,
787-
Pp.Command_Lines.No_Tab,
788-
Options.insertSpaces);
841+
Update_Pp_Formatting_Options
842+
(Pp_Options => Self.PP_Options,
843+
LSP_Options => Options);
789844

790845
Success := Document.Formatting
791846
(Context => Self,
@@ -808,15 +863,12 @@ package body LSP.Ada_Contexts is
808863
Success : out Boolean;
809864
Messages : out VSS.String_Vectors.Virtual_String_Vector) is
810865
begin
811-
Pp.Command_Lines.Pp_Nat_Switches.Set_Arg
812-
(Self.PP_Options,
813-
Pp.Command_Lines.Indentation,
814-
Natural (Options.tabSize));
815-
816-
Pp.Command_Lines.Pp_Flag_Switches.Set_Arg
817-
(Self.PP_Options,
818-
Pp.Command_Lines.No_Tab,
819-
Options.insertSpaces);
866+
-- Take into account the options set by the request only if the
867+
-- corresponding GPR switches are not explicitly set.
868+
869+
Update_Pp_Formatting_Options
870+
(Pp_Options => Self.PP_Options,
871+
LSP_Options => Options);
820872

821873
Success := Document.Range_Formatting
822874
(Context => Self,

source/ada/lsp-ada_handlers.adb

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,8 @@ package body LSP.Ada_Handlers is
230230
(Self : in out LSP.Ada_Contexts.Context;
231231
Document : LSP.Ada_Documents.Document_Access;
232232
Span : LSP.Messages.Span;
233-
Options : LSP.Messages.FormattingOptions)
233+
Options : LSP.Messages.FormattingOptions;
234+
Handler : access Message_Handler)
234235
return LSP.Messages.Server_Responses.Formatting_Response;
235236
-- Format the text of the given document in the given range (span).
236237

@@ -813,7 +814,8 @@ package body LSP.Ada_Handlers is
813814
(Self : in out LSP.Ada_Contexts.Context;
814815
Document : LSP.Ada_Documents.Document_Access;
815816
Span : LSP.Messages.Span;
816-
Options : LSP.Messages.FormattingOptions)
817+
Options : LSP.Messages.FormattingOptions;
818+
Handler : access Message_Handler)
817819
return LSP.Messages.Server_Responses.Formatting_Response
818820
is
819821
Response : LSP.Messages.Server_Responses.Formatting_Response
@@ -849,6 +851,15 @@ package body LSP.Ada_Handlers is
849851
data => <>));
850852
return Response;
851853
end;
854+
else
855+
-- If the formntting succeeded, still display messages in the client
856+
-- if any.
857+
for Msg of Messages loop
858+
Show_Message
859+
(Self => Handler,
860+
Text => Msg,
861+
Mode => LSP.Messages.Info);
862+
end loop;
852863
end if;
853864

854865
return Response;
@@ -5508,7 +5519,8 @@ package body LSP.Ada_Handlers is
55085519
(Self => Context.all,
55095520
Document => Document,
55105521
Span => LSP.Messages.Empty_Span,
5511-
Options => Request.params.options);
5522+
Options => Request.params.options,
5523+
Handler => Self);
55125524
begin
55135525
return Response;
55145526
end;
@@ -5907,7 +5919,8 @@ package body LSP.Ada_Handlers is
59075919
(Self => Context.all,
59085920
Document => Document,
59095921
Span => Request.params.span,
5910-
Options => Request.params.options));
5922+
Options => Request.params.options,
5923+
Handler => Self));
59115924
Response : Server_Responses.Range_Formatting_Response
59125925
(Is_Error => Result.Is_Error);
59135926
begin
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
project Default is
2+
3+
for Main use ("main.adb");
4+
5+
package Pretty_Printer is
6+
for Default_Switches ("Ada") use ("--indentation=10");
7+
end Pretty_Printer;
8+
9+
end Default;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
with Ada.Unchecked_Deallocation;
2+
3+
procedure Main is
4+
5+
type My_Type is new Integer;
6+
type My_Type_Access is access all My_Type;
7+
8+
procedure Free is new Ada.Unchecked_Deallocation (My_Type, My_Type_Access);
9+
10+
A : My_Type_Access := new My_Type'(3);
11+
begin
12+
Free (A);
13+
end Main;

0 commit comments

Comments
 (0)