From ed563ef49bb934096bd7ff384f54107676ca2d77 Mon Sep 17 00:00:00 2001 From: iphydf Date: Wed, 6 Nov 2024 17:18:52 +0000 Subject: [PATCH] fix: Fix `showNode` to display ANSI colours. Without this, tokstyle will not show colours in error messages. --- src/Language/Cimple/Pretty.hs | 2 +- test/Language/Cimple/PrettySpec.hs | 25 ++++++++++++++++++++----- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/Language/Cimple/Pretty.hs b/src/Language/Cimple/Pretty.hs index ce7f8a7..a5b4c80 100644 --- a/src/Language/Cimple/Pretty.hs +++ b/src/Language/Cimple/Pretty.hs @@ -576,7 +576,7 @@ ppTranslationUnit :: [Node (Lexeme Text)] -> Doc AnsiStyle ppTranslationUnit decls = (ppToplevel . map ppNode $ decls) <> line showNode :: Node (Lexeme Text) -> Text -showNode = Text.pack . show . ppNode +showNode = render . ppNode renderSmart :: Float -> Int -> Doc AnsiStyle -> SimpleDocStream AnsiStyle renderSmart ribbonFraction widthPerLine diff --git a/test/Language/Cimple/PrettySpec.hs b/test/Language/Cimple/PrettySpec.hs index bfaad61..f3fe0a5 100644 --- a/test/Language/Cimple/PrettySpec.hs +++ b/test/Language/Cimple/PrettySpec.hs @@ -1,11 +1,15 @@ +{-# LANGUAGE OverloadedStrings #-} module Language.Cimple.PrettySpec where import Test.Hspec (Spec, describe, it, shouldBe) +import Data.Text (Text) import qualified Data.Text as Text import qualified Data.Text.Lazy as TL +import Language.Cimple (Lexeme, Node) import Language.Cimple.IO (parseText) -import Language.Cimple.Pretty (plain, ppTranslationUnit) +import Language.Cimple.Pretty (plain, ppTranslationUnit, + showNode) import Prettyprinter (SimpleDocStream, layoutCompact) import Prettyprinter.Render.Terminal (AnsiStyle, renderLazy) @@ -18,9 +22,7 @@ pretty = show . plain . ppTranslationUnit - . getRight - . parseText - . Text.pack + . mustParse compact :: String -> String compact = @@ -28,7 +30,11 @@ compact = . layoutCompact . plain . ppTranslationUnit - . getRight + . mustParse + +mustParse :: String -> [Node (Lexeme Text)] +mustParse = + getRight . parseText . Text.pack @@ -40,6 +46,15 @@ displayS sdoc = spec :: Spec spec = do + describe "showNode" $ do + it "prints code with syntax highlighting" $ do + let pp = showNode $ head $ mustParse "int a(void) { return 3; }" + pp <> "\n" `shouldBe` Text.unlines + [ "\ESC[0;32mint\ESC[0m a(\ESC[0;32mvoid\ESC[0m) {" + , " \ESC[0;31mreturn\ESC[0m \ESC[0;31m3\ESC[0m;" + , "}" + ] + describe "renderPretty" $ do it "pretty-prints a simple C function" $ do let pp = pretty "int a(void) { return 3; }"