Skip to content

Commit 63558e6

Browse files
Merge branch 'topic/als.1568.snippet_using_gnatformat' into 'master'
Replace GNATpp by GNATformat for snippets See merge request eng/ide/ada_language_server!2039
2 parents 70b8412 + 067885f commit 63558e6

File tree

9 files changed

+44
-531
lines changed

9 files changed

+44
-531
lines changed

source/ada/lsp-ada_completions.adb

Lines changed: 37 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,19 @@
1717

1818
with Ada.Containers.Indefinite_Hashed_Maps;
1919
with Ada.Containers.Hashed_Sets;
20+
with Ada.Strings.Fixed;
21+
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
2022

23+
with Gnatformat;
24+
with Gnatformat.Formatting;
2125
with VSS.Characters.Latin;
2226
with VSS.Regular_Expressions;
2327
with VSS.Strings; use VSS.Strings;
2428
with VSS.Strings.Conversions;
2529
with VSS.Strings.Cursors.Iterators.Characters;
2630
with VSS.Strings.Cursors.Markers;
2731
with VSS.Strings.Hash;
32+
with VSS.String_Vectors;
2833
with VSS.Transformers.Casing;
2934

3035
with LSP.Ada_Configurations;
@@ -33,12 +38,7 @@ with LSP.Ada_Documents;
3338
with LSP.Enumerations;
3439
with LSP.Utils;
3540

36-
with Pp.Actions;
37-
with Pp.Command_Lines;
3841
with Pp.Scanner;
39-
with Utils.Command_Lines;
40-
with Utils.Command_Lines.Common;
41-
with Utils.Char_Vectors;
4242

4343
package body LSP.Ada_Completions is
4444
pragma Warnings (Off);
@@ -208,10 +208,6 @@ package body LSP.Ada_Completions is
208208
-- {"foobar_1" : "$1", "foobar_2" : "${2: Integer}"}
209209
-- $0 will not be replaced
210210

211-
procedure Set_PP_Switches
212-
(Cmd : in out Standard.Utils.Command_Lines.Command_Line);
213-
-- Force switches not enabled by default by GNATpp
214-
215211
function Encode_String (S : Virtual_String) return Virtual_String;
216212
-- Create pseudo code for the snippet
217213

@@ -237,38 +233,6 @@ package body LSP.Ada_Completions is
237233
-- Indent the block using the initial location and add back $0 (it has
238234
-- been removed to not generate invalid pseudo code)
239235

240-
---------------------
241-
-- Set_PP_Switches --
242-
---------------------
243-
244-
procedure Set_PP_Switches
245-
(Cmd : in out Standard.Utils.Command_Lines.Command_Line) is
246-
247-
begin
248-
-- If not set by the user: align parameters and aggregates
249-
250-
if not Pp.Command_Lines.Pp_Nat_Switches.Explicit
251-
(Cmd, Pp.Command_Lines.Call_Threshold)
252-
then
253-
Pp.Command_Lines.Pp_Nat_Switches.Set_Arg
254-
(Cmd, Pp.Command_Lines.Call_Threshold, 1);
255-
end if;
256-
257-
if not Pp.Command_Lines.Pp_Nat_Switches.Explicit
258-
(Cmd, Pp.Command_Lines.Par_Threshold)
259-
then
260-
Pp.Command_Lines.Pp_Nat_Switches.Set_Arg
261-
(Cmd, Pp.Command_Lines.Par_Threshold, 1);
262-
end if;
263-
264-
if not Pp.Command_Lines.Pp_Boolean_Switches.Explicit
265-
(Cmd, Pp.Command_Lines.Vertical_Named_Aggregates)
266-
then
267-
Pp.Command_Lines.Pp_Boolean_Switches.Set_Arg
268-
(Cmd, Pp.Command_Lines.Vertical_Named_Aggregates);
269-
end if;
270-
end Set_PP_Switches;
271-
272236
-------------------
273237
-- Encode_String --
274238
-------------------
@@ -433,6 +397,31 @@ package body LSP.Ada_Completions is
433397
Res := S;
434398
end if;
435399

