Skip to content

Commit

Permalink
Break a long type-level list into multiple lines (#665)
Browse files Browse the repository at this point in the history
* Create the "Promoted lists" section

* Break a long type-level list into multiple lines

* Word choice
  • Loading branch information
toku-sa-n authored Jan 8, 2023
1 parent 6926b7d commit 211a1ab
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 20 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- A newline is no longer inserted after a pattern signature ([#663]).
- A type with many type applications are now broken into multiple lines.
([#664]).
- A long type-level list is broken into multiple lines. ([#665]).

### Removed

Expand Down Expand Up @@ -305,6 +306,7 @@ This version is accidentally pushlished, and is the same as 5.3.3.
[@uhbif19]: https://github.com/uhbif19
[@toku-sa-n]: https://github.com/toku-sa-n

[#665]: https://github.com/mihaimaruseac/hindent/pull/665
[#664]: https://github.com/mihaimaruseac/hindent/pull/664
[#663]: https://github.com/mihaimaruseac/hindent/pull/663
[#662]: https://github.com/mihaimaruseac/hindent/pull/662
Expand Down
52 changes: 34 additions & 18 deletions TESTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -1654,24 +1654,6 @@ foo ::

#### Promoted types

Promoted lists

```haskell
fun1 :: Def ('[ Ref s (Stored Uint32), IBool] T.:-> IBool)
fun1 = undefined

fun2 :: Def ('[ Ref s (Stored Uint32), IBool] :-> IBool)
fun2 = undefined
```

Promoted list (issue #348)

```haskell
a :: A '[ 'True]
-- nested promoted list with multiple elements.
b :: A '[ '[ 'True, 'False], '[ 'False, 'True]]
```

Class constraints should leave `::` on same line

``` haskell
Expand Down Expand Up @@ -1708,6 +1690,40 @@ c :: '(:->) 'True 'False
d :: (:->) 'True 'False
```

##### Promoted lists

Short

```haskell
fun1 :: Def ('[ Ref s (Stored Uint32), IBool] T.:-> IBool)
fun1 = undefined

fun2 :: Def ('[ Ref s (Stored Uint32), IBool] :-> IBool)
fun2 = undefined
```

Long

```haskell
-- https://github.com/mihaimaruseac/hindent/issues/522
type OurContext
= '[ AuthHandler W.Request (ExtendedPayloadWrapper UserSession)
, BasicAuthCheck GameInstanceId
, BasicAuthCheck (RegionId, RegionName)
, BasicAuthCheck Alert.SourceId
, M.MultipartOptions M.Tmp
]
```

Nested

```haskell
-- https://github.com/mihaimaruseac/hindent/issues/348
a :: A '[ 'True]
-- nested promoted list with multiple elements.
b :: A '[ '[ 'True, 'False], '[ 'False, 'True]]
```

#### Symbol type constructors

Infix
Expand Down
2 changes: 1 addition & 1 deletion src/HIndent/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1081,7 +1081,7 @@ prettyHsType (HsRecTy _ xs) = hvFields $ fmap pretty xs
prettyHsType (HsExplicitListTy _ _ xs) =
case xs of
[] -> string "'[]"
_ -> hPromotedList $ fmap pretty xs
_ -> hvPromotedList $ fmap pretty xs
prettyHsType (HsExplicitTupleTy _ xs) = hPromotedTuple $ fmap pretty xs
prettyHsType (HsTyLit _ x) = pretty x
prettyHsType HsWildCardTy {} = string "_"
Expand Down
12 changes: 11 additions & 1 deletion src/HIndent/Pretty/Combinators/Lineup.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module HIndent.Pretty.Combinators.Lineup
, -- * Lists
hList
, vList
, hPromotedList
, hvPromotedList
, -- * Bars
hvBarSep
, hBarSep
Expand Down Expand Up @@ -135,10 +135,20 @@ hList = brackets . hCommaSep
vList :: [Printer ()] -> Printer ()
vList = vCommaSepWrapped ("[", "]")

-- | Runs printers to construct a promoted list where elements are aligned
-- in a line or vertically.
hvPromotedList :: [Printer ()] -> Printer ()
hvPromotedList = (<-|>) <$> hPromotedList <*> vPromotedList

-- | Runs printers to construct a promoted list in a line.
hPromotedList :: [Printer ()] -> Printer ()
hPromotedList = promotedListBrackets . hCommaSep

-- | Runs printers to construct a promoted list where elements are aligned
-- vertically.
vPromotedList :: [Printer ()] -> Printer ()
vPromotedList = vCommaSepWrapped ("'[", " ]")

-- | Runs printers in a line with a space as the separator.
spaced :: [Printer ()] -> Printer ()
spaced = inter space
Expand Down

0 comments on commit 211a1ab

Please sign in to comment.