Skip to content

Commit

Permalink
Merge pull request #202 from NixOS/print-ast
Browse files Browse the repository at this point in the history
Add an AST pretty-printing mode
  • Loading branch information
tomberek authored May 28, 2024
2 parents 3b79463 + 3a25444 commit c67a7b6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
10 changes: 8 additions & 2 deletions main/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ data Nixfmt = Nixfmt
width :: Width,
check :: Bool,
quiet :: Bool,
verify :: Bool
verify :: Bool,
ast :: Bool
}
deriving (Show, Data, Typeable)

Expand All @@ -60,7 +61,11 @@ options =
verify =
False
&= help
"Apply sanity checks on the output after formatting"
"Apply sanity checks on the output after formatting",
ast =
False
&= help
"Pretty print the internal AST, only for debugging"
}
&= summary ("nixfmt v" ++ showVersion version)
&= help "Format Nix source code"
Expand Down Expand Up @@ -128,6 +133,7 @@ toTargets Nixfmt{check = True, files = paths} = map checkFileTarget <$> collectA
type Formatter = FilePath -> Text -> Either String Text

toFormatter :: Nixfmt -> Formatter
toFormatter Nixfmt{ast = True} = Nixfmt.printAst
toFormatter Nixfmt{width, verify = True} = Nixfmt.formatVerify width
toFormatter Nixfmt{width, verify = False} = Nixfmt.format width

Expand Down
1 change: 1 addition & 0 deletions nixfmt.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ library
, scientific >= 0.3.0 && < 0.4.0
, text >= 1.2.3 && < 2.2
, transformers
, pretty-simple
default-language: Haskell2010
ghc-options:
-Wall
Expand Down
15 changes: 12 additions & 3 deletions src/Nixfmt.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@ module Nixfmt (
Width,
format,
formatVerify,
printAst,
)
where

import Data.Bifunctor (bimap, first)
import Data.Either (fromRight)
import Data.Text (Text, unpack)
import Nixfmt.Parser (file)
import Data.Text.Lazy (toStrict)
import qualified Nixfmt.Parser as Parser
import Nixfmt.Predoc (layout)
import Nixfmt.Pretty ()
import Nixfmt.Types (Expression, ParseErrorBundle, Whole (..), walkSubprograms)
import qualified Text.Megaparsec as Megaparsec (parse)
import Text.Megaparsec.Error (errorBundlePretty)
import Text.Pretty.Simple (pShow)

-- import Debug.Trace (traceShow, traceShowId)

Expand All @@ -27,7 +30,13 @@ type Width = Int
format :: Width -> FilePath -> Text -> Either String Text
format width filename =
bimap errorBundlePretty (layout width)
. Megaparsec.parse file filename
. Megaparsec.parse Parser.file filename

-- | Pretty print the internal AST for debugging
printAst :: FilePath -> Text -> Either String Text
printAst path unformatted = do
Whole unformattedParsed' _ <- first errorBundlePretty . Megaparsec.parse Parser.file path $ unformatted
Left (unpack $ toStrict $ pShow unformattedParsed')

-- Same functionality as `format`, but add sanity checks to guarantee the following properties of the formatter:
-- - Correctness: The formatted output parses, and the parse tree is identical to the input's
Expand Down Expand Up @@ -65,7 +74,7 @@ formatVerify width path unformatted = do
<> unpack (layout width (fromRight (error "TODO") $ parse $ layout width minimized))
else Right formattedOnce
where
parse = first errorBundlePretty . Megaparsec.parse file path
parse = first errorBundlePretty . Megaparsec.parse Parser.file path
pleaseReport x = path <> ": " <> x <> " This is a bug in nixfmt. Please report it at https://github.com/NixOS/nixfmt"

minimize :: Expression -> (Expression -> Bool) -> Expression
Expand Down

0 comments on commit c67a7b6

Please sign in to comment.