Skip to content
This repository was archived by the owner on Oct 7, 2020. It is now read-only.

Commit 7fa1382

Browse files
authored
Merge pull request #1589 from Avi-D-coder/ormolu-opts
Pass Ormolu cradle flags & default-extensions
2 parents 4906a53 + 1ff3592 commit 7fa1382

File tree

9 files changed

+95
-42
lines changed

9 files changed

+95
-42
lines changed

cabal.project

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,4 @@ constraints:
1616

1717
write-ghc-environment-files: never
1818

19-
index-state: 2020-01-21T18:23:31Z
20-
allow-newer: ormolu:optparse-applicative
19+
index-state: 2020-01-24T16:47:33Z

haskell-ide-engine.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ library
104104
, unliftio
105105
, hlint >= 2.2.8
106106
if impl(ghc >= 8.6)
107-
build-depends: ormolu
107+
build-depends: ormolu >= 0.0.3.1
108108

109109
ghc-options: -Wall -Wredundant-constraints
110110
if flag(pedantic)

hie-plugin-api/Haskell/Ide/Engine/GhcModuleCache.hs

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -99,24 +99,49 @@ data LookupCradleResult = ReuseCradle | LoadCradle CachedCradle | NewCradle File
9999
-- via 'setCurrentCradle' before the Cradle can be cached via 'cacheCradle'.
100100
lookupCradle :: FilePath -> GhcModuleCache -> LookupCradleResult
101101
lookupCradle fp gmc =
102-
case currentCradle gmc of
103-
Just (dirs, _c) | (any (\d -> d `isPrefixOf` fp) dirs) -> ReuseCradle
104-
_ -> case T.match (cradleCache gmc) (B.pack fp) of
105-
Just (_k, c, _suf) -> LoadCradle c
106-
Nothing -> NewCradle fp
107-
108-
data CachedCradle = CachedCradle BIOS.Cradle HscEnv
102+
lookupInCache fp gmc (const $ const ReuseCradle) LoadCradle $ NewCradle fp
103+
104+
-- | Find the cradle wide 'ComponentOptions' that apply to a 'FilePath'
105+
lookupComponentOptions
106+
:: HasGhcModuleCache m => FilePath -> m (Maybe BIOS.ComponentOptions)
107+
lookupComponentOptions fp = do
108+
gmc <- getModuleCache
109+
return $ lookupInCache fp gmc (const Just) (Just . compOpts) Nothing
110+
111+
lookupInCache
112+
:: FilePath
113+
-> GhcModuleCache
114+
-- | Called when file is in the current cradle
115+
-> (BIOS.Cradle -> BIOS.ComponentOptions -> a)
116+
-- | Called when file is a member of a cached cradle
117+
-> (CachedCradle -> a)
118+
-- | Default value to return if a cradle is not found
119+
-> a
120+
-> a
121+
lookupInCache fp gmc cur cached def = case currentCradle gmc of
122+
Just (dirs, c, co) | any (`isPrefixOf` fp) dirs -> cur c co
123+
_ -> case T.match (cradleCache gmc) (B.pack fp) of
124+
Just (_k, c, _suf) -> cached c
125+
Nothing -> def
126+
127+
-- | A 'Cradle', it's 'HscEnv' and 'ComponentOptions'
128+
data CachedCradle = CachedCradle
129+
{ ccradle :: BIOS.Cradle
130+
, hscEnv :: HscEnv
131+
, compOpts :: BIOS.ComponentOptions
132+
}
109133

110134
instance Show CachedCradle where
111-
show (CachedCradle x _) = show x
135+
show (CachedCradle x _ _) = show x
112136

113137
data GhcModuleCache = GhcModuleCache
114138
{ cradleCache :: !(T.Trie CachedCradle)
115-
-- ^ map from FilePath to cradles
139+
-- ^ map from FilePath to cradle and it's config.
140+
-- May not include currentCradle
116141
, uriCaches :: !UriCaches
117-
, currentCradle :: Maybe ([FilePath], BIOS.Cradle)
118-
-- ^ The current cradle and which FilePath's it is
119-
-- responsible for
142+
, currentCradle :: Maybe ([FilePath], BIOS.Cradle, BIOS.ComponentOptions)
143+
-- ^ The current cradle, it's config,
144+
-- and which FilePath's it is responsible for.
120145
} deriving (Show)
121146

122147
-- ---------------------------------------------------------------------

hie-plugin-api/Haskell/Ide/Engine/ModuleCache.hs

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import System.Directory
4848

4949

5050
import qualified GHC
51+
import qualified GhcMake as GHC
5152
import qualified HscMain as GHC
5253
import qualified HIE.Bios as Bios
5354
import qualified HIE.Bios.Ghc.Api as Bios
@@ -131,13 +132,13 @@ loadCradle _ _ ReuseCradle _def action = do
131132
debugm "Reusing cradle"
132133
IdeResultOk <$> action
133134

134-
loadCradle _ _iniDynFlags (LoadCradle (CachedCradle crd env)) _def action = do
135+
loadCradle _ _iniDynFlags (LoadCradle (CachedCradle crd env co)) _def action = do
135136
-- Reloading a cradle happens on component switch
136137
logm $ "Switch to cradle: " ++ show crd
137138
-- Cache the existing cradle
138139
maybe (return ()) cacheCradle =<< (currentCradle <$> getModuleCache)
139140
GHC.setSession env
140-
setCurrentCradle crd
141+
setCurrentCradle crd co
141142
IdeResultOk <$> action
142143

143144
loadCradle publishDiagnostics iniDynFlags (NewCradle fp) def action = do
@@ -166,7 +167,7 @@ loadCradle publishDiagnostics iniDynFlags (NewCradle fp) def action = do
166167
initialiseCradle :: (MonadIde m, HasGhcModuleCache m, GHC.GhcMonad m)
167168
=> Bios.Cradle -> (Progress -> IO ()) -> m (IdeResult a)
168169
initialiseCradle cradle f = do
169-
res <- Bios.initializeFlagsWithCradleWithMessage (Just (toMessager f)) fp cradle
170+
res <- initializeFlagsWithCradleWithMessage (Just (toMessager f)) fp cradle
170171
case res of
171172
Bios.CradleNone ->
172173
-- Note: The action is not run if we are in the none cradle, we
@@ -194,7 +195,7 @@ loadCradle publishDiagnostics iniDynFlags (NewCradle fp) def action = do
194195
, ideMessage = Text.unwords (take 2 msgTxt)
195196
, ideInfo = Aeson.Null
196197
}
197-
Bios.CradleSuccess init_session -> do
198+
Bios.CradleSuccess (init_session, copts) -> do
198199
-- Note that init_session contains a Hook to 'f'.
199200
-- So, it can still provide Progress Reports.
200201
-- Therefore, invocation of 'init_session' must happen
@@ -225,37 +226,58 @@ loadCradle publishDiagnostics iniDynFlags (NewCradle fp) def action = do
225226
-- be that slow, even though the cradle isn't cached because the
226227
-- `.hi` files will be saved.
227228
Right Bios.Succeeded -> do
228-
setCurrentCradle cradle
229+
setCurrentCradle cradle copts
229230
logm "Cradle set succesfully"
230231
IdeResultOk <$> action
231232

232233
Right Bios.Failed -> do
233-
setCurrentCradle cradle
234+
setCurrentCradle cradle copts
234235
logm "Cradle did not load succesfully"
235236
IdeResultOk <$> action
236237

238+
-- TODO remove after hie-bios update
239+
initializeFlagsWithCradleWithMessage ::
240+
GHC.GhcMonad m
241+
=> Maybe GHC.Messager
242+
-> FilePath -- ^ The file we are loading the 'Cradle' because of
243+
-> Bios.Cradle -- ^ The cradle we want to load
244+
-> m (Bios.CradleLoadResult (m GHC.SuccessFlag, Bios.ComponentOptions)) -- ^ Whether we actually loaded the cradle or not.
245+
initializeFlagsWithCradleWithMessage msg fp cradle =
246+
fmap (initSessionWithMessage msg) <$> liftIO (Bios.getCompilerOptions fp cradle)
247+
248+
initSessionWithMessage :: (GHC.GhcMonad m)
249+
=> Maybe GHC.Messager
250+
-> Bios.ComponentOptions
251+
-> (m GHC.SuccessFlag, Bios.ComponentOptions)
252+
initSessionWithMessage msg copts = (do
253+
targets <- Bios.initSession copts
254+
GHC.setTargets targets
255+
-- Get the module graph using the function `getModuleGraph`
256+
mod_graph <- GHC.depanal [] True
257+
GHC.load' GHC.LoadAllTargets msg mod_graph, copts)
258+
237259
-- | Sets the current cradle for caching.
238260
-- Retrieves the current GHC Module Graph, to find all modules
239261
-- that belong to this cradle.
240262
-- If the cradle does not load any module, it is responsible for an empty
241263
-- list of Modules.
242-
setCurrentCradle :: (HasGhcModuleCache m, GHC.GhcMonad m) => Bios.Cradle -> m ()
243-
setCurrentCradle cradle = do
264+
setCurrentCradle :: (HasGhcModuleCache m, GHC.GhcMonad m) => Bios.Cradle -> Bios.ComponentOptions -> m ()
265+
setCurrentCradle cradle co = do
244266
mg <- GHC.getModuleGraph
245267
let ps = mapMaybe (GHC.ml_hs_file . GHC.ms_location) (mgModSummaries mg)
246268
debugm $ "Modules in the cradle: " ++ show ps
247269
ps' <- liftIO $ mapM canonicalizePath ps
248-
modifyCache (\s -> s { currentCradle = Just (ps', cradle) })
270+
modifyCache (\s -> s { currentCradle = Just (ps', cradle, co) })
249271

250272
-- | Cache the given Cradle.
251273
-- Caches the given Cradle together with all Modules this Cradle is responsible
252274
-- for.
253275
-- Via 'lookupCradle' it can be checked if a given FilePath is managed by
254276
-- a any Cradle that has already been loaded.
255-
cacheCradle :: (HasGhcModuleCache m, GHC.GhcMonad m) => ([FilePath], Bios.Cradle) -> m ()
256-
cacheCradle (ds, c) = do
277+
cacheCradle :: (HasGhcModuleCache m, GHC.GhcMonad m) => ([FilePath], Bios.Cradle, Bios.ComponentOptions) -> m ()
278+
cacheCradle (ds, c, co) = do
257279
env <- GHC.getSession
258-
let cc = CachedCradle c env
280+
let cc = CachedCradle c env co
259281
new_map = T.fromList (map (, cc) (map B.pack ds))
260282
modifyCache (\s -> s { cradleCache = T.unionWith (\a _ -> a) new_map (cradleCache s) })
261283

src/Haskell/Ide/Engine/Plugin/Ormolu.hs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ import Haskell.Ide.Engine.MonadTypes
88

99
#if __GLASGOW_HASKELL__ >= 806
1010
import Control.Exception
11+
import Control.Monad
1112
import Control.Monad.IO.Class ( liftIO , MonadIO(..) )
1213
import Data.Aeson ( Value ( Null ) )
13-
import Data.Text
14+
import Data.List
15+
import Data.Maybe
16+
import qualified Data.Text as T
1417
import Ormolu
15-
#if __GLASGOW_HASKELL__ < 808
16-
import Ormolu.Config (defaultConfig)
17-
import Ormolu.Exception (OrmoluException)
18-
#endif
1918
import Haskell.Ide.Engine.PluginUtils
19+
import HIE.Bios.Types
2020
#endif
2121

2222
ormoluDescriptor :: PluginId -> PluginDescriptor
@@ -37,12 +37,21 @@ provider :: FormattingProvider
3737
provider _contents _uri _typ _opts =
3838
#if __GLASGOW_HASKELL__ >= 806
3939
case _typ of
40-
FormatRange _ -> return $ IdeResultFail (IdeError PluginError (pack "Selection formatting for Ormolu is not currently supported.") Null)
40+
FormatRange _ -> return $ IdeResultFail (IdeError PluginError (T.pack "Selection formatting for Ormolu is not currently supported.") Null)
4141
FormatText -> pluginGetFile _contents _uri $ \file -> do
42-
result <- liftIO $ try @OrmoluException (ormolu defaultConfig file (unpack _contents))
42+
opts <- lookupComponentOptions file
43+
let opts' = map DynOption $ filter exop $ join $ maybeToList $ componentOptions <$> opts
44+
conf = Config opts' False False True False
45+
result <- liftIO $ try @OrmoluException (ormolu conf file (T.unpack _contents))
46+
4347
case result of
44-
Left err -> return $ IdeResultFail (IdeError PluginError (pack $ "ormoluCmd: " ++ show err) Null)
48+
Left err -> return $ IdeResultFail (IdeError PluginError (T.pack $ "ormoluCmd: " ++ show err) Null)
4549
Right new -> return $ IdeResultOk [TextEdit (fullRange _contents) new]
50+
where
51+
exop s =
52+
"-X" `isPrefixOf` s
53+
|| "-fplugin=" `isPrefixOf` s
54+
|| "-pgmF=" `isPrefixOf` s
4655
#else
4756
return $ IdeResultOk [] -- NOP formatter
4857
#endif

stack-8.6.4.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ extra-deps:
2929
- monad-dijkstra-0.1.1.2@rev:1
3030
- monad-memo-0.4.1
3131
- multistate-0.8.0.1
32-
- ormolu-0.0.3.0
32+
- ormolu-0.0.3.1
3333
- parser-combinators-1.2.1
3434
- rope-utf16-splay-0.3.1.0
3535
- syz-0.2.0.0

stack-8.6.5.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ extra-deps:
2727
- hsimport-0.11.0
2828
- lsp-test-0.10.0.0
2929
- monad-dijkstra-0.1.1.2@rev:1
30-
- ormolu-0.0.3.0
30+
- ormolu-0.0.3.1
3131
- parser-combinators-1.2.1
3232
- syz-0.2.0.0
3333
- temporary-1.2.1.1

stack-8.8.1.yaml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ extra-deps:
2222
- hsimport-0.11.0
2323
- ilist-0.3.1.0
2424
- monad-dijkstra-0.1.1.2
25-
- optparse-applicative-0.15.1.0
26-
- ormolu-0.0.3.0
25+
- ormolu-0.0.3.1
2726
- semigroups-0.18.5
2827
- temporary-1.2.1.1
2928

@@ -33,8 +32,7 @@ flags:
3332
hie-plugin-api:
3433
pedantic: true
3534

36-
# Required to build ormolu with optparse-applicative-0.15.1.0
37-
allow-newer: true
35+
# allow-newer: true
3836

3937
nix:
4038
packages: [ icu libcxx zlib ]

stack.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ extra-deps:
2929
- lsp-test-0.10.0.0
3030
- monad-dijkstra-0.1.1.2@rev:1
3131
- parser-combinators-1.2.1
32-
- ormolu-0.0.3.0
32+
- ormolu-0.0.3.1
3333
- syz-0.2.0.0
3434
- temporary-1.2.1.1
3535
- unix-compat-0.5.2

0 commit comments

Comments
 (0)