From 73cc30c5d1564ea8846956cef2cf742a50ea003e Mon Sep 17 00:00:00 2001 From: Basile Henry Date: Sun, 16 Jun 2019 12:36:47 +0200 Subject: [PATCH] Add AnnD printer --- .../declaration/annotation/annotation-out.hs | 12 +++++++++ .../declaration/annotation/annotation.hs | 14 ++++++++++ .../value/function/explicit-type-out.hs | 5 ++++ .../value/function/explicit-type.hs | 4 +++ .../function/pattern/splice-pattern-out.hs | 2 +- .../value/function/pattern/splice-pattern.hs | 2 +- ormolu.cabal | 1 + src/Ormolu/Anns.hs | 2 +- src/Ormolu/Printer/Meat/Declaration.hs | 3 ++- .../Printer/Meat/Declaration/Annotation.hs | 27 +++++++++++++++++++ src/Ormolu/Printer/Meat/Declaration/Value.hs | 3 ++- 11 files changed, 70 insertions(+), 5 deletions(-) create mode 100644 data/examples/declaration/annotation/annotation-out.hs create mode 100644 data/examples/declaration/annotation/annotation.hs create mode 100644 data/examples/declaration/value/function/explicit-type-out.hs create mode 100644 data/examples/declaration/value/function/explicit-type.hs create mode 100644 src/Ormolu/Printer/Meat/Declaration/Annotation.hs diff --git a/data/examples/declaration/annotation/annotation-out.hs b/data/examples/declaration/annotation/annotation-out.hs new file mode 100644 index 0000000..ed567c6 --- /dev/null +++ b/data/examples/declaration/annotation/annotation-out.hs @@ -0,0 +1,12 @@ +{-# ANN module (5 :: Int) #-} +{-# ANN + module + ( 5 + :: Int + ) + #-} +{-# ANN foo "hey" #-} +{-# ANN + Char + (Just 42) + #-}{- Comment -} diff --git a/data/examples/declaration/annotation/annotation.hs b/data/examples/declaration/annotation/annotation.hs new file mode 100644 index 0000000..b73ee6a --- /dev/null +++ b/data/examples/declaration/annotation/annotation.hs @@ -0,0 +1,14 @@ +{-#ANN module (5 :: Int)#-} + +{-# ANN module (5 + :: Int)#-} + +{-# ANN foo "hey" #-} + +{-# ANN + Char + + + (Just 42)#-} + +{- Comment -} diff --git a/data/examples/declaration/value/function/explicit-type-out.hs b/data/examples/declaration/value/function/explicit-type-out.hs new file mode 100644 index 0000000..de5f8ff --- /dev/null +++ b/data/examples/declaration/value/function/explicit-type-out.hs @@ -0,0 +1,5 @@ +foo = 5 :: Int + +bar = + 5 + :: Int diff --git a/data/examples/declaration/value/function/explicit-type.hs b/data/examples/declaration/value/function/explicit-type.hs new file mode 100644 index 0000000..fb285be --- /dev/null +++ b/data/examples/declaration/value/function/explicit-type.hs @@ -0,0 +1,4 @@ +foo = 5 :: Int + +bar = 5 + :: Int diff --git a/data/examples/declaration/value/function/pattern/splice-pattern-out.hs b/data/examples/declaration/value/function/pattern/splice-pattern-out.hs index 0b66cdc..51832fe 100644 --- a/data/examples/declaration/value/function/pattern/splice-pattern-out.hs +++ b/data/examples/declaration/value/function/pattern/splice-pattern-out.hs @@ -8,5 +8,5 @@ multiline = case () of y ) -> () $( y - "something'" + "something" ) -> () diff --git a/data/examples/declaration/value/function/pattern/splice-pattern.hs b/data/examples/declaration/value/function/pattern/splice-pattern.hs index 690ead9..43e8fce 100644 --- a/data/examples/declaration/value/function/pattern/splice-pattern.hs +++ b/data/examples/declaration/value/function/pattern/splice-pattern.hs @@ -8,4 +8,4 @@ multiline = case () of $(x + y) -> () $(y - "something'") -> () + "something") -> () diff --git a/ormolu.cabal b/ormolu.cabal index 4a26441..5be7287 100644 --- a/ormolu.cabal +++ b/ormolu.cabal @@ -60,6 +60,7 @@ library , Ormolu.Printer.Internal , Ormolu.Printer.Meat.Common , Ormolu.Printer.Meat.Declaration + , Ormolu.Printer.Meat.Declaration.Annotation , Ormolu.Printer.Meat.Declaration.Class , Ormolu.Printer.Meat.Declaration.Data , Ormolu.Printer.Meat.Declaration.Default diff --git a/src/Ormolu/Anns.hs b/src/Ormolu/Anns.hs index 7f7e5c0..747c7f3 100644 --- a/src/Ormolu/Anns.hs +++ b/src/Ormolu/Anns.hs @@ -1,4 +1,4 @@ --- | Ormolu-specfiic representation of GHC annotations. +-- | Ormolu-specific representation of GHC annotations. module Ormolu.Anns ( Anns (..) diff --git a/src/Ormolu/Printer/Meat/Declaration.hs b/src/Ormolu/Printer/Meat/Declaration.hs index 2eaba55..4664f0f 100644 --- a/src/Ormolu/Printer/Meat/Declaration.hs +++ b/src/Ormolu/Printer/Meat/Declaration.hs @@ -11,6 +11,7 @@ where import GHC import Ormolu.Printer.Combinators import Ormolu.Printer.Meat.Common +import Ormolu.Printer.Meat.Declaration.Annotation import Ormolu.Printer.Meat.Declaration.Class import Ormolu.Printer.Meat.Declaration.Data import Ormolu.Printer.Meat.Declaration.Default @@ -33,7 +34,7 @@ p_hsDecl = \case DefD NoExt x -> p_defaultDecl x ForD _ _ -> notImplemented "ForD" WarningD _ _ -> notImplemented "WarningD" - AnnD _ _ -> notImplemented "AnnD" + AnnD NoExt x -> p_annDecl x RuleD _ _ -> notImplemented "RuleD" SpliceD _ _ -> notImplemented "SpliceD" DocD _ _ -> notImplemented "DocD" diff --git a/src/Ormolu/Printer/Meat/Declaration/Annotation.hs b/src/Ormolu/Printer/Meat/Declaration/Annotation.hs new file mode 100644 index 0000000..5e90d58 --- /dev/null +++ b/src/Ormolu/Printer/Meat/Declaration/Annotation.hs @@ -0,0 +1,27 @@ +{-# LANGUAGE LambdaCase #-} +{-# LANGUAGE OverloadedStrings #-} + +module Ormolu.Printer.Meat.Declaration.Annotation + ( p_annDecl + ) +where + +import GHC +import Ormolu.Printer.Combinators +import Ormolu.Printer.Meat.Common +import Ormolu.Printer.Meat.Declaration.Value +import Ormolu.Utils + +p_annDecl :: AnnDecl GhcPs -> R () +p_annDecl = \case + HsAnnotation NoExt _ annProv expr -> pragma "ANN" $ do + p_annProv annProv + breakpoint + located expr p_hsExpr + XAnnDecl {} -> notImplemented "XAnnDecl" + +p_annProv :: AnnProvenance (IdP GhcPs) -> R () +p_annProv = \case + ValueAnnProvenance name -> p_rdrName name + TypeAnnProvenance name -> p_rdrName name + ModuleAnnProvenance -> txt "module" diff --git a/src/Ormolu/Printer/Meat/Declaration/Value.hs b/src/Ormolu/Printer/Meat/Declaration/Value.hs index 15e7137..17c38b2 100644 --- a/src/Ormolu/Printer/Meat/Declaration/Value.hs +++ b/src/Ormolu/Printer/Meat/Declaration/Value.hs @@ -5,6 +5,7 @@ module Ormolu.Printer.Meat.Declaration.Value ( p_valDecl , p_pat + , p_hsExpr ) where @@ -359,7 +360,7 @@ p_hsExpr = \case located rupd_expr p_hsExpr breakpoint inci $ braces $ velt (withSep comma (located' p_hsRecField) rupd_flds) - ExprWithTySig affix x -> do + ExprWithTySig affix x -> sitcc $ do located x p_hsExpr breakpoint inci $ do