diff --git a/src/Curry/LanguageServer/Handlers/Initialized.hs b/src/Curry/LanguageServer/Handlers/Initialized.hs index e4a80b1..a609c14 100644 --- a/src/Curry/LanguageServer/Handlers/Initialized.hs +++ b/src/Curry/LanguageServer/Handlers/Initialized.hs @@ -1,6 +1,10 @@ {-# LANGUAGE OverloadedStrings #-} module Curry.LanguageServer.Handlers.Initialized (initializedHandler) where +import Control.Concurrent (forkIO) +import Control.Monad (forM_, void) +import Control.Monad.IO.Class (liftIO) +import Control.Monad.IO.Unlift (askRunInIO) import Curry.LanguageServer.FileLoader (fileLoader) import Curry.LanguageServer.Handlers.Diagnostics (emitDiagnostics) import Curry.LanguageServer.Utils.Logging (infoM) @@ -11,16 +15,27 @@ import qualified Data.Text as T import qualified Language.LSP.Server as S import qualified Language.LSP.Protocol.Types as J import qualified Language.LSP.Protocol.Message as J +import System.FilePath (takeFileName) initializedHandler :: S.Handlers LSM initializedHandler = S.notificationHandler J.SMethod_Initialized $ \_nt -> do - infoM "Building index store..." - workspaceFolders <- fromMaybe [] <$> S.getWorkspaceFolders - let folders = maybeToList . folderToPath =<< workspaceFolders - mapM_ addDirToIndexStore folders - count <- I.getModuleCount - infoM $ "Indexed " <> T.pack (show count) <> " files" - where folderToPath (J.WorkspaceFolder uri _) = J.uriToFilePath uri + runInIO <- askRunInIO + void $ liftIO $ forkIO $ runInIO $ S.withProgress "Curry: Adding folders" Nothing S.NotCancellable $ \update -> do + infoM "Building index store..." + workspaceFolders <- fromMaybe [] <$> S.getWorkspaceFolders + + let folders = maybeToList . folderToPath =<< workspaceFolders + folderCount = length folders + + forM_ (zip [0..] folders) $ \(i, fp) -> do + let percent = (i * 100) `div` folderCount + msg = T.pack $ "[" ++ show (i + 1) ++ " of " ++ show folderCount ++ "] " ++ takeFileName fp + update (S.ProgressAmount (Just $ fromIntegral percent) $ Just msg) + addDirToIndexStore fp + + count <- I.getModuleCount + infoM $ "Indexed " <> T.pack (show count) <> " files" + where folderToPath (J.WorkspaceFolder uri _) = J.uriToFilePath uri -- | Indexes a workspace folder recursively. addDirToIndexStore :: FilePath -> LSM ()