Power-Fx icon indicating copy to clipboard operation
Power-Fx copied to clipboard

Public PrettyPrint (similar to TexlParser.Format)

Open modery opened this issue 1 year ago • 1 comments

I'm looking at integrating PowerFx.Core into my application so that I can format code snippets to a more user-friendly format.

I can see that https://github.com/microsoft/Power-Fx/blob/main/src/tests/Microsoft.PowerFx.Core.Tests.Shared/FormatterTests.cs#L259 has got a test that covers what I would need, however the TexlParser.Format (https://github.com/microsoft/Power-Fx/blob/main/src/libraries/Microsoft.PowerFx.Core/Parser/TexlParser.cs#L1964) function is internal only.

I also saw that TexlNode.ToString() prettifies the output, but not in the same way (https://github.com/microsoft/Power-Fx/blob/main/src/libraries/Microsoft.PowerFx.Core/Syntax/Nodes/TexlNode.cs#L278)

For example, using the following code snippet If(CountRows(Filter('Time Off Requests','Created On'>=Today()&&Owner=LookUp(Users_1,'User Name'=User().Email,User)))>0,Navigate([@'Attendance Already Submitted'],ScreenTransition.Cover),NewForm(AttendanceForm))

Running the above through TexlNode.ToString() (via Engine.Parse("...").Root.ToString()) gives me If(CountRows(Filter('Time Off Requests', 'Created On' >= Today() && Owner = LookUp(Users_1, 'User Name' = User().Email, User))) > 0, Navigate([@'Attendance Already Submitted'], ScreenTransition.Cover), NewForm(AttendanceForm)) (so some spacing has been added)

However, TexlParser.Format would return

If(
    CountRows(
        Filter(
            'Time Off Requests',
            'Created On' >= Today() && Owner = LookUp(
                Users_1,
                'User Name' = User().Email,
                User
            )
        )
    ) > 0,
    Navigate(
        [@'Attendance Already Submitted'],
        ScreenTransition.Cover
    ),
    NewForm(AttendanceForm)
)

Is there a possibility to get a public PrettyPrint function somewhere (TexlNode.PrettyPrint(), for example)?

modery avatar Aug 07 '24 14:08 modery

Totally possible! Would you like to submit a Pull request to enable this?

Possible places to expose this are either:

  • a new ToString(...) overload on TexlNode.
  • something on CheckResult. That already has helpers to anonymize it. https://github.com/microsoft/Power-Fx/blob/main/src/libraries/Microsoft.PowerFx.Core/Public/CheckResult.cs

And be sure to add a unit test.

MikeStall avatar Aug 07 '24 21:08 MikeStall