Skip to content

group should be more idempotent #120

@sjakobi

Description

@sjakobi

Currently we have:

> diag $ group . group . group $ "x" <> line
Union 
    ( Cat ( Char 'x' ) ( Char ' ' ) ) 
    ( Union 
        ( Cat ( Char 'x' ) ( Char ' ' ) ) 
        ( Union 
            ( Cat ( Char 'x' ) ( Char ' ' ) ) 
            ( Cat ( Char 'x' ) 
                ( FlatAlt Line ( Char ' ' ) )
            )
        )
    )

It would be nice if we could get just

Union 
    ( Cat ( Char 'x' ) ( Char ' ' ) ) 
    ( Cat ( Char 'x' ) 
        ( FlatAlt Line ( Char ' ' ) )
    )

instead. This patch should do the trick:

@@ -523,6 +523,7 @@ hardline = Line
 -- use of it.
 group :: Doc ann -> Doc ann
 -- See note [Group: special flattening]
+group x@Union{} = x
 group x = case changesUponFlattening x of
     Nothing -> x
     Just x' -> Union x' x

But maybe we could even get this?!

Union 
    ( Cat ( Char 'x' ) ( Char ' ' ) ) 
    ( Cat ( Char 'x' ) Line )

Related: #99, #112, #115.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions