libadalang_tools_22.0.0_c9028428/src/laltools-refactor.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
------------------------------------------------------------------------------
--                                                                          --
--                             Libadalang Tools                             --
--                                                                          --
--                       Copyright (C) 2021, 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.Strings; use Ada.Strings;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
with GNAT.Directory_Operations; use GNAT.Directory_Operations;

with Ada.Text_IO; use Ada.Text_IO;

package body Laltools.Refactor is

   function Image (S : Source_Location_Range) return String;
   --  Return a Source_Location_Range as a string in a human readable format

   procedure Print (E : Text_Edit);
   --  Print an Edit in an human readable format to the standard output

   procedure Print (S : Text_Edit_Ordered_Set);
   --  Print an Edit_Ordered_Set in an human readable format to the standart
   --  output.

   --------------
   -- Contains --
   --------------

   function Contains
     (Edits     : Laltools.Refactor.Text_Edit_Map;
      File_Name : Laltools.Refactor.File_Name_Type;
      Edit      : Laltools.Refactor.Text_Edit)
         return Boolean
   is (Edits.Contains (File_Name) and then Edits (File_Name).Contains (Edit));

   -----------
   -- Image --
   -----------

   function Image (S : Source_Location_Range) return String is
   begin
      return Trim (S.Start_Line'Image, Both)
        & ":"
        & Trim (S.Start_Column'Image, Both)
        & "-"
        & Trim (S.End_Line'Image, Both)
        & ":"
        & Trim (S.End_Column'Image, Both);
   end Image;

   -----------
   -- Merge --
   -----------

   procedure Merge
     (Source : in out Text_Edit_Map;
      Target : Text_Edit_Map)
   is
      Map_Cursor : Text_Edit_Ordered_Maps.Cursor := Target.First;

   begin
      while Text_Edit_Ordered_Maps.Has_Element (Map_Cursor) loop
         if Source.Contains (Text_Edit_Ordered_Maps.Key (Map_Cursor)) then
            declare
               Set_Cursor : Text_Edit_Ordered_Sets.Cursor :=
                 Target.Constant_Reference (Map_Cursor).First;

            begin
               while Text_Edit_Ordered_Sets.Has_Element (Set_Cursor) loop
                  Source.Reference (Text_Edit_Ordered_Maps.Key (Map_Cursor)).
                    Insert (Text_Edit_Ordered_Sets.Element (Set_Cursor));
                  Text_Edit_Ordered_Sets.Next (Set_Cursor);
               end loop;
            end;

         else
            Source.Insert
              (Text_Edit_Ordered_Maps.Key (Map_Cursor),
               Text_Edit_Ordered_Maps.Element (Map_Cursor));
         end if;

         Text_Edit_Ordered_Maps.Next (Map_Cursor);
      end loop;
   end Merge;

   -----------
   -- Print --
   -----------

   procedure Print (E : Text_Edit) is
   begin
      Ada.Text_IO.Put_Line (Image (E.Location) & " " & To_String (E.Text));
   end Print;

   -----------
   -- Print --
   -----------

   procedure Print (S : Text_Edit_Ordered_Set) is
   begin
      for E of S loop
         Print (E);
      end loop;
   end Print;

   -----------
   -- Print --
   -----------

   procedure Print (M : Text_Edit_Map) is
      use Text_Edit_Ordered_Maps;
      C : Cursor := M.First;

   begin
      while Has_Element (C) loop
         Ada.Text_IO.Put_Line (Base_Name (Key (C)));
         Print (Element (C));
         Next (C);
      end loop;

      New_Line;
   end Print;

   -----------
   -- Print --
   -----------

   procedure Print (S : File_Creation_Ordered_Set) is
   begin
      for F of S loop
         Ada.Text_IO.Put_Line (Base_Name (To_String (F.Filepath)));
         Ada.Text_IO.Put_Line (To_String (F.Content));
      end loop;
   end Print;

   -----------
   -- Print --
   -----------

   procedure Print (S : File_Deletion_Ordered_Set) is
   begin
      for F of S loop
         Ada.Text_IO.Put_Line (Base_Name (To_String (F)));
      end loop;
   end Print;

   -----------
   -- Print --
   -----------

   procedure Print (S : File_Rename_Ordered_Set) is
   begin
      for F of S loop
         Ada.Text_IO.Put_Line (Base_Name (To_String (F.Filepath)));
         Ada.Text_IO.Put_Line (Base_Name (To_String (F.New_Name)));
      end loop;
   end Print;

   -----------
   -- Print --
   -----------

   procedure Print (E : Refactoring_Edits) is
   begin
      Print (E.Text_Edits);
      Print (E.File_Creations);
      Print (E.File_Deletions);
      Print (E.File_Renames);
   end Print;

   -----------------
   -- Safe_Insert --
   -----------------

   procedure Safe_Insert
     (Edits : in out Text_Edit_Ordered_Set;
      Edit  : Text_Edit) is
   begin
      if not Edits.Contains (Edit) then
         Edits.Insert (Edit);
      end if;
   end Safe_Insert;

   -----------------
   -- Safe_Insert --
   -----------------

   procedure Safe_Insert
     (Edits    : in out Text_Edit_Map;
      File_Name : File_Name_Type;
      Edit      : Text_Edit)
   is
      Edits_Set : Text_Edit_Ordered_Set;

   begin
      if Edits.Contains (File_Name) then
         Safe_Insert (Edits.Reference (File_Name), Edit);

      else
         Edits_Set.Insert (Edit);
         Edits.Insert (File_Name, Edits_Set);
      end if;
   end Safe_Insert;

end Laltools.Refactor;