Skip to content

Commit ec311b7

Browse files
authored
Merge pull request #164 from avh4/format-infix-order
format: (kernel modules) keep infix operators in the original order
2 parents 2e163e0 + ac5aceb commit ec311b7

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

compiler/src/Gren/Format.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ formatModule (Src.Module moduleName exports docs imports values unions aliases b
228228
],
229229
formatCommentBlock (commentsAfterLine <> commentsAfterDocComment),
230230
Just $ Block.stack $ Block.blankLine :| fmap formatImport imports,
231-
infixDefs,
231+
Block.stack . (Block.blankLine :|) . pure <$> infixDefs,
232232
let defs =
233233
fmap snd $
234234
List.sortOn fst $

compiler/src/Parse/Module.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ chompInfixes infixes =
210210
binop <- Decl.infix_
211211
chompInfixes (binop : infixes)
212212
]
213-
infixes
213+
(reverse infixes)
214214

215215
-- MODULE DOC COMMENT
216216

tests/Integration/FormatSpec.hs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import Data.Text.Encoding qualified as TE
1111
import Data.Text.Lazy qualified as LazyText
1212
import Data.Text.Lazy.Encoding qualified as LTE
1313
import Format qualified
14+
import Gren.Package qualified
1415
import Parse.Module qualified as Parse
1516
import Test.Hspec
1617

@@ -318,6 +319,19 @@ spec = do
318319
"f =",
319320
" 0"
320321
]
322+
describe "operator declarations" $ do
323+
it "formats" $
324+
[ "infix left 0 (|>) = apR",
325+
"infix right 0 (<|) = apL",
326+
"f = {}"
327+
]
328+
`shouldFormatKernelModuleBodyAs` [ "infix left 0 (|>) = apR",
329+
"infix right 0 (<|) = apL",
330+
"",
331+
"",
332+
"f =",
333+
" {}"
334+
]
321335

322336
describe "expressions" $ do
323337
describe "array literals" $ do
@@ -686,10 +700,18 @@ assertFormattedModuleBody lines_ =
686700
lines_ `shouldFormatModuleBodyAs` lines_
687701

688702
shouldFormatModuleBodyAs :: [Text] -> [Text] -> IO ()
689-
shouldFormatModuleBodyAs inputLines expectedOutputLines =
703+
shouldFormatModuleBodyAs =
704+
shouldFormatModuleBodyAs_ Parse.Application
705+
706+
shouldFormatKernelModuleBodyAs :: [Text] -> [Text] -> IO ()
707+
shouldFormatKernelModuleBodyAs =
708+
shouldFormatModuleBodyAs_ (Parse.Package Gren.Package.kernel)
709+
710+
shouldFormatModuleBodyAs_ :: Parse.ProjectType -> [Text] -> [Text] -> IO ()
711+
shouldFormatModuleBodyAs_ projectType inputLines expectedOutputLines =
690712
let input = TE.encodeUtf8 $ Text.unlines inputLines
691713
expectedOutput = LazyText.unlines $ fmap LazyText.fromStrict expectedOutputLines
692-
actualOutput = LTE.decodeUtf8 . Builder.toLazyByteString <$> Format.formatByteString Parse.Application input
714+
actualOutput = LTE.decodeUtf8 . Builder.toLazyByteString <$> Format.formatByteString projectType input
693715
in case LazyText.stripPrefix "module Main exposing (..)\n\n\n\n" <$> actualOutput of
694716
Left err ->
695717
expectationFailure ("shouldFormatModuleBodyAs: failed to format" <> show err)

0 commit comments

Comments
 (0)