module HIndent
(
reformat
,prettyPrint
,parseMode
,Style(..)
,styles
,chrisDone
,johanTibell
,fundamental
,gibiansky
,test
,testAll
,testAst
)
where
import HIndent.Comments
import HIndent.Pretty
import HIndent.Styles.ChrisDone (chrisDone)
import HIndent.Styles.Fundamental (fundamental)
import HIndent.Styles.Gibiansky (gibiansky)
import HIndent.Styles.JohanTibell (johanTibell)
import HIndent.Types
import Control.Monad.State.Strict
import Control.Monad.Trans.Maybe
import Data.Functor.Identity
import Data.Maybe (fromMaybe)
import Data.Monoid
import qualified Data.Text.IO as ST
import Data.Text.Lazy (Text)
import qualified Data.Text.Lazy as T
import Data.Text.Lazy.Builder (Builder)
import qualified Data.Text.Lazy.Builder as T
import qualified Data.Text.Lazy.IO as T
import Language.Haskell.Exts.Annotated hiding (Style,prettyPrint,Pretty,style,parse)
reformat :: Style -> Maybe [Extension] -> Text -> Either String Builder
reformat style mexts x =
case parseModuleWithComments mode
(T.unpack x) of
ParseOk (m,comments) ->
let (cs,ast) =
annotateComments (fromMaybe m $ applyFixities baseFixities m) comments
in Right (prettyPrint
mode
style
(do mapM_ (printComment Nothing) (reverse cs)
pretty ast))
ParseFailed _ e -> Left e
where mode = (case mexts of
Just exts -> parseMode {extensions = exts}
Nothing -> parseMode)
prettyPrint :: ParseMode -> Style -> (forall s. Printer s ()) -> Builder
prettyPrint mode style m =
case style of
Style _name _author _desc st extenders config ->
maybe (error "Printer failed with mzero call.")
psOutput
(runIdentity
(runMaybeT (execStateT (runPrinter m)
(PrintState 0 mempty False 0 1 st extenders config False False mode))))
parseMode :: ParseMode
parseMode =
defaultParseMode {extensions = allExtensions
,fixities = Nothing}
where allExtensions =
filter isDisabledExtention knownExtensions
isDisabledExtention (DisableExtension _) = False
isDisabledExtention _ = True
test :: Style -> Text -> IO ()
test style =
either error (T.putStrLn . T.toLazyText) .
reformat style Nothing
testAll :: Text -> IO ()
testAll i =
forM_ styles
(\style ->
do ST.putStrLn ("-- " <> styleName style <> ":")
test style i
ST.putStrLn "")
testAst :: Text -> Either String ([ComInfo], Module NodeInfo)
testAst x =
case parseModuleWithComments parseMode
(T.unpack x) of
ParseOk (m,comments) ->
Right (annotateComments m comments)
ParseFailed _ e -> Left e
styles :: [Style]
styles =
[fundamental,chrisDone,johanTibell,gibiansky]