Skip to content

Commit 49e2f89

Browse files
godunkoreznikmm
authored andcommitted
Langkit's Text_Type formatters.
1 parent a1c04c6 commit 49e2f89

File tree

4 files changed

+120
-45
lines changed

4 files changed

+120
-45
lines changed

source/ada/lsp-ada_documents.adb

Lines changed: 30 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ with Ada.Unchecked_Deallocation;
2020

2121
with GNAT.Strings;
2222
with GNATCOLL.Traces;
23-
with GNATCOLL.Utils;
2423
with GNATCOLL.VFS;
2524

2625
with Langkit_Support.Symbols;
@@ -47,13 +46,14 @@ with LSP.Ada_Completions.Filters;
4746
with LSP.Ada_Contexts;
4847
with LSP.Ada_Documentation;
4948
with LSP.Ada_Documents.LAL_Diagnostics;
49+
with LSP.Ada_Handlers.Locations;
5050
with LSP.Ada_Id_Iterators;
5151
with LSP.Enumerations;
5252
with LSP.Formatters.File_Names;
53+
with LSP.Formatters.Texts;
5354
with LSP.Predicates;
54-
with LSP.Utils;
5555
with LSP.Structures.LSPAny_Vectors;
56-
with LSP.Ada_Handlers.Locations;
56+
with LSP.Utils;
5757

5858
package body LSP.Ada_Documents is
5959
pragma Warnings (Off);
@@ -347,50 +347,41 @@ package body LSP.Ada_Documents is
347347
for Param of Params loop
348348
for Id of Param.F_Ids loop
349349
declare
350-
Mode : constant Langkit_Support.Text.Text_Type :=
350+
Mode : constant Langkit_Support.Text.Text_Type :=
351351
Param.F_Mode.Text;
352+
Mode_Text : constant Langkit_Support.Text.Text_Type :=
353+
(if Mode /= "" then Mode & " " else "");
354+
355+
Named_Template : constant
356+
VSS.Strings.Templates.Virtual_String_Template :=
357+
"{} => ${{{}:{} : {}{}}, ";
358+
Positional_Template : constant
359+
VSS.Strings.Templates.Virtual_String_Template :=
360+
"${{{}:{} : {}{}}, ";
361+
Text : VSS.Strings.Virtual_String;
352362

353363
begin
354364
if Use_Named_Notation then
355-
Insert_Text.Append
356-
(VSS.Strings.To_Virtual_String (Id.Text));
357-
Insert_Text.Append (" => ");
358-
Insert_Text.Append ("${");
359-
Insert_Text.Append
360-
(VSS.Strings.Conversions.To_Virtual_String
361-
(GNATCOLL.Utils.Image (Idx, Min_Width => 1)));
362-
Insert_Text.Append (':');
363-
Insert_Text.Append
364-
(VSS.Strings.To_Virtual_String (Id.Text));
365-
Insert_Text.Append (" : ");
366-
Insert_Text.Append
367-
((if Mode /= ""
368-
then VSS.Strings.To_Virtual_String (Mode & " ")
369-
else ""));
370-
Insert_Text.Append
371-
(VSS.Strings.To_Virtual_String
372-
(Param.F_Type_Expr.Text));
373-
Insert_Text.Append ("}, ");
365+
Text :=
366+
Named_Template.Format
367+
(LSP.Formatters.Texts.Image (Id.Text),
368+
VSS.Strings.Formatters.Integers.Image (Idx),
369+
LSP.Formatters.Texts.Image (Id.Text),
370+
LSP.Formatters.Texts.Image (Mode_Text),
371+
LSP.Formatters.Texts.Image
372+
(Param.F_Type_Expr.Text));
374373

375374
else
376-
Insert_Text.Append ("${");
377-
Insert_Text.Append
378-
(VSS.Strings.Conversions.To_Virtual_String
379-
(GNATCOLL.Utils.Image (Idx, Min_Width => 1)));
380-
Insert_Text.Append (':');
381-
Insert_Text.Append
382-
(VSS.Strings.To_Virtual_String (Id.Text));
383-
Insert_Text.Append (" : ");
384-
Insert_Text.Append
385-
((if Mode /= ""
386-
then VSS.Strings.To_Virtual_String (Mode & " ")
387-
else ""));
388-
Insert_Text.Append
389-
(VSS.Strings.To_Virtual_String
390-
(Param.F_Type_Expr.Text));
391-
Insert_Text.Append ("}, ");
375+
Text :=
376+
Positional_Template.Format
377+
(VSS.Strings.Formatters.Integers.Image (Idx),
378+
LSP.Formatters.Texts.Image (Id.Text),
379+
LSP.Formatters.Texts.Image (Mode_Text),
380+
LSP.Formatters.Texts.Image
381+
(Param.F_Type_Expr.Text));
392382
end if;
393383

384+
Insert_Text.Append (Text);
394385
Idx := Idx + 1;
395386
end;
396387
end loop;

source/ada/lsp-ada_handlers.adb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ with LSP.Constants;
8989
with LSP.Diagnostic_Sources;
9090
with LSP.Enumerations;
9191
with LSP.Errors;
92+
with LSP.Formatters.Texts;
9293
with LSP.Generic_Cancel_Check;
9394
with LSP.GNATCOLL_Tracers.Handle;
9495
with LSP.Search;
@@ -3959,16 +3960,16 @@ package body LSP.Ada_Handlers is
39593960

39603961
if not Errors.Is_Empty then
39613962
declare
3962-
Template : VSS.Strings.Templates.Virtual_String_Template :=
3963-
"Can't rename identifier '{}'";
3964-
3965-
Message : constant VSS.Strings.Virtual_String :=
3963+
Template : constant
3964+
VSS.Strings.Templates.Virtual_String_Template :=
3965+
"Can't rename identifier '{}'";
3966+
Message : constant VSS.Strings.Virtual_String :=
39663967
Template.Format
3967-
(VSS.Strings.Formatters.Strings.Image
3968-
(VSS.Strings.To_Virtual_String (Name_Node.Text)));
3968+
(LSP.Formatters.Texts.Image (Name_Node.Text));
39693969

39703970
Diag_Params : LSP.Structures.PublishDiagnosticsParams;
39713971
Diagnostic : LSP.Structures.Diagnostic;
3972+
39723973
begin
39733974
Diagnostic.a_range :=
39743975
Self.To_LSP_Location (Name_Node).a_range;

source/ada/lsp-formatters-texts.adb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
------------------------------------------------------------------------------
2+
-- Language Server Protocol --
3+
-- --
4+
-- Copyright (C) 2023, AdaCore --
5+
-- --
6+
-- This is free software; you can redistribute it and/or modify it under --
7+
-- terms of the GNU General Public License as published by the Free Soft- --
8+
-- ware Foundation; either version 3, or (at your option) any later ver- --
9+
-- sion. This software is distributed in the hope that it will be useful, --
10+
-- but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- --
11+
-- TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public --
12+
-- License for more details. You should have received a copy of the GNU --
13+
-- General Public License distributed with this software; see file --
14+
-- COPYING3. If not, go to http://www.gnu.org/licenses for a complete copy --
15+
-- of the license. --
16+
------------------------------------------------------------------------------
17+
18+
package body LSP.Formatters.Texts is
19+
20+
-----------
21+
-- Image --
22+
-----------
23+
24+
function Image (Item : Langkit_Support.Text.Text_Type) return Formatter is
25+
begin
26+
return
27+
(VSS.Strings.Formatters.Strings.Image
28+
(VSS.Strings.To_Virtual_String (Item)) with null record);
29+
end Image;
30+
31+
-----------
32+
-- Image --
33+
-----------
34+
35+
function Image
36+
(Name : VSS.Strings.Virtual_String;
37+
Item : Langkit_Support.Text.Text_Type) return Formatter is
38+
begin
39+
return
40+
(VSS.Strings.Formatters.Strings.Image
41+
(Name, VSS.Strings.To_Virtual_String (Item)) with null record);
42+
end Image;
43+
44+
end LSP.Formatters.Texts;

source/ada/lsp-formatters-texts.ads

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
------------------------------------------------------------------------------
2+
-- Language Server Protocol --
3+
-- --
4+
-- Copyright (C) 2023, AdaCore --
5+
-- --
6+
-- This is free software; you can redistribute it and/or modify it under --
7+
-- terms of the GNU General Public License as published by the Free Soft- --
8+
-- ware Foundation; either version 3, or (at your option) any later ver- --
9+
-- sion. This software is distributed in the hope that it will be useful, --
10+
-- but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- --
11+
-- TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public --
12+
-- License for more details. You should have received a copy of the GNU --
13+
-- General Public License distributed with this software; see file --
14+
-- COPYING3. If not, go to http://www.gnu.org/licenses for a complete copy --
15+
-- of the license. --
16+
------------------------------------------------------------------------------
17+
18+
with Langkit_Support.Text;
19+
20+
with VSS.Strings.Formatters;
21+
private with VSS.Strings.Formatters.Strings;
22+
23+
package LSP.Formatters.Texts is
24+
25+
type Formatter is
26+
new VSS.Strings.Formatters.Abstract_Formatter with private;
27+
28+
function Image (Item : Langkit_Support.Text.Text_Type) return Formatter;
29+
30+
function Image
31+
(Name : VSS.Strings.Virtual_String;
32+
Item : Langkit_Support.Text.Text_Type) return Formatter;
33+
34+
private
35+
36+
type Formatter is
37+
new VSS.Strings.Formatters.Strings.Formatter with null record;
38+
39+
end LSP.Formatters.Texts;

0 commit comments

Comments
 (0)