Skip to content

Commit

Permalink
correct and test linebreak count for modules with documentation comme…
Browse files Browse the repository at this point in the history
…nt but without imports
  • Loading branch information
lue-bird committed Nov 3, 2024
1 parent d542190 commit b148f4a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 18 deletions.
3 changes: 3 additions & 0 deletions changes.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#### 1.0.2
- fix linebreak count for modules with documentation comment but without imports

#### 1.0.1
- fix type function `a -> (b -> c) -> d` being printed without the parens
- fix type construct `A a` being printed in one line even though an argument is not on the same line
49 changes: 31 additions & 18 deletions src/ElmSyntaxPrint.elm
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,20 @@ module_ syntaxModule =
syntaxModule.moduleDefinition
|> Elm.Syntax.Node.range
|> .end

commentsBeforeDeclarations : List String
commentsBeforeDeclarations =
case syntaxModule.declarations of
[] ->
-- invalid syntax
[]

(Elm.Syntax.Node.Node declaration0Range _) :: _ ->
commentsInRange
{ start = lastSyntaxLocationBeforeDeclarations
, end = declaration0Range.start
}
commentsAndPortDocumentationComments.comments
in
syntaxModule.moduleDefinition
|> Elm.Syntax.Node.value
Expand All @@ -123,7 +137,18 @@ module_ syntaxModule =
|> Print.followedBy
(case syntaxModule.imports of
[] ->
Print.empty
case maybeModuleDocumentation of
Nothing ->
Print.linebreak

Just _ ->
case commentsBeforeDeclarations of
[] ->
Print.linebreak
|> Print.followedBy Print.linebreak

_ :: _ ->
Print.empty

(Elm.Syntax.Node.Node import0Range import0) :: import1Up ->
(case
Expand Down Expand Up @@ -151,29 +176,17 @@ module_ syntaxModule =
|> imports commentsAndPortDocumentationComments.comments
)
|> Print.followedBy Print.linebreak
|> Print.followedBy Print.linebreak
)
|> Print.followedBy Print.linebreak
|> Print.followedBy Print.linebreak
|> Print.followedBy
(case syntaxModule.declarations of
(case commentsBeforeDeclarations of
[] ->
-- invalid syntax
Print.empty

(Elm.Syntax.Node.Node declaration0Range _) :: _ ->
case
commentsInRange
{ start = lastSyntaxLocationBeforeDeclarations
, end = declaration0Range.start
}
commentsAndPortDocumentationComments.comments
of
[] ->
Print.empty

comment0 :: comment1Up ->
moduleLevelCommentsBeforeDeclaration
{ comment0 = comment0, comment1Up = comment1Up }
comment0 :: comment1Up ->
moduleLevelCommentsBeforeDeclaration
{ comment0 = comment0, comment1Up = comment1Up }
)
|> Print.followedBy
(syntaxModule.declarations
Expand Down
22 changes: 22 additions & 0 deletions tests/Tests.elm
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,28 @@ a =
{-| A module about A.
-}


a =
"a"
"""
)
, Test.test "before comments"
(\() ->
"""module A exposing (..)
{-| A module about A.
-}
--
a =
"a\""""
|> expectPrintedAs
"""module A exposing (..)
{-| A module about A.
-}
--
a =
"a"
"""
Expand Down

0 comments on commit b148f4a

Please sign in to comment.