Skip to content

Commit

Permalink
Convert display functions into Pretty instances
Browse files Browse the repository at this point in the history
We have a lot of `showType` functions that are effectively a `Pretty`
instance but less composable. Let's make them proper `Pretty` instances.

Split off of #10524
  • Loading branch information
9999years committed Dec 10, 2024
1 parent 949464d commit fa7fa8b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ module Distribution.Solver.Types.ConstraintSource

import Distribution.Solver.Compat.Prelude
import Distribution.Solver.Types.ProjectConfigPath (ProjectConfigPath, docProjectConfigPath)
import Text.PrettyPrint (render)
import Distribution.Pretty (Pretty(pretty), prettyShow)
import Text.PrettyPrint (text)

-- | Source of a 'PackageConstraint'.
data ConstraintSource =
Expand Down Expand Up @@ -55,31 +56,37 @@ data ConstraintSource =
-- | An internal constraint due to compatibility issues with the Setup.hs
-- command line interface requires a maximum upper bound on Cabal
| ConstraintSetupCabalMaxVersion
deriving (Eq, Show, Generic)
deriving (Show, Eq, Generic)

instance Binary ConstraintSource
instance Structured ConstraintSource

-- | Description of a 'ConstraintSource'.
showConstraintSource :: ConstraintSource -> String
showConstraintSource (ConstraintSourceMainConfig path) =
"main config " ++ path
showConstraintSource (ConstraintSourceProjectConfig path) =
"project config " ++ render (docProjectConfigPath path)
showConstraintSource (ConstraintSourceUserConfig path)= "user config " ++ path
showConstraintSource ConstraintSourceCommandlineFlag = "command line flag"
showConstraintSource ConstraintSourceUserTarget = "user target"
showConstraintSource ConstraintSourceNonReinstallablePackage =
"non-reinstallable package"
showConstraintSource ConstraintSourceFreeze = "cabal freeze"
showConstraintSource ConstraintSourceConfigFlagOrTarget =
"config file, command line flag, or user target"
showConstraintSource ConstraintSourceMultiRepl =
"--enable-multi-repl"
showConstraintSource ConstraintSourceProfiledDynamic =
"--enable-profiling-shared"
showConstraintSource ConstraintSourceUnknown = "unknown source"
showConstraintSource ConstraintSetupCabalMinVersion =
"minimum version of Cabal used by Setup.hs"
showConstraintSource ConstraintSetupCabalMaxVersion =
"maximum version of Cabal used by Setup.hs"
showConstraintSource = prettyShow

instance Pretty ConstraintSource where
pretty constraintSource = case constraintSource of
(ConstraintSourceMainConfig path) ->
text "main config" <+> text path
(ConstraintSourceProjectConfig path) ->
text "project config" <+> docProjectConfigPath path
(ConstraintSourceUserConfig path)-> text "user config " <+> text path
ConstraintSourceCommandlineFlag -> text "command line flag"
ConstraintSourceUserTarget -> text "user target"
ConstraintSourceNonReinstallablePackage ->
text "non-reinstallable package"
ConstraintSourceFreeze -> text "cabal freeze"
ConstraintSourceConfigFlagOrTarget ->
text "config file, command line flag, or user target"
ConstraintSourceMultiRepl ->
text "--enable-multi-repl"
ConstraintSourceProfiledDynamic ->
text "--enable-profiling-shared"
ConstraintSourceUnknown -> text "unknown source"
ConstraintSetupCabalMinVersion ->
text "minimum version of Cabal used by Setup.hs"
ConstraintSetupCabalMaxVersion ->
text "maximum version of Cabal used by Setup.hs"
ConstraintSourceImplicit ->

Check failure on line 91 in cabal-install-solver/src/Distribution/Solver/Types/ConstraintSource.hs

View workflow job for this annotation

GitHub Actions / Doctest Cabal

Not in scope: data constructor ‘ConstraintSourceImplicit’
text "implicit target"
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ module Distribution.Solver.Types.PackageConstraint (
scopeToPackageName,
constraintScopeMatches,
PackageProperty(..),
dispPackageProperty,
PackageConstraint(..),
dispPackageConstraint,
showPackageConstraint,
packageConstraintToDependency
) where
Expand All @@ -23,7 +21,7 @@ import Prelude ()

import Distribution.Package (PackageName)
import Distribution.PackageDescription (FlagAssignment, dispFlagAssignment)
import Distribution.Pretty (flatStyle, pretty)
import Distribution.Pretty (flatStyle, Pretty(pretty))
import Distribution.Types.PackageVersionConstraint (PackageVersionConstraint (..))
import Distribution.Version (VersionRange, simplifyVersionRange)

Expand Down Expand Up @@ -82,12 +80,11 @@ constraintScopeMatches (ScopeAnySetupQualifier pn) (Q pp pn') =
in setup pp && pn == pn'
constraintScopeMatches (ScopeAnyQualifier pn) (Q _ pn') = pn == pn'

-- | Pretty-prints a constraint scope.
dispConstraintScope :: ConstraintScope -> Disp.Doc
dispConstraintScope (ScopeTarget pn) = pretty pn <<>> Disp.text "." <<>> pretty pn
dispConstraintScope (ScopeQualified q pn) = dispQualifier q <<>> pretty pn
dispConstraintScope (ScopeAnySetupQualifier pn) = Disp.text "setup." <<>> pretty pn
dispConstraintScope (ScopeAnyQualifier pn) = Disp.text "any." <<>> pretty pn
instance Pretty ConstraintScope where
pretty (ScopeTarget pn) = pretty pn <<>> Disp.text "." <<>> pretty pn
pretty (ScopeQualified q pn) = dispQualifier q <<>> pretty pn
pretty (ScopeAnySetupQualifier pn) = Disp.text "setup." <<>> pretty pn
pretty (ScopeAnyQualifier pn) = Disp.text "any." <<>> pretty pn

-- | A package property is a logical predicate on packages.
data PackageProperty
Expand All @@ -101,32 +98,30 @@ data PackageProperty
instance Binary PackageProperty
instance Structured PackageProperty

-- | Pretty-prints a package property.
dispPackageProperty :: PackageProperty -> Disp.Doc
dispPackageProperty (PackagePropertyVersion verrange) = pretty verrange
dispPackageProperty PackagePropertyInstalled = Disp.text "installed"
dispPackageProperty PackagePropertySource = Disp.text "source"
dispPackageProperty (PackagePropertyFlags flags) = dispFlagAssignment flags
dispPackageProperty (PackagePropertyStanzas stanzas) =
Disp.hsep $ map (Disp.text . showStanza) stanzas
instance Pretty PackageProperty where
pretty (PackagePropertyVersion verrange) = pretty verrange
pretty PackagePropertyInstalled = Disp.text "installed"
pretty PackagePropertySource = Disp.text "source"
pretty (PackagePropertyFlags flags) = dispFlagAssignment flags
pretty (PackagePropertyStanzas stanzas) =
Disp.hsep $ map (Disp.text . showStanza) stanzas

-- | A package constraint consists of a scope plus a property
-- that must hold for all packages within that scope.
data PackageConstraint = PackageConstraint ConstraintScope PackageProperty
deriving (Eq, Show)

-- | Pretty-prints a package constraint.
dispPackageConstraint :: PackageConstraint -> Disp.Doc
dispPackageConstraint (PackageConstraint scope prop) =
dispConstraintScope scope <+> dispPackageProperty prop
instance Pretty PackageConstraint where
pretty (PackageConstraint scope prop) =
pretty scope <+> pretty prop

-- | Alternative textual representation of a package constraint
-- for debugging purposes (slightly more verbose than that
-- produced by 'dispPackageConstraint').
--
showPackageConstraint :: PackageConstraint -> String
showPackageConstraint pc@(PackageConstraint scope prop) =
Disp.renderStyle flatStyle . postprocess $ dispPackageConstraint pc2
Disp.renderStyle flatStyle . postprocess $ pretty pc2
where
pc2 = case prop of
PackagePropertyVersion vr ->
Expand Down
2 changes: 1 addition & 1 deletion cabal-install/src/Distribution/Client/Targets.hs
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ readUserConstraint str =

instance Pretty UserConstraint where
pretty (UserConstraint scope prop) =
dispPackageConstraint $ PackageConstraint (fromUserConstraintScope scope) prop
pretty $ PackageConstraint (fromUserConstraintScope scope) prop

instance Parsec UserConstraint where
parsec = do
Expand Down

0 comments on commit fa7fa8b

Please sign in to comment.