Skip to content

Commit

Permalink
Allow nice and fancy algorithms to add modules according to whether e…
Browse files Browse the repository at this point in the history
…xisting modules in target field are separated by spaces instead of commas

Add tests which verify correct behaviour
  • Loading branch information
Jana Chadt committed Oct 16, 2024
1 parent 45960ef commit 5f8fbe8
Show file tree
Hide file tree
Showing 3 changed files with 302 additions and 16 deletions.
4 changes: 3 additions & 1 deletion cabal-add.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,10 @@ test-suite cabal-add-tests

test-suite cabal-add-unit-tests
type: exitcode-stdio-1.0
hs-source-dirs: tests/
main-is: UnitTests.hs
hs-source-dirs: tests/
default-language: GHC2021
ghc-options: -Wall
build-depends:
base <5,
bytestring,
Expand Down
27 changes: 16 additions & 11 deletions src/Distribution/Client/Add.hs
Original file line number Diff line number Diff line change
Expand Up @@ -409,11 +409,15 @@ isFieldWithName name = \case
Field (Name _ fieldName) _ -> name == fieldName
_ -> False

detectLeadingComma :: ByteString -> Maybe ByteString
detectLeadingComma xs = case B.uncons xs of
detectLeadingSeparator :: ByteString -> Maybe ByteString
detectLeadingSeparator xs = case B.uncons xs of
Just (',', ys) -> Just $ B.cons ',' $ B.takeWhile (== ' ') ys
Just (' ', ys) -> Just $ B.takeWhile (== ' ') ys
_ -> Nothing

isCommaSeparated :: ByteString -> Bool
isCommaSeparated xs = ',' `B.elem` xs

dropRepeatingSpaces :: ByteString -> ByteString
dropRepeatingSpaces xs = case B.uncons xs of
Just (' ', ys) -> B.cons ' ' (B.dropWhile (== ' ') ys)
Expand All @@ -437,14 +441,14 @@ fancyAlgorithm Config {cnfFields, cnfComponent, cnfOrigContents, cnfAdditions, c
_ -> Nothing
fillerPred c = isSpaceChar8 c || c == ','

let (B.takeWhileEnd fillerPred -> pref, B.takeWhile fillerPred -> suff) =
splitAtPosition firstDepPos cnfOrigContents
let (rawPref, rawSuff) = splitAtPosition firstDepPos cnfOrigContents
(B.takeWhileEnd fillerPred -> pref, B.takeWhile fillerPred -> suff) = (rawPref, rawSuff)
prefSuff = pref <> suff

noCommaSeparation = not (isCommaSeparated rawSuff) && (cnfTargetField /= BuildDepends)
(afterLast, inBetween, beforeFirst) = case secondDepPos of
Nothing ->
( if B.any (== ',') prefSuff then pref' else "," <> pref'
, if B.any (== ',') prefSuff then prefSuff' else "," <> prefSuff'
( if B.any (== ',') prefSuff || noCommaSeparation then pref' else "," <> pref'
, if B.any (== ',') prefSuff || noCommaSeparation then prefSuff' else "," <> prefSuff'
, suff
)
where
Expand Down Expand Up @@ -478,13 +482,14 @@ niceAlgorithm Config {cnfFields, cnfComponent, cnfOrigContents, cnfAdditions, cn

let (before, after) = splitAtPosition pos cnfOrigContents
(_, targetHeader) = splitAtPosition (getFieldNameAnn targetField) before
leadingSeparatorStyle = detectLeadingSeparator after
filler = dropRepeatingSpaces $ B.drop 1 $ B.dropWhile (/= ':') targetHeader
leadingCommaStyle = detectLeadingComma after
filler' = maybe ("," <> filler) (filler <>) leadingCommaStyle
defaultSep = if isCommaSeparated after || cnfTargetField == BuildDepends then "," else ""
filler' = maybe (defaultSep <> filler) (filler <>) leadingSeparatorStyle
newFieldContents =
fromMaybe "" leadingCommaStyle
fromMaybe "" leadingSeparatorStyle
<> B.intercalate filler' (NE.toList cnfAdditions)
<> (if isJust leadingCommaStyle then filler else filler')
<> (if isJust leadingSeparatorStyle then filler else filler')
pure $
before <> newFieldContents <> after

Expand Down
Loading

0 comments on commit 5f8fbe8

Please sign in to comment.