libadalang_tools_24.0.0_d864b5a8/testsuite/ada_drivers/partial_gnatpp/src/partial_gnatpp.adb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
------------------------------------------------------------------------------
--                                                                          --
--                             Libadalang Tools                             --
--                                                                          --
--                       Copyright (C) 2022, AdaCore                        --
--                                                                          --
-- Libadalang Tools  is free software; you can redistribute it and/or modi- --
-- fy  it  under  terms of the  GNU General Public License  as published by --
-- the Free Software Foundation;  either version 3, or (at your option) any --
-- later version. This software  is distributed in the hope that it will be --
-- useful but  WITHOUT  ANY  WARRANTY; without even the implied warranty of --
-- MERCHANTABILITY  or  FITNESS  FOR A PARTICULAR PURPOSE.                  --
--                                                                          --
-- As a special  exception  under  Section 7  of  GPL  version 3,  you are  --
-- granted additional  permissions described in the  GCC  Runtime  Library  --
-- Exception, version 3.1, as published by the Free Software Foundation.    --
--                                                                          --
-- You should have received a copy of the GNU General Public License and a  --
-- copy of the GCC Runtime Library Exception along with this program;  see  --
-- the files COPYING3 and COPYING.RUNTIME respectively.  If not, see        --
-- <http://www.gnu.org/licenses/>.                                          --
------------------------------------------------------------------------------

with Ada.Assertions;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Text_IO; use Ada.Text_IO;

with GNAT.OS_Lib;
with GNAT.Strings;
with GNATCOLL.Opt_Parse; use GNATCOLL.Opt_Parse;
with GNATCOLL.Projects;

with Langkit_Support.Slocs; use Langkit_Support.Slocs;

with Libadalang.Analysis; use Libadalang.Analysis;
with Libadalang.Helpers; use Libadalang.Helpers;

with Pp.Command_Lines;
with Pp.Scanner;

with Utils.Char_Vectors;
with Utils.Command_Lines;
with Utils.Command_Lines.Common;

with Laltools.Partial_GNATPP;

--  This procedure defines the partial gnatpp formatting tool

--  Usage:
--  partial_gnatpp -S <source-file> -SL <start-line> -EL <end-line>
--  -SC <start-column> -EC <end-column>
--
--  -S,  --source-file     Source code file of the selection to reformat
--  -SL, --start-line      Line of the first statement to extract
--  -EL, --end-line        Line of the last statement to extract
--  -SC, --start-column    Column of the first statement to extract
--  -EC, --end-column      Column of the last statement to extract

