Skip to content

Commit

Permalink
Merge branch 'issue_30' into 'main'
Browse files Browse the repository at this point in the history
Do not group segments before/between/after table separators

Closes #30

See merge request eng/libadalang/prettier-ada!35
  • Loading branch information
joaopsazevedo committed Aug 30, 2024
2 parents 5adba9f + ae1a161 commit 8fc0463
Show file tree
Hide file tree
Showing 12 changed files with 569 additions and 172 deletions.
22 changes: 13 additions & 9 deletions src/prettier_ada-documents-builders.adb
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ package body Prettier_Ada.Documents.Builders is
(Elements_Aggregate.First_Element);

else
Row_Elements.Append (Group (Elements_Aggregate));
Row_Elements.Append (List (Elements_Aggregate));
end if;

Elements_Aggregate.Clear;
Expand All @@ -577,14 +577,14 @@ package body Prettier_Ada.Documents.Builders is
-- Elements_Aggregate won't be empty. If so, add it to
-- Row_Elements.

if not Elements_Aggregate.Is_Empty then
if Elements_Aggregate.Length = 1 then
Row_Elements.Append (Elements_Aggregate.First_Element);
if not Elements_Aggregate.Is_Empty then
if Elements_Aggregate.Length = 1 then
Row_Elements.Append (Elements_Aggregate.First_Element);

else
Row_Elements.Append (Group (Elements_Aggregate));
end if;
else
Row_Elements.Append (List (Elements_Aggregate));
end if;
end if;

-- Flush this row's elements and separators
Elements.Append (Row_Elements);
Expand All @@ -593,16 +593,20 @@ package body Prettier_Ada.Documents.Builders is
end loop;
end Normalize_Table;

use type Ada.Containers.Count_Type;

begin
Normalize_Table;

return
Wrap_Command
(new Command_Type'
(Kind => Command_Alignment_Table,
(Kind => Command_Alignment_Table,
Alignment_Table_Elements => Elements,
Alignment_Table_Separators => Separators,
Alignment_Table_Must_Break => Must_Break));
Alignment_Table_Must_Break => Must_Break,
Break_Parents =>
Must_Break and Elements.Length > 1));
end Alignment_Table;

-------------------------------
Expand Down
61 changes: 59 additions & 2 deletions src/prettier_ada-documents-implementation.adb
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ package body Prettier_Ada.Documents.Implementation is
(Group_Stack : in out Document_Vector);
-- TODO: Description

procedure Break_Parent_Table
(Table_Stack : in out Document_Vector);
-- Sets Alignment_Table_Must_Break and Break_Parents to True for the last
-- element of Table_Stack.

