Skip to content

Commit 5bae0dd

Browse files
committed
Rebase ghcide changes, and match in hls branch
1 parent 1470977 commit 5bae0dd

File tree

5 files changed

+59
-28
lines changed

5 files changed

+59
-28
lines changed

cabal.project

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ package ghcide
1616

1717
write-ghc-environment-files: never
1818

19-
index-state: 2020-02-09T06:58:05Z
19+
index-state: 2020-03-03T21:14:55Z

exe/Main.hs

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,15 @@ module Main(main) where
1212

1313
import Arguments
1414
import Control.Concurrent.Extra
15-
import Control.DeepSeq (NFData)
1615
import Control.Exception
1716
import Control.Monad.Extra
1817
import Control.Monad.IO.Class
1918
import qualified Crypto.Hash.SHA1 as H
20-
import Data.Binary (Binary)
2119
import Data.ByteString.Base16
2220
import qualified Data.ByteString.Char8 as B
2321
import Data.Default
24-
import Data.Dynamic (Typeable)
22+
import Data.Functor ((<&>))
2523
import qualified Data.HashSet as HashSet
26-
import Data.Hashable (Hashable)
2724
import Data.List.Extra
2825
import qualified Data.Map.Strict as Map
2926
import Data.Maybe
@@ -46,10 +43,9 @@ import Development.IDE.Types.Diagnostics
4643
import Development.IDE.Types.Location
4744
import Development.IDE.Types.Logger
4845
import Development.IDE.Types.Options
49-
import Development.Shake (Action, RuleResult, Rules, action, doesFileExist, need)
46+
import Development.Shake (Action, Rules, action, doesFileExist, doesDirectoryExist, need)
5047
import DynFlags
5148
import GHC hiding (def)
52-
import GHC.Generics (Generic)
5349
-- import qualified GHC.Paths
5450
import HIE.Bios
5551
import HIE.Bios.Cradle
@@ -63,6 +59,7 @@ import Language.Haskell.LSP.Types (LspId(IdInt))
6359
import qualified Language.Haskell.LSP.Core as LSP
6460
import Linker
6561
-- import Paths_haskell_language_server
62+
import RuleTypes
6663
import qualified System.Directory.Extra as IO
6764
-- import System.Environment
6865
import System.Exit
@@ -152,7 +149,7 @@ main = do
152149
, optShakeProfiling = argsShakeProfiling
153150
}
154151
debouncer <- newAsyncDebouncer
155-
initialise caps (loadGhcSessionIO >> mainRule >> pluginRules plugins >> action kick)
152+
initialise caps (cradleRules >> mainRule >> pluginRules plugins >> action kick)
156153
getLspId event (logger minBound) debouncer options vfs
157154
else do
158155
putStrLn $ "(haskell-language-server)Ghcide setup tester in " ++ dir ++ "."
@@ -189,7 +186,7 @@ main = do
189186
let options =
190187
(defaultIdeOptions $ return $ return . grab)
191188
{ optShakeProfiling = argsShakeProfiling }
192-
ide <- initialise def (loadGhcSessionIO >> mainRule) (pure $ IdInt 0) (showEvent lock) (logger Info) noopDebouncer options vfs
189+
ide <- initialise def (cradleRules >> mainRule) (pure $ IdInt 0) (showEvent lock) (logger Info) noopDebouncer options vfs
193190

194191
putStrLn "\nStep 6/6: Type checking the files"
195192
setFilesOfInterest ide $ HashSet.fromList $ map toNormalizedFilePath files
@@ -203,6 +200,10 @@ main = do
203200

204201
unless (null failed) exitFailure
205202

203+
cradleRules :: Rules ()
204+
cradleRules = do
205+
loadGhcSessionIO
206+
cradleToSession
206207

207208
expandFiles :: [FilePath] -> IO [FilePath]
208209
expandFiles = concatMapM $ \x -> do
@@ -230,19 +231,6 @@ showEvent lock (EventFileDiagnostics (toNormalizedFilePath -> file) diags) =
230231
showEvent lock e = withLock lock $ print e
231232

232233