procedure Partial_GNATpp is

   procedure Partial_GNATpp_App_Setup
     (Context : App_Context;
      Jobs : App_Job_Context_Array);

   package Partial_GNATpp_App is new Libadalang.Helpers.App
     (Name             => "Partial_GNATpp",
      Description      => "Partial_GNATpp",
      App_setup        => Partial_GNATpp_App_Setup);

   package Args is

      package Source is new GNATCOLL.Opt_Parse.Parse_Option
        (Parser      => Partial_GNATpp_App.Args.Parser,
         Short       => "-S",
         Long        => "--source-file",
         Help        => "Source code file of the selection",
         Arg_Type    => Unbounded_String,
         Convert     => To_Unbounded_String,
         Default_Val => Null_Unbounded_String,
         Enabled     => True);

      package Start_Line is new GNATCOLL.Opt_Parse.Parse_Option
        (Parser      => Partial_GNATpp_App.Args.Parser,
         Short       => "-SL",
         Long        => "--start-line",
         Help        => "Start line",
         Arg_Type    => Natural,
         Convert     => Natural'Value,
         Default_Val => 1,
         Enabled     => True);

      package Start_Column is new GNATCOLL.Opt_Parse.Parse_Option
        (Parser      => Partial_GNATpp_App.Args.Parser,
         Short       => "-SC",
         Long        => "--start-column",
         Help        => "Start column",
         Arg_Type    => Natural,
         Convert     => Natural'Value,
         Default_Val => 1,
         Enabled     => True);

      package End_Line is new GNATCOLL.Opt_Parse.Parse_Option
        (Parser      => Partial_GNATpp_App.Args.Parser,
         Short       => "-EL",
         Long        => "--end-line",
         Help        => "End line",
         Arg_Type    => Natural,
         Convert     => Natural'Value,
         Default_Val => 1,
         Enabled     => True);

      package End_Column is new GNATCOLL.Opt_Parse.Parse_Option
        (Parser      => Partial_GNATpp_App.Args.Parser,
         Short       => "-EC",
         Long        => "--end-column",
         Help        => "End column",
         Arg_Type    => Natural,
         Convert     => Natural'Value,
         Default_Val => 1,
         Enabled     => True);

      package Source_Line_Breaks is new GNATCOLL.Opt_Parse.Parse_Flag
        (Parser      => Partial_GNATpp_App.Args.Parser,
         Long        => "--source-line-breaks",
         Help        => "Take line breaks only from source",
         Enabled     => True);

   end Args;

   --------------------------------
   --  Partial_GNATpp_App_Setup  --
   --------------------------------

   procedure Partial_GNATpp_App_Setup
     (Context : App_Context;
      Jobs    : App_Job_Context_Array)
   is
      use Pp.Command_Lines;
      use Utils.Command_Lines;
      use Laltools.Partial_GNATPP;

      procedure Setup_Pretty_Printer_Switches;

      --  Setups PP_Options by doing the first pass and then checks if this
      --  project has a "Pretty_Printer" package with additional switches.
      --  If so, do a second and final parse to update PP_Options with these.

      Source_File : constant String := To_String (Args.Source.Get);

      Selection_Range : constant Source_Location_Range :=
        (Line_Number (Args.Start_Line.Get),
         Line_Number (Args.End_Line.Get),
         Column_Number (Args.Start_Column.Get),
         Column_Number (Args.End_Column.Get));

      Unit : constant Analysis_Unit :=
        Jobs (1).Analysis_Ctx.Get_From_File (Source_File);

      PP_Options : Command_Line (Pp.Command_Lines.Descriptor'Access);

      -----------------------------------
      -- Setup_Pretty_Printer_Switches --
      -----------------------------------

      procedure Setup_Pretty_Printer_Switches is
         Dummy : GNAT.Strings.String_List_Access :=
           new GNAT.Strings.String_List (1 .. 0);

      begin
         Parse
           (Dummy,
            PP_Options,
            Phase              => Cmd_Line_1,
            Callback           => null,
            Collect_File_Names => False,
            Ignore_Errors      => True);
         GNAT.OS_Lib.Free (Dummy);

         --  If Context.Provider.Kind is in Project_File, it means that a
         --  project was given by the -P option.
         --  Partial_GNATpp_App.Args.Project_File cannot be an empty string
         --  in that case.
         if Context.Provider.Kind in Project_File then
            Ada.Assertions.Assert
               (To_String (Partial_GNATpp_App.Args.Project_File.Get) /= "");

            --  Set the Project_File option in PP_Options
            Utils.Command_Lines.Common.Common_String_Switches.Set_Arg
               (PP_Options,
               Utils.Command_Lines.Common.Project_File,
               To_String (Partial_GNATpp_App.Args.Project_File.Get));

            --  Check if this project has a "Pretty_Printer" package with
            --  additional switches. If so, do a second and final parse to
            --  update PP_Options with these.
            declare
               use GNATCOLL.Projects;
               use GNAT.OS_Lib;

               Project          : constant Project_Type :=
                  Root_Project (Context.Provider.Project.all);
               PP_Switches      : constant Attribute_Pkg_List :=
                  Build ("Pretty_Printer", "Default_Switches");
               PP_Switches_Text : Argument_List_Access := null;

            begin
               if Has_Attribute (Project, PP_Switches, "ada") then
                  PP_Switches_Text :=
                     Attribute_Value (Project, PP_Switches, "ada");
                  if PP_Switches_Text /= null then
                     Parse
                        (PP_Switches_Text,
                        PP_Options,
                        Phase              => Project_File,
                        Callback           => null,
                        Collect_File_Names => False,
                        Ignore_Errors      => True);
                     Free (PP_Switches_Text);
                  end if;
               end if;
            end;
         end if;
      end Setup_Pretty_Printer_Switches;

   begin
      Setup_Pretty_Printer_Switches;

      if Args.Source_Line_Breaks.Get then

         --  Format the selected range of the text. If the --source-line-breaks
         --  switch is passed then the formetted text will be filtered and only
         --  the reformatted initial selected lines will be returned.
         --  Otherwise, the enclosing parent node will be rewritten, the node
         --  is returned as value of Formatted_Node parameter in this call.
         --  This is based on the gnatpp engine and has as entry point the
         --  Format_Vector of PP.Actions.

         declare
            Enclosing_Node  : Ada_Node;
            Output          : Utils.Char_Vectors.Char_Vector;
            Output_SL_Range : Source_Location_Range;
            Messages        : Pp.Scanner.Source_Message_Vector;

         begin

            Format_Selection
              (Main_Unit                => Unit,
               Input_Selection_Range    => Selection_Range,
               Output                   => Output,
               Output_Selection_Range   => Output_SL_Range,
               PP_Messages              => Messages,
               Formatted_Node           => Enclosing_Node,
               PP_Options               => PP_Options,
               Force_Source_Line_Breaks => Args.Source_Line_Breaks.Get);

            --  Create the text edits to be passed to the IDE related to the
            --  rewritten selection.

            declare
               Output_Str : constant String :=
                 Utils.Char_Vectors.Char_Vectors.Elems (Output)
                   (1 .. Utils.Char_Vectors.Char_Vectors.Last_Index (Output));

               Edit : Partial_Formatting_Edit;

            begin
               Edit := Partial_Formatting_Edit'
                 (Diagnostics    => Messages,
                  Formatted_Node => Enclosing_Node,
                  Indentation    => 0,
                  Edit           =>
                    Text_Edit'
                      (Location => Output_SL_Range,
                       Text     =>
                         Ada.Strings.Unbounded.To_Unbounded_String
                           (Output_Str)));
               Ada.Text_IO.Put_Line (Image (Edit));
               New_Line;
            end;
         end;
      else
         Ada.Text_IO.Put_Line
           (Image (Format_Selection (Unit, Selection_Range, PP_Options)));
         New_Line;
      end if;

   end Partial_GNATpp_App_Setup;

begin
   Partial_GNATpp_App.Run;
end Partial_GNATpp;