@@ -8,7 +8,9 @@ module Ormolu.Printer.Meat.Declaration.Value
10import Bag (bagToList)
10import Bag (bagToList)
11import Control.Monad
12import Control.Monad
12import Data.List (sortOn)
14import Data.List (sortOn)
14import Ormolu.Printer.Combinators
16import Ormolu.Printer.Combinators
@@ -18,6 +20,7 @@ import Ormolu.Printer.Meat.Declaration.Pat
18import Ormolu.Printer.Meat.Declaration.Signature
20import Ormolu.Printer.Meat.Declaration.Signature
19import Ormolu.Utils
21import Ormolu.Utils
20import SrcLoc (isOneLineSpan)
22import SrcLoc (isOneLineSpan)
23+import Outputable (Outputable (..))
21import qualified Data.List.NonEmpty as NE
24import qualified Data.List.NonEmpty as NE
23p_valDecl :: HsBindLR GhcPs GhcPs -> R ()
26p_valDecl :: HsBindLR GhcPs GhcPs -> R ()
@@ -108,25 +111,32 @@ p_match style m_pats m_grhss = do
108 case style of
111 case style of
109 Lambda -> return ()
112 Lambda -> return ()
110 _ -> breakpoint
113 _ -> breakpoint
111− newlineSep (located' p_grhs) grhssGRHSs
114+ newlineSep (located' (p_grhs Pattern)) grhssGRHSs
112 unless (GHC.isEmptyLocalBindsPR (unL grhssLocalBinds)) $ do
115 unless (GHC.isEmptyLocalBindsPR (unL grhssLocalBinds)) $ do
114 line (txt "where")
117 line (txt "where")
115 inci (located grhssLocalBinds p_hsLocalBinds)
118 inci (located grhssLocalBinds p_hsLocalBinds)
117−p_grhs :: GRHS GhcPs (LHsExpr GhcPs) -> R ()
124+p_grhs :: GroupStyle -> GRHS GhcPs (LHsExpr GhcPs) -> R ()
118−p_grhs (GRHS NoExt guards body) =
125+p_grhs style (GRHS NoExt guards body) =
119 case guards of
126 case guards of
120 [] -> p_body
127 [] -> p_body
123 velt $ withSep comma (located' p_stmt) xs
130 velt $ withSep comma (located' p_stmt) xs
125 breakpoint
135 breakpoint
126 inci p_body
136 inci p_body
128 p_body = located body p_hsExpr
138 p_body = located body p_hsExpr
129−p_grhs (XGRHS NoExt) = notImplemented "XGRHS"
139+p_grhs _ (XGRHS NoExt) = notImplemented "XGRHS"
131p_stmt :: Stmt GhcPs (LHsExpr GhcPs) -> R ()
141p_stmt :: Stmt GhcPs (LHsExpr GhcPs) -> R ()
132p_stmt = \case
142p_stmt = \case
@@ -156,6 +166,22 @@ p_hsLocalBinds = \case
156 EmptyLocalBinds NoExt -> return ()
166 EmptyLocalBinds NoExt -> return ()
157 XHsLocalBindsLR _ -> notImplemented "XHsLocalBindsLR"
167 XHsLocalBindsLR _ -> notImplemented "XHsLocalBindsLR"
170+ :: (Data id, Outputable id)
171+ => HsRecField' id (LHsExpr GhcPs)
173+p_hsRecField = \HsRecField {..} -> do
174+ located hsRecFieldLbl atom
175+ unless hsRecPun $ do
177+ located hsRecFieldArg p_hsExpr
179+p_hsTupArg :: HsTupArg GhcPs -> R ()
181+ Present NoExt x -> located x p_hsExpr
182+ Missing NoExt -> pure ()
183+ XTupArg {} -> notImplemented "XTupArg"
159p_hsExpr :: HsExpr GhcPs -> R ()
185p_hsExpr :: HsExpr GhcPs -> R ()
160p_hsExpr = \case
186p_hsExpr = \case
161 HsVar NoExt name -> p_rdrName name
187 HsVar NoExt name -> p_rdrName name
@@ -166,7 +192,9 @@ p_hsExpr = \case
166 Unambiguous NoExt name -> p_rdrName name
192 Unambiguous NoExt name -> p_rdrName name
167 Ambiguous NoExt name -> p_rdrName name
193 Ambiguous NoExt name -> p_rdrName name
168 XAmbiguousFieldOcc NoExt -> notImplemented "XAmbiguousFieldOcc"
194 XAmbiguousFieldOcc NoExt -> notImplemented "XAmbiguousFieldOcc"
169− HsOverLabel NoExt _ _ -> notImplemented "HsOverLabel"
195+ HsOverLabel NoExt _ v -> do
170 HsIPVar NoExt (HsIPName name) -> atom name
198 HsIPVar NoExt (HsIPName name) -> atom name
171 HsOverLit NoExt v -> atom (ol_val v)
199 HsOverLit NoExt v -> atom (ol_val v)
172 HsLit NoExt lit -> atom lit
200 HsLit NoExt lit -> atom lit
@@ -197,10 +225,26 @@ p_hsExpr = \case
198 located e p_hsExpr
226 located e p_hsExpr
199 HsPar NoExt e -> parens (located e p_hsExpr)
227 HsPar NoExt e -> parens (located e p_hsExpr)
200− SectionL {} -> notImplemented "SectionL"
228+ SectionL NoExt x op -> do
201− SectionR {} -> notImplemented "SectionR"
229+ located x p_hsExpr
202− ExplicitTuple {} -> notImplemented "ExplicitTuple"
230+ breakpoint
203− ExplicitSum {} -> notImplemented "ExplicitSum"
231+ inci (located op p_hsExpr)
232+ SectionR NoExt op x -> do
235+ inci (located x p_hsExpr)
236+ ExplicitTuple NoExt args boxity -> do
240+ Unboxed -> parensHash
241+ parens' $ velt (withSep comma (located' p_hsTupArg) args)
242+ ExplicitSum NoExt tag arity x -> do
243+ let before = tag - 1
244+ after = arity - before - 1
245+ args = replicate before Nothing <> [Just x] <> replicate after Nothing
247+ velt (withSep (txt "| ") (maybe (pure ()) (located' p_hsExpr)) args)
204 HsCase NoExt e mgroup -> do
248 HsCase NoExt e mgroup -> do
205 txt "case "
249 txt "case "
206 located e p_hsExpr
250 located e p_hsExpr
@@ -220,7 +264,11 @@ p_hsExpr = \case
220 located else' $ \x -> do
264 located else' $ \x -> do
221 breakpoint
265 breakpoint
222 inci (p_hsExpr x)
266 inci (p_hsExpr x)
223− HsMultiIf {} -> notImplemented "MulitiIf"
267+ HsMultiIf NoExt guards -> do
269+ inci $ forM_ guards $ \g -> do
271+ located g (p_grhs MultiIf)
224 HsLet NoExt localBinds e -> do
272 HsLet NoExt localBinds e -> do
225 txt "let "
273 txt "let "
226 sitcc (located localBinds p_hsLocalBinds)
274 sitcc (located localBinds p_hsLocalBinds)
@@ -230,12 +278,60 @@ p_hsExpr = \case
230 HsDo {} -> notImplemented "HsDo"
278 HsDo {} -> notImplemented "HsDo"
231 ExplicitList _ _ xs -> do
279 ExplicitList _ _ xs -> do
232 brackets $ velt (withSep comma (located' p_hsExpr) xs)
280 brackets $ velt (withSep comma (located' p_hsExpr) xs)
233− RecordCon {} -> notImplemented "RecordCon"
281+ RecordCon {..} -> do
282+ located rcon_con_name atom
284+ let HsRecFields {..} = rcon_flds
285+ fields = located' p_hsRecField <$> rec_flds
288+ Just {} -> [txt ".."]
290+ inci $ braces $ velt (withSep comma id (fields <> dotdot))
234− RecordUpd {} -> notImplemented "RecordUpd"
291+ RecordUpd {..} -> do
292+ located rupd_expr p_hsExpr
294+ inci $ braces $ velt (withSep comma (located' p_hsRecField) rupd_flds)
235− ExprWithTySig {} -> notImplemented "ExprWithTySig"
295+ ExprWithTySig affix x -> do
236− ArithSeq {} -> notImplemented "ArithSeq"
296+ located x p_hsExpr
237− HsSCC {} -> notImplemented "HsSCC"
297+ breakpoint
238− HsCoreAnn {} -> notImplemented "HsCoreAnn"
298+ inci $ do
300+ let HsWC {..} = affix
301+ HsIB {..} = hswc_body
302+ located hsib_body p_hsType
303+ ArithSeq NoExt _ x ->
305+ From from -> brackets $ do
306+ located from p_hsExpr
309+ FromThen from next -> brackets $ do
310+ velt (withSep comma (located' p_hsExpr) [from, next])
313+ FromTo from to -> brackets $ do
314+ located from p_hsExpr
318+ FromThenTo from next to -> brackets $ do
319+ velt (withSep comma (located' p_hsExpr) [from, next])
323+ HsSCC NoExt _ name x -> do
329+ HsCoreAnn NoExt _ value x -> do
239 HsBracket {} -> notImplemented "HsBracket"
335 HsBracket {} -> notImplemented "HsBracket"
240 HsRnBracketOut {} -> notImplemented "HsRnBracketOut"
336 HsRnBracketOut {} -> notImplemented "HsRnBracketOut"
241 HsTcBracketOut {} -> notImplemented "HsTcBracketOut"
337 HsTcBracketOut {} -> notImplemented "HsTcBracketOut"
@@ -244,13 +340,13 @@ p_hsExpr = \case
244 HsStatic _ e -> do
340 HsStatic _ e -> do
245 txt "static"
341 txt "static"
246 breakpoint
342 breakpoint
247− inci $ located e p_hsExpr
343+ inci (located e p_hsExpr)
248 HsArrApp {} -> notImplemented "HsArrApp"
344 HsArrApp {} -> notImplemented "HsArrApp"
249 HsArrForm {} -> notImplemented "HsArrForm"
345 HsArrForm {} -> notImplemented "HsArrForm"
250 HsTick {} -> notImplemented "HsTick"
346 HsTick {} -> notImplemented "HsTick"
251 HsBinTick {} -> notImplemented "HsBinTick"
347 HsBinTick {} -> notImplemented "HsBinTick"
252 HsTickPragma {} -> notImplemented "HsTickPragma"
348 HsTickPragma {} -> notImplemented "HsTickPragma"
253− EWildPat NoExt -> notImplemented "EWildPat"
349+ EWildPat NoExt -> txt "_"
254 EAsPat {} -> notImplemented "EAsPat"
350 EAsPat {} -> notImplemented "EAsPat"
255 EViewPat {} -> notImplemented "EViewPat"
351 EViewPat {} -> notImplemented "EViewPat"
256 ELazyPat {} -> notImplemented "ELazyPat"
352 ELazyPat {} -> notImplemented "ELazyPat"