233-
-- Rule type for caching GHC sessions.
234-
type instance RuleResult GetHscEnv = HscEnvEq
235-
236-
data GetHscEnv = GetHscEnv
237-
{ hscenvOptions :: [String] -- componentOptions from hie-bios
238-
, hscenvDependencies :: [FilePath] -- componentDependencies from hie-bios
239-
}
240-
deriving (Eq, Show, Typeable, Generic)
241-
instance Hashable GetHscEnv
242-
instance NFData GetHscEnv
243-
instance Binary GetHscEnv
244-
245-
246234
loadGhcSessionIO :: Rules ()
247235
loadGhcSessionIO =
248236
-- This rule is for caching the GHC session. E.g., even when the cabal file
@@ -255,6 +243,7 @@ loadGhcSessionIO =
255243
getComponentOptions :: Cradle a -> IO ComponentOptions
256244
getComponentOptions cradle = do
257245
let showLine s = putStrLn ("> " ++ s)
246+
-- WARNING 'runCradle is very expensive and must be called as few times as possible
258247
cradleRes <- runCradle (cradleOptsProg cradle) showLine ""
259248
case cradleRes of
260249
CradleSuccess r -> pure r
@@ -310,8 +299,14 @@ setHiDir f d =
310299
-- override user settings to avoid conflicts leading to recompilation
311300
d { hiDir = Just f}
312301

313-
cradleToSession :: Maybe FilePath -> Cradle a -> Action HscEnvEq
314-
cradleToSession mbYaml cradle = do
302+
cradleToSession :: Rules ()
303+
cradleToSession = define $ \LoadCradle nfp -> do
304+
let f = fromNormalizedFilePath nfp
305+
306+
-- If the path points to a directory, load the implicit cradle
307+
mbYaml <- doesDirectoryExist f <&> \isDir -> if isDir then Nothing else Just f
308+
cradle <- liftIO $ maybe (loadImplicitCradle $ addTrailingPathSeparator f) loadCradle mbYaml
309+
315310
cmpOpts <- liftIO $ getComponentOptions cradle
316311
let opts = componentOptions cmpOpts
317312
deps = componentDependencies cmpOpts
@@ -321,7 +316,7 @@ cradleToSession mbYaml cradle = do
321316
_ -> deps
322317
existingDeps <- filterM doesFileExist deps'
323318
need existingDeps
324-
useNoFile_ $ GetHscEnv opts deps
319+
([],) . pure <$> useNoFile_ (GetHscEnv opts deps)
325320

326321

327322
loadSession :: FilePath -> Action (FilePath -> Action HscEnvEq)
@@ -335,8 +330,9 @@ loadSession dir = liftIO $ do
335330
return $ normalise <$> res'
336331
let session :: Maybe FilePath -> Action HscEnvEq
337332
session file = do
338-
c <- liftIO $ maybe (loadImplicitCradle $ addTrailingPathSeparator dir) loadCradle file
339-
cradleToSession file c
333+
-- In the absence of a cradle file, just pass the directory from where to calculate an implicit cradle
334+
let cradle = toNormalizedFilePath $ fromMaybe dir file
335+
use_ LoadCradle cradle
340336
return $ \file -> session =<< liftIO (cradleLoc file)
341337

342338

exe/RuleTypes.hs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{-# LANGUAGE DeriveGeneric #-}
2+
{-# LANGUAGE TypeFamilies #-}
3+
module RuleTypes (GetHscEnv(..), LoadCradle(..)) where
4+
5+
import Control.DeepSeq
6+
import Data.Binary
7+
import Data.Hashable (Hashable)
8+
import Development.Shake
9+
import Development.IDE.GHC.Util
10+
import Data.Typeable (Typeable)
11+
import GHC.Generics (Generic)
12+
13+
-- Rule type for caching GHC sessions.
14+
type instance RuleResult GetHscEnv = HscEnvEq
15+
16+
data GetHscEnv = GetHscEnv
17+
{ hscenvOptions :: [String] -- componentOptions from hie-bios
18+
, hscenvDependencies :: [FilePath] -- componentDependencies from hie-bios
19+
}
20+
deriving (Eq, Show, Typeable, Generic)
21+
22+
instance Hashable GetHscEnv
23+
instance NFData GetHscEnv
24+
instance Binary GetHscEnv
25+
26+
-- Rule type for caching cradle loading
27+
type instance RuleResult LoadCradle = HscEnvEq
28+
29+
data LoadCradle = LoadCradle
30+
deriving (Eq, Show, Typeable, Generic)
31+
32+
instance Hashable LoadCradle
33+
instance NFData LoadCradle
34+
instance Binary LoadCradle

haskell-language-server.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ executable haskell-language-server
9292
other-modules:
9393
Arguments
9494
Paths_haskell_language_server
95+
RuleTypes
9596
autogen-modules:
9697
Paths_haskell_language_server
9798
ghc-options:

0 commit comments

Comments
 (0)