400+
-- Add the offset for each lines except the first one which is
401+
-- already aligned with the cursor
402+
declare
403+
Lines : constant VSS.String_Vectors.Virtual_String_Vector :=
404+
Res.Split_Lines
405+
(Terminators =>
406+
(VSS.Strings.CR | VSS.Strings.CRLF | VSS.Strings.LF => True,
407+
others => False),
408+
Keep_Terminator => True);
409+
First : Boolean := True;
410+
begin
411+
Res := Empty_Virtual_String;
412+
for Line of Lines loop
413+
if First then
414+
First := False;
415+
Res.Append (Line);
416+
else
417+
Res.Append
418+
(VSS.Strings.Conversions.To_Virtual_String
419+
(Ada.Strings.Fixed."*" (Offset, " "))
420+
& Line);
421+
end if;
422+
end loop;
423+
end;
424+
436425
-- Add back snippet terminator
437426
Append (Res, "$0");
438427
return Res;
@@ -446,40 +435,27 @@ package body LSP.Ada_Completions is
446435
and then Result.insertTextFormat.Value = LSP.Enumerations.Snippet
447436
then
448437
declare
449-
Input : Standard.Utils.Char_Vectors.Char_Vector;
450-
Output : Standard.Utils.Char_Vectors.Char_Vector;
438+
Output : Unbounded_String;
451439
PP_Messages : Pp.Scanner.Source_Message_Vector;
452440
S : Virtual_String;
453441
Tmp_Unit : Libadalang.Analysis.Analysis_Unit;
454-
Cmd : Standard.Utils.Command_Lines.Command_Line :=
455-
Context.Get_PP_Options;
456442
Tmp_Context : constant Libadalang.Analysis.Analysis_Context :=
457-
Libadalang.Analysis.Create_Context
458-
(Charset => Standard.Utils.Command_Lines.Common.
459-
Wide_Character_Encoding (Cmd));
443+
Libadalang.Analysis.Create_Context;
460444
begin
461-
Set_PP_Switches (Cmd);
462-
463445
declare
464446
Full : constant String :=
465447
VSS.Strings.Conversions.To_UTF_8_String
466448
(Prefix & Encode_String (Result.insertText));
467449
begin
468-
Input.Append (Full);
469450
Tmp_Unit :=
470451
Libadalang.Analysis.Get_From_Buffer
471452
(Context => Tmp_Context,
472453
Filename => "",
473454
Buffer => Full,
474455
Rule => Rule);
475-
Pp.Actions.Format_Vector
476-
(Cmd => Cmd,
477-
Input => Input,
478-
Node => Libadalang.Analysis.Root (Tmp_Unit),
479-
Output => Output,
480-
Messages => PP_Messages,
481-
Initial_Indentation => Offset,
482-
Partial_GNATPP => True);
456+
Output := Gnatformat.Formatting.Format
457+
(Unit => Tmp_Unit,
458+
Format_Options => Context.Get_Format_Options);
483459
exception
484460
when E : others =>
485461
-- Failed to pretty print the snippet, keep the previous
@@ -488,7 +464,8 @@ package body LSP.Ada_Completions is
488464
return;
489465
end;
490466

491-
S := VSS.Strings.Conversions.To_Virtual_String (Output.To_Array);
467+
S :=
468+
VSS.Strings.Conversions.To_Virtual_String (To_String (Output));
492469

493470
if not S.Is_Empty then
494471
-- The text is already formatted, don't try to indent it

testsuite/ada_lsp/U614-031.completion.simple_aggregate_formatting/test.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@
170170
"character": 33
171171
}
172172
},
173-
"newText": "My_Record'\n (I => ${1:Integer},\n J => ${2:Integer},\n F => ${3:Float})$0"
173+
"newText": "My_Record'(I => ${1:Integer}, J => ${2:Integer}, F => ${3:Float})$0"
174174
},
175175
"data": {
176176
"uri": "$URI{src/foo.adb}",
@@ -320,7 +320,7 @@
320320
"character": 23
321321
}
322322
},
323-
"newText": "(I => ${1:Integer},\n J => ${2:Integer},\n F => ${3:Float})$0"
323+
"newText": "(I => ${1:Integer}, J => ${2:Integer}, F => ${3:Float})$0"
324324
},
325325
"data": {
326326
"uri": "$URI{src/foo.adb}",

testsuite/ada_lsp/completion.snippet.formatting2/test.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[
22
{
33
"comment": [
4-
"Test the formatting of snippet via GNATPP => when adding",
4+
"Test the formatting of snippet via GNATFormat => when adding",
55
"parameters in a call it should reformat the existing parameters"
66
]
77
},
@@ -170,7 +170,7 @@
170170
"character": 8
171171
}
172172
},
173-
"newText": "Bar\n (A => ${1:Integer},\n BBBBB => ${2:Integer},\n CCCCCCC => ${3:Integer},\n DDD => ${4:Float})$0"
173+
"newText": "Bar (A => ${1:Integer}, BBBBB => ${2:Integer}, CCCCCCC => ${3:Integer}, DDD => ${4:Float})$0"
174174
},
175175
"data": {
176176
"uri": "$URI{foo.adb}",
@@ -352,7 +352,7 @@
352352
"character": 15
353353
}
354354
},
355-
"newText": "Bar\n (A => 1,\n BBBBB => ${2:Integer},\n CCCCCCC => ${3:Integer},\n DDD => ${4:Float})$0"
355+
"newText": "Bar (A => 1, BBBBB => ${2:Integer}, CCCCCCC => ${3:Integer}, DDD => ${4:Float})$0"
356356
},
357357
"data": {
358358
"uri": "$URI{foo.adb}",

testsuite/ada_lsp/completion.snippet.formatting3/test.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[
22
{
33
"comment": [
4-
"Test the formatting of snippet via GNATPP for package generic",
4+
"Test the formatting of snippet via GNATFormat for package generic",
55
"instantiation"
66
]
77
},
@@ -171,7 +171,7 @@
171171
"character": 32
172172
}
173173
},
174-
"newText": "Foo_Bar\n (A => ${1:A : Integer;},\n BBBBBB => ${2:BBBBBB : Float;},\n CCCC => ${3:CCCC : String;},\n Bar => ${4:with procedure Bar (X : Integer);})$0"
174+
"newText": "Foo_Bar (A => ${1:A : Integer;}, BBBBBB => ${2:BBBBBB : Float;}, CCCC => ${3:CCCC : String;}, Bar => ${4:with procedure Bar (X : Integer);})$0"
175175
},
176176
"data": {
177177
"uri": "$URI{foo.adb}",

testsuite/ada_lsp/completion.snippet.lowercase_formatting/default.gpr

Lines changed: 0 additions & 5 deletions
This file was deleted.

testsuite/ada_lsp/completion.snippet.lowercase_formatting/foo.adb

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)