Skip to content

Commit b963eb2

Browse files
committed
Merge branch 'issue_1354' into 'edge'
Add GNATformat as a secondary formatter See merge request eng/ide/ada_language_server!1565
2 parents efdc89d + eefa6da commit b963eb2

File tree

27 files changed

+736
-37
lines changed

27 files changed

+736
-37
lines changed

doc/traces.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,9 @@ in `initialize` response.
6161
Log lalpp output if `yes`.
6262

6363
ALS.LAL_PP_OUTPUT_ON_FORMATTING=yes
64+
65+
## `ALS.GNATFORMAT` (default no)
66+
67+
Use GNATformat as format provider.
68+
69+
ALS.GNATFORMAT=yes

gnat/lsp_server.gpr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ with "lal_refactor.gpr";
1111
with "ada_libfswatch.gpr";
1212
with "libgnatdoc.gpr";
1313
with "spawn.gpr";
14+
with "gnatformat.gpr";
1415

1516
with "lsp_3_17";
1617
with "lsp_common";

source/ada/lsp-ada_contexts.adb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,9 @@ package body LSP.Ada_Contexts is
760760

761761
Self.Reload;
762762
Update_Source_Files;
763+
763764
Pretty_Printer_Setup;
765+
Self.Format_Options := Gnatformat.Configuration.From_Project (Root);
764766
end Load_Project;
765767

766768
------------

source/ada/lsp-ada_contexts.ads

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ with GNATCOLL.VFS;
2323

2424
with GNATdoc.Comments.Options;
2525

26+
with Gnatformat.Configuration;
27+
2628
with GPR2.Project.Tree;
2729
with GPR2.Project.View;
2830

@@ -177,6 +179,10 @@ package LSP.Ada_Contexts is
177179
Utils.Command_Lines.Command_Line;
178180
-- Return the command line for the Pretty Printer
179181

182+
function Get_Format_Options
183+
(Self : Context) return Gnatformat.Configuration.Format_Options_Type;
184+
-- Return the formatting options for Gnatformat
185+
180186
function Get_Documentation_Style (Self : Context) return
181187
GNATdoc.Comments.Options.Documentation_Style;
182188
-- Get the documentation style used for this context.
@@ -314,6 +320,8 @@ private
314320
(Pp.Command_Lines.Descriptor'Access);
315321
-- Object to keep gnatpp options
316322

323+
Format_Options : Gnatformat.Configuration.Format_Options_Type;
324+
317325
Style : GNATdoc.Comments.Options.Documentation_Style :=
318326
GNATdoc.Comments.Options.GNAT;
319327
-- The context's documentation style.
@@ -339,6 +347,11 @@ private
339347
function Get_PP_Options (Self : Context) return
340348
Utils.Command_Lines.Command_Line is (Self.PP_Options);
341349

350+
function Get_Format_Options
351+
(Self : Context)
352+
return Gnatformat.Configuration.Format_Options_Type
353+
is (Self.Format_Options);
354+
342355
function Get_Documentation_Style (Self : Context) return
343356
GNATdoc.Comments.Options.Documentation_Style is (Self.Style);
344357

source/ada/lsp-ada_documents.adb

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ with GNAT.Strings;
2121
with GNATCOLL.Traces;
2222
with GNATCOLL.VFS;
2323

24+
with Gnatformat.Formatting;
25+
2426
with Langkit_Support.Symbols;
2527
with Langkit_Support.Text;
2628

@@ -613,6 +615,28 @@ package body LSP.Ada_Documents is
613615
return False;
614616
end Formatting;
615617

618+
------------
619+
-- Format --
620+
------------
621+
622+
function Format
623+
(Self : Document;
624+
Context : LSP.Ada_Contexts.Context)
625+
return LSP.Structures.TextEdit_Vector
626+
is
627+
Result : LSP.Structures.TextEdit_Vector;
628+
629+
Formatted_Document : constant VSS.Strings.Virtual_String :=
630+
VSS.Strings.Conversions.To_Virtual_String
631+
(Gnatformat.Formatting.Format (Self.Unit (Context),
632+
Context.Get_Format_Options));
633+
634+
begin
635+
Self.Diff (New_Text => Formatted_Document, Edit => Result);
636+
637+
return Result;
638+
end Format;
639+
616640
--------------------
617641
-- Get_Any_Symbol --
618642
--------------------
@@ -1211,6 +1235,39 @@ package body LSP.Ada_Documents is
12111235
return False;
12121236
end Range_Formatting;
12131237

1238+
------------------
1239+
-- Range_Format --
1240+
------------------
1241+
1242+
function Range_Format
1243+
(Self : Document;
1244+
Context : LSP.Ada_Contexts.Context;
1245+
Span : LSP.Structures.A_Range)
1246+
return LSP.Structures.TextEdit
1247+
is
1248+
use type LSP.Structures.A_Range;
1249+
1250+
begin
1251+
if Span = LSP.Text_Documents.Empty_Range then
1252+
return (LSP.Constants.Empty, VSS.Strings.Empty_Virtual_String);
1253+
end if;
1254+
1255+
declare
1256+
Range_Formatted_Document :
1257+
constant Gnatformat.Formatting.Formatted_Edits :=
1258+
Gnatformat.Formatting.Range_Format
1259+
(Self.Unit (Context),
1260+
Self.To_Source_Location_Range (Span),
1261+
Context.Get_Format_Options);
1262+
1263+
begin
1264+
return
1265+
(Self.To_A_Range (Range_Formatted_Document.Edit.Location),
1266+
VSS.Strings.Conversions.To_Virtual_String
1267+
(Range_Formatted_Document.Edit.Text));
1268+
end;
1269+
end Range_Format;
1270+
12141271
------------------------
12151272
-- Reset_Symbol_Cache --
12161273
------------------------

source/ada/lsp-ada_documents.ads

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,12 @@ package LSP.Ada_Documents is
177177
Messages : out VSS.String_Vectors.Virtual_String_Vector) return Boolean;
178178
-- Format document or its part defined in Span
179179

