From 9c71752cb0905853b1b9afce467648b2297638e4 Mon Sep 17 00:00:00 2001 From: fwcd Date: Mon, 14 Oct 2024 16:18:11 +0200 Subject: [PATCH 1/4] Rename Initialized -> Initialize --- curry-language-server.cabal | 2 +- src/Curry/LanguageServer/Handlers.hs | 2 +- .../LanguageServer/Handlers/{Initialized.hs => Initialize.hs} | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) rename src/Curry/LanguageServer/Handlers/{Initialized.hs => Initialize.hs} (94%) diff --git a/curry-language-server.cabal b/curry-language-server.cabal index 88729bc..56fa9ab 100644 --- a/curry-language-server.cabal +++ b/curry-language-server.cabal @@ -35,7 +35,7 @@ library Curry.LanguageServer.Handlers Curry.LanguageServer.Handlers.Cancel Curry.LanguageServer.Handlers.Diagnostics - Curry.LanguageServer.Handlers.Initialized + Curry.LanguageServer.Handlers.Initialize Curry.LanguageServer.Handlers.TextDocument.CodeAction Curry.LanguageServer.Handlers.TextDocument.CodeLens Curry.LanguageServer.Handlers.TextDocument.Completion diff --git a/src/Curry/LanguageServer/Handlers.hs b/src/Curry/LanguageServer/Handlers.hs index 51b6f95..7d3a86a 100644 --- a/src/Curry/LanguageServer/Handlers.hs +++ b/src/Curry/LanguageServer/Handlers.hs @@ -9,7 +9,7 @@ import Curry.LanguageServer.Handlers.TextDocument.DocumentSymbol (documentSymbol import Curry.LanguageServer.Handlers.TextDocument.Notifications (didOpenHandler, didChangeHandler, didSaveHandler, didCloseHandler) import Curry.LanguageServer.Handlers.TextDocument.Hover (hoverHandler) import Curry.LanguageServer.Handlers.TextDocument.SignatureHelp (signatureHelpHandler) -import Curry.LanguageServer.Handlers.Initialized (initializedHandler) +import Curry.LanguageServer.Handlers.Initialize (initializedHandler) import Curry.LanguageServer.Handlers.Workspace.Command (executeCommandHandler) import Curry.LanguageServer.Handlers.Workspace.Notifications (didChangeConfigurationHandler) import Curry.LanguageServer.Handlers.Workspace.Symbol (workspaceSymbolHandler) diff --git a/src/Curry/LanguageServer/Handlers/Initialized.hs b/src/Curry/LanguageServer/Handlers/Initialize.hs similarity index 94% rename from src/Curry/LanguageServer/Handlers/Initialized.hs rename to src/Curry/LanguageServer/Handlers/Initialize.hs index e4a80b1..b26f310 100644 --- a/src/Curry/LanguageServer/Handlers/Initialized.hs +++ b/src/Curry/LanguageServer/Handlers/Initialize.hs @@ -1,5 +1,5 @@ {-# LANGUAGE OverloadedStrings #-} -module Curry.LanguageServer.Handlers.Initialized (initializedHandler) where +module Curry.LanguageServer.Handlers.Initialize (initializedHandler) where import Curry.LanguageServer.FileLoader (fileLoader) import Curry.LanguageServer.Handlers.Diagnostics (emitDiagnostics) From 0d641f3ac0ae0afe673dcbb5d516a91dead87968 Mon Sep 17 00:00:00 2001 From: fwcd Date: Mon, 14 Oct 2024 16:41:52 +0200 Subject: [PATCH 2/4] Handle initialize request instead of initialized notification This will let us report progress. --- app/Main.hs | 3 ++- src/Curry/LanguageServer/Handlers.hs | 2 -- src/Curry/LanguageServer/Handlers/Initialize.hs | 15 ++++++++------- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index ddcf6bc..d2db4f9 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -10,6 +10,7 @@ import qualified Language.LSP.Server as S import qualified Language.LSP.Protocol.Types as J import qualified Curry.LanguageServer.Config as CFG import Curry.LanguageServer.Handlers +import Curry.LanguageServer.Handlers.Initialize (initializeHandler) import Curry.LanguageServer.Handlers.Workspace.Command (commands) import Curry.LanguageServer.Monad (runLSM, newLSStateVar) import System.Exit (ExitCode(ExitFailure), exitSuccess, exitWith) @@ -32,7 +33,7 @@ runLanguageServer = do -- TODO: Handle configuration changes (ideally from here, not in the didChangeConfiguration handler) -- See https://hackage.haskell.org/package/lsp-2.7.0.0/docs/Language-LSP-Server.html#t:ServerDefinition , S.onConfigChange = const $ pure () - , S.doInitialize = const . pure . Right + , S.doInitialize = \env req -> runLSM (initializeHandler req) state env >> return (Right env) , S.staticHandlers = handlers , S.interpretHandler = \env -> S.Iso (\lsm -> runLSM lsm state env) liftIO , S.options = S.defaultOptions diff --git a/src/Curry/LanguageServer/Handlers.hs b/src/Curry/LanguageServer/Handlers.hs index 7d3a86a..8748be8 100644 --- a/src/Curry/LanguageServer/Handlers.hs +++ b/src/Curry/LanguageServer/Handlers.hs @@ -9,7 +9,6 @@ import Curry.LanguageServer.Handlers.TextDocument.DocumentSymbol (documentSymbol import Curry.LanguageServer.Handlers.TextDocument.Notifications (didOpenHandler, didChangeHandler, didSaveHandler, didCloseHandler) import Curry.LanguageServer.Handlers.TextDocument.Hover (hoverHandler) import Curry.LanguageServer.Handlers.TextDocument.SignatureHelp (signatureHelpHandler) -import Curry.LanguageServer.Handlers.Initialize (initializedHandler) import Curry.LanguageServer.Handlers.Workspace.Command (executeCommandHandler) import Curry.LanguageServer.Handlers.Workspace.Notifications (didChangeConfigurationHandler) import Curry.LanguageServer.Handlers.Workspace.Symbol (workspaceSymbolHandler) @@ -30,7 +29,6 @@ handlers _caps = mconcat , codeLensHandler , signatureHelpHandler -- Notification handlers - , initializedHandler , didOpenHandler , didChangeHandler , didSaveHandler diff --git a/src/Curry/LanguageServer/Handlers/Initialize.hs b/src/Curry/LanguageServer/Handlers/Initialize.hs index b26f310..1ba00ce 100644 --- a/src/Curry/LanguageServer/Handlers/Initialize.hs +++ b/src/Curry/LanguageServer/Handlers/Initialize.hs @@ -1,6 +1,7 @@ -{-# LANGUAGE OverloadedStrings #-} -module Curry.LanguageServer.Handlers.Initialize (initializedHandler) where +{-# LANGUAGE DataKinds, OverloadedStrings #-} +module Curry.LanguageServer.Handlers.Initialize (initializeHandler) where +import qualified Curry.LanguageServer.Config as CFG import Curry.LanguageServer.FileLoader (fileLoader) import Curry.LanguageServer.Handlers.Diagnostics (emitDiagnostics) import Curry.LanguageServer.Utils.Logging (infoM) @@ -8,19 +9,19 @@ import qualified Curry.LanguageServer.Index.Store as I import Curry.LanguageServer.Monad (LSM) import Data.Maybe (maybeToList, fromMaybe) 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 qualified Language.LSP.Server as S -initializedHandler :: S.Handlers LSM -initializedHandler = S.notificationHandler J.SMethod_Initialized $ \_nt -> do +initializeHandler :: J.TMessage J.Method_Initialize -> LSM () +initializeHandler req = do infoM "Building index store..." workspaceFolders <- fromMaybe [] <$> S.getWorkspaceFolders - let folders = maybeToList . folderToPath =<< workspaceFolders + let folderToPath (J.WorkspaceFolder uri _) = J.uriToFilePath uri + 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 -- | Indexes a workspace folder recursively. addDirToIndexStore :: FilePath -> LSM () From 31f5f6fcab47bb1e3b29855ba86ae22e7b7d8520 Mon Sep 17 00:00:00 2001 From: fwcd Date: Mon, 14 Oct 2024 16:51:13 +0200 Subject: [PATCH 3/4] Report progress for Curry initialization --- .../LanguageServer/Handlers/Initialize.hs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/Curry/LanguageServer/Handlers/Initialize.hs b/src/Curry/LanguageServer/Handlers/Initialize.hs index 1ba00ce..5e141e0 100644 --- a/src/Curry/LanguageServer/Handlers/Initialize.hs +++ b/src/Curry/LanguageServer/Handlers/Initialize.hs @@ -1,7 +1,7 @@ {-# LANGUAGE DataKinds, OverloadedStrings #-} module Curry.LanguageServer.Handlers.Initialize (initializeHandler) where -import qualified Curry.LanguageServer.Config as CFG +import Control.Lens ((^.)) import Curry.LanguageServer.FileLoader (fileLoader) import Curry.LanguageServer.Handlers.Diagnostics (emitDiagnostics) import Curry.LanguageServer.Utils.Logging (infoM) @@ -9,19 +9,22 @@ import qualified Curry.LanguageServer.Index.Store as I import Curry.LanguageServer.Monad (LSM) import Data.Maybe (maybeToList, fromMaybe) import qualified Data.Text as T +import qualified Language.LSP.Protocol.Lens as J import qualified Language.LSP.Protocol.Types as J import qualified Language.LSP.Protocol.Message as J import qualified Language.LSP.Server as S initializeHandler :: J.TMessage J.Method_Initialize -> LSM () initializeHandler req = do - infoM "Building index store..." - workspaceFolders <- fromMaybe [] <$> S.getWorkspaceFolders - let folderToPath (J.WorkspaceFolder uri _) = J.uriToFilePath uri - folders = maybeToList . folderToPath =<< workspaceFolders - mapM_ addDirToIndexStore folders - count <- I.getModuleCount - infoM $ "Indexed " <> T.pack (show count) <> " files" + let token = req ^. J.params . J.workDoneToken + S.withIndefiniteProgress "Initializing Curry..." token S.NotCancellable $ \updater -> do + infoM "Building index store..." + workspaceFolders <- fromMaybe [] <$> S.getWorkspaceFolders + let folderToPath (J.WorkspaceFolder uri _) = J.uriToFilePath uri + folders = maybeToList . folderToPath =<< workspaceFolders + mapM_ addDirToIndexStore folders + count <- I.getModuleCount + infoM $ "Indexed " <> T.pack (show count) <> " files" -- | Indexes a workspace folder recursively. addDirToIndexStore :: FilePath -> LSM () From a32946ed22acc62ca63a7900b07a72f7648de6b9 Mon Sep 17 00:00:00 2001 From: fwcd Date: Mon, 14 Oct 2024 16:59:14 +0200 Subject: [PATCH 4/4] Move diagnostic reporting back into the initialized notification handler --- src/Curry/LanguageServer/Handlers.hs | 2 ++ src/Curry/LanguageServer/Handlers/Initialize.hs | 10 ++++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Curry/LanguageServer/Handlers.hs b/src/Curry/LanguageServer/Handlers.hs index 8748be8..aa7e137 100644 --- a/src/Curry/LanguageServer/Handlers.hs +++ b/src/Curry/LanguageServer/Handlers.hs @@ -1,6 +1,7 @@ module Curry.LanguageServer.Handlers (handlers) where import Curry.LanguageServer.Handlers.Cancel (cancelHandler) +import Curry.LanguageServer.Handlers.Initialize (initializedHandler) import Curry.LanguageServer.Handlers.TextDocument.CodeAction (codeActionHandler) import Curry.LanguageServer.Handlers.TextDocument.CodeLens (codeLensHandler) import Curry.LanguageServer.Handlers.TextDocument.Completion (completionHandler) @@ -29,6 +30,7 @@ handlers _caps = mconcat , codeLensHandler , signatureHelpHandler -- Notification handlers + , initializedHandler , didOpenHandler , didChangeHandler , didSaveHandler diff --git a/src/Curry/LanguageServer/Handlers/Initialize.hs b/src/Curry/LanguageServer/Handlers/Initialize.hs index 5e141e0..2dcef6b 100644 --- a/src/Curry/LanguageServer/Handlers/Initialize.hs +++ b/src/Curry/LanguageServer/Handlers/Initialize.hs @@ -1,5 +1,5 @@ {-# LANGUAGE DataKinds, OverloadedStrings #-} -module Curry.LanguageServer.Handlers.Initialize (initializeHandler) where +module Curry.LanguageServer.Handlers.Initialize (initializeHandler, initializedHandler) where import Control.Lens ((^.)) import Curry.LanguageServer.FileLoader (fileLoader) @@ -26,12 +26,14 @@ initializeHandler req = do count <- I.getModuleCount infoM $ "Indexed " <> T.pack (show count) <> " files" +initializedHandler :: S.Handlers LSM +initializedHandler = S.notificationHandler J.SMethod_Initialized $ \_nt -> do + entries <- I.getModuleList + mapM_ (uncurry emitDiagnostics) entries + -- | Indexes a workspace folder recursively. addDirToIndexStore :: FilePath -> LSM () addDirToIndexStore dirPath = do fl <- fileLoader cfg <- S.getConfig I.addWorkspaceDir cfg fl dirPath - entries <- I.getModuleList - mapM_ (uncurry emitDiagnostics) entries - \ No newline at end of file