Skip to content

Commit

Permalink
Merge pull request #164 from avh4/format-infix-order
Browse files Browse the repository at this point in the history
format: (kernel modules) keep infix operators in the original order
  • Loading branch information
robinheghan authored Dec 3, 2022
2 parents 2e163e0 + ac5aceb commit ec311b7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
2 changes: 1 addition & 1 deletion compiler/src/Gren/Format.hs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ formatModule (Src.Module moduleName exports docs imports values unions aliases b
],
formatCommentBlock (commentsAfterLine <> commentsAfterDocComment),
Just $ Block.stack $ Block.blankLine :| fmap formatImport imports,
infixDefs,
Block.stack . (Block.blankLine :|) . pure <$> infixDefs,
let defs =
fmap snd $
List.sortOn fst $
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/Parse/Module.hs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ chompInfixes infixes =
binop <- Decl.infix_
chompInfixes (binop : infixes)
]
infixes
(reverse infixes)

-- MODULE DOC COMMENT

Expand Down
26 changes: 24 additions & 2 deletions tests/Integration/FormatSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Data.Text.Encoding qualified as TE
import Data.Text.Lazy qualified as LazyText
import Data.Text.Lazy.Encoding qualified as LTE
import Format qualified
import Gren.Package qualified
import Parse.Module qualified as Parse
import Test.Hspec

Expand Down Expand Up @@ -318,6 +319,19 @@ spec = do
"f =",
" 0"
]
describe "operator declarations" $ do
it "formats" $
[ "infix left 0 (|>) = apR",
"infix right 0 (<|) = apL",
"f = {}"
]
`shouldFormatKernelModuleBodyAs` [ "infix left 0 (|>) = apR",
"infix right 0 (<|) = apL",
"",
"",
"f =",
" {}"
]

describe "expressions" $ do
describe "array literals" $ do
Expand Down Expand Up @@ -686,10 +700,18 @@ assertFormattedModuleBody lines_ =
lines_ `shouldFormatModuleBodyAs` lines_

shouldFormatModuleBodyAs :: [Text] -> [Text] -> IO ()
shouldFormatModuleBodyAs inputLines expectedOutputLines =
shouldFormatModuleBodyAs =
shouldFormatModuleBodyAs_ Parse.Application

shouldFormatKernelModuleBodyAs :: [Text] -> [Text] -> IO ()
shouldFormatKernelModuleBodyAs =
shouldFormatModuleBodyAs_ (Parse.Package Gren.Package.kernel)

shouldFormatModuleBodyAs_ :: Parse.ProjectType -> [Text] -> [Text] -> IO ()
shouldFormatModuleBodyAs_ projectType inputLines expectedOutputLines =
let input = TE.encodeUtf8 $ Text.unlines inputLines
expectedOutput = LazyText.unlines $ fmap LazyText.fromStrict expectedOutputLines
actualOutput = LTE.decodeUtf8 . Builder.toLazyByteString <$> Format.formatByteString Parse.Application input
actualOutput = LTE.decodeUtf8 . Builder.toLazyByteString <$> Format.formatByteString projectType input
in case LazyText.stripPrefix "module Main exposing (..)\n\n\n\n" <$> actualOutput of
Left err ->
expectationFailure ("shouldFormatModuleBodyAs: failed to format" <> show err)
Expand Down

0 comments on commit ec311b7

Please sign in to comment.