Skip to content

Commit 2ca82f8

Browse files
soupipaf31
authored andcommitted
marking code in error messages with backticks (purescript#2079)
* marking code in error messages with backticks * adding 'endWith' box combinator and using it to mark code on boxes * removing backticks from multiple lines * marking code with color * refactoring prettyPrintSingleError to get an Options record. namely - PPEOptions * Checking for terminal
1 parent bbd5a83 commit 2ca82f8

File tree

12 files changed

+254
-184
lines changed

12 files changed

+254
-184
lines changed

hierarchy/Main.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ compile (HierarchyOptions inputGlob mOutput) = do
6565
input <- glob inputGlob
6666
modules <- readInput input
6767
case modules of
68-
Left errs -> hPutStr stderr (P.prettyPrintMultipleErrors False errs) >> exitFailure
68+
Left errs -> hPutStr stderr (P.prettyPrintMultipleErrors P.defaultPPEOptions errs) >> exitFailure
6969
Right ms -> do
7070
for_ ms $ \(P.Module _ _ moduleName decls _) ->
7171
let name = runModuleName moduleName

psc-docs/Main.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ docgen (PSCDocsOptions fmt inputGlob output) = do
9595
Right x ->
9696
return x
9797
Left err -> do
98-
hPutStrLn stderr $ P.prettyPrintMultipleErrors False err
98+
hPutStrLn stderr $ P.prettyPrintMultipleErrors P.defaultPPEOptions err
9999
exitFailure
100100

101101
takeByName = takeModulesByName D.modName

psc/Main.hs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import Control.Monad
1111
import Control.Monad.Writer.Strict
1212

1313
import qualified Data.Aeson as A
14+
import Data.Bool (bool)
1415
import qualified Data.ByteString.Lazy as B
1516
import qualified Data.ByteString.UTF8 as BU8
1617
import qualified Data.Map as M
@@ -24,6 +25,7 @@ import Options.Applicative as Opts
2425

2526
import qualified Paths_purescript as Paths
2627

28+
import qualified System.Console.ANSI as ANSI
2729
import System.Exit (exitSuccess, exitFailure)
2830
import System.FilePath.Glob (glob)
2931
import System.IO (hSetEncoding, hPutStrLn, stdout, stderr, utf8)
@@ -40,11 +42,13 @@ data PSCMakeOptions = PSCMakeOptions
4042
-- | Argumnets: verbose, use JSON, warnings, errors
4143
printWarningsAndErrors :: Bool -> Bool -> P.MultipleErrors -> Either P.MultipleErrors a -> IO ()
4244
printWarningsAndErrors verbose False warnings errors = do
45+
cc <- bool Nothing (Just P.defaultCodeColor) <$> ANSI.hSupportsANSI stderr
46+
let ppeOpts = P.defaultPPEOptions { P.ppeCodeColor = cc, P.ppeFull = verbose }
4347
when (P.nonEmpty warnings) $
44-
hPutStrLn stderr (P.prettyPrintMultipleWarnings verbose warnings)
48+
hPutStrLn stderr (P.prettyPrintMultipleWarnings ppeOpts warnings)
4549
case errors of
4650
Left errs -> do
47-
hPutStrLn stderr (P.prettyPrintMultipleErrors verbose errs)
51+
hPutStrLn stderr (P.prettyPrintMultipleErrors ppeOpts errs)
4852
exitFailure
4953
Right _ -> return ()
5054
printWarningsAndErrors verbose True warnings errors = do

psci/Main.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ main = getOpt >>= loop
107107
(externs, env) <- ExceptT . runMake . make $ modules
108108
return (modules, externs, env)
109109
case e of
110-
Left errs -> putStrLn (P.prettyPrintMultipleErrors False errs) >> exitFailure
110+
Left errs -> putStrLn (P.prettyPrintMultipleErrors P.defaultPPEOptions errs) >> exitFailure
111111
Right (modules, externs, env) -> do
112112
historyFilename <- getHistoryFilename
113113
let settings = defaultSettings { historyFile = Just historyFilename }

purescript.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ library
9494
build-depends: base >=4.8 && <5,
9595
aeson >= 0.8 && < 0.12,
9696
aeson-better-errors >= 0.8,
97+
ansi-terminal >= 0.6.2 && < 0.7,
9798
base-compat >=0.6.0,
9899
bower-json >= 0.8,
99100
boxes >= 0.1.4 && < 0.2.0,
@@ -303,6 +304,7 @@ executable psc
303304
build-depends: base >=4 && <5,
304305
purescript -any,
305306
aeson >= 0.8 && < 0.12,
307+
ansi-terminal >= 0.6.2 && < 0.7,
306308
base-compat >=0.6.0,
307309
bytestring -any,
308310
containers -any,

src/Language/PureScript/Errors.hs

Lines changed: 231 additions & 171 deletions
Large diffs are not rendered by default.

src/Language/PureScript/Errors/JSON.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ toJSONErrors verbose level = map (toJSONError verbose level) . P.runMultipleErro
4747
toJSONError :: Bool -> P.Level -> P.ErrorMessage -> JSONError
4848
toJSONError verbose level e =
4949
JSONError (toErrorPosition <$> sspan)
50-
(P.renderBox (P.prettyPrintSingleError verbose level False (P.stripModuleAndSpan e)))
50+
(P.renderBox (P.prettyPrintSingleError (P.PPEOptions Nothing verbose level False) (P.stripModuleAndSpan e)))
5151
(P.errorCode e)
5252
(P.wikiUri e)
5353
(P.spanName <$> sspan)

src/Language/PureScript/Interactive.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ import System.Process (readProcessWithExitCode)
4444

4545
-- | Pretty-print errors
4646
printErrors :: MonadIO m => P.MultipleErrors -> m ()
47-
printErrors = liftIO . putStrLn . P.prettyPrintMultipleErrors False
47+
printErrors = liftIO . putStrLn . P.prettyPrintMultipleErrors P.defaultPPEOptions
4848

4949
-- | This is different than the runMake in 'Language.PureScript.Make' in that it specifies the
5050
-- options and ignores the warning messages.

src/Language/PureScript/Interactive/Module.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ supportModuleIsDefined = any ((== supportModuleName) . P.getModuleName)
2626
loadModule :: FilePath -> IO (Either String [P.Module])
2727
loadModule filename = do
2828
content <- readUTF8File filename
29-
return $ either (Left . P.prettyPrintMultipleErrors False) (Right . map snd) $ P.parseModulesFromFiles id [(filename, content)]
29+
return $ either (Left . P.prettyPrintMultipleErrors P.defaultPPEOptions) (Right . map snd) $ P.parseModulesFromFiles id [(filename, content)]
3030

3131
-- |
3232
-- Load all modules.

src/Language/PureScript/Pretty/Common.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,7 @@ before b1 b2 | rows b1 > 1 = b1 // b2
153153

154154
beforeWithSpace :: Box -> Box -> Box
155155
beforeWithSpace b1 = before (b1 <> text " ")
156+
157+
-- | Place a Box on the bottom right of another
158+
endWith :: Box -> Box -> Box
159+
endWith l r = l <> vcat top [emptyBox (rows l - 1) (cols r), r]

0 commit comments

Comments
 (0)