lal_refactor_25.0.0_15241508/src/lal_refactor-file_edits.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
--
--  Copyright (C) 2022-2023, AdaCore
--
--  SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
--

with Ada.Containers.Vectors;
with Ada.Strings;
with Ada.Strings.Fixed;

with VSS.Characters;
with VSS.Characters.Latin;
with VSS.Strings.Conversions;
with VSS.String_Vectors;
with VSS.Text_Streams;
with VSS.Text_Streams.File_Input;
with VSS.Text_Streams.File_Output;
with VSS.Strings.Cursors.Iterators;
with VSS.Strings.Cursors.Iterators.Characters;

package body LAL_Refactor.File_Edits is

   package Positive_Vectors is new Ada.Containers.Vectors (Positive, Positive);

   subtype Positive_Vector is Positive_Vectors.Vector;

   procedure Compute_Line_Length
     (Line        : VSS.Strings.Virtual_String;
      Length      : out Natural;
      Tabs_Length : out Positive_Vector);
   --  Computes Line's Length by iterating through each character and
   --  considering that each VSS.Characters.Latin.Character_Tabulation has a
   --  maximum length of 8. The length of each
   --  VSS.Characters.Latin.Character_Tabulation also appended to Tabs_Length.

   -------------------------
   -- Compute_Line_Length --
   -------------------------

   procedure Compute_Line_Length
     (Line        : VSS.Strings.Virtual_String;
      Length      : out Natural;
      Tabs_Length : out Positive_Vector)
   is
      Character_It :
        VSS.Strings.Cursors.Iterators.Characters.Character_Iterator :=
          Line.Before_First_Character;

      use type VSS.Characters.Virtual_Character;

   begin
      Length := 0;
      Tabs_Length := Positive_Vectors.Empty_Vector;
      while Character_It.Forward loop
         if Character_It.Element =
              VSS.Characters.Latin.Character_Tabulation
         then
            Tabs_Length.Append (8 - (Length mod 8));
            Length := @ + (8 - (@ mod 8));
         else
            Length := @ + 1;
         end if;
      end loop;
   end Compute_Line_Length;

   -----------------
   -- Apply_Edits --
   -----------------

   procedure Apply_Edits
     (Edits : LAL_Refactor.Text_Edit_Map)
   is
      use File_Name_To_Virtual_String_Maps;

      File_Edits   : constant File_Name_To_Virtual_String_Map :=
        Apply_Edits (Edits);
      Edits_Cursor : Cursor := File_Edits.First;

      Edits_Length_Image : constant String :=
        Ada.Strings.Fixed.Trim (File_Edits.Length'Image, Ada.Strings.Both);
      File_Counter       : Positive := 1;

   begin
      if File_Edits.Is_Empty then
         return;
      end if;

      Refactor_Trace.Trace ("Editing files on disk");
      while Has_Element (Edits_Cursor) loop
         declare
            Output : VSS.Text_Streams.File_Output.File_Output_Text_Stream;
            Buffer : constant VSS.Strings.Virtual_String :=
              Element (Edits_Cursor);
            Ignore : Boolean := True;

         begin
            Log_Progress
              (Current => File_Counter,
               Total   => Edits_Length_Image,
               Message => "Editing " & Key (Edits_Cursor));

            Output.Create
              (VSS.Strings.Conversions.To_Virtual_String (Key (Edits_Cursor)));
            Output.Put (Buffer, Ignore);
            Output.Close;

         exception
            when E : others =>
               Refactor_Trace.Trace ("Failed to edit " & Key (Edits_Cursor));
               Refactor_Trace.Trace (E);
         end;

         Next (Edits_Cursor);
         File_Counter := @ + 1;
      end loop;
   end Apply_Edits;

   -----------------
   -- Apply_Edits --
   -----------------

   function Apply_Edits
     (Edits : LAL_Refactor.Text_Edit_Map)
      return File_Name_To_Virtual_String_Map
   is
      use LAL_Refactor.Text_Edit_Ordered_Maps;

      Edits_Cursor : LAL_Refactor.Text_Edit_Ordered_Maps.Cursor :=
        Edits.First;
      Result       : File_Name_To_Virtual_String_Map;

      Edits_Length_Image : constant String :=
        Ada.Strings.Fixed.Trim (Edits.Length'Image, Ada.Strings.Both);
      File_Counter       : Positive := 1;

   begin
      if Edits.Is_Empty then
         return Result;
      end if;

      Refactor_Trace.Trace
        ("Found edits for " & Edits_Length_Image & " files");

      while Has_Element (Edits_Cursor) loop
         declare
            Original_Filename : constant VSS.Strings.Virtual_String :=
              VSS.Strings.Conversions.To_Virtual_String (Key (Edits_Cursor));
            Original_File     :
              VSS.Text_Streams.File_Input.File_Input_Text_Stream;
            Input_Buffer      : VSS.Strings.Virtual_String;
            Output_Buffer     : VSS.Strings.Virtual_String;

            Text_Edits        : constant Constant_Reference_Type :=
              Constant_Reference (Edits, Edits_Cursor);
            Text_Edits_Cursor : Text_Edit_Ordered_Sets.Cursor :=
              Text_Edits.Last;
            Current_Text_Edit : Text_Edit :=
              Text_Edit_Ordered_Sets.Element (Text_Edits_Cursor);
            Inside_Text_Edit  : Boolean := False;

            Current_Line_Number   : Natural;
            Current_Column_Number : Natural;

            Current_Character : VSS.Characters.Virtual_Character;
            Success           : Boolean := True;

         begin
            Log_Progress
              (Current => File_Counter,
               Total   => Edits_Length_Image,
               Message =>
                 "Creating buffer with " & Key (Edits_Cursor) & " edits");

            Original_File.Open (Original_Filename, "utf-8");
            while not Original_File.Is_End_Of_Stream loop
               Original_File.Get (Current_Character, Success);
               Input_Buffer.Append (Current_Character);
            end loop;
            Original_File.Close;

            declare
               Lines          :
                 constant VSS.String_Vectors.Virtual_String_Vector :=
                   Input_Buffer.Split_Lines (Keep_Terminator => True);
               Lines_Iterator :
                 constant VSS.String_Vectors.Reversible_Iterator :=
                   VSS.String_Vectors.Iterate (Lines);
               Lines_Cursor   : VSS.String_Vectors.Cursor :=
                 VSS.String_Vectors.Last (Lines_Iterator);

               Current_Line           : VSS.Strings.Virtual_String;
               Current_Line_Length    : Natural;
               Current_Line_Tabs_Size : Positive_Vector;

               Ignore : Boolean;

               use type VSS.Characters.Virtual_Character;

            begin
               Current_Line_Tabs_Size.Reserve_Capacity (10);

               Current_Line_Number := Lines.Length;
               while VSS.String_Vectors.Has_Element (Lines_Cursor) loop
                  Current_Line :=
                    VSS.String_Vectors.Element (Lines, Lines_Cursor);

                  Compute_Line_Length
                    (Current_Line,
                     Current_Line_Length,
                     Current_Line_Tabs_Size);

                  Current_Column_Number := Current_Line_Length;

                  declare
                     Current_Line_Character_It :
                       VSS.Strings.Cursors.Iterators.Characters.
                         Character_Iterator :=
                           Current_Line.After_Last_Character;

                  begin
                     while Current_Line_Character_It.Backward loop
                        if not Inside_Text_Edit then
                           Output_Buffer.Prepend
                             (Current_Line_Character_It.Element);
                        end if;

                        if Current_Text_Edit /= No_Text_Edit then
                           if Current_Line_Number =
                                Natural (Current_Text_Edit.Location.Start_Line)
                             and then Current_Column_Number =
                                        Natural
                                          (Current_Text_Edit.Location.
                                             Start_Column)
                           then
                              Text_Edit_Ordered_Sets.Previous
                                (Text_Edits_Cursor);
                              if Text_Edit_Ordered_Sets.
                                   Has_Element (Text_Edits_Cursor)
                              then
                                 Current_Text_Edit :=
                                   Text_Edit_Ordered_Sets.
                                     Element (Text_Edits_Cursor);
                              else
                                 Current_Text_Edit := No_Text_Edit;
                              end if;
                              Inside_Text_Edit := False;
                           end if;

                           if Current_Line_Number =
                                Natural (Current_Text_Edit.Location.End_Line)
                             and then Current_Column_Number =
                                        Natural
                                          (Current_Text_Edit.Location.
                                             End_Column)
                           then
                              Output_Buffer.Prepend
                                (VSS.Strings.Conversions.To_Virtual_String
                                   (To_String (Current_Text_Edit.Text)));
                              Inside_Text_Edit := True;
                           end if;
                        end if;

                        if Current_Line_Character_It.Element =
                          VSS.Characters.Latin.Character_Tabulation
                        then
                           Current_Column_Number :=
                             @ - Current_Line_Tabs_Size.Last_Element;
                           Current_Line_Tabs_Size.Delete_Last;
                        else
                           Current_Column_Number := @ - 1;
                        end if;
                     end loop;
                  end;

                  Lines_Cursor :=
                    VSS.String_Vectors.Previous (Lines_Iterator, Lines_Cursor);
                  Current_Line_Number := @ - 1;
               end loop;
            end;

            Result.Insert
              (Text_Edit_Ordered_Maps.Key (Edits_Cursor),
               Output_Buffer);

         exception
            when E : others =>
               Refactor_Trace.Trace
                 ("Failed to process "
                  & Key (Edits_Cursor)
                  & " edits at"
                  & Current_Line_Number'Image
                  & ":"
                  & Ada.Strings.Fixed.Trim
                      (Current_Column_Number'Image, Ada.Strings.Both));
               Refactor_Trace.Trace (E);
         end;

         Text_Edit_Ordered_Maps.Next (Edits_Cursor);
         File_Counter := @ + 1;
      end loop;

      return Result;
   end Apply_Edits;

end LAL_Refactor.File_Edits;