Output the context of a type error with syntax highlighting (#1556)

This commit is contained in:
Simon Jakobi 2019-11-19 08:11:22 +01:00 committed by mergify[bot]
parent e12dd9adce
commit 3fdf075259
2 changed files with 43 additions and 38 deletions

View File

@ -4252,21 +4252,23 @@ data TypeError s a = TypeError
} }
instance (Eq a, Pretty s, Pretty a) => Show (TypeError s a) where instance (Eq a, Pretty s, Pretty a) => Show (TypeError s a) where
show = Pretty.renderString . Dhall.Pretty.layout . Pretty.pretty show = Pretty.renderString . Dhall.Pretty.layout . prettyTypeError
instance (Eq a, Pretty s, Pretty a, Typeable s, Typeable a) => Exception (TypeError s a) instance (Eq a, Pretty s, Pretty a, Typeable s, Typeable a) => Exception (TypeError s a)
instance (Eq a, Pretty s, Pretty a) => Pretty (TypeError s a) where instance (Eq a, Pretty s, Pretty a) => Pretty (TypeError s a) where
pretty (TypeError _ expr msg) pretty = Pretty.unAnnotate . prettyTypeError
= Pretty.unAnnotate
( "\n" prettyTypeError :: (Eq a, Pretty s, Pretty a) => TypeError s a -> Doc Ann
<> shortTypeMessage msg <> "\n" prettyTypeError (TypeError _ expr msg) =
<> source ( "\n"
) <> shortTypeMessage msg <> "\n"
where <> source
source = case expr of )
Note s _ -> pretty s where
_ -> mempty source = case expr of
Note s _ -> pretty s
_ -> mempty
{-| Wrap a type error in this exception type to censor source code and {-| Wrap a type error in this exception type to censor source code and
`Text` literals from the error message `Text` literals from the error message
@ -4425,36 +4427,39 @@ newtype DetailedTypeError s a = DetailedTypeError (TypeError s a)
deriving (Typeable) deriving (Typeable)
instance (Eq a, Pretty s, Pretty a) => Show (DetailedTypeError s a) where instance (Eq a, Pretty s, Pretty a) => Show (DetailedTypeError s a) where
show = Pretty.renderString . Dhall.Pretty.layout . Pretty.pretty show = Pretty.renderString . Dhall.Pretty.layout . prettyDetailedTypeError
instance (Eq a, Pretty s, Pretty a, Typeable s, Typeable a) => Exception (DetailedTypeError s a) instance (Eq a, Pretty s, Pretty a, Typeable s, Typeable a) => Exception (DetailedTypeError s a)
instance (Eq a, Pretty s, Pretty a) => Pretty (DetailedTypeError s a) where instance (Eq a, Pretty s, Pretty a) => Pretty (DetailedTypeError s a) where
pretty (DetailedTypeError (TypeError ctx expr msg)) pretty = Pretty.unAnnotate . prettyDetailedTypeError
= Pretty.unAnnotate
( "\n"
<> ( if null (Dhall.Context.toList ctx)
then ""
else prettyContext ctx <> "\n\n"
)
<> longTypeMessage msg <> "\n"
<> "────────────────────────────────────────────────────────────────────────────────\n"
<> "\n"
<> source
)
where
prettyKV (key, val) =
pretty key <> " : " <> Dhall.Util.snipDoc (pretty val)
prettyContext = prettyDetailedTypeError :: (Eq a, Pretty s, Pretty a) => DetailedTypeError s a -> Doc Ann
Pretty.vsep prettyDetailedTypeError (DetailedTypeError (TypeError ctx expr msg)) =
. map prettyKV ( "\n"
. reverse <> ( if null (Dhall.Context.toList ctx)
. Dhall.Context.toList then ""
else prettyContext ctx <> "\n\n"
)
<> longTypeMessage msg <> "\n"
<> "────────────────────────────────────────────────────────────────────────────────\n"
<> "\n"
<> source
)
where
prettyKV (key, val) =
Dhall.Util.snipDoc
(Dhall.Pretty.Internal.prettyLabel key <> " : " <> Dhall.Pretty.prettyExpr val)
source = case expr of prettyContext =
Note s _ -> pretty s Pretty.vsep
_ -> mempty . map prettyKV
. reverse
. Dhall.Context.toList
source = case expr of
Note s _ -> pretty s
_ -> mempty
{-| This function verifies that a custom context is well-formed so that {-| This function verifies that a custom context is well-formed so that
type-checking will not loop type-checking will not loop

View File

@ -31,8 +31,8 @@ import Dhall.Src (Src)
import qualified Control.Exception import qualified Control.Exception
import qualified Data.Text import qualified Data.Text
import qualified Data.Text.IO import qualified Data.Text.IO
import qualified Data.Text.Prettyprint.Doc as Pretty import qualified Data.Text.Prettyprint.Doc as Pretty
import qualified Data.Text.Prettyprint.Doc.Render.Text as Pretty import qualified Data.Text.Prettyprint.Doc.Render.Terminal as Pretty.Terminal
import qualified Dhall.Parser import qualified Dhall.Parser
import qualified Dhall.Pretty import qualified Dhall.Pretty
@ -81,7 +81,7 @@ snipDoc doc = Pretty.align (Pretty.pretty (snip text))
ansiStream = fmap Dhall.Pretty.annToAnsiStyle stream ansiStream = fmap Dhall.Pretty.annToAnsiStyle stream
text = Pretty.renderStrict ansiStream text = Pretty.Terminal.renderStrict ansiStream
takeEnd :: Int -> [a] -> [a] takeEnd :: Int -> [a] -> [a]
takeEnd n l = go (drop n l) l takeEnd n l = go (drop n l) l