From 0fc4233c3f4f297bef2f606dc30a52529def7c49 Mon Sep 17 00:00:00 2001 From: Andry Ogorodnik Date: Fri, 29 Apr 2022 18:29:18 +0300 Subject: [PATCH] V413-006 Fixing tabulations handling in code-fix Change-Id: I91d1c3720154bac50428f4a690cdfcd00979b475 Depends-On: I87bbdb69504ec92127c517e611346d057ecfa735 Depends-On: I6ffe8f79e75993c4a3e68a9ceb6de0df0715952d --- codefix/core/src/codefix-ada_tools.adb | 16 ++- codefix/core/src/codefix-formal_errors.adb | 18 ++- codefix/core/src/codefix-gnat_parser.adb | 7 +- .../src/codefix-text_manager-ada_commands.adb | 86 ++++++++---- .../src/codefix-text_manager-commands.adb | 22 +-- codefix/core/src/codefix-text_manager.adb | 125 ++++++++++++------ codefix/core/src/codefix.adb | 87 ++++++++++-- codefix/core/src/codefix.ads | 31 ++++- codefix/ui/src/codefix-gps_io.adb | 30 ++++- 9 files changed, 304 insertions(+), 118 deletions(-) diff --git a/codefix/core/src/codefix-ada_tools.adb b/codefix/core/src/codefix-ada_tools.adb index 6ed8f101a5..559b1e71cb 100644 --- a/codefix/core/src/codefix-ada_tools.adb +++ b/codefix/core/src/codefix-ada_tools.adb @@ -281,7 +281,9 @@ package body Codefix.Ada_Tools is Line => Get_Construct (Iterator).Sloc_Start.Line, Column => To_Column_Index (String_Index_Type - (Get_Construct (Iterator).Sloc_Start.Column), Line)); + (Get_Construct (Iterator).Sloc_Start.Column), + Line, + Current_Text.Tab_Width (File_Name))); end; Append (Result, New_Clause); @@ -374,7 +376,9 @@ package body Codefix.Ada_Tools is (Current_Cursor, To_Column_Index (String_Index_Type - (Get_Construct (Last_Info).Sloc_End.Column) + 1, Line)); + (Get_Construct (Last_Info).Sloc_End.Column) + 1, + Line, + Current_Text.Tab_Width (File_Name))); end; else Set_Location (Current_Cursor, 0, 1); @@ -454,7 +458,9 @@ package body Codefix.Ada_Tools is (Current_Cursor, To_Column_Index (String_Index_Type - (Get_Construct (Last_Info).Sloc_End.Column) + 1, Line)); + (Get_Construct (Last_Info).Sloc_End.Column) + 1, + Line, + Current_Text.Tab_Width (File_Name))); end; else Set_Location (Current_Cursor, 0, 1); @@ -499,7 +505,9 @@ package body Codefix.Ada_Tools is (Result, Get_Construct (Iterator).Sloc_Start.Line, To_Column_Index (String_Index_Type - (Get_Construct (Iterator).Sloc_Start.Column), Line)); + (Get_Construct (Iterator).Sloc_Start.Column), + Line, + Current_Text.Tab_Width (File_Name))); end; Lock.Unlock; diff --git a/codefix/core/src/codefix-formal_errors.adb b/codefix/core/src/codefix-formal_errors.adb index daed9dd308..f182209ba5 100644 --- a/codefix/core/src/codefix-formal_errors.adb +++ b/codefix/core/src/codefix-formal_errors.adb @@ -725,14 +725,18 @@ package body Codefix.Formal_Errors is if Apply_Also_On_Decl then declare File : constant Structured_File_Access := - Current_Text.Get_Structured_File - (Message.Get_File); - Decl_Entity : constant Entity_Access := Find_Declaration - (Lang => Get_Tree_Language (File), - File => File, - Line => Word.Get_Line, - Column => String_Index_Type (Word.Get_Column)); + Current_Text.Get_Structured_File + (Message.Get_File); + Decl_Entity : Entity_Access; begin + if File /= null then + Decl_Entity := Find_Declaration + (Lang => Get_Tree_Language (File), + File => File, + Line => Word.Get_Line, + Column => String_Index_Type (Word.Get_Column)); + end if; + if Decl_Entity /= Null_Entity_Access then declare Decl_Construct : constant access diff --git a/codefix/core/src/codefix-gnat_parser.adb b/codefix/core/src/codefix-gnat_parser.adb index cd70a317a7..7acff42534 100644 --- a/codefix/core/src/codefix-gnat_parser.adb +++ b/codefix/core/src/codefix-gnat_parser.adb @@ -2390,9 +2390,9 @@ package body Codefix.GNAT_Parser is begin Solutions := Unexpected - (Current_Text, - Message, - To_Unbounded_String + (Current_Text => Current_Text, + Message => Message, + String_Unexpected => To_Unbounded_String (Get_Message (Message) (Matches (1).First .. Matches (1).Last))); end Fix; @@ -4140,7 +4140,6 @@ package body Codefix.GNAT_Parser is if Length (Solutions) = 0 then raise Uncorrectable_Message; end if; - end Fix; ---------------- diff --git a/codefix/core/src/codefix-text_manager-ada_commands.adb b/codefix/core/src/codefix-text_manager-ada_commands.adb index 30140036ce..2d3c961f98 100644 --- a/codefix/core/src/codefix-text_manager-ada_commands.adb +++ b/codefix/core/src/codefix-text_manager-ada_commands.adb @@ -97,7 +97,9 @@ package body Codefix.Text_Manager.Ada_Commands is Close_Cursor.Set_Line (Sloc_Start.Line); Close_Cursor.Set_Column (To_Column_Index - (String_Index_Type (Sloc_Start.Column), Line)); + (String_Index_Type (Sloc_Start.Column), + Line, + Current_Text.Tab_Width (Open_Cursor.File))); return True; end if; @@ -201,10 +203,11 @@ package body Codefix.Text_Manager.Ada_Commands is loop declare Full_Line : constant String := - Get_Line (Current_Text, End_Cursor, 1); - J : String_Index_Type := - To_Char_Index - (Get_Column (End_Cursor), Full_Line) - 1; + Get_Line (Current_Text, End_Cursor, 1); + J : String_Index_Type := To_Char_Index + (Get_Column (End_Cursor), + Full_Line, + Current_Text.Tab_Width (End_Cursor.Get_File)) - 1; begin -- Skip previous blanks @@ -222,7 +225,9 @@ package body Codefix.Text_Manager.Ada_Commands is Set_Location (End_Cursor, Get_Line (End_Cursor), - To_Column_Index (J, Full_Line) + 1); + To_Column_Index + (J, Full_Line, + Current_Text.Tab_Width (End_Cursor.Get_File)) + 1); exit; @@ -244,7 +249,9 @@ package body Codefix.Text_Manager.Ada_Commands is Set_Location (End_Cursor, Get_Line (End_Cursor), - To_Column_Index (J, Full_Line)); + To_Column_Index + (J, Full_Line, + Current_Text.Tab_Width (End_Cursor.Get_File))); exit; end if; @@ -876,7 +883,9 @@ package body Codefix.Text_Manager.Ada_Commands is Set_Location (Position, Get_Construct (Declaration).Sloc_End.Line, - To_Column_Index (Char_Ind, Line)); + To_Column_Index + (Char_Ind, Line, + Current_Text.Tab_Width (Cursor.Get_File))); end; end Add_Pragma; @@ -909,7 +918,9 @@ package body Codefix.Text_Manager.Ada_Commands is Set_Location (Position, Get_Construct (Clause).Sloc_End.Line, - To_Column_Index (Char_Ind, Line)); + To_Column_Index + (Char_Ind, Line, + Current_Text.Tab_Width (Cursor.Get_File))); end; end Add_Clause_Pragma; @@ -943,7 +954,9 @@ package body Codefix.Text_Manager.Ada_Commands is Set_Location (Position, Get_Construct (Declaration).Sloc_End.Line, - To_Column_Index (Char_Ind, Line)); + To_Column_Index + (Char_Ind, Line, + Current_Text.Tab_Width (Cursor.Get_File))); end; end Add_Literal_Pragma; @@ -971,7 +984,9 @@ package body Codefix.Text_Manager.Ada_Commands is (Position, Get_Construct (Declaration).Sloc_Entity.Line, To_Column_Index (String_Index_Type - (Get_Construct (Declaration).Sloc_Entity.Column), Line)); + (Get_Construct (Declaration).Sloc_Entity.Column), + Line, + Current_Text.Tab_Width (Cursor.Get_File))); end; Garbage := Position; @@ -1292,7 +1307,8 @@ package body Codefix.Text_Manager.Ada_Commands is Begin_Cursor.Line := Sloc_Start.Line; Begin_Cursor.Col := To_Column_Index (String_Index_Type (Sloc_Start.Column), - Get_Line (Current_Text, Begin_Cursor, 1)); + Get_Line (Current_Text, Begin_Cursor, 1), + Current_Text.Tab_Width (Begin_Cursor.Get_File)); end if; end Begin_Of_Profile; @@ -1306,7 +1322,8 @@ package body Codefix.Text_Manager.Ada_Commands is End_Cursor.Line := Last_Entity_Line; End_Cursor.Col := To_Column_Index (String_Index_Type (Last_Entity_Column), - Get_Line (Current_Text, End_Cursor, 1)); + Get_Line (Current_Text, End_Cursor, 1), + Current_Text.Tab_Width (End_Cursor.Get_File)); if Begin_Cursor = Null_File_Cursor then End_Cursor.Col := End_Cursor.Col + 1; @@ -1389,7 +1406,8 @@ package body Codefix.Text_Manager.Ada_Commands is To_Column_Index (String_Index_Type (Get_Construct (Position_It).Sloc_Start.Column), - Get_Line (Current_Text, Begin_Analyze_Cursor, 1))); + Get_Line (Current_Text, Begin_Analyze_Cursor, 1), + Current_Text.Tab_Width (Begin_Analyze_Cursor.Get_File))); Last_Entity_Column := Integer (Begin_Analyze_Cursor.Col); Last_Entity_Line := Begin_Analyze_Cursor.Line; @@ -1720,7 +1738,10 @@ package body Codefix.Text_Manager.Ada_Commands is (Get_File (Line_Cursor)).Indent_Line (Line_Cursor); else Char_Ind := To_Char_Index - (This.Force_Column, Get_Line (Current_Text, Line_Cursor, 1)); + (This.Force_Column, + Get_Line (Current_Text, Line_Cursor, 1), + Current_Text.Tab_Width (Line_Cursor.Get_File)); + Indent_Size := Natural (Char_Ind) - 1; declare @@ -1929,7 +1950,9 @@ package body Codefix.Text_Manager.Ada_Commands is (This => Cursor, Line => Sloc_End.Line, Column => To_Column_Index - (String_Index_Type (Sloc_End.Column), Line)); + (String_Index_Type (Sloc_End.Column), + Line, + Current_Text.Tab_Width (Cursor.Get_File))); end if; return Stop_Scanning; @@ -2207,7 +2230,9 @@ package body Codefix.Text_Manager.Ada_Commands is End_Line := Sloc_End.Line; End_Column := To_Column_Index - (String_Index_Type (Sloc_End.Column), Line); + (String_Index_Type (Sloc_End.Column), + Line, + Current_Text.Tab_Width (Cursor.Get_File)); return False; end Scan_Forward_Callback; @@ -2288,8 +2313,8 @@ package body Codefix.Text_Manager.Ada_Commands is Current_Text.Get_Current_Cursor (This.Cursor.all); Line : constant String := Get_Line (Current_Text, Cursor, 1); New_Id : String (1 .. Line'Length); - Index : String_Index_Type := - To_Char_Index (Get_Column (Cursor), Line); + Index : String_Index_Type := To_Char_Index + (Get_Column (Cursor), Line, Current_Text.Tab_Width (Cursor.Get_File)); New_Id_Index : Integer := 1; Start_Index : String_Index_Type; begin @@ -2325,7 +2350,8 @@ package body Codefix.Text_Manager.Ada_Commands is end if; end loop; - Cursor.Col := To_Column_Index (Start_Index, Line); + Cursor.Col := To_Column_Index + (Start_Index, Line, Current_Text.Tab_Width (Cursor.Get_File)); Current_Text.Replace (Cursor, @@ -2384,7 +2410,8 @@ package body Codefix.Text_Manager.Ada_Commands is Pragma_Cursor.Set_Column (To_Column_Index (String_Index_Type (Get_Construct (It).Sloc_Start.Column), - Current_Text.Get_Line (Pragma_Cursor, 0))); + Current_Text.Get_Line (Pragma_Cursor, 0), + Current_Text.Tab_Width (Pragma_Cursor.Get_File))); declare List : Ada_Statement; @@ -2581,7 +2608,8 @@ package body Codefix.Text_Manager.Ada_Commands is End_Line : constant String := Current_Text.Get_Line (Src_End_Cursor, 0); begin - Src_End_Cursor.Col := To_Column_Index (End_Line'Length, End_Line); + Src_End_Cursor.Col := To_Column_Index + (End_Line'Length, End_Line, Current_Text.Tab_Width (Cursor.File)); end; if Prev_Entity = Null_Construct_Tree_Iterator then @@ -2596,7 +2624,10 @@ package body Codefix.Text_Manager.Ada_Commands is End_Line : constant String := Current_Text.Get_Line (Dst_Cursor, 0); begin - Dst_Cursor.Col := To_Column_Index (End_Line'Length, End_Line) + 1; + Dst_Cursor.Col := To_Column_Index + (End_Line'Length, + End_Line, + Current_Text.Tab_Width (Dst_Cursor.Get_File)) + 1; end; else -- If there is a subprogram before, place it after the end of that @@ -2608,7 +2639,10 @@ package body Codefix.Text_Manager.Ada_Commands is End_Line : constant String := Current_Text.Get_Line (Dst_Cursor, 0); begin - Dst_Cursor.Col := To_Column_Index (End_Line'Length, End_Line) + 1; + Dst_Cursor.Col := To_Column_Index + (End_Line'Length, + End_Line, + Current_Text.Tab_Width (Dst_Cursor.Get_File)) + 1; end; end if; @@ -2863,7 +2897,9 @@ package body Codefix.Text_Manager.Ada_Commands is Name_Cursor.Set_Location (Sloc_Start.Line, To_Column_Index - (String_Index_Type (Sloc_Start.Column), Line)); + (String_Index_Type (Sloc_Start.Column), + Line, + Current_Text.Tab_Width (Name_Cursor.Get_File))); Name_Length := Sloc_End.Column - Sloc_Start.Column + 1; diff --git a/codefix/core/src/codefix-text_manager-commands.adb b/codefix/core/src/codefix-text_manager-commands.adb index ca5be18945..30fd388eb8 100644 --- a/codefix/core/src/codefix-text_manager-commands.adb +++ b/codefix/core/src/codefix-text_manager-commands.adb @@ -97,9 +97,9 @@ package body Codefix.Text_Manager.Commands is Match : constant String := Word.Get_Matching_Word (Current_Text); Str_Parsed : constant String := - Do_Tab_Expansion - (Current_Text.Get_Line (Word, Start_Col => 1), - Current_Text.Tab_Width (Get_File (Word))); + Do_Tab_Expansion + (Current_Text.Get_Line (Word, Start_Col => 1), + Current_Text.Tab_Width (Get_File (Word))); Column : Natural := Natural (Get_Column (Word)); begin @@ -161,13 +161,14 @@ package body Codefix.Text_Manager.Commands is exit; end if; - -- Check if we must remove another occurrence found immediately - -- after the removed text + -- Check if we must remove another occurrence found + -- immediately after the removed text exit when not This.All_Occurrences; declare - Str_Parsed : constant String := Current_Text.Get_Line (Word); + Str_Parsed : constant String := + Current_Text.Get_Line (Word); begin exit when Str_Parsed'Length < Match'Length @@ -269,12 +270,15 @@ package body Codefix.Text_Manager.Commands is New_Pos.Col := To_Column_Index (String_Index_Type (Matches (1).Last) + 1, - Get_Line (Current_Text, New_Pos, 1)); + Get_Line (Current_Text, New_Pos, 1), + Current_Text.Tab_Width (New_Pos.File)); end; end if; - Word_Char_Index := - To_Char_Index (New_Pos.Col, Get_Line (Current_Text, Line_Cursor)); + Word_Char_Index := To_Char_Index + (New_Pos.Col, + Get_Line (Current_Text, Line_Cursor), + Current_Text.Tab_Width (New_Pos.File)); if This.Position = Specified then if This.Add_Spaces then diff --git a/codefix/core/src/codefix-text_manager.adb b/codefix/core/src/codefix-text_manager.adb index 4ecb0107d2..87deb7d061 100644 --- a/codefix/core/src/codefix-text_manager.adb +++ b/codefix/core/src/codefix-text_manager.adb @@ -35,7 +35,8 @@ package body Codefix.Text_Manager is use type Basic_Types.Visible_Column_Type; function Search_Tokens - (Line : String; + (This : Text_Interface'Class; + Line : String; Cursor : File_Cursor'Class; Searched : Token_List; Step : Step_Way := Normal_Step) return Word_Cursor'Class; @@ -310,6 +311,15 @@ package body Codefix.Text_Manager is Real_Cursor.Line := 1; end if; + -- Convert GCC column to GS column to create a mark + -- in The proper position + Real_Cursor.Set_Column + (Convert_Column_With_Tab_Width + (Column => Cursor.Get_Column, + Str => Current_Text.Get_Line (Cursor, Start_Col => 1), + From_Width => 8, + To_Width => Current_Text.Tab_Width (Cursor.Get_File))); + declare Res : Mark_Abstr'Class := Get_New_Mark (Get_File (Current_Text, Get_File (Real_Cursor)).all, @@ -623,7 +633,7 @@ package body Codefix.Text_Manager is -- can find the beginning of a construct Unit_Info := Get_Iterator_At - (Current_Text, Cursor, From_Type => Start_Construct); + (Current_Text, Cursor, From_Type => Start_Construct); end if; if Unit_Info = Null_Construct_Tree_Iterator then @@ -660,10 +670,12 @@ package body Codefix.Text_Manager is Body_Begin.Col := To_Column_Index (String_Index_Type (Get_Construct (Body_Info).Sloc_Start.Column), - Get_Line (Current_Text, Body_Begin)); + Get_Line (Current_Text, Body_Begin), + Current_Text.Tab_Width (Get_File (Body_Begin))); Body_End.Col := To_Column_Index (String_Index_Type (Get_Construct (Body_Info).Sloc_End.Column), - Get_Line (Current_Text, Body_End)); + Get_Line (Current_Text, Body_End), + Current_Text.Tab_Width (Get_File (Body_End))); Spec_Begin.Col := 1; Spec_End.Col := 1; @@ -672,10 +684,12 @@ package body Codefix.Text_Manager is Spec_Begin.Col := To_Column_Index (String_Index_Type (Get_Construct (Unit_Info).Sloc_Start.Column), - Get_Line (Current_Text, Spec_Begin)); + Get_Line (Current_Text, Spec_Begin), + Current_Text.Tab_Width (Get_File (Spec_Begin))); Spec_End.Col := To_Column_Index (String_Index_Type (Get_Construct (Unit_Info).Sloc_End.Column), - Get_Line (Current_Text, Spec_End)); + Get_Line (Current_Text, Spec_End), + Current_Text.Tab_Width (Get_File (Spec_End))); else Set_File (Body_Begin, Get_File (Cursor)); Set_File (Body_End, Get_File (Cursor)); @@ -687,10 +701,12 @@ package body Codefix.Text_Manager is Body_Begin.Col := To_Column_Index (String_Index_Type (Get_Construct (Unit_Info).Sloc_Start.Column), - Get_Line (Current_Text, Body_Begin)); + Get_Line (Current_Text, Body_Begin), + Current_Text.Tab_Width (Get_File (Body_Begin))); Body_End.Col := To_Column_Index (String_Index_Type (Get_Construct (Unit_Info).Sloc_End.Column), - Get_Line (Current_Text, Body_End)); + Get_Line (Current_Text, Body_End), + Current_Text.Tab_Width (Get_File (Body_End))); Assign (Spec_Begin, Null_File_Cursor); Assign (Spec_End, Null_File_Cursor); @@ -868,8 +884,10 @@ package body Codefix.Text_Manager is (Get_Tree (Get_Structured_File (Current_Text)), (Absolute_Offset => False, Line => Get_Line (Cursor), - Line_Offset => - To_Char_Index (Get_Column (Cursor), Line_Cursor)), + Line_Offset => To_Char_Index + (Get_Column (Cursor), + Line_Cursor, + Text_Interface'Class (Current_Text.all).Tab_Width)), From_Type, Position, Categories_Seeked); @@ -905,13 +923,15 @@ package body Codefix.Text_Manager is Char_Start, Char_End : String_Index_Type; begin if C.Line = Start.Line then - Char_Start := To_Char_Index (Start.Col, Line); + Char_Start := To_Char_Index + (Start.Col, Line, Text_Interface'Class (This).Tab_Width); else Char_Start := String_Index_Type (Line'First); end if; if C.Line = Stop.Line then - Char_End := To_Char_Index (Stop.Col, Line); + Char_End := To_Char_Index + (Stop.Col, Line, Text_Interface'Class (This).Tab_Width); else Char_End := String_Index_Type (Line'Last); end if; @@ -988,7 +1008,8 @@ package body Codefix.Text_Manager is ------------------- function Search_Tokens - (Line : String; + (This : Text_Interface'Class; + Line : String; Cursor : File_Cursor'Class; Searched : Token_List; Step : Step_Way := Normal_Step) return Word_Cursor'Class @@ -1028,7 +1049,7 @@ package body Codefix.Text_Manager is Found := True; Result.Col := To_Column_Index - (String_Index_Type (Sloc_Start.Index), Line); + (String_Index_Type (Sloc_Start.Index), Line, This.Tab_Width); Result.String_Match := To_Unbounded_String (Line (Sloc_Start.Index .. Sloc_End.Index)); @@ -1057,7 +1078,7 @@ package body Codefix.Text_Manager is if Result.Col = 0 then Start_Index := String_Index_Type (Line'Last); else - Start_Index := To_Char_Index (Result.Col, Line); + Start_Index := To_Char_Index (Result.Col, Line, This.Tab_Width); end if; case Step is @@ -1109,10 +1130,11 @@ package body Codefix.Text_Manager is Result := Word_Cursor (Search_Tokens - (This.Get_Line (New_Cursor, 1), - New_Cursor, - Searched, - Step)); + (This => This, + Line => This.Get_Line (New_Cursor, 1), + Cursor => New_Cursor, + Searched => Searched, + Step => Step)); if Result /= Null_Word_Cursor then return Result; @@ -1237,7 +1259,7 @@ package body Codefix.Text_Manager is Line_Cursor.Col := 1; Current_Line := To_Unbounded_String (Get_Line (This, Line_Cursor)); Cursor_Char_Index := To_Char_Index - (Get_Column (Cursor), To_String (Current_Line)); + (Get_Column (Cursor), To_String (Current_Line), This.Tab_Width); while Is_Blank (Slice @@ -1286,7 +1308,8 @@ package body Codefix.Text_Manager is end if; Word.Line := Line_Cursor.Line; - Word.Col := To_Column_Index (Begin_Word, To_String (Current_Line)); + Word.Col := To_Column_Index + (Begin_Word, To_String (Current_Line), This.Tab_Width); if Cursor_Char_Index = String_Index_Type (Length (Current_Line)) then Cursor_Char_Index := 1; @@ -1295,8 +1318,8 @@ package body Codefix.Text_Manager is Cursor_Char_Index := Cursor_Char_Index + 1; end if; - Cursor.Col := - To_Column_Index (Cursor_Char_Index, To_String (Current_Line)); + Cursor.Col := To_Column_Index + (Cursor_Char_Index, To_String (Current_Line), This.Tab_Width); end Next_Word; -------------------- @@ -1333,7 +1356,7 @@ package body Codefix.Text_Manager is Line_Cursor.Col := 1; Current_Line := To_Unbounded_String (Get_Line (This, Line_Cursor)); Cursor_Char_Index := To_Char_Index - (Get_Column (Cursor), To_String (Current_Line)); + (Get_Column (Cursor), To_String (Current_Line), This.Tab_Width); while Is_Blank (Slice (Current_Line, 1, Natural (Cursor_Char_Index))) @@ -1366,10 +1389,10 @@ package body Codefix.Text_Manager is Word.Line := Line_Cursor.Line; Word.Col := To_Column_Index - (Cursor_Char_Index, To_String (Current_Line)); + (Cursor_Char_Index, To_String (Current_Line), This.Tab_Width); - Cursor.Col := - To_Column_Index (Cursor_Char_Index, To_String (Current_Line)); + Cursor.Col := To_Column_Index + (Cursor_Char_Index, To_String (Current_Line), This.Tab_Width); end Previouse_Word; ------------------------- @@ -1437,13 +1460,16 @@ package body Codefix.Text_Manager is Current_Line := To_Unbounded_String (Get_Line (This, Line_Cursor)); Result.Col := To_Column_Index - (String_Index_Type (Length (Current_Line)), Current_Line); + (String_Index_Type (Length (Current_Line)), + Current_Line, + This.Tab_Width); end if; if not Is_Blank (Element (Current_Line, - Natural (To_Char_Index (Result.Col, Current_Line)))) + Natural (To_Char_Index + (Result.Col, Current_Line, This.Tab_Width)))) then return Result; end if; @@ -1471,7 +1497,8 @@ package body Codefix.Text_Manager is Line_Offset := Get_Line (Start) - 1; Col_Offset := Integer (To_Column_Index - (String_Index_Type (Get_Column (Start)), First_Line)) - 1; + (String_Index_Type + (Get_Column (Start)), First_Line, This.Tab_Width)) - 1; while not Stop and then Line_Offset + 1 <= Last_Line loop declare @@ -1616,7 +1643,10 @@ package body Codefix.Text_Manager is begin Dest_Stop.Col := To_Column_Index (To_Char_Index - (Dest_Start.Col, Line) + String_Index_Type (Len) - 1, Line); + (Dest_Start.Col, Line, This.Tab_Width (Position.Get_File)) + + String_Index_Type (Len) - 1, + Line, + This.Tab_Width (Position.Get_File)); Replace (This => This, @@ -1661,11 +1691,13 @@ package body Codefix.Text_Manager is begin case Pos is when First => - End_Text := To_Char_Index (Tmp_Cursor.Col, Line); + End_Text := To_Char_Index + (Tmp_Cursor.Col, Line, This.Tab_Width (Dest_Start.File)); Begin_Text := End_Text - 1; when Last => - Begin_Text := To_Char_Index (Tmp_Cursor.Col, Line); + Begin_Text := To_Char_Index + (Tmp_Cursor.Col, Line, This.Tab_Width (Dest_Start.File)); End_Text := Begin_Text + 1; end case; @@ -1679,7 +1711,8 @@ package body Codefix.Text_Manager is if Integer (Begin_Text) < Line'First then Tmp_Cursor.Col := 1; else - Tmp_Cursor.Col := To_Column_Index (Begin_Text, Line) + 1; + Tmp_Cursor.Col := To_Column_Index + (Begin_Text, Line, This.Tab_Width (Dest_Start.File)) + 1; end if; Blank_Length := End_Text - Begin_Text - 1; @@ -1745,11 +1778,12 @@ package body Codefix.Text_Manager is declare Line : constant String := This.Get_Line (Dest_Start, 1); - Index : constant String_Index_Type := - To_Char_Index (Dest_Start.Col, Line) + Index : constant String_Index_Type := To_Char_Index + (Dest_Start.Col, Line, This.Tab_Width (Dest_Start.File)) + New_Text'Length - 1; begin - Dest_Stop.Col := To_Column_Index (Index, Line); + Dest_Stop.Col := To_Column_Index + (Index, Line, This.Tab_Width (Dest_Stop.File)); end; else -- If there are several lines, then get the position of the last one. @@ -1761,7 +1795,9 @@ package body Codefix.Text_Manager is Dest_Stop.Col := To_Column_Index (String_Index_Type (New_Text'Last - Last_Begin_Line + 1), - Line_Indexes (New_Text (Last_Begin_Line .. New_Text'Last))); + Line_Indexes (New_Text (Last_Begin_Line .. New_Text'Last)), + This.Tab_Width (Dest_Stop.File)); + end; end if; @@ -1826,8 +1862,10 @@ package body Codefix.Text_Manager is declare Current_String : constant String := This.Get_Line (Start, 1); begin - Start_Char_Index := To_Char_Index (Start.Col, Current_String); - Stop_Char_Index := To_Char_Index (Stop.Col, Current_String); + Start_Char_Index := To_Char_Index + (Start.Col, Current_String, This.Tab_Width); + Stop_Char_Index := To_Char_Index + (Stop.Col, Current_String, This.Tab_Width); declare Back : constant String := Current_String @@ -1867,7 +1905,8 @@ package body Codefix.Text_Manager is declare Current_String : constant String := This.Get_Line (Start, 1); begin - Start_Char_Index := To_Char_Index (Start.Col, Current_String); + Start_Char_Index := To_Char_Index + (Start.Col, Current_String, This.Tab_Width); if Is_Blank (Current_String (1 .. Natural (Start_Char_Index) - 1)) @@ -2189,8 +2228,8 @@ package body Codefix.Text_Manager is Matcher : constant Pattern_Matcher := Compile (To_String (Word.String_Match)); Str_Parsed : constant String := Text.Get_Line (Word, 1); - Index : constant String_Index_Type := - To_Char_Index (Word.Col, Str_Parsed); + Index : constant String_Index_Type := To_Char_Index + (Word.Col, Str_Parsed, Text.Tab_Width (Word.Get_File)); begin Match (Matcher, diff --git a/codefix/core/src/codefix.adb b/codefix/core/src/codefix.adb index b8af3a41e5..d2e3dcc43c 100644 --- a/codefix/core/src/codefix.adb +++ b/codefix/core/src/codefix.adb @@ -17,6 +17,7 @@ with String_Utils; use String_Utils; with GNATCOLL.Utils; use GNATCOLL.Utils; +with GNATCOLL.Xref; package body Codefix is @@ -25,13 +26,17 @@ package body Codefix is ------------------- function To_Char_Index - (Index : Visible_Column_Type; Str : String) return String_Index_Type + (Index : Visible_Column_Type; + Str : String; + Tab_Width : Integer) + return String_Index_Type is Current_Index : Integer := Str'First; begin - Skip_To_Column (Str => Str, - Columns => Integer (Index), - Index => Current_Index); + Skip_To_Column (Str => Str, + Columns => Integer (Index), + Index => Current_Index, + Tab_Width => Tab_Width); return String_Index_Type (Current_Index); end To_Char_Index; @@ -40,15 +45,18 @@ package body Codefix is ------------------- function To_Char_Index - (Index : Visible_Column_Type; - Str : Unbounded_String) return String_Index_Type + (Index : Visible_Column_Type; + Str : Unbounded_String; + Tab_Width : Integer) + return String_Index_Type is Current_Index : Integer := 1; begin - Skip_To_Column (Str => To_String (Str), - Columns => Integer (Index), - Index => Current_Index); + Skip_To_Column (Str => To_String (Str), + Columns => Integer (Index), + Index => Current_Index, + Tab_Width => Tab_Width); return String_Index_Type (Current_Index); end To_Char_Index; @@ -57,7 +65,10 @@ package body Codefix is --------------------- function To_Column_Index - (Index : String_Index_Type; Str : String) return Visible_Column_Type + (Index : String_Index_Type; + Str : String; + Tab_Width : Integer) + return Visible_Column_Type is Current_Index : String_Index_Type := String_Index_Type (Str'First); Current_Col : Visible_Column_Type := 1; @@ -66,7 +77,8 @@ package body Codefix is (Buffer => Str, Columns => Current_Col, Index_In_Line => Index, - Index => Current_Index); + Index => Current_Index, + Tab_Width => Tab_Width); return Current_Col; end To_Column_Index; @@ -76,8 +88,10 @@ package body Codefix is --------------------- function To_Column_Index - (Index : String_Index_Type; - Str : Unbounded_String) return Visible_Column_Type + (Index : String_Index_Type; + Str : Unbounded_String; + Tab_Width : Integer) + return Visible_Column_Type is Current_Index : String_Index_Type := 1; Current_Col : Visible_Column_Type := 1; @@ -87,11 +101,56 @@ package body Codefix is (Buffer => Str, Columns => Current_Col, Index_In_Line => Index, - Index => Current_Index); + Index => Current_Index, + Tab_Width => Tab_Width); return Current_Col; end To_Column_Index; + ----------------------------------- + -- Convert_Column_With_Tab_Width -- + ----------------------------------- + + function Convert_Column_With_Tab_Width + (Column : Visible_Column_Type; + Str : String; + From_Width : Natural; + To_Width : Natural) + return Visible_Column_Type + is + use type GNATCOLL.Xref.Visible_Column; + + Current_Col : Integer := 1; + Index : Integer := 1; + Result : Visible_Column_Type := 1; + begin + if Str = "" then + return Column; + end if; + + while Current_Col < Integer (Column) + and then Natural (Index) <= Str'Last + and then Str (Natural (Index)) /= ASCII.LF + loop + if Natural (Index) < Str'Last + and then Str (Natural (Index)) = ASCII.HT + then + Current_Col := Current_Col + + (From_Width - (Current_Col - 1) mod From_Width); + + Result := Result + GNATCOLL.Xref.Visible_Column + (To_Width - Integer (Result - 1) mod To_Width); + else + Current_Col := Current_Col + 1; + Result := Result + 1; + end if; + + Index := Forward_UTF8_Char (Str, Natural (Index)); + end loop; + + return Result; + end Convert_Column_With_Tab_Width; + ------------ -- Is_Set -- ------------ diff --git a/codefix/core/src/codefix.ads b/codefix/core/src/codefix.ads index 7289a53cba..e30a314ab1 100644 --- a/codefix/core/src/codefix.ads +++ b/codefix/core/src/codefix.ads @@ -39,22 +39,41 @@ package Codefix is -- String used to insert an end of line. function To_Char_Index - (Index : Visible_Column_Type; Str : String) return String_Index_Type + (Index : Visible_Column_Type; + Str : String; + Tab_Width : Integer) + return String_Index_Type with Post => To_Char_Index'Result <= String_Index_Type (Str'Last + 1); function To_Char_Index - (Index : Visible_Column_Type; - Str : Unbounded_String) return String_Index_Type; + (Index : Visible_Column_Type; + Str : Unbounded_String; + Tab_Width : Integer) return String_Index_Type; -- Return the char position corresponding to the column given in parameter -- This will handle tabulations function To_Column_Index - (Index : String_Index_Type; Str : String) return Visible_Column_Type; + (Index : String_Index_Type; + Str : String; + Tab_Width : Integer) + return Visible_Column_Type; function To_Column_Index - (Index : String_Index_Type; - Str : Unbounded_String) return Visible_Column_Type; + (Index : String_Index_Type; + Str : Unbounded_String; + Tab_Width : Integer) + return Visible_Column_Type; -- Return the column index corresponding to the char index given in -- parameter. This will handle tabulations. + function Convert_Column_With_Tab_Width + (Column : Visible_Column_Type; + Str : String; + From_Width : Natural; + To_Width : Natural) + return Visible_Column_Type; + -- Convert column based on From_Width for one tab to column based on + -- To_Width Needed to convert GCC columns to GS columns because GCC uses + -- 8 spaces for one tab but GS tab's width depends on user preferences. + type Root_Error_Parser is abstract tagged null record; -- Root type for all error parsers diff --git a/codefix/ui/src/codefix-gps_io.adb b/codefix/ui/src/codefix-gps_io.adb index 777af32883..1a2f196bbb 100644 --- a/codefix/ui/src/codefix-gps_io.adb +++ b/codefix/ui/src/codefix-gps_io.adb @@ -93,8 +93,8 @@ package body Codefix.GPS_Io is Len : Natural) return String is Line : constant String := Get_Line (This, Cursor, 1); - Char_Ind : constant String_Index_Type := - To_Char_Index (Get_Column (Cursor), Line); + Char_Ind : constant String_Index_Type := To_Char_Index + (Get_Column (Cursor), Line, This.Tab_Width); begin return Line (Natural (Char_Ind) .. Natural (Char_Ind) + Len - 1); @@ -110,7 +110,7 @@ package body Codefix.GPS_Io is is Line : constant String := Get_Line (This, Cursor, 1); Char_Ind : constant String_Index_Type := - To_Char_Index (Get_Column (Cursor), Line); + To_Char_Index (Get_Column (Cursor), Line, This.Tab_Width); begin return Line (Natural (Char_Ind)); end Get; @@ -137,11 +137,27 @@ package body Codefix.GPS_Io is Char_Ind : String_Index_Type; Last_Ind : Integer := Line'Last; + begin if Start_Col = 0 then - Char_Ind := To_Char_Index (Get_Column (Cursor), Line); + -- Start_Col is not set, using Cursor + if Get_Column (Cursor) > 1 then + -- The cursor is not at the beginning of the line, so may + -- contains tabulators + Char_Ind := To_Char_Index + (Get_Column (Cursor), Line, This.Tab_Width); + else + -- At the beginning of the line + Char_Ind := 1; + end if; + + elsif Start_Col = 1 then + -- Start_Col at the beginning of the line + Char_Ind := 1; + else - Char_Ind := To_Char_Index (Start_Col, Line); + -- Converting tabulators + Char_Ind := To_Char_Index (Start_Col, Line, This.Tab_Width); end if; while Last_Ind >= Line'First and then Line (Last_Ind) = ASCII.LF loop @@ -255,7 +271,9 @@ package body Codefix.GPS_Io is (Insert_Position, Get_Line (Insert_Position), To_Column_Index - (String_Index_Type (Line_Str'Last), Line_Str) + 1); + (String_Index_Type (Line_Str'Last), + Line_Str, + This.Tab_Width) + 1); Replace (This, Insert_Position, 0, EOL_Str & New_Line); end; end if;