Skip to content

Commit

Permalink
Always float comments to the top of field values (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
tfausak authored Jun 19, 2024
1 parent c3f6a93 commit ba8b2e5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
34 changes: 24 additions & 10 deletions source/library/CabalGild/Unstable/Action/FormatFields.hs
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ field ::
Fields.Field (p, [c]) ->
Fields.Field (p, [c])
field csv f = case f of
Fields.Field n fls -> case Map.lookup (Name.value n) parsers of
Nothing -> f
Just spp ->
let position =
maybe (fst $ Name.annotation n) (fst . FieldLine.annotation) $
Maybe.listToMaybe fls
in Fields.Field n $ fieldLines csv position fls spp
Fields.Field n fls ->
let position =
maybe (fst $ Name.annotation n) (fst . FieldLine.annotation) $
Maybe.listToMaybe fls
in Fields.Field n $ case Map.lookup (Name.value n) parsers of
Nothing -> floatComments position fls
Just spp -> fieldLines csv position fls spp
Fields.Section n sas fs ->
let result =
Parsec.runParsecParser' csv (Condition.parseCondition Variable.parseVariable) "<conditional>"
Expand Down Expand Up @@ -93,14 +93,28 @@ fieldLines ::
[Fields.FieldLine (p, [c])]
fieldLines csv position fls SPP.SomeParsecParser {SPP.parsec = parsec, SPP.pretty = pretty} =
case Parsec.runParsecParser' csv parsec "" $ FieldLine.toFieldLineStream fls of
Left _ -> fls
Left _ -> floatComments position fls
Right r ->
fmap (\(c, l) -> Fields.FieldLine c $ String.toUtf8 l)
. zip ((,) position <$> concatMap (snd . FieldLine.annotation) fls : repeat [])
zipWith
(\b l -> Fields.FieldLine (position, if b then collectComments fls else []) $ String.toUtf8 l)
(True : repeat False)
. lines
. PrettyPrint.renderStyle style
$ pretty csv r

floatComments ::
p ->
[Fields.FieldLine (p, [c])] ->
[Fields.FieldLine (p, [c])]
floatComments p fls =
zipWith
(\b -> Fields.FieldLine (p, if b then collectComments fls else []) . FieldLine.value)
(True : repeat False)
fls

collectComments :: [Fields.FieldLine (p, [c])] -> [c]
collectComments = concatMap (snd . FieldLine.annotation)

-- | This style attempts to force everything to be on its own line.
style :: PrettyPrint.Style
style =
Expand Down
16 changes: 13 additions & 3 deletions source/test-suite/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ main = Hspec.hspec . Hspec.parallel . Hspec.describe "cabal-gild" $ do
Hspec.it "formats a comment in a field's value" $ do
expectGilded
"f:\n 1\n -- c\n 2"
"f:\n 1\n -- c\n 2\n"
"f:\n -- c\n 1\n 2\n"

Hspec.it "formats a comment after a field's value" $ do
expectGilded
Expand Down Expand Up @@ -512,12 +512,12 @@ main = Hspec.hspec . Hspec.parallel . Hspec.describe "cabal-gild" $ do
Hspec.it "does not insert extra blank lines before comments" $ do
expectGilded
"cabal-version: 3.0\ndescription:\n -- c\n 1\n -- d\n 2"
"cabal-version: 3.0\ndescription:\n -- c\n 1\n -- d\n 2\n"
"cabal-version: 3.0\ndescription:\n -- c\n -- d\n 1\n 2\n"

Hspec.it "does not consider comments for indentation" $ do
expectGilded
"cabal-version: 3.0\ndescription:\n 1\n -- c\n 2"
"cabal-version: 3.0\ndescription:\n 1\n -- c\n 2\n"
"cabal-version: 3.0\ndescription:\n -- c\n 1\n 2\n"

Hspec.it "properly formats conditionals" $ do
expectGilded
Expand Down Expand Up @@ -1517,6 +1517,16 @@ main = Hspec.hspec . Hspec.parallel . Hspec.describe "cabal-gild" $ do
"-- cabal-gild: discover\nlicense-files:"
"-- cabal-gild: discover\nlicense-files: example.txt\n"

Hspec.it "floats comments on unknown fields" $ do
expectGilded
"unknown-field:\n the\n -- some comment\n value"
"unknown-field:\n -- some comment\n the\n value\n"

Hspec.it "floats comments when parsing field fails" $ do
expectGilded
"build-depends:\n >> no\n -- comment\n parse"
"build-depends:\n -- comment\n >> no\n parse\n"

Hspec.around_ withTemporaryDirectory
. Hspec.it "discovers modules on the file system"
$ do
Expand Down

0 comments on commit ba8b2e5

Please sign in to comment.