function Fits
(Next : Print_Command_Type;
Rest_Commands : Print_Command_Type_Vector;
Expand Down Expand Up @@ -269,6 +274,27 @@ package body Prettier_Ada.Documents.Implementation is
end if;
end Break_Parent_Group;

------------------------
-- Break_Parent_Table --
------------------------

procedure Break_Parent_Table
(Table_Stack : in out Document_Vector) is
begin
if not Table_Stack.Is_Empty then
Table_Stack
.Last_Element
.Bare_Document
.Command
.Alignment_Table_Must_Break := True;
Table_Stack
.Last_Element
.Bare_Document
.Command
.Break_Parents := True;
end if;
end Break_Parent_Table;

----------
-- Fits --
----------
Expand Down Expand Up @@ -2529,6 +2555,7 @@ package body Prettier_Ada.Documents.Implementation is
procedure Propagate_Breaks (Document : Document_Type) is
Already_Visited : Document_Hashed_Set;
Group_Stack : Document_Vector;
Table_Stack : Document_Vector;

function Propagate_Breaks_On_Enter
(Document : Document_Type)
Expand Down Expand Up @@ -2559,6 +2586,7 @@ package body Prettier_Ada.Documents.Implementation is
case Document.Bare_Document.Command.Kind is
when Command_Break_Parent =>
Break_Parent_Group (Group_Stack);
Break_Parent_Table (Table_Stack);

when Command_Group =>
Group_Stack.Append (Document);
Expand All @@ -2569,6 +2597,15 @@ package body Prettier_Ada.Documents.Implementation is
end if;
Already_Visited.Insert (Document);

when Command_Alignment_Table =>
Table_Stack.Append (Document);
if Already_Visited.Contains (Document) then
return
Optional_Boolean'
(Is_Set => True, Value => False);
end if;
Already_Visited.Insert (Document);

when others =>
null;
end case;
Expand Down Expand Up @@ -2604,8 +2641,25 @@ package body Prettier_Ada.Documents.Implementation is
Group_Stack.Delete_Last;
if Group.Bare_Document.Command.Break then
Break_Parent_Group (Group_Stack);
Break_Parent_Table (Table_Stack);
end if;
end;

elsif Document.Bare_Document.Command.Kind
in Command_Alignment_Table
then
declare
Table : constant Document_Type :=
Table_Stack.Last_Element;

begin
Table_Stack.Delete_Last;
if Table.Bare_Document.Command.Break_Parents then
Break_Parent_Group (Group_Stack);
Break_Parent_Table (Table_Stack);
end if;
end;

end if;

when others =>
Expand Down Expand Up @@ -2963,7 +3017,7 @@ package body Prettier_Ada.Documents.Implementation is
.Command
.Alignment_Table_Elements
.Constant_Reference (Row_Index)
.Last_Index;
.First_Index;
Last_Column_Index : constant Positive :=
Doc
.Bare_Document
Expand Down Expand Up @@ -3095,7 +3149,10 @@ package body Prettier_Ada.Documents.Implementation is
end if;

Item.Text := Item.Text.Slice (MF, ML);
Item.Display_Width := @ - Trim_Count;
Item.Display_Width :=
(if Trim_Count > Item.Display_Width
then 0
else Item.Display_Width - Trim_Count);
end Trim;

----------
Expand Down
10 changes: 10 additions & 0 deletions src/prettier_ada-documents-implementation.ads
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,16 @@ private package Prettier_Ada.Documents.Implementation is
Alignment_Table_Elements : Document_Table;
Alignment_Table_Separators : Document_Table;
Alignment_Table_Must_Break : Boolean;
-- If True, adds a Hardline between table rows. If the table has
-- more than one row, also breaks parents (and implies that
-- Break_Parents = True).

Break_Parents : Boolean;
-- Flag used by the Propagate_Breaks procedure. If True, breaks
-- parent documents. This is initially set by the
-- Alignment_Table builder based on the Must_Break value and rows
-- count. The Propagate_Breaks procedure can change this value to
-- True if it detects Break_Parent commands inside the table rows.

when Command_Alignment_Table_Separator =>
Alignment_Table_Separator_Text : Prettier_String;
Expand Down
5 changes: 4 additions & 1 deletion src/prettier_ada-documents-json.adb
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,8 @@ package body Prettier_Ada.Documents.Json is
From_Document_Table (Command.Alignment_Table_Separators));
Result.Set_Field
("mustBreak", Command.Alignment_Table_Must_Break);
Result.Set_Field
("breakParents", Command.Break_Parents);

when Command_Alignment_Table_Separator =>
Result.Set_Field ("command", "alignmentTableSeparator");
Expand Down Expand Up @@ -935,7 +937,8 @@ package body Prettier_Ada.Documents.Json is
To_Document_Table (Get (Json, "elements")),
Alignment_Table_Separators =>
To_Document_Table (Get (Json, "separators")),
Alignment_Table_Must_Break => Get (Json, "mustBreak"));
Alignment_Table_Must_Break => Get (Json, "mustBreak"),
Break_Parents => Get (Json, "breakParents"));
end To_Command_Alignment_Table;

--------------------------------
Expand Down
Loading

0 comments on commit 8fc0463

Please sign in to comment.