@@ -39,7 +39,9 @@ with Utils.Command_Lines.Common;
3939
4040package 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,
0 commit comments