180+
function Format
181+
(Self : Document;
182+
Context : LSP.Ada_Contexts.Context)
183+
return LSP.Structures.TextEdit_Vector;
184+
-- Format Self with formatting options based on Context
185+
180186
function Range_Formatting
181187
(Self : Document;
182188
Context : LSP.Ada_Contexts.Context;
@@ -187,6 +193,14 @@ package LSP.Ada_Documents is
187193
return Boolean;
188194
-- Format document or its part defined in Span
189195

196+
function Range_Format
197+
(Self : Document;
198+
Context : LSP.Ada_Contexts.Context;
199+
Span : LSP.Structures.A_Range)
200+
return LSP.Structures.TextEdit;
201+
-- Format part of Self defined by Span with formatting options based on
202+
-- Context.
203+
190204
procedure Find_All_References
191205
(Self : Document;
192206
Context : LSP.Ada_Contexts.Context;

source/ada/lsp-ada_driver.adb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ with GNAT.OS_Lib;
2424
with GNAT.Strings;
2525

2626
pragma Warnings (Off, "is an internal GNAT unit");
27+
with Gnatformat.Configuration;
2728
with System.Soft_Links;
2829
with System.Secondary_Stack;
2930

@@ -428,6 +429,8 @@ begin
428429
Ada.Text_IO.Set_Output (Ada.Text_IO.Standard_Error);
429430
-- Protect stdout from pollution by accidental Put_Line calls
430431

432+
Gnatformat.Configuration.Elaborate_GPR2;
433+
431434
declare
432435
Allow_Incremental_Text_Changes : constant GNATCOLL.Traces.Trace_Handle
433436
:= GNATCOLL.Traces.Create ("ALS.ALLOW_INCREMENTAL_TEXT_CHANGES",

source/ada/lsp-ada_handlers-formatting.adb

Lines changed: 127 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ package body LSP.Ada_Handlers.Formatting is
2727
Formatting_Trace : constant GNATCOLL.Traces.Trace_Handle :=
2828
GNATCOLL.Traces.Create ("ALS.FORMATTING", GNATCOLL.Traces.On);
2929

30+
Gnatformat_Trace : constant GNATCOLL.Traces.Trace_Handle :=
31+
GNATCOLL.Traces.Create ("ALS.GNATFORMAT", GNATCOLL.Traces.Off);
32+
3033
procedure Update_Pp_Formatting_Options
3134
(Pp_Options : in out Utils.Command_Lines.Command_Line;
3235
LSP_Options : LSP.Structures.FormattingOptions);
@@ -48,36 +51,75 @@ package body LSP.Ada_Handlers.Formatting is
4851
Messages : out VSS.String_Vectors.Virtual_String_Vector;
4952
Error : out LSP.Errors.ResponseError)
5053
is
51-
PP_Options : Utils.Command_Lines.Command_Line := Context.Get_PP_Options;
54+
procedure Gnatpp_Format;
55+
56+
procedure Gnatformat_Format;
57+
58+
-------------------
59+
-- Gnatpp_Format --
60+
-------------------
61+
62+
procedure Gnatpp_Format
63+
is
64+
PP_Options : Utils.Command_Lines.Command_Line :=
65+
Context.Get_PP_Options;
66+
67+
begin
68+
-- Take into account the options set by the request only if the
69+
-- corresponding GPR switches are not explicitly set.
70+
71+
Update_Pp_Formatting_Options
72+
(Pp_Options => PP_Options, LSP_Options => Options);
73+
74+
Success := Document.Formatting
75+
(Context => Context,
76+
Span => Span,
77+
Cmd => PP_Options,
78+
Edit => Response,
79+
Messages => Messages);
80+
81+
if not Success then
82+
Error :=
83+
(code => LSP.Enumerations.InternalError,
84+
message => Messages.Join (' '));
85+
Messages.Clear;
86+
end if;
87+
end Gnatpp_Format;
88+
89+
-----------------------
90+
-- Gnatformat_Format --
91+
-----------------------
92+
93+
procedure Gnatformat_Format
94+
is
95+
begin
96+
Success := True;
97+
Response := Document.Format (Context);
98+
99+
exception
100+
when E : others =>
101+
Context.Tracer.Trace_Exception (E, "in GNATformat Format");
102+
Success := False;
103+
Error :=
104+
(code => LSP.Enumerations.InternalError,
105+
message => "GNATformat failed to format source");
106+
end Gnatformat_Format;
52107

53108
begin
54109
if Document.Has_Diagnostics (Context) then
55110
Success := False;
56111
Error :=
57-
(code => LSP.Enumerations.InternalError,
112+
(code => LSP.Enumerations.InternalError,
58113
message => "Incorrect code can't be formatted");
59114

60115
return;
61116
end if;
62117

63-
-- Take into account the options set by the request only if the
64-
-- corresponding GPR switches are not explicitly set.
118+
if Gnatformat_Trace.Is_Active then
119+
Gnatformat_Format;
65120

66-
Update_Pp_Formatting_Options
67-
(Pp_Options => PP_Options, LSP_Options => Options);
68-
69-
Success := Document.Formatting
70-
(Context => Context,
71-
Span => Span,
72-
Cmd => PP_Options,
73-
Edit => Response,
74-
Messages => Messages);
75-
76-
if not Success then
77-
Error :=
78-
(code => LSP.Enumerations.InternalError,
79-
message => Messages.Join (' '));
80-
Messages.Clear;
121+
else
122+
Gnatpp_Format;
81123
end if;
82124
end Format;
83125

@@ -94,8 +136,68 @@ package body LSP.Ada_Handlers.Formatting is
94136
Response : out LSP.Structures.TextEdit_Vector;
95137
Error : out LSP.Errors.ResponseError)
96138
is
97-
PP_Options : Utils.Command_Lines.Command_Line := Context.Get_PP_Options;
98-
Messages : VSS.String_Vectors.Virtual_String_Vector;
139+
procedure Gnatpp_Range_Format;
140+
141+
procedure Gnatformat_Range_Format;
142+
143+
-------------------------
144+
-- Gnatpp_Range_Format --
145+
-------------------------
146+
147+
procedure Gnatpp_Range_Format
148+
is
149+
PP_Options : Utils.Command_Lines.Command_Line :=
150+
Context.Get_PP_Options;
151+
Messages : VSS.String_Vectors.Virtual_String_Vector;
152+
153+
begin
154+
-- Take into account the options set by the request only if the
155+
-- corresponding GPR switches are not explicitly set.
156+
157+
Update_Pp_Formatting_Options
158+
(Pp_Options => PP_Options, LSP_Options => Options);
159+
160+
Success := Document.Range_Formatting
161+
(Context => Context,
162+
Span => Span,
163+
PP_Options => PP_Options,
164+
Edit => Response,
165+
Messages => Messages);
166+
167+
if not Success then
168+
Error :=
169+
(code => LSP.Enumerations.InternalError,
170+
message => Messages.Join (' '));
171+
end if;
172+
if Document.Has_Diagnostics (Context) then
173+
Success := False;
174+
Error :=
175+
(code => LSP.Enumerations.InternalError,
176+
message => "Incorrect code can't be formatted");
177+
178+
return;
179+
end if;
180+
end Gnatpp_Range_Format;
181+
182+
----------------------------
183+
-- Gnatformat_Range_Format --
184+
-----------------------------
185+
186+
procedure Gnatformat_Range_Format
187+
is
188+
begin
189+
Success := True;
190+
Response.Clear;
191+
Response.Append (Document.Range_Format (Context, Span));
192+
193+
exception
194+
when E : others =>
195+
Context.Tracer.Trace_Exception (E, "in GNATformat Range_Format");
196+
Success := False;
197+
Error :=
198+
(code => LSP.Enumerations.InternalError,
199+
message => "GNATformat failed to format source");
200+
end Gnatformat_Range_Format;
99201

100202
begin
101203
if Document.Has_Diagnostics (Context) then
@@ -107,23 +209,11 @@ package body LSP.Ada_Handlers.Formatting is
107209
return;
108210
end if;
109211

110-
-- Take into account the options set by the request only if the
111-
-- corresponding GPR switches are not explicitly set.
212+
if Gnatformat_Trace.Is_Active then
213+
Gnatformat_Range_Format;
112214

113-
Update_Pp_Formatting_Options
114-
(Pp_Options => PP_Options, LSP_Options => Options);
115-
116-
Success := Document.Range_Formatting
117-
(Context => Context,
118-
Span => Span,
119-
PP_Options => PP_Options,
120-
Edit => Response,
121-
Messages => Messages);
122-
123-
if not Success then
124-
Error :=
125-
(code => LSP.Enumerations.InternalError,
126-
message => Messages.Join (' '));
215+
else
216+
Gnatpp_Range_Format;
127217
end if;
128218
end Range_Format;
129219

0 commit comments

Comments
 (0)