Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print (prettier) pkg:sublib #6898

Merged
merged 1 commit into from
Jun 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions Cabal/Distribution/Compat/NonEmptySet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ module Distribution.Compat.NonEmptySet (
NonEmptySet,
-- * Construction
singleton,
-- * Insertion
insert,
-- * Deletion
delete,
-- * Conversions
toNonEmpty,
fromNonEmpty,
Expand All @@ -15,7 +19,7 @@ module Distribution.Compat.NonEmptySet (
map,
) where

import Prelude (Bool (..), Eq, Ord (..), Read, Show (..), String, error, return, showParen, showString, ($), (++), (.))
import Prelude (Bool (..), Eq, Maybe (..), Ord (..), Read, Show (..), String, error, otherwise, return, showParen, showString, ($), (++), (.))

import Control.DeepSeq (NFData (..))
import Data.Data (Data)
Expand All @@ -26,7 +30,7 @@ import Data.Typeable (Typeable)
import qualified Data.Foldable as F
import qualified Data.Set as Set

import Distribution.Compat.Binary (Binary (..))
import Distribution.Compat.Binary (Binary (..))
import Distribution.Utils.Structured

#if MIN_VERSION_binary(0,6,0)
Expand Down Expand Up @@ -86,6 +90,24 @@ instance F.Foldable NonEmptySet where
singleton :: a -> NonEmptySet a
singleton = NES . Set.singleton

-------------------------------------------------------------------------------
-- Insertion
-------------------------------------------------------------------------------

insert :: Ord a => a -> NonEmptySet a -> NonEmptySet a
insert x (NES xs) = NES (Set.insert x xs)

-------------------------------------------------------------------------------
-- Deletion
-------------------------------------------------------------------------------

delete :: Ord a => a -> NonEmptySet a -> Maybe (NonEmptySet a)
delete x (NES xs)
| Set.null res = Nothing
| otherwise = Just (NES xs)
where
res = Set.delete x xs

-------------------------------------------------------------------------------
-- Conversions
-------------------------------------------------------------------------------
Expand Down
33 changes: 24 additions & 9 deletions Cabal/Distribution/Types/Dependency.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import Distribution.Types.LibraryName
import Distribution.Types.PackageName
import Distribution.Types.UnqualComponentName

import qualified Distribution.Compat.NonEmptySet as NonEmptySet
import qualified Distribution.Compat.NonEmptySet as NES
import qualified Text.PrettyPrint as PP

-- | Describes a dependency on a source package (API)
Expand Down Expand Up @@ -59,7 +59,7 @@ depLibraries (Dependency _ _ cs) = cs
-- @since 3.4.0.0
--
mkDependency :: PackageName -> VersionRange -> NonEmptySet LibraryName -> Dependency
mkDependency pn vr lb = Dependency pn vr (NonEmptySet.map conv lb)
mkDependency pn vr lb = Dependency pn vr (NES.map conv lb)
where
pn' = packageNameToUnqualComponentName pn

Expand All @@ -71,18 +71,33 @@ instance Binary Dependency
instance Structured Dependency
instance NFData Dependency where rnf = genericRnf

-- |
--
-- >>> prettyShow $ Dependency "pkg" anyVersion mainLibSet
-- "pkg"
--
-- >>> prettyShow $ Dependency "pkg" anyVersion $ NES.insert (LSubLibName "sublib") mainLibSet
-- "pkg:{pkg, sublib}"
--
-- >>> prettyShow $ Dependency "pkg" anyVersion $ NES.singleton (LSubLibName "sublib")
-- "pkg:sublib"
--
-- >>> prettyShow $ Dependency "pkg" anyVersion $ NES.insert (LSubLibName "sublib-b") $ NES.singleton (LSubLibName "sublib-a")
-- "pkg:{sublib-a, sublib-b}"
--
instance Pretty Dependency where
pretty (Dependency name ver sublibs) = withSubLibs (pretty name) <+> pver
where
-- TODO: change to isAnyVersion after #6736
pver | isAnyVersionLight ver = PP.empty
| otherwise = pretty ver

withSubLibs doc
| sublibs == mainLibSet = doc
| otherwise = doc <<>> PP.colon <<>> PP.braces prettySublibs
withSubLibs doc = case NES.toList sublibs of
[LMainLibName] -> doc
[LSubLibName uq] -> doc <<>> PP.colon <<>> pretty uq
_ -> doc <<>> PP.colon <<>> PP.braces prettySublibs

prettySublibs = PP.hsep $ PP.punctuate PP.comma $ prettySublib <$> NonEmptySet.toList sublibs
prettySublibs = PP.hsep $ PP.punctuate PP.comma $ prettySublib <$> NES.toList sublibs

prettySublib LMainLibName = PP.text $ unPackageName name
prettySublib (LSubLibName un) = PP.text $ unUnqualComponentName un
Expand Down Expand Up @@ -127,7 +142,7 @@ instance Parsec Dependency where
_ <- char ':'
versionGuardMultilibs
parsecWarning PWTExperimental "colon specifier is experimental feature (issue #5660)"
NonEmptySet.singleton <$> parseLib <|> parseMultipleLibs
NES.singleton <$> parseLib <|> parseMultipleLibs

spaces -- https://github.com/haskell/cabal/issues/5846

Expand All @@ -138,7 +153,7 @@ instance Parsec Dependency where
parseMultipleLibs = between
(char '{' *> spaces)
(spaces *> char '}')
(NonEmptySet.fromNonEmpty <$> parsecCommaNonEmpty parseLib)
(NES.fromNonEmpty <$> parsecCommaNonEmpty parseLib)

versionGuardMultilibs :: CabalParsing m => m ()
versionGuardMultilibs = do
Expand All @@ -152,7 +167,7 @@ versionGuardMultilibs = do

-- | Library set with main library.
mainLibSet :: NonEmptySet LibraryName
mainLibSet = NonEmptySet.singleton LMainLibName
mainLibSet = NES.singleton LMainLibName

-- | Simplify the 'VersionRange' expression in a 'Dependency'.
-- See 'simplifyVersionRange'.
Expand Down
4 changes: 2 additions & 2 deletions Cabal/tests/ParserTests/regressions/issue-5846.format
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ library
default-language: Haskell2010
build-depends:
lib1:{a, b},
lib2:{c},
lib3:{d} >=1,
lib2:c,
lib3:d >=1,
lib4:{a, b} >=1
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ Preprocessing library 'sublib' for issue-6894..
Building library 'sublib' for issue-6894..
Warning: issue.cabal:7:30: colon specifier is experimental feature (issue #5660)
Configuring library for issue-6894..
Warning: The package has an extraneous version range for a dependency on an internal library: issue:{sublib} >=0 && ==6894. This version range includes the current package but isn't needed as the current package's library will always be used.
Warning: The package has an extraneous version range for a dependency on an internal library: issue:sublib >=0 && ==6894. This version range includes the current package but isn't needed as the current package's library will always be used.
Preprocessing library for issue-6894..
Building library for issue-6894..