1717
1818with Ada.Containers.Indefinite_Hashed_Maps ;
1919with 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 ;
2125with VSS.Characters.Latin ;
2226with VSS.Regular_Expressions ;
2327with VSS.Strings ; use VSS.Strings;
2428with VSS.Strings.Conversions ;
2529with VSS.Strings.Cursors.Iterators.Characters ;
2630with VSS.Strings.Cursors.Markers ;
2731with VSS.Strings.Hash ;
32+ with VSS.String_Vectors ;
2833with VSS.Transformers.Casing ;
2934
3035with LSP.Ada_Configurations ;
@@ -33,12 +38,7 @@ with LSP.Ada_Documents;
3338with LSP.Enumerations ;
3439with LSP.Utils ;
3540
36- with Pp.Actions ;
37- with Pp.Command_Lines ;
3841with Pp.Scanner ;
39- with Utils.Command_Lines ;
40- with Utils.Command_Lines.Common ;
41- with Utils.Char_Vectors ;
4242
4343package 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
0 commit comments