Skip to content

hardlines at the end of nest blocks #11

@Alasdair

Description

@Alasdair

Hi,

I've been working on a formatter for a language with C style line comments, and I've ran into an issue with the behavior of hardline and nest that is quite tricky to work around, the problem is if you have:

nest n (x ^^ hardline) ^^ y

then the hardline immediately outputs spaces to the nest n level causing the indentation to 'leak' out of the nest block and indent the first line of y. Obviously in the simple case this can be worked around by writing nest n x ^^ hardline ^^ y instead, but the problem is if the hardline is generated by some other function like:

let line_comment contents = string "//" ^^ string contents ^^ hardline

and it occurs in a situation like nest n (x ^^ y) ^^ z, where y eventually ends in a line comment after potentially many calls to other formatting functions. Trying to avoid this would require re-writing my formatter in a rather contorted way.

It would be more useful to me if there was a linebreak combinator that satisfies the property:

nest n (x ^^ linebreak) ^^ y == nest x ^^ linebreak ^^ y

I was able to achieve this by modifying PPrintEngine like so: Alasdair@bac7464 such that continue takes a pending hardline parameter and only prints the spaces when it encounters KCons. Modifying the behavior of hardline itself might be a breaking change for other users (and I am not sure if there are unintended consequences of that change), so perhaps a separate combinator would be better?

Best,
Alasdair

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions