Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix showNode to display ANSI colours. #113

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Language/Cimple/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
NodeF (..), Scope (..),
UnaryOp (..), lexemeLine,
lexemeText)
import Language.Cimple.PrettyColor (black, blue, cyan, dullcyan,

Check warning on line 22 in src/Language/Cimple/Pretty.hs

View workflow job for this annotation

GitHub Actions / publish / Publish to Hackage

The import of ‘black’
dullgreen, dullmagenta, dullred,
dullyellow, underline)
import Prettyprinter
Expand Down Expand Up @@ -576,7 +576,7 @@
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
Expand Down
25 changes: 20 additions & 5 deletions test/Language/Cimple/PrettySpec.hs
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -18,17 +22,19 @@ pretty =
show
. plain
. ppTranslationUnit
. getRight
. parseText
. Text.pack
. mustParse

compact :: String -> String
compact =
flip displayS ""
. layoutCompact
. plain
. ppTranslationUnit
. getRight
. mustParse

mustParse :: String -> [Node (Lexeme Text)]
mustParse =
getRight
. parseText
. Text.pack

Expand All @@ -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; }"
Expand Down
Loading