1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
|
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE DoAndIfThenElse #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE RecordWildCards #-}
module Language.Haskell.Stylish.Printer
( Printer(..)
, PrinterConfig(..)
, PrinterState(..)
-- * Alias
, P
-- * Functions to use the printer
, runPrinter
, runPrinter_
-- ** Combinators
, comma
, dot
, getCurrentLine
, getCurrentLineLength
, newline
, parenthesize
, prefix
, putComment
, putMaybeLineComment
, putOutputable
, putCond
, putType
, putRdrName
, putText
, sep
, space
, spaces
, suffix
, pad
-- ** Advanced combinators
, withColumns
, modifyCurrentLine
, wrapping
) where
--------------------------------------------------------------------------------
import Prelude hiding (lines)
--------------------------------------------------------------------------------
import qualified GHC.Hs as GHC
import GHC.Hs.Extension (GhcPs)
import GHC.Types.Name.Reader (RdrName (..))
import GHC.Types.SrcLoc (GenLocated (..))
import qualified GHC.Types.SrcLoc as GHC
import GHC.Utils.Outputable (Outputable)
--------------------------------------------------------------------------------
import Control.Monad (forM_, replicateM_)
import Control.Monad.Reader (MonadReader, ReaderT (..),
asks, local)
import Control.Monad.State (MonadState, State, get, gets,
modify, put, runState)
import Data.List (foldl')
--------------------------------------------------------------------------------
import Language.Haskell.Stylish.GHC (showOutputable)
import Language.Haskell.Stylish.Module (Lines)
-- | Shorthand for 'Printer' monad
type P = Printer
-- | Printer that keeps state of file
newtype Printer a = Printer (ReaderT PrinterConfig (State PrinterState) a)
deriving (Applicative, Functor, Monad, MonadReader PrinterConfig, MonadState PrinterState)
-- | Configuration for printer, currently empty
data PrinterConfig = PrinterConfig
{ columns :: !(Maybe Int)
}
-- | State of printer
data PrinterState = PrinterState
{ lines :: !Lines
, linePos :: !Int
, currentLine :: !String
}
-- | Run printer to get printed lines out of module as well as return value of monad
runPrinter :: PrinterConfig -> Printer a -> (a, Lines)
runPrinter cfg (Printer printer) =
let
(a, PrinterState parsedLines _ startedLine) = runReaderT printer cfg `runState` PrinterState [] 0 ""
in
(a, parsedLines <> if startedLine == [] then [] else [startedLine])
-- | Run printer to get printed lines only
runPrinter_ :: PrinterConfig -> Printer a -> Lines
runPrinter_ cfg printer = snd (runPrinter cfg printer)
-- | Print text
putText :: String -> P ()
putText txt = do
l <- gets currentLine
modify \s -> s { currentLine = l <> txt }
-- | Check condition post action, and use fallback if false
putCond :: (PrinterState -> Bool) -> P b -> P b -> P b
putCond p action fallback = do
prevState <- get
res <- action
currState <- get
if p currState then pure res
else put prevState >> fallback
-- | Print an 'Outputable'
putOutputable :: Outputable a => a -> P ()
putOutputable = putText . showOutputable
-- | Put all comments that has positions within 'SrcSpan' and separate by
-- passed @P ()@
{-
putAllSpanComments :: P () -> SrcSpan -> P ()
putAllSpanComments suff = \case
UnhelpfulSpan _ -> pure ()
RealSrcSpan rspan -> do
cmts <- removeComments \(L rloc _) ->
srcSpanStartLine rloc >= srcSpanStartLine rspan &&
srcSpanEndLine rloc <= srcSpanEndLine rspan
forM_ cmts (\c -> putComment c >> suff)
-}
-- | Print any comment
putComment :: GHC.EpaComment -> P ()
putComment epaComment = case GHC.ac_tok epaComment of
GHC.EpaDocComment hs -> putText $ show hs
GHC.EpaLineComment s -> putText s
GHC.EpaDocOptions s -> putText s
GHC.EpaBlockComment s -> putText s
GHC.EpaEofComment -> pure ()
putMaybeLineComment :: Maybe GHC.EpaComment -> P ()
putMaybeLineComment = \case
Nothing -> pure ()
Just cmt -> space >> putComment cmt
-- | Print a 'RdrName'
putRdrName :: GenLocated GHC.SrcSpanAnnN RdrName -> P ()
putRdrName rdrName = case GHC.unLoc rdrName of
Unqual name -> do
let (pre, post) = nameAnnAdornments $
GHC.epAnnAnnsL $ GHC.ann $ GHC.getLoc rdrName
putText pre
putText (showOutputable name)
putText post
Qual modulePrefix name ->
putModuleName modulePrefix >> dot >> putText (showOutputable name)
Orig _ name ->
putText (showOutputable name)
Exact name ->
putText (showOutputable name)
nameAnnAdornments :: [GHC.NameAnn] -> (String, String)
nameAnnAdornments = foldl'
(\(accl, accr) nameAnn ->
let (l, r) = nameAnnAdornment nameAnn in (accl ++ l, r ++ accr))
(mempty, mempty)
nameAnnAdornment :: GHC.NameAnn -> (String, String)
nameAnnAdornment = \case
GHC.NameAnn {..} -> fromAdornment nann_adornment
GHC.NameAnnCommas {..} -> fromAdornment nann_adornment
GHC.NameAnnBars {..} -> fromAdornment nann_adornment
GHC.NameAnnOnly {..} -> fromAdornment nann_adornment
GHC.NameAnnRArrow {} -> (mempty, mempty)
GHC.NameAnnQuote {} -> ("'", mempty)
GHC.NameAnnTrailing {} -> (mempty, mempty)
where
fromAdornment GHC.NameParens = ("(", ")")
fromAdornment GHC.NameBackquotes = ("`", "`")
fromAdornment GHC.NameParensHash = ("#(", "#)")
fromAdornment GHC.NameSquare = ("[", "]")
-- | Print module name
putModuleName :: GHC.ModuleName -> P ()
putModuleName = putText . GHC.moduleNameString
-- | Print type
putType :: GHC.LHsType GhcPs -> P ()
putType ltp = case GHC.unLoc ltp of
GHC.HsFunTy _ arrowTp argTp funTp -> do
putOutputable argTp
space
case arrowTp of
GHC.HsUnrestrictedArrow {} -> putText "->"
GHC.HsLinearArrow {} -> putText "%1 ->"
GHC.HsExplicitMult {} -> putOutputable arrowTp
space
putType funTp
GHC.HsAppTy _ t1 t2 ->
putType t1 >> space >> putType t2
GHC.HsExplicitListTy _ _ xs -> do
putText "'["
sep
(comma >> space)
(fmap putType xs)
putText "]"
GHC.HsExplicitTupleTy _ xs -> do
putText "'("
sep
(comma >> space)
(fmap putType xs)
putText ")"
GHC.HsOpTy _ _ lhs op rhs -> do
putType lhs
space
putRdrName op
space
putType rhs
GHC.HsTyVar _ flag rdrName -> do
case flag of
GHC.IsPromoted -> putText "'"
GHC.NotPromoted -> pure ()
putRdrName rdrName
GHC.HsTyLit _ tp ->
putOutputable tp
GHC.HsParTy _ tp -> do
putText "("
putType tp
putText ")"
GHC.HsTupleTy _ _ xs -> do
putText "("
sep
(comma >> space)
(fmap putType xs)
putText ")"
GHC.HsForAllTy {} ->
putOutputable ltp
GHC.HsQualTy {} ->
putOutputable ltp
GHC.HsAppKindTy _ _ _ ->
putOutputable ltp
GHC.HsListTy _ _ ->
putOutputable ltp
GHC.HsSumTy _ _ ->
putOutputable ltp
GHC.HsIParamTy _ _ _ ->
putOutputable ltp
GHC.HsKindSig _ _ _ ->
putOutputable ltp
GHC.HsStarTy _ _ ->
putOutputable ltp
GHC.HsSpliceTy _ _ ->
putOutputable ltp
GHC.HsDocTy _ _ _ ->
putOutputable ltp
GHC.HsBangTy _ _ _ ->
putOutputable ltp
GHC.HsRecTy _ _ ->
putOutputable ltp
GHC.HsWildCardTy _ ->
putOutputable ltp
GHC.XHsType _ ->
putOutputable ltp
-- | Print a newline
newline :: P ()
newline = do
l <- gets currentLine
modify \s -> s { currentLine = "", linePos = 0, lines = lines s <> [l] }
-- | Print a space
space :: P ()
space = putText " "
-- | Print a number of spaces
spaces :: Int -> P ()
spaces i = replicateM_ i space
-- | Print a dot
dot :: P ()
dot = putText "."
-- | Print a comma
comma :: P ()
comma = putText ","
-- | Add parens around a printed action
parenthesize :: P a -> P a
parenthesize action = putText "(" *> action <* putText ")"
-- | Add separator between each element of the given printers
sep :: P a -> [P a] -> P ()
sep _ [] = pure ()
sep s (first : rest) = first >> forM_ rest ((>>) s)
-- | Prefix a printer with another one
prefix :: P a -> P b -> P b
prefix pa pb = pa >> pb
-- | Suffix a printer with another one
suffix :: P a -> P b -> P a
suffix pa pb = pb >> pa
-- | Indent to a given number of spaces. If the current line already exceeds
-- that number in length, nothing happens.
pad :: Int -> P ()
pad n = do
len <- length <$> getCurrentLine
spaces $ n - len
-- | Get current line
getCurrentLine :: P String
getCurrentLine = gets currentLine
-- | Get current line length
getCurrentLineLength :: P Int
getCurrentLineLength = fmap length getCurrentLine
modifyCurrentLine :: (String -> String) -> P ()
modifyCurrentLine f = do
s0 <- get
put s0 {currentLine = f $ currentLine s0}
wrapping
:: P a -- ^ First printer to run
-> P a -- ^ Printer to run if first printer violates max columns
-> P a -- ^ Result of either the first or the second printer
wrapping p1 p2 = do
maxCols <- asks columns
case maxCols of
-- No wrapping
Nothing -> p1
Just c -> do
s0 <- get
x <- p1
s1 <- get
if length (currentLine s1) <= c
-- No need to wrap
then pure x
else do
put s0
y <- p2
s2 <- get
if length (currentLine s1) == length (currentLine s2)
-- Wrapping didn't help!
then put s1 >> pure x
-- Wrapped
else pure y
withColumns :: Maybe Int -> P a -> P a
withColumns c = local $ \pc -> pc {columns = c}
|