Skip to content

Commit

Permalink
--vscode option
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszcz committed Nov 8, 2024
1 parent 75d7167 commit f228cbb
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
8 changes: 7 additions & 1 deletion app/App.hs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,13 @@ reAppIO args@RunAppIOArgs {..} =
. mkAnsiText
. run
. runReader (project' @GenericOptions g)
$ Error.render (not (_runAppIOArgsGlobalOptions ^. globalNoColors)) (g ^. globalIdeEndErrorChar) e
$ Error.render renderType (g ^. globalIdeEndErrorChar) e
where
renderType :: Error.RenderType
renderType
| g ^. globalVSCode = Error.RenderVSCode
| g ^. globalNoColors = Error.RenderText
| otherwise = Error.RenderAnsi

getEntryPoint' ::
(Members '[App, EmbedIO, TaggedLock] r) =>
Expand Down
8 changes: 7 additions & 1 deletion app/Commands/Repl.hs
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,13 @@ catchAll = Repline.dontCrash . catchJuvixError
. hPutStrLn stderr
. run
. runReader (project' @GenericOptions opts)
$ Error.render (not (opts ^. globalNoColors) && hasAnsi) Nothing e
$ Error.render (renderType opts hasAnsi) Nothing e
where
renderType :: GlobalOptions -> Bool -> Error.RenderType
renderType opts hasAnsi
| opts ^. globalVSCode = Error.RenderVSCode
| opts ^. globalNoColors || not hasAnsi = Error.RenderText
| otherwise = Error.RenderAnsi

catchErrorS :: ReplS () -> ReplS ()
catchErrorS = (`Except.catchError` printErrorS)
Expand Down
7 changes: 7 additions & 0 deletions app/GlobalOptions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import Juvix.Data.Field

data GlobalOptions = GlobalOptions
{ _globalNoColors :: Bool,
_globalVSCode :: Bool,
_globalShowNameIds :: Bool,
_globalBuildDir :: Maybe (AppPath Dir),
_globalIdeEndErrorChar :: Maybe Char,
Expand Down Expand Up @@ -60,6 +61,7 @@ defaultGlobalOptions :: GlobalOptions
defaultGlobalOptions =
GlobalOptions
{ _globalNoColors = False,
_globalVSCode = False,
_globalNumThreads = defaultNumThreads,
_globalShowNameIds = False,
_globalIdeEndErrorChar = Nothing,
Expand All @@ -85,6 +87,11 @@ parseGlobalFlags = do
( long "no-colors"
<> help "Disable ANSI formatting"
)
_globalVSCode <-
switch
( long "vscode"
<> help "Enable VSCode compatible output"
)
_globalBuildDir <-
optional
( parseBuildDir
Expand Down
24 changes: 16 additions & 8 deletions src/Juvix/Data/Error/GenericError.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@ module Juvix.Data.Error.GenericError
)
where

import Data.Text qualified as Text
import Juvix.Data.Loc
import Juvix.Prelude.Base
import Juvix.Prelude.Pretty
import Prettyprinter.Render.Terminal qualified as Ansi
import System.Console.ANSI qualified as Ansi

data RenderType
= RenderAnsi
| RenderText
| RenderVSCode

data GenericError = GenericError
{ _genericErrorLoc :: Interval,
_genericErrorMessage :: AnsiText,
Expand Down Expand Up @@ -68,29 +74,31 @@ errorIntervals e = do
e' <- genericError e
return (e' ^. genericErrorIntervals)

render :: (ToGenericError e, Member (Reader GenericOptions) r) => Bool -> Maybe Char -> e -> Sem r Text
render ansi endChar err = do
render :: (ToGenericError e, Member (Reader GenericOptions) r) => RenderType -> Maybe Char -> e -> Sem r Text
render renderType endChar err = do
g <- genericError err
let gMsg = g ^. genericErrorMessage
header = genericErrorHeader g
helper f x = (f . layoutPretty defaultLayoutOptions) (header <> x <> lastChar)
if
| ansi -> return $ helper Ansi.renderStrict (toAnsiDoc gMsg)
| otherwise -> return $ helper renderStrict (toTextDoc gMsg)
helper f x = f (header <> x <> lastChar)
return $
case renderType of
RenderAnsi -> helper (Ansi.renderStrict . layoutPretty defaultLayoutOptions) (toAnsiDoc gMsg)
RenderText -> helper (renderStrict . layoutPretty defaultLayoutOptions) (toTextDoc gMsg)
RenderVSCode -> Text.replace "\n" " " $ helper (renderStrict . layoutCompact) (toTextDoc gMsg)
where
lastChar :: Doc a
lastChar = maybe "" pretty endChar

-- | Render the error to Text.
renderText :: (ToGenericError e, Member (Reader GenericOptions) r) => e -> Sem r Text
renderText = render False Nothing
renderText = render RenderText Nothing

renderTextDefault :: (ToGenericError e) => e -> Text
renderTextDefault = run . runReader defaultGenericOptions . renderText

-- | Render the error with Ansi formatting (if any).
renderAnsiText :: (ToGenericError e, Member (Reader GenericOptions) r) => e -> Sem r Text
renderAnsiText = render True Nothing
renderAnsiText = render RenderAnsi Nothing

printErrorAnsi :: (ToGenericError e, Members '[EmbedIO, Reader GenericOptions] r) => e -> Sem r ()
printErrorAnsi e = renderAnsiText e >>= \txt -> hPutStrLn stderr txt
Expand Down

0 comments on commit f228cbb

Please sign in to comment.