Skip to content
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

Fix behaviour with QualifiedDo #696

Merged
merged 4 commits into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

### Fixed

- ...
- Fixed module names being removed from uses of qualified `do` ([#696]).

### Removed

Expand Down Expand Up @@ -338,6 +338,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

[#696]: https://github.com/mihaimaruseac/hindent/pull/696
[#672]: https://github.com/mihaimaruseac/hindent/pull/672
[#671]: https://github.com/mihaimaruseac/hindent/pull/671
[#670]: https://github.com/mihaimaruseac/hindent/pull/670
Expand Down
23 changes: 23 additions & 0 deletions TESTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2299,6 +2299,29 @@ g = mdo
bar
```

#### `QualifiedDo`

Qualified do

```haskell
{-# LANGUAGE QualifiedDo #-}

f = Module.Path.do
a <- foo
return a
```

Qualified do with `mdo`

```haskell
{-# LANGUAGE RecursiveDo #-}
{-# LANGUAGE QualifiedDo #-}

f = Module.Path.mdo
a <- foo
return a
```

### Function applications

Long line, tuple
Expand Down
63 changes: 37 additions & 26 deletions src/HIndent/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -553,12 +553,13 @@ prettyHsExpr (HsIf _ cond t f) = do
branch :: String -> LHsExpr GhcPs -> Printer ()
branch str e =
case e of
(L _ (HsDo _ DoExpr {} xs)) -> doStmt "do" xs
(L _ (HsDo _ MDoExpr {} xs)) -> doStmt "mdo" xs
(L _ (HsDo _ (DoExpr m) xs)) -> doStmt (QualifiedDo m Do) xs
(L _ (HsDo _ (MDoExpr m) xs)) -> doStmt (QualifiedDo m Mdo) xs
_ -> string str |=> pretty e
where
doStmt pref stmts = do
string $ str ++ pref
doStmt qDo stmts = do
string str
pretty qDo
newline
indentedBlock $ printCommentsAnd stmts (lined . fmap pretty)
prettyHsExpr (HsMultiIf _ guards) =
Expand All @@ -578,8 +579,10 @@ prettyHsExpr (HsDo _ MonadComp {} (L _ [])) =
error "Not enough arguments are passed to pretty-print a list comprehension."
prettyHsExpr (HsDo _ MonadComp {} (L l (lhs:rhs))) =
pretty $ L l $ ListComprehension lhs rhs
prettyHsExpr (HsDo _ DoExpr {} (L l xs)) = pretty $ L l $ DoExpression xs Do
prettyHsExpr (HsDo _ MDoExpr {} (L l xs)) = pretty $ L l $ DoExpression xs Mdo
prettyHsExpr (HsDo _ (DoExpr m) (L l xs)) =
pretty $ L l $ DoExpression xs (QualifiedDo m Do)
prettyHsExpr (HsDo _ (MDoExpr m) (L l xs)) =
pretty $ L l $ DoExpression xs (QualifiedDo m Mdo)
prettyHsExpr (HsDo _ GhciStmtCtxt {} _) = error "We're not using GHCi, are we?"
prettyHsExpr (ExplicitList _ xs) = horizontal <-|> vertical
where
Expand Down Expand Up @@ -1165,18 +1168,18 @@ instance Pretty GRHSExpr where
space
rhsSeparator grhsExprType
case unLoc body of
HsDo _ DoExpr {} stmts ->
printCommentsAnd body (const (doExpr "do" stmts))
HsDo _ MDoExpr {} stmts ->
printCommentsAnd body (const (doExpr "mdo" stmts))
HsDo _ (DoExpr m) stmts ->
printCommentsAnd body (const (doExpr (QualifiedDo m Do) stmts))
HsDo _ (MDoExpr m) stmts ->
printCommentsAnd body (const (doExpr (QualifiedDo m Mdo) stmts))
_ ->
let hor = space >> pretty body
ver = newline >> indentedBlock (pretty body)
in hor <-|> ver
where
doExpr pref stmts = do
doExpr qDo stmts = do
space
string pref
pretty qDo
newline
indentedBlock $ printCommentsAnd stmts (lined . fmap pretty)
pretty' (GRHSExpr {grhsExpr = (GRHS _ guards body), ..}) = do
Expand All @@ -1188,16 +1191,16 @@ instance Pretty GRHSExpr where
space
rhsSeparator grhsExprType
printCommentsAnd body $ \case
HsDo _ DoExpr {} stmts -> doExpr "do" stmts
HsDo _ MDoExpr {} stmts -> doExpr "mdo" stmts
HsDo _ (DoExpr m) stmts -> doExpr (QualifiedDo m Do) stmts
HsDo _ (MDoExpr m) stmts -> doExpr (QualifiedDo m Mdo) stmts
x ->
let hor = space >> pretty x
ver = newline >> indentedBlock (pretty x)
in hor <-|> ver
where
doExpr pref stmts = do
doExpr qDo stmts = do
space
string pref
pretty qDo
let hor = space >> printCommentsAnd stmts (lined . fmap pretty)
ver = do
newline
Expand Down Expand Up @@ -1480,12 +1483,14 @@ instance Pretty InfixApp where
pretty (InfixExpr op)
return newline
case unLoc rhs of
(HsDo _ (DoExpr _) xs) -> do
string " do"
(HsDo _ (DoExpr m) xs) -> do
space
pretty (QualifiedDo m Do)
newline
indentedBlock $ printCommentsAnd xs (lined . fmap pretty)
(HsDo _ (MDoExpr _) xs) -> do
string " mdo"
(HsDo _ (MDoExpr m) xs) -> do
space
pretty (QualifiedDo m Mdo)
newline
indentedBlock $ printCommentsAnd xs (lined . fmap pretty)
HsLam {} -> do
Expand Down Expand Up @@ -2157,12 +2162,18 @@ instance Pretty ListComprehension where

instance Pretty DoExpression where
pretty' DoExpression {..} =
(string pref >> space) |=> lined (fmap pretty doStmts)
where
pref =
case doOrMdo of
Do -> "do"
Mdo -> "mdo"
(pretty qualifiedDo >> space) |=> lined (fmap pretty doStmts)

instance Pretty DoOrMdo where
pretty' Do = string "do"
pretty' Mdo = string "mdo"

instance Pretty QualifiedDo where
pretty' (QualifiedDo (Just m) d) = do
pretty m
string "."
pretty d
pretty' (QualifiedDo Nothing d) = pretty d

instance Pretty LetIn where
pretty' LetIn {..} =
Expand Down
6 changes: 6 additions & 0 deletions src/HIndent/Pretty/NodeComments.hs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ instance CommentExtraction (HsExpr GhcPs) where
instance CommentExtraction LambdaCase where
nodeComments (LambdaCase x _) = nodeComments x

instance CommentExtraction DoOrMdo where
nodeComments = const emptyNodeComments

instance CommentExtraction QualifiedDo where
nodeComments = const emptyNodeComments

nodeCommentsHsExpr :: HsExpr GhcPs -> NodeComments
nodeCommentsHsExpr HsVar {} = emptyNodeComments
nodeCommentsHsExpr (HsUnboundVar x _) = nodeComments x
Expand Down
8 changes: 7 additions & 1 deletion src/HIndent/Pretty/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ module HIndent.Pretty.Types
, ListComprehension(..)
, DoExpression(..)
, DoOrMdo(..)
, QualifiedDo(..)
, LetIn(..)
, NodeComments(..)
, GRHSExprType(..)
Expand Down Expand Up @@ -277,7 +278,7 @@ data ListComprehension = ListComprehension
-- | Use this type to pretty-print a do expression.
data DoExpression = DoExpression
{ doStmts :: [ExprLStmt GhcPs]
, doOrMdo :: DoOrMdo
, qualifiedDo :: QualifiedDo
}

-- | Use this type to pretty-print a @let ... in ...@ expression.
Expand All @@ -298,6 +299,11 @@ data DoOrMdo
= Do
| Mdo

-- | Values indicating whether the `do` is qualified with a module name (and
-- whether `do` or `mdo` is used)
data QualifiedDo =
QualifiedDo (Maybe ModuleName) DoOrMdo

-- | Values indicating in which context a RHS is located.
data GRHSExprType
= GRHSExprNormal
Expand Down