@@ -34,8 +34,8 @@ import Development.IDE.Core.Shake
3434import Development.IDE.LSP.LanguageServer
3535import Development.IDE.LSP.Protocol
3636import Development.IDE.Plugin
37+ import Development.IDE.Session (loadSession , findCradle , defaultLoadingOptions , cacheDir )
3738import Development.IDE.Plugin.HLS
38- import Development.IDE.Session (loadSession , findCradle , defaultLoadingOptions )
3939import Development.IDE.Types.Diagnostics
4040import Development.IDE.Types.Location
4141import Development.IDE.Types.Logger as G
@@ -57,7 +57,25 @@ import qualified System.Log.Logger as L
5757import System.Time.Extra
5858import Development.Shake (action )
5959
60- ghcIdePlugins :: T. Text -> IdePlugins IdeState -> (Plugin Config , [T. Text ])
60+ import HieDb.Create
61+ import HieDb.Types
62+ import Database.SQLite.Simple
63+ import qualified Data.ByteString.Char8 as B
64+ import qualified Crypto.Hash.SHA1 as H
65+ import Control.Concurrent.Async
66+ import Control.Exception
67+ import System.Directory
68+ import Data.ByteString.Base16
69+
70+ -- ---------------------------------------------------------------------
71+ -- ghcide partialhandlers
72+ import Development.IDE.Plugin.CodeAction as CodeAction
73+ import Development.IDE.Plugin.Completions as Completions
74+ import Development.IDE.LSP.HoverDefinition as HoverDefinition
75+
76+ -- ---------------------------------------------------------------------
77+
78+ ghcIdePlugins :: T. Text -> IdePlugins -> (Plugin Config , [T. Text ])
6179ghcIdePlugins pid ps = (asGhcIdePlugin ps, allLspCmdIds' pid ps)
6280
6381defaultMain :: Arguments -> IdePlugins IdeState -> IO ()
@@ -84,21 +102,35 @@ defaultMain args idePlugins = do
84102 hPutStrLn stderr hlsVer
85103 runLspMode lspArgs idePlugins
86104
87- -- ---------------------------------------------------------------------
88-
89- hlsLogger :: G. Logger
90- hlsLogger = G. Logger $ \ pri txt ->
91- case pri of
92- G. Telemetry -> logm (T. unpack txt)
93- G. Debug -> debugm (T. unpack txt)
94- G. Info -> logm (T. unpack txt)
95- G. Warning -> warningm (T. unpack txt)
96- G. Error -> errorm (T. unpack txt)
105+ getHieDbLoc :: FilePath -> IO FilePath
106+ getHieDbLoc dir = do
107+ let db = dirHash++ " -" ++ takeBaseName dir++ " -" ++ VERSION_ghc <.> " hiedb"
108+ dirHash = B. unpack $ encode $ H. hash $ B. pack dir
109+ cDir <- IO. getXdgDirectory IO. XdgCache cacheDir
110+ createDirectoryIfMissing True cDir
111+ pure (cDir </> db)
97112
98- -- ---------------------------------------------------------------------
99-
100- runLspMode :: LspArguments -> IdePlugins IdeState -> IO ()
101- runLspMode lspArgs@ LspArguments {.. } idePlugins = do
113+ runLspMode :: LspArguments -> IdePlugins -> IO ()
114+ runLspMode lspArgs idePlugins = do
115+ dir <- IO. getCurrentDirectory
116+ dbLoc <- getHieDbLoc dir
117+ runWithDb dbLoc $ runLspMode' lspArgs idePlugins
118+
119+ runWithDb :: FilePath -> (HieDb -> HieWriterChan -> IO () ) -> IO ()
120+ runWithDb fp k =
121+ withHieDb fp $ \ writedb -> do
122+ execute_ (getConn writedb) " PRAGMA journal_mode=WAL;"
123+ initConn writedb
124+ chan <- newChan
125+ race_ (writerThread writedb chan) (withHieDb fp (flip k chan))
126+ where
127+ writerThread db chan = forever $ do
128+ k <- readChan chan
129+ k db `catch` \ e@ SQLError {} -> do
130+ hPutStrLn stderr $ " Error in worker, ignoring: " ++ show e
131+
132+ runLspMode' :: LspArguments -> IdePlugins -> HieDb -> HieWriterChan -> IO ()
133+ runLspMode' lspArgs@ LspArguments {.. } idePlugins hiedb hiechan = do
102134 LSP. setupLogger argsLogFile [" hls" , " hie-bios" ]
103135 $ if argsDebugOn then L. DEBUG else L. INFO
104136
@@ -142,6 +174,7 @@ runLspMode lspArgs@LspArguments{..} idePlugins = do
142174 debouncer <- newAsyncDebouncer
143175 initialise caps (mainRule >> pluginRules plugins >> action kick)
144176 getLspId event wProg wIndefProg hlsLogger debouncer options vfs
177+ hiedb hiechan
145178 else do
146179 -- GHC produces messages with UTF8 in them, so make sure the terminal doesn't error
147180 hSetEncoding stdout utf8
@@ -170,7 +203,7 @@ runLspMode lspArgs@LspArguments{..} idePlugins = do
170203 debouncer <- newAsyncDebouncer
171204 let dummyWithProg _ _ f = f (const (pure () ))
172205 sessionLoader <- loadSession dir
173- ide <- initialise def mainRule (pure $ IdInt 0 ) (showEvent lock) dummyWithProg (const (const id )) (logger Info ) debouncer (defaultIdeOptions sessionLoader) vfs
206+ ide <- initialise def mainRule (pure $ IdInt 0 ) (showEvent lock) dummyWithProg (const (const id )) (logger Info ) debouncer (defaultIdeOptions sessionLoader) vfs hiedb hiechan
174207
175208 putStrLn " \n Step 4/4: Type checking the files"
176209 setFilesOfInterest ide $ HashMap. fromList $ map ((, OnDisk ) . toNormalizedFilePath') files
0 commit comments