Skip to content

Commit 75bbc7d

Browse files
committed
Made addPackages idempotent
1 parent 887cde9 commit 75bbc7d

File tree

1 file changed

+42
-17
lines changed
  • plugins/default/src/Ide/Plugin/Eval

1 file changed

+42
-17
lines changed

plugins/default/src/Ide/Plugin/Eval/GHC.hs

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module Ide.Plugin.Eval.GHC (
1515
) where
1616

1717
import Data.List (isPrefixOf)
18+
import Data.Maybe (mapMaybe)
1819
import Development.IDE.GHC.Compat
1920
import qualified EnumSet
2021
import GHC.LanguageExtensions.Type (Extension (..))
@@ -39,9 +40,9 @@ import StringBuffer (stringToStringBuffer)
3940
{- $setup
4041
>>> import GHC
4142
>>> import GHC.Paths
42-
>>> run act = runGhc (Just libdir) (getSessionDynFlags >>= act)
43+
>>> run act = runGhc (Just libdir) (getInteractiveDynFlags >>= act)
4344
>>> libdir
44-
"/Users/titto/.stack/programs/x86_64-osx/ghc-8.10.2/lib/ghc-8.10.2"
45+
"/Users/titto/.ghcup/ghc/8.8.4/lib/ghc-8.8.4"
4546
-}
4647

4748
{- | Returns true if string is an expression
@@ -82,38 +83,61 @@ Right True
8283
>>> hasPackageTst "ghc"
8384
Right True
8485
86+
>>> hasPackageTst "extra"
87+
Left "<command line>: cannot satisfy -package extra\n (use -v for more information)"
88+
8589
>>> hasPackageTst "QuickCheck"
8690
Left "<command line>: cannot satisfy -package QuickCheck\n (use -v for more information)"
8791
-}
8892
hasPackage :: DynFlags -> String -> Bool
89-
hasPackage df name =
90-
any
93+
hasPackage df = hasPackage_ (packageFlags df)
94+
95+
hasPackage_ :: [PackageFlag] -> [Char] -> Bool
96+
hasPackage_ pkgFlags name = any (name `isPrefixOf`) (pkgNames_ pkgFlags)
97+
98+
{- |
99+
>>> run (return . pkgNames)
100+
[]
101+
-}
102+
pkgNames :: DynFlags -> [String]
103+
pkgNames = pkgNames_ . packageFlags
104+
105+
pkgNames_ :: [PackageFlag] -> [String]
106+
pkgNames_ =
107+
mapMaybe
91108
( \case
92-
ExposePackage _ (PackageArg n) _ | name `isPrefixOf` n -> True
93-
ExposePackage _ (UnitIdArg (DefiniteUnitId (DefUnitId (InstalledUnitId n)))) _ | name `isPrefixOf` asS n -> True
94-
_ -> False
109+
ExposePackage _ (PackageArg n) _ -> Just n
110+
ExposePackage _ (UnitIdArg (DefiniteUnitId n)) _ -> Just $ asS n
111+
_ -> Nothing
95112
)
96-
$ packageFlags df
97113

98-
{- | Expose a list of packages
114+
{- | Expose a list of packages.
99115
>>> addPackagesTest pkgs = run (\_ -> (packageFlags <$>) <$> addPackages pkgs)
100116
101117
>>> addPackagesTest []
102118
Right []
103119
104-
>>> addPackagesTest ["base","array"]
120+
>>> addPackagesTest ["base","base","array"]
105121
Right [-package base{package base True ([])},-package array{package array True ([])}]
106122
123+
>>> addPackagesTest ["Cabal"]
124+
Right [-package Cabal{package Cabal True ([])}]
125+
107126
>>> addPackagesTest ["QuickCheck"]
108127
Left "<command line>: cannot satisfy -package QuickCheck\n (use -v for more information)"
109128
110-
>>> addPackagesTest ["notThere"]
129+
>>> addPackagesTest ["base","notThere"]
111130
Left "<command line>: cannot satisfy -package notThere\n (use -v for more information)"
131+
132+
prop> \(x::Int) -> x + x == 2 * x
133+
+++ OK, passed 100 tests.
112134
-}
113135
addPackages :: [String] -> Ghc (Either String DynFlags)
114-
addPackages pkgNames = gStrictTry $ modifyFlags (\df -> df{packageFlags = map expose pkgNames ++ packageFlags df})
136+
addPackages pkgNames = gStrictTry $
137+
modifyFlags $ \df ->
138+
df{packageFlags = foldr (\pkgName pf -> if hasPackage_ pf pkgName then pf else expose pkgName : pf) (packageFlags df) pkgNames}
115139
where
116-
expose name = ExposePackage ("-package " ++ name) (PackageArg name) (ModRenaming True []) -- -package-id filepath-1.4.2.1
140+
expose name = ExposePackage ("-package " ++ name) (PackageArg name) (ModRenaming True [])
117141

118142
modifyFlags :: GhcMonad m => (DynFlags -> DynFlags) -> m DynFlags
119143
modifyFlags f = do
@@ -168,11 +192,12 @@ showDynFlags df =
168192
[ ("extensions", ppr . extensions $ df)
169193
, ("extensionFlags", ppr . EnumSet.toList . extensionFlags $ df)
170194
, ("importPaths", vList $ importPaths df)
171-
-- , ("includePaths", text . show $ includePaths df)
172-
-- , ("packageEnv", ppr $ packageEnv df)
173-
-- , ("packageFlags", vcat . map ppr $ packageFlags df)
195+
, -- , ("includePaths", text . show $ includePaths df)
196+
-- ("packageEnv", ppr $ packageEnv df)
197+
("pkgNames", vcat . map text $ pkgNames df)
198+
, ("packageFlags", vcat . map ppr $ packageFlags df)
174199
-- ,("pkgDatabase",(map) (ppr . installedPackageId) . pkgDatabase $ df)
175-
-- ,("pkgDatabase",text . show <$> pkgDatabase $ df)
200+
-- ("pkgDatabase", text . show <$> pkgDatabase $ df)
176201
]
177202

178203
vList :: [String] -> SDoc

0 commit comments

Comments
 (0)