module Language.PureScript.Pretty.Common where
import Control.Monad.State
import Data.List (intercalate)
import Language.PureScript.Parser.Lexer (reservedPsNames, opChars)
parens :: String -> String
parens s = ('(':s) ++ ")"
newtype PrinterState = PrinterState { indent :: Int } deriving (Show, Eq, Ord)
blockIndent :: Int
blockIndent = 4
withIndent :: StateT PrinterState Maybe String -> StateT PrinterState Maybe String
withIndent action = do
modify $ \st -> st { indent = indent st + blockIndent }
result <- action
modify $ \st -> st { indent = indent st blockIndent }
return result
currentIndent :: StateT PrinterState Maybe String
currentIndent = do
current <- get
return $ replicate (indent current) ' '
prettyPrintMany :: (a -> StateT PrinterState Maybe String) -> [a] -> StateT PrinterState Maybe String
prettyPrintMany f xs = do
ss <- mapM f xs
indentString <- currentIndent
return $ intercalate "\n" $ map (indentString ++) ss
prettyPrintObjectKey :: String -> String
prettyPrintObjectKey s | s `elem` reservedPsNames = show s
| any (`elem` opChars) s = show s
| otherwise = s