Skip to content

Commit

Permalink
showFreeText
Browse files Browse the repository at this point in the history
  • Loading branch information
phadej committed Jan 14, 2024
1 parent d6fa927 commit 9ad1a31
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
8 changes: 5 additions & 3 deletions src/CabalFmt.hs
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,11 @@ prettyFieldLines fn pos fls =
knownField :: MonadCabalFmt r m => C.FieldName -> C.Position -> [C.FieldLine CommentsPragmas] -> m (Maybe PP.Doc)
knownField fn pos fls = do
opts <- asks (view options)
let v = optSpecVersion opts
let ft = fieldlinesToFreeText v pos (fmap (fmap fstOf3) fls)
return $ join $ fieldDescrLookup (fieldDescrs opts) fn (Just (PP.text ft)) $ \p pp ->
let v = optSpecVersion opts
let ft = fieldlinesToFreeText v pos (fmap (fmap fstOf3) fls)
let ft' = showFreeText v ft

return $ join $ fieldDescrLookup (fieldDescrs opts) fn (Just ft') $ \p pp ->
case C.runParsecParser' v p "<input>" (C.fieldLinesToStream fls) of
Right x -> Just (pp x)
Left _ -> Nothing
Expand Down
13 changes: 12 additions & 1 deletion src/CabalFmt/FreeText.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{-# LANGUAGE OverloadedStrings #-}
module CabalFmt.FreeText (
fieldlinesToFreeText,
showFreeText,
) where

import Data.List (foldl')
Expand All @@ -9,10 +10,20 @@ import qualified Distribution.CabalSpecVersion as C
import qualified Distribution.Fields.Field as C
import qualified Distribution.Parsec as C
import qualified Distribution.Parsec.Position as C
import qualified Distribution.Utils.String as C (trim)
import qualified Distribution.Pretty as C
import qualified Distribution.Utils.String as C (trim)
import qualified Text.PrettyPrint as PP

import CabalFmt.Prelude

showFreeText :: C.CabalSpecVersion -> String -> PP.Doc
showFreeText v
| v >= C.CabalSpecV3_0
= C.showFreeTextV3

| otherwise
= C.showFreeText

-- This should perfectly be exported from Cabal-syntax
fieldlinesToFreeText :: C.CabalSpecVersion -> C.Position -> [C.FieldLine C.Position] -> String
fieldlinesToFreeText v
Expand Down
6 changes: 5 additions & 1 deletion tests/golden.hs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ goldenTest' n = goldenTest n readGolden makeTest cmp writeGolden
Left err -> fail ("Second pass: " ++ show err)
Right (output'', _) -> do
unless (output' == output'') $ do
print output'
putStrLn "<<<<<<<"
putStr output'
putStrLn "======="
putStr output''
putStrLn ">>>>>>>"
fail "Output not idempotent"
return (toUTF8BS $ unlines (map ("-- " ++) ws) ++ output')

Expand Down

0 comments on commit 9ad1a31

Please sign in to comment.