Skip to content

Commit 7f76991

Browse files
committed
Unparsing: fix recurse_field to account for field configurations
When processing a recurse_field template, the "fields" configuration that corresponds to the field being unparsed should be used to decorate the result of field unparsing. The initial implementation for recurse_field omitted this point: this commit fixes this omission.
1 parent af7bd9a commit 7f76991

File tree

3 files changed

+65
-19
lines changed

3 files changed

+65
-19
lines changed

langkit/support/langkit_support-generic_api-unparsing.adb

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1556,6 +1556,15 @@ package body Langkit_Support.Generic_API.Unparsing is
15561556
-- Using the unparsing configuration for N, unparse it to a Prettier
15571557
-- document.
15581558

1559+
function Unparse_Field
1560+
(Node : Lk_Node;
1561+
Node_Config : Node_Config_Record;
1562+
Child : Lk_Node;
1563+
Field_Ref : Struct_Member_Index) return Document_Type;
1564+
-- Unparse ``Child``, which is the ``Field_Ref`` field of ``Node``.
1565+
-- ``Node_Config`` must be the node unparsing configuration for
1566+
-- ``Node``.
1567+
15591568
------------------
15601569
-- Unparse_Node --
15611570
------------------
@@ -1602,22 +1611,12 @@ package body Langkit_Support.Generic_API.Unparsing is
16021611
end;
16031612

16041613
when Field_Fragment =>
1605-
declare
1606-
Field : constant Document_Type :=
1607-
Unparse_Node (F.Node);
1608-
Template : constant Template_Type :=
1609-
Node_Config.Field_Configs.Element (To_Index (F.Field));
1610-
Args : constant Template_Instantiation_Args :=
1611-
(Kind => With_Recurse, With_Recurse_Doc => Field);
1612-
begin
1613-
pragma Assert (Template.Kind = With_Recurse);
1614-
Items.Append
1615-
(Instantiate_Template
1616-
(Pool => Pool,
1617-
Node => N,
1618-
Template => Template,
1619-
Arguments => Args));
1620-
end;
1614+
Items.Append
1615+
(Unparse_Field
1616+
(Node => N,
1617+
Node_Config => Node_Config,
1618+
Child => F.Node,
1619+
Field_Ref => To_Index (F.Field)));
16211620

16221621
when List_Child_Fragment =>
16231622
Items.Append (Unparse_Node (F.Node));
@@ -1669,7 +1668,12 @@ package body Langkit_Support.Generic_API.Unparsing is
16691668
Process_Fragment (Fragment_For (Id, T));
16701669
end loop;
16711670

1672-
Items.Append (Unparse_Node (Child));
1671+
Items.Append
1672+
(Unparse_Field
1673+
(Node => N,
1674+
Node_Config => Node_Config,
1675+
Child => Child,
1676+
Field_Ref => Field_Unparser.Member));
16731677

16741678
for T of Field_Unparser.Post_Tokens.all loop
16751679
Process_Fragment (Fragment_For (Id, T));
@@ -1690,6 +1694,31 @@ package body Langkit_Support.Generic_API.Unparsing is
16901694
end case;
16911695
end Unparse_Node;
16921696

1697+
-------------------
1698+
-- Unparse_Field --
1699+
-------------------
1700+
1701+
function Unparse_Field
1702+
(Node : Lk_Node;
1703+
Node_Config : Node_Config_Record;
1704+
Child : Lk_Node;
1705+
Field_Ref : Struct_Member_Index) return Document_Type
1706+
is
1707+
Field_Template : constant Template_Type :=
1708+
Node_Config.Field_Configs.Element (Field_Ref);
1709+
pragma Assert (Field_Template.Kind = With_Recurse);
1710+
1711+
Field_Template_Args : constant Template_Instantiation_Args :=
1712+
(Kind => With_Recurse,
1713+
With_Recurse_Doc => Unparse_Node (Child));
1714+
begin
1715+
return Instantiate_Template
1716+
(Pool => Pool,
1717+
Node => Node,
1718+
Template => Field_Template,
1719+
Arguments => Field_Template_Args);
1720+
end Unparse_Field;
1721+
16931722
begin
16941723
if Config.Value = null then
16951724
raise Precondition_Failure with "null unparsing configuration";

testsuite/tests/ada_api/unparsing/cmd_recurse_field.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
{"kind": "text", "text": "="},
1616
{"kind": "recurse_field", "field": "f_value"},
1717
{"kind": "text", "text": ";"}
18-
]
18+
],
19+
"fields": {
20+
"f_value": {"kind": "indent", "contents": "recurse"}
21+
}
1922
}
2023
}
2124
}

testsuite/tests/ada_api/unparsing/test.out

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,21 @@ Done.
622622
},
623623
{
624624
"kind": "text",
625-
"text": "Int=0;"
625+
"text": "Int="
626+
},
627+
{
628+
"kind": "command",
629+
"command": {
630+
"command": "indent",
631+
"indentContents": {
632+
"kind": "text",
633+
"text": "0"
634+
}
635+
}
636+
},
637+
{
638+
"kind": "text",
639+
"text": ";"
626640
}
627641
]
628642
}

0 commit comments

Comments
 (0)