-
Notifications
You must be signed in to change notification settings - Fork 43
Change group not to produce a Union when the document cannot be flattened #116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6f8da2e
Change group to reveal failing Union alternatives more prominently
sjakobi cad6a23
Fail is Unflattenable
sjakobi f5362d4
Better names?
sjakobi 821f286
Return x instead of (Union Fail x)
sjakobi 7c4551a
Better names, docs
sjakobi 5a40e84
Wibble
sjakobi b1d20da
Save space
sjakobi 5531a77
Markup
sjakobi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -524,8 +524,9 @@ hardline = Line | |||||
| group :: Doc ann -> Doc ann | ||||||
| -- See note [Group: special flattening] | ||||||
| group x = case changesUponFlattening x of | ||||||
| Nothing -> x | ||||||
| Just x' -> Union x' x | ||||||
| Flattened x' -> Union x' x | ||||||
| AlreadyFlat -> x | ||||||
| NeverFlat -> x | ||||||
|
|
||||||
| -- Note [Group: special flattening] | ||||||
| -- | ||||||
|
|
@@ -540,35 +541,52 @@ group x = case changesUponFlattening x of | |||||
| -- See https://github.com/quchen/prettyprinter/issues/22 for the corresponding | ||||||
| -- ticket. | ||||||
|
|
||||||
| data FlattenResult a | ||||||
| = Flattened a | ||||||
| -- ^ @a@ is likely flatter than the input. | ||||||
| | AlreadyFlat | ||||||
| -- ^ The input was already flat, e.g. a 'Text'. | ||||||
| | NeverFlat | ||||||
| -- ^ The input couldn't be flattened: It contained a 'Line' or 'Fail'. | ||||||
|
|
||||||
| instance Functor FlattenResult where | ||||||
| fmap f (Flattened a) = Flattened (f a) | ||||||
| fmap _ AlreadyFlat = AlreadyFlat | ||||||
| fmap _ NeverFlat = NeverFlat | ||||||
|
|
||||||
| -- | Choose the first element of each @Union@, and discard the first field of | ||||||
| -- all @FlatAlt@s. | ||||||
| -- | ||||||
| -- The result is 'Just' if the element might change depending on the layout | ||||||
| -- algorithm (i.e. contains differently renderable sub-documents), and 'Nothing' | ||||||
| -- if the document is static (e.g. contains only a plain 'Empty' node). See | ||||||
| -- [Group: special flattening] for further explanations. | ||||||
| changesUponFlattening :: Doc ann -> Maybe (Doc ann) | ||||||
| -- The result is 'Flattened' if the element might change depending on the layout | ||||||
| -- algorithm (i.e. contains differently renderable sub-documents), and 'AlreadyFlat' | ||||||
| -- if the document is static (e.g. contains only a plain 'Empty' node). | ||||||
| -- 'NeverFlat' is returned when the document cannot be flattened because it | ||||||
| -- contains a hard 'Line' or 'Fail'. | ||||||
| -- See [Group: special flattening] for further explanations. | ||||||
| changesUponFlattening :: Doc ann -> FlattenResult (Doc ann) | ||||||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe this would be a better name:
Suggested change
|
||||||
| changesUponFlattening = \doc -> case doc of | ||||||
| FlatAlt _ y -> Just (flatten y) | ||||||
| Line -> Just Fail | ||||||
| Union x _ -> changesUponFlattening x <|> Just x | ||||||
| FlatAlt _ y -> Flattened (flatten y) | ||||||
| Line -> NeverFlat | ||||||
| Union x _ -> Flattened x | ||||||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||
| Nest i x -> fmap (Nest i) (changesUponFlattening x) | ||||||
| Annotated ann x -> fmap (Annotated ann) (changesUponFlattening x) | ||||||
|
|
||||||
| Column f -> Just (Column (flatten . f)) | ||||||
| Nesting f -> Just (Nesting (flatten . f)) | ||||||
| WithPageWidth f -> Just (WithPageWidth (flatten . f)) | ||||||
| Column f -> Flattened (Column (flatten . f)) | ||||||
| Nesting f -> Flattened (Nesting (flatten . f)) | ||||||
| WithPageWidth f -> Flattened (WithPageWidth (flatten . f)) | ||||||
|
|
||||||
| Cat x y -> case (changesUponFlattening x, changesUponFlattening y) of | ||||||
| (Nothing, Nothing) -> Nothing | ||||||
| (Just x', Nothing) -> Just (Cat x' y ) | ||||||
| (Nothing, Just y') -> Just (Cat x y') | ||||||
| (Just x', Just y') -> Just (Cat x' y') | ||||||
|
|
||||||
| Empty -> Nothing | ||||||
| Char{} -> Nothing | ||||||
| Text{} -> Nothing | ||||||
| Fail -> Nothing | ||||||
| (NeverFlat , _ ) -> NeverFlat | ||||||
| (_ , NeverFlat ) -> NeverFlat | ||||||
| (Flattened x' , Flattened y') -> Flattened (Cat x' y') | ||||||
| (Flattened x' , AlreadyFlat ) -> Flattened (Cat x' y) | ||||||
| (AlreadyFlat , Flattened y') -> Flattened (Cat x y') | ||||||
| (AlreadyFlat , AlreadyFlat ) -> AlreadyFlat | ||||||
|
|
||||||
| Empty -> AlreadyFlat | ||||||
| Char{} -> AlreadyFlat | ||||||
| Text{} -> AlreadyFlat | ||||||
| Fail -> NeverFlat | ||||||
| where | ||||||
| -- Flatten, but don’t report whether anything changes. | ||||||
| flatten :: Doc ann -> Doc ann | ||||||
|
|
||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The main idea of this PR is to represent the old
Nothingresult with two constructors: One to indicate that the result is already flat enough, the other to indicate that flattening is impossible, and which allows shortcuts.