2017-03-06 23:19:57 +00:00
|
|
|
-- ghc-mod: Happy Haskell Hacking
|
2015-03-03 20:12:43 +00:00
|
|
|
-- Copyright (C) 2015 Daniel Gröber <dxld ÄT darkboxed DOT org>
|
|
|
|
--
|
|
|
|
-- This program is free software: you can redistribute it and/or modify
|
|
|
|
-- it under the terms of the GNU Affero General Public License as published by
|
|
|
|
-- the Free Software Foundation, either version 3 of the License, or
|
|
|
|
-- (at your option) any later version.
|
|
|
|
--
|
|
|
|
-- This program is distributed in the hope that it will be useful,
|
|
|
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
-- GNU Affero General Public License for more details.
|
|
|
|
--
|
|
|
|
-- You should have received a copy of the GNU Affero General Public License
|
|
|
|
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-12-15 18:16:37 +00:00
|
|
|
module Language.Haskell.GhcMod.Pretty
|
|
|
|
( render
|
|
|
|
, renderSDoc
|
|
|
|
, gmComponentNameDoc
|
|
|
|
, gmLogLevelDoc
|
|
|
|
, (<+>:)
|
|
|
|
, fnDoc
|
|
|
|
, showToDoc
|
|
|
|
, warnDoc
|
|
|
|
, strLnDoc
|
|
|
|
, strDoc
|
|
|
|
) where
|
2015-03-03 20:12:43 +00:00
|
|
|
|
|
|
|
import Control.Arrow hiding ((<+>))
|
2015-03-04 20:48:21 +00:00
|
|
|
import Data.Char
|
|
|
|
import Data.List
|
2015-03-15 19:48:55 +00:00
|
|
|
import Distribution.Helper
|
2016-12-13 00:40:05 +00:00
|
|
|
import Pretty
|
|
|
|
import GHC
|
|
|
|
import Outputable (SDoc, withPprStyleDoc)
|
2015-03-03 20:12:43 +00:00
|
|
|
|
|
|
|
import Language.Haskell.GhcMod.Types
|
2016-12-13 00:40:05 +00:00
|
|
|
import Language.Haskell.GhcMod.Doc
|
2016-12-15 18:16:37 +00:00
|
|
|
import Language.Haskell.GhcMod.Gap (render)
|
2016-12-13 00:40:05 +00:00
|
|
|
|
|
|
|
renderSDoc :: GhcMonad m => SDoc -> m Doc
|
|
|
|
renderSDoc sdoc = do
|
|
|
|
df <- getSessionDynFlags
|
|
|
|
ppsty <- getStyle
|
|
|
|
return $ withPprStyleDoc df ppsty sdoc
|
2015-03-03 20:12:43 +00:00
|
|
|
|
2015-03-15 19:48:55 +00:00
|
|
|
gmComponentNameDoc :: ChComponentName -> Doc
|
|
|
|
gmComponentNameDoc ChSetupHsName = text $ "Setup.hs"
|
2016-05-11 13:13:19 +00:00
|
|
|
gmComponentNameDoc (ChLibName "") = text $ "library"
|
|
|
|
gmComponentNameDoc (ChLibName n) = text $ "library:" ++ n
|
2015-03-15 19:48:55 +00:00
|
|
|
gmComponentNameDoc (ChExeName n) = text $ "exe:" ++ n
|
|
|
|
gmComponentNameDoc (ChTestName n) = text $ "test:" ++ n
|
|
|
|
gmComponentNameDoc (ChBenchName n) = text $ "bench:" ++ n
|
2015-03-03 20:12:43 +00:00
|
|
|
|
|
|
|
gmLogLevelDoc :: GmLogLevel -> Doc
|
2015-05-06 14:32:53 +00:00
|
|
|
gmLogLevelDoc GmSilent = error "GmSilent MUST not be used for log messages"
|
2015-03-03 20:12:43 +00:00
|
|
|
gmLogLevelDoc GmPanic = text "PANIC"
|
|
|
|
gmLogLevelDoc GmException = text "EXCEPTION"
|
|
|
|
gmLogLevelDoc GmError = text "ERROR"
|
|
|
|
gmLogLevelDoc GmWarning = text "Warning"
|
|
|
|
gmLogLevelDoc GmInfo = text "info"
|
|
|
|
gmLogLevelDoc GmDebug = text "DEBUG"
|
2015-08-03 06:09:24 +00:00
|
|
|
gmLogLevelDoc GmVomit = text "VOMIT"
|
2015-03-03 20:12:43 +00:00
|
|
|
|
|
|
|
infixl 6 <+>:
|
|
|
|
(<+>:) :: Doc -> Doc -> Doc
|
|
|
|
a <+>: b = (a <> colon) <+> b
|
|
|
|
|
|
|
|
fnDoc :: FilePath -> Doc
|
|
|
|
fnDoc = doubleQuotes . text
|
|
|
|
|
2016-12-15 18:16:37 +00:00
|
|
|
showToDoc :: Show a => a -> Doc
|
|
|
|
showToDoc = strLnDoc . show
|
2015-03-03 20:12:43 +00:00
|
|
|
|
|
|
|
warnDoc :: Doc -> Doc
|
|
|
|
warnDoc d = text "Warning" <+>: d
|
|
|
|
|
2015-08-18 02:54:10 +00:00
|
|
|
strLnDoc :: String -> Doc
|
|
|
|
strLnDoc str = doc (dropWhileEnd isSpace str)
|
|
|
|
where
|
|
|
|
doc = lines >>> map text >>> foldr ($+$) empty
|
|
|
|
|
2015-03-03 20:12:43 +00:00
|
|
|
strDoc :: String -> Doc
|
2015-03-04 20:48:21 +00:00
|
|
|
strDoc str = doc (dropWhileEnd isSpace str)
|
2015-03-03 20:12:43 +00:00
|
|
|
where
|
|
|
|
doc :: String -> Doc
|
|
|
|
doc = lines
|
|
|
|
>>> map (words >>> map text >>> fsep)
|
|
|
|
>>> \l -> case l of (x:xs) -> hang x 4 (vcat xs); [] -> empty
|