1
1
mirror of https://github.com/google/ormolu.git synced 2025-01-05 22:16:03 +03:00

[#52] Implement WARNING/DEPRECATED in module header

This commit is contained in:
Rupert Horlick 2019-06-15 11:15:02 +01:00 committed by Mark Karpov
parent f8b920767c
commit 7307b6debd
10 changed files with 74 additions and 0 deletions

View File

@ -0,0 +1,11 @@
module Test
{-# DEPRECATED
[ "This module is deprecated."
, "Please use OtherModule instead."
]
#-}
( foo
, bar
, baz
)
where

View File

@ -0,0 +1,8 @@
module Test {-# DEPRECATED ["This module is deprecated.",
"Please use OtherModule instead."
]#-}
( foo
, bar
, baz
)
where

View File

@ -0,0 +1,8 @@
module Test {-# DEPRECATED "This module is unstable" #-}
( foo
, bar
, baz
)
where
import Blah

View File

@ -0,0 +1,4 @@
module Test {-# DEPRECATED "This module is unstable" #-}
(foo, bar, baz) where
import Blah

View File

@ -0,0 +1 @@
module Test {-# WARNING "This module is very internal" #-} where

View File

@ -0,0 +1 @@
module Test {-# WARNING "There's only one line here." #-} where

View File

@ -0,0 +1 @@
module Test {-# WArnING ["There's only one line here."] #-} where

View File

@ -0,0 +1,2 @@
module Test {-# WARNING "This module is very internal"
#-} where

View File

@ -38,6 +38,7 @@ module Ormolu.Printer.Combinators
, parens
, parensHash
, pragmaBraces
, pragma
-- ** Literals
, comma
, space
@ -270,6 +271,19 @@ pragmaBraces m = sitcc $ do
vlayout space (newline >> txt " ")
txt "#-}"
-- | This surrounds the body in a pragma, which includes the 'pragmaBraces' and
--- the keyword for the pragma. It also takes a 'SrcSpan', which should be the
--- combined span of the body. This means multiline layouts are only triggered
--- when the body spans multiple lines, not just when the braces or pragma
--- keyword do.
pragma :: SrcSpan -> Text -> R () -> R ()
pragma srcSpan pragmaText body = switchLayout srcSpan . pragmaBraces $ do
txt pragmaText
breakpoint
inci body
-- | Surround given entity by optional space before and a newline after, iff
-- current layout is multiline.

View File

@ -1,3 +1,4 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE RecordWildCards #-}
@ -9,8 +10,10 @@ module Ormolu.Printer.Meat.Module
)
where
import BasicTypes hiding (InlinePragma)
import Control.Monad
import Data.Maybe (isJust)
import Data.Text (Text)
import GHC
import Ormolu.Imports
import Ormolu.Printer.Combinators
@ -35,6 +38,7 @@ p_hsModule loc@(L moduleSpan hsModule) = do
Nothing -> pure ()
Just hsmodName' -> line $ do
located hsmodName' p_hsmodName
maybe (pure ()) (located' p_warningTxt) hsmodDeprecMessage
case hsmodExports of
Nothing -> return ()
Just hsmodExports' -> do
@ -59,6 +63,26 @@ p_hsModule loc@(L moduleSpan hsModule) = do
when (trailingComments && isJust hsmodName) newline
spitRemainingComments
-- | Layout the WARNING/DEPRECATED pragmas in the module head
p_warningTxt :: WarningTxt -> R ()
p_warningTxt = \case
WarningTxt _ lits -> p_pragma "WARNING" lits
DeprecatedTxt _ lits -> p_pragma "DEPRECATED" lits
where
p_pragma :: Text -> [Located StringLiteral] -> R ()
p_pragma pragmaText lits = switchLayout (litsSpan lits) $ do
breakpoint
inci $ pragma (litsSpan lits) pragmaText (p_lits lits)
litsSpan :: [Located StringLiteral] -> SrcSpan
litsSpan lits = combineSrcSpans (getLoc $ head lits) (getLoc $ last lits)
p_lits :: [Located StringLiteral] -> R ()
p_lits = \case
[l] -> atom l
ls -> brackets . velt $ withSep comma atom ls
-- | Determine if these declarations should be separated by a blank line.
separatedDecls