Skip to content

Commit

Permalink
Merge pull request #135 from fourmolu/chinn/haddock-style-module
Browse files Browse the repository at this point in the history
Add haddock-style-module configuration
  • Loading branch information
brandonchinn178 committed Nov 11, 2022
2 parents 70b40c9 + ef45822 commit c550536
Show file tree
Hide file tree
Showing 17 changed files with 345 additions and 9 deletions.
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -53,6 +53,7 @@ Defaults are in bold.
| `record-brace-space` | `true`, **`false`** | `rec {x = 1}` _vs_ `rec{x = 1}`
| `newlines-between-decls` | any integer (**`1`**) | Number of newlines between top-level declarations
| `haddock-style` | `single-line`, **`multi-line`**, `multi-line-compact` | Use `-- \|`, `{- \|`, or `{-\|` for multiline haddocks (single-line haddocks always use `--`)
| `haddock-style-module` | same as `haddock-style` | `haddock-style`, but specifically for the module docstring (not specifying anything = use the same setting as `haddock-style`) |
| `let-style` | `inline`, `newline`, **`auto`**, `mixed` | How to style `let` blocks (`auto` uses `newline` if there's a newline in the input and `inline` otherwise, and `mixed` uses `inline` only when the `let` has exactly one binding)
| `in-style` | `left-align`, **`right-align`** | How to align the `in` keyword with respect to `let`
| `unicode` | `always`, `detect`, **`never`** | Output Unicode syntax. With `detect` we output Unicode syntax exactly when the extension is seen to be enabled.
Expand All @@ -76,6 +77,7 @@ indent-wheres: false
record-brace-space: false
newlines-between-decls: 1
haddock-style: multi-line
haddock-style-module:
let-style: auto
in-style: right-align
respectful: true
Expand All @@ -94,6 +96,7 @@ indent-wheres: true
record-brace-space: true
newlines-between-decls: 1
haddock-style: single-line
haddock-style-module:
let-style: inline
in-style: right-align
respectful: false
Expand Down
1 change: 1 addition & 0 deletions changelog.d/haddock-style-module.md
@@ -0,0 +1 @@
Add `haddock-style-module` option ([#135](https://github.com/fourmolu/fourmolu/pull/135))
@@ -0,0 +1,31 @@
{- | This is a test multiline
module haddock
-}
module Foo where

-- | This is a singleline function haddock
single1 :: Int

-- | This is a singleline function haddock
single2 :: Int

{- | This is a multiline
function haddock
-}
multi1 :: Int

{- |
This is a multiline
function haddock
-}
multi2 :: Int

{- | This is a haddock
with two consecutive newlines
https://github.com/fourmolu/fourmolu/issues/172
-}
foo :: Int
foo = 42
@@ -0,0 +1,31 @@
{-| This is a test multiline
module haddock
-}
module Foo where

-- | This is a singleline function haddock
single1 :: Int

-- | This is a singleline function haddock
single2 :: Int

{- | This is a multiline
function haddock
-}
multi1 :: Int

{- |
This is a multiline
function haddock
-}
multi2 :: Int

{- | This is a haddock
with two consecutive newlines
https://github.com/fourmolu/fourmolu/issues/172
-}
foo :: Int
foo = 42
@@ -0,0 +1,30 @@
-- | This is a test multiline
-- module haddock
module Foo where

-- | This is a singleline function haddock
single1 :: Int

-- | This is a singleline function haddock
single2 :: Int

{- | This is a multiline
function haddock
-}
multi1 :: Int

{- |
This is a multiline
function haddock
-}
multi2 :: Int

{- | This is a haddock
with two consecutive newlines
https://github.com/fourmolu/fourmolu/issues/172
-}
foo :: Int
foo = 42
@@ -0,0 +1,31 @@
{- | This is a test multiline
module haddock
-}
module Foo where

-- | This is a singleline function haddock
single1 :: Int

-- | This is a singleline function haddock
single2 :: Int

{-| This is a multiline
function haddock
-}
multi1 :: Int

{-|
This is a multiline
function haddock
-}
multi2 :: Int

{-| This is a haddock
with two consecutive newlines
https://github.com/fourmolu/fourmolu/issues/172
-}
foo :: Int
foo = 42
@@ -0,0 +1,31 @@
{-| This is a test multiline
module haddock
-}
module Foo where

-- | This is a singleline function haddock
single1 :: Int

-- | This is a singleline function haddock
single2 :: Int

{-| This is a multiline
function haddock
-}
multi1 :: Int

{-|
This is a multiline
function haddock
-}
multi2 :: Int

{-| This is a haddock
with two consecutive newlines
https://github.com/fourmolu/fourmolu/issues/172
-}
foo :: Int
foo = 42
@@ -0,0 +1,30 @@
-- | This is a test multiline
-- module haddock
module Foo where

-- | This is a singleline function haddock
single1 :: Int

-- | This is a singleline function haddock
single2 :: Int

{-| This is a multiline
function haddock
-}
multi1 :: Int

{-|
This is a multiline
function haddock
-}
multi2 :: Int

{-| This is a haddock
with two consecutive newlines
https://github.com/fourmolu/fourmolu/issues/172
-}
foo :: Int
foo = 42
@@ -0,0 +1,28 @@
{- | This is a test multiline
module haddock
-}
module Foo where

-- | This is a singleline function haddock
single1 :: Int

-- | This is a singleline function haddock
single2 :: Int

-- | This is a multiline
-- function haddock
multi1 :: Int

-- |
--This is a multiline
--function haddock
multi2 :: Int

-- | This is a haddock
--
-- with two consecutive newlines
--
--
-- https://github.com/fourmolu/fourmolu/issues/172
foo :: Int
foo = 42
@@ -0,0 +1,28 @@
{-| This is a test multiline
module haddock
-}
module Foo where

-- | This is a singleline function haddock
single1 :: Int

-- | This is a singleline function haddock
single2 :: Int

-- | This is a multiline
-- function haddock
multi1 :: Int

-- |
--This is a multiline
--function haddock
multi2 :: Int

-- | This is a haddock
--
-- with two consecutive newlines
--
--
-- https://github.com/fourmolu/fourmolu/issues/172
foo :: Int
foo = 42
@@ -0,0 +1,27 @@
-- | This is a test multiline
-- module haddock
module Foo where

-- | This is a singleline function haddock
single1 :: Int

-- | This is a singleline function haddock
single2 :: Int

-- | This is a multiline
-- function haddock
multi1 :: Int

-- |
--This is a multiline
--function haddock
multi2 :: Int

-- | This is a haddock
--
-- with two consecutive newlines
--
--
-- https://github.com/fourmolu/fourmolu/issues/172
foo :: Int
foo = 42
1 change: 1 addition & 0 deletions fourmolu.yaml
Expand Up @@ -7,6 +7,7 @@ indent-wheres: true
record-brace-space: true
newlines-between-decls: 1
haddock-style: single-line
haddock-style-module:
let-style: inline
in-style: right-align
unicode: never
Expand Down
22 changes: 22 additions & 0 deletions src/Ormolu/Config.hs
Expand Up @@ -34,6 +34,7 @@ module Ormolu.Config
fillMissingPrinterOpts,
CommaStyle (..),
HaddockPrintStyle (..),
HaddockPrintStyleModule (..),
ImportExportStyle (..),
LetStyle (..),
InStyle (..),
Expand Down Expand Up @@ -227,6 +228,7 @@ overFieldsM f $(unpackFieldsWithSuffix 'PrinterOpts "0") = do
poRecordBraceSpace <- f poRecordBraceSpace0
poNewlinesBetweenDecls <- f poNewlinesBetweenDecls0
poHaddockStyle <- f poHaddockStyle0
poHaddockStyleModule <- f poHaddockStyleModule0
poLetStyle <- f poLetStyle0
poInStyle <- f poInStyle0
poUnicode <- f poUnicode0
Expand Down Expand Up @@ -344,6 +346,14 @@ printerOptsMeta =
(showAllValues haddockPrintStyleMap),
metaDefault = HaddockMultiLine
},
poHaddockStyleModule =
PrinterOptsFieldMeta
{ metaName = "haddock-style-module",
metaGetField = poHaddockStyleModule,
metaPlaceholder = "STYLE",
metaHelp = "How to print module docstring",
metaDefault = PrintStyleInherit
},
poLetStyle =
PrinterOptsFieldMeta
{ metaName = "let-style",
Expand Down Expand Up @@ -482,6 +492,18 @@ instance PrinterOptsFieldType HaddockPrintStyle where
parseText = parseTextWith haddockPrintStyleMap
showText = show . showTextWith haddockPrintStyleMap

instance PrinterOptsFieldType HaddockPrintStyleModule where
parseJSON = \case
Aeson.Null -> pure PrintStyleInherit
Aeson.String "" -> pure PrintStyleInherit
v -> PrintStyleOverride <$> parseJSON v
parseText = \case
"" -> pure PrintStyleInherit
s -> PrintStyleOverride <$> parseText s
showText = \case
PrintStyleInherit -> "same as 'haddock-style'"
PrintStyleOverride x -> showText x

instance PrinterOptsFieldType ImportExportStyle where
parseJSON = parseJSONWith importExportStyleMap "ImportExportStyle"
parseText = parseTextWith importExportStyleMap
Expand Down
8 changes: 8 additions & 0 deletions src/Ormolu/Config/Types.hs
Expand Up @@ -6,6 +6,7 @@ module Ormolu.Config.Types
CommaStyle (..),
FunctionArrowsStyle (..),
HaddockPrintStyle (..),
HaddockPrintStyleModule (..),
ImportExportStyle (..),
LetStyle (..),
InStyle (..),
Expand Down Expand Up @@ -33,6 +34,8 @@ data PrinterOpts f = PrinterOpts
poNewlinesBetweenDecls :: f Int,
-- | How to print doc comments
poHaddockStyle :: f HaddockPrintStyle,
-- | How to print the module docstring (defaults to poHaddockStyle)
poHaddockStyleModule :: f HaddockPrintStyleModule,
-- | Styling of let blocks
poLetStyle :: f LetStyle,
-- | How to align in keyword
Expand Down Expand Up @@ -60,6 +63,11 @@ data HaddockPrintStyle
| HaddockMultiLineCompact
deriving (Eq, Show, Enum, Bounded)

data HaddockPrintStyleModule
= PrintStyleInherit
| PrintStyleOverride HaddockPrintStyle
deriving (Eq, Show)

data ImportExportStyle
= ImportExportLeading
| ImportExportTrailing
Expand Down

0 comments on commit c550536

Please sign in to comment.