-
-
Notifications
You must be signed in to change notification settings - Fork 391
Use restricted monad for plugins (#4057) #4304
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fce69dc
Use restricted monad for plugins (#4057)
awjchen ff82b4b
Renaming: PluginM -> HandlerM
awjchen 3a9dd1d
Explain intent for HandlerM
awjchen 53428b0
Fix comment
awjchen 189ee7c
Apply stylish-haskell
awjchen 45a64aa
Merge branch 'master' into alex/4057
michaelpj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ module Ide.Types | |
, PluginCommand(..), CommandId(..), CommandFunction, mkLspCommand, mkLspCmdId | ||
, PluginId(..) | ||
, PluginHandler(..), mkPluginHandler | ||
, PluginM, runPluginM, pluginGetClientCapabilities, pluginGetVirtualFile, pluginGetVersionedTextDoc, pluginSendNotification, pluginSendRequest, pluginWithIndefiniteProgress | ||
, PluginHandlers(..) | ||
, PluginMethod(..) | ||
, PluginMethodHandler | ||
|
@@ -62,6 +63,7 @@ import Control.Lens (_Just, view, (.~), (?~), (^.), | |
(^?)) | ||
import Control.Monad (void) | ||
import Control.Monad.Error.Class (MonadError (throwError)) | ||
import Control.Monad.IO.Class (MonadIO) | ||
import Control.Monad.Trans.Class (MonadTrans (lift)) | ||
import Control.Monad.Trans.Except (ExceptT, runExceptT) | ||
import Data.Aeson hiding (Null, defaultOptions) | ||
|
@@ -94,7 +96,7 @@ import Ide.Plugin.Properties | |
import qualified Language.LSP.Protocol.Lens as L | ||
import Language.LSP.Protocol.Message | ||
import Language.LSP.Protocol.Types | ||
import Language.LSP.Server (LspM, LspT, getVirtualFile) | ||
import Language.LSP.Server | ||
import Language.LSP.VFS | ||
import Numeric.Natural | ||
import OpenTelemetry.Eventlog | ||
|
@@ -103,6 +105,7 @@ import Prettyprinter as PP | |
import System.FilePath | ||
import System.IO.Unsafe | ||
import Text.Regex.TDFA.Text () | ||
import UnliftIO (MonadUnliftIO) | ||
-- --------------------------------------------------------------------- | ||
|
||
data IdePlugins ideState = IdePlugins_ | ||
|
@@ -890,9 +893,52 @@ instance GEq IdeNotification where | |
instance GCompare IdeNotification where | ||
gcompare (IdeNotification a) (IdeNotification b) = gcompare a b | ||
|
||
-- | Restricted version of 'LspM' specific to plugins | ||
newtype PluginM config a = PluginM { _runPluginM :: LspM config a } | ||
awjchen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
deriving newtype (Applicative, Functor, Monad, MonadIO, MonadUnliftIO) | ||
|
||
runPluginM :: PluginM config a -> LspM config a | ||
runPluginM = _runPluginM | ||
|
||
-- | Wrapper of 'getVirtualFile' for PluginM | ||
-- | ||
-- TODO: To be replaced by a lookup of the Shake build graph | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. YES! This is exactly the sort of thing we want to spot, excellent. |
||
pluginGetVirtualFile :: NormalizedUri -> PluginM config (Maybe VirtualFile) | ||
pluginGetVirtualFile uri = PluginM $ getVirtualFile uri | ||
|
||
-- | Version of 'getVersionedTextDoc' for PluginM | ||
-- | ||
-- TODO: Should use 'pluginGetVirtualFile' instead of wrapping 'getVersionedTextDoc'. | ||
-- At the time of writing, 'pluginGetVirtualFile' of the "lsp" package is implemented with 'getVirtualFile'. | ||
pluginGetVersionedTextDoc :: TextDocumentIdentifier -> PluginM config VersionedTextDocumentIdentifier | ||
pluginGetVersionedTextDoc = PluginM . getVersionedTextDoc | ||
|
||
-- | Wrapper of 'getClientCapabilities' for PluginM | ||
pluginGetClientCapabilities :: PluginM config ClientCapabilities | ||
pluginGetClientCapabilities = PluginM getClientCapabilities | ||
|
||
-- | Wrapper of 'sendNotification for PluginM | ||
-- | ||
-- TODO: Return notification in result instead of calling `sendNotification` directly | ||
pluginSendNotification :: forall (m :: Method ServerToClient Notification) config. SServerMethod m -> MessageParams m -> PluginM config () | ||
pluginSendNotification smethod params = PluginM $ sendNotification smethod params | ||
|
||
-- | Wrapper of 'sendRequest' for PluginM | ||
-- | ||
-- TODO: Return request in result instead of calling `sendRequest` directly | ||
pluginSendRequest :: forall (m :: Method ServerToClient Request) config. SServerMethod m -> MessageParams m -> (Either (TResponseError m) (MessageResult m) -> PluginM config ()) -> PluginM config (LspId m) | ||
pluginSendRequest smethod params action = PluginM $ sendRequest smethod params (runPluginM . action) | ||
|
||
-- | Wrapper of 'withIndefiniteProgress' for PluginM | ||
pluginWithIndefiniteProgress :: T.Text -> Maybe ProgressToken -> ProgressCancellable -> ((T.Text -> PluginM config ()) -> PluginM config a) -> PluginM config a | ||
pluginWithIndefiniteProgress title progressToken cancellable updateAction = | ||
PluginM $ | ||
withIndefiniteProgress title progressToken cancellable $ \putUpdate -> | ||
runPluginM $ updateAction (PluginM . putUpdate) | ||
|
||
-- | Combine handlers for the | ||
newtype PluginHandler a (m :: Method ClientToServer Request) | ||
= PluginHandler (PluginId -> a -> MessageParams m -> LspM Config (NonEmpty (Either PluginError (MessageResult m)))) | ||
= PluginHandler (PluginId -> a -> MessageParams m -> PluginM Config (NonEmpty (Either PluginError (MessageResult m)))) | ||
|
||
newtype PluginNotificationHandler a (m :: Method ClientToServer Notification) | ||
= PluginNotificationHandler (PluginId -> a -> VFS -> MessageParams m -> LspM Config ()) | ||
|
@@ -917,7 +963,7 @@ instance Semigroup (PluginNotificationHandlers a) where | |
instance Monoid (PluginNotificationHandlers a) where | ||
mempty = PluginNotificationHandlers mempty | ||
|
||
type PluginMethodHandler a m = a -> PluginId -> MessageParams m -> ExceptT PluginError (LspM Config) (MessageResult m) | ||
type PluginMethodHandler a m = a -> PluginId -> MessageParams m -> ExceptT PluginError (PluginM Config) (MessageResult m) | ||
|
||
type PluginNotificationMethodHandler a m = a -> VFS -> PluginId -> MessageParams m -> LspM Config () | ||
|
||
|
@@ -930,7 +976,7 @@ mkPluginHandler | |
-> PluginHandlers ideState | ||
mkPluginHandler m f = PluginHandlers $ DMap.singleton (IdeMethod m) (PluginHandler (f' m)) | ||
where | ||
f' :: SMethod m -> PluginId -> ideState -> MessageParams m -> LspT Config IO (NonEmpty (Either PluginError (MessageResult m))) | ||
f' :: SMethod m -> PluginId -> ideState -> MessageParams m -> PluginM Config (NonEmpty (Either PluginError (MessageResult m))) | ||
-- We need to have separate functions for each method that supports resolve, so far we only support CodeActions | ||
-- CodeLens, and Completion methods. | ||
f' SMethod_TextDocumentCodeAction pid ide params@CodeActionParams{_textDocument=TextDocumentIdentifier {_uri}} = | ||
|
@@ -1034,7 +1080,7 @@ type CommandFunction ideState a | |
= ideState | ||
-> Maybe ProgressToken | ||
-> a | ||
-> ExceptT PluginError (LspM Config) (Value |? Null) | ||
-> ExceptT PluginError (PluginM Config) (Value |? Null) | ||
|
||
-- --------------------------------------------------------------------- | ||
|
||
|
@@ -1044,7 +1090,7 @@ type ResolveFunction ideState a (m :: Method ClientToServer Request) = | |
-> MessageParams m | ||
-> Uri | ||
-> a | ||
-> ExceptT PluginError (LspM Config) (MessageResult m) | ||
-> ExceptT PluginError (PluginM Config) (MessageResult m) | ||
|
||
-- | Make a handler for resolve methods. In here we take your provided ResolveFunction | ||
-- and turn it into a PluginHandlers. See Note [Resolve in PluginHandlers] | ||
|
@@ -1126,7 +1172,7 @@ type FormattingHandler a | |
-> T.Text | ||
-> NormalizedFilePath | ||
-> FormattingOptions | ||
-> ExceptT PluginError (LspM Config) ([TextEdit] |? Null) | ||
-> ExceptT PluginError (PluginM Config) ([TextEdit] |? Null) | ||
|
||
mkFormattingHandlers :: forall a. FormattingHandler a -> PluginHandlers a | ||
mkFormattingHandlers f = mkPluginHandler SMethod_TextDocumentFormatting ( provider SMethod_TextDocumentFormatting) | ||
|
@@ -1135,7 +1181,7 @@ mkFormattingHandlers f = mkPluginHandler SMethod_TextDocumentFormatting ( provid | |
provider :: forall m. FormattingMethod m => SMethod m -> PluginMethodHandler a m | ||
provider m ide _pid params | ||
| Just nfp <- uriToNormalizedFilePath $ toNormalizedUri uri = do | ||
mf <- lift $ getVirtualFile $ toNormalizedUri uri | ||
mf <- lift $ pluginGetVirtualFile $ toNormalizedUri uri | ||
case mf of | ||
Just vf -> do | ||
let (typ, mtoken) = case m of | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Worth explaining why this is here! Something like:
We use this instead of
LspM
, since there are parts of the LSP server state whichplugins should not access directly, but instead only via the build system.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added your explanation to the comment!