Skip to content

Commit 1aae2cf

Browse files
committed
ghcide code lens command now works
1 parent b555a6e commit 1aae2cf

File tree

8 files changed

+47
-24
lines changed

8 files changed

+47
-24
lines changed

exe/Main.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ idePlugins pid includeExamples
114114
-- , hsimportDescriptor "hsimport"
115115
-- , liquidDescriptor "liquid"
116116
-- , packageDescriptor "package"
117-
GhcIde.descriptor "ghc"
117+
GhcIde.descriptor "ghcide"
118118
, Pragmas.descriptor "pragmas"
119119
, Floskell.descriptor "floskell"
120120
-- , genericDescriptor "generic"
@@ -164,7 +164,7 @@ main = do
164164
plugins = Completions.plugin <> CodeAction.plugin <>
165165
Plugin mempty HoverDefinition.setHandlersDefinition <>
166166
ps
167-
options = def { LSP.executeCommandCommands = Just (pid <> ":typesignature.add":commandIds)
167+
options = def { LSP.executeCommandCommands = Just commandIds
168168
, LSP.completionTriggerCharacters = Just "."
169169
}
170170

src/Ide/Plugin.hs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ executeCommandHandlers ecs = PartialHandlers $ \WithMessage{..} x -> return x{
205205
-- -> ExecuteCommandParams
206206
-- -> IO (Either ResponseError Value, Maybe (ServerMethod, ApplyWorkspaceEditParams))
207207
makeExecuteCommands :: [(PluginId, [PluginCommand])] -> LSP.LspFuncs Config -> ExecuteCommandProvider
208-
makeExecuteCommands ecs _lf _params = do
208+
makeExecuteCommands ecs lf ide = do
209209
let
210210
pluginMap = Map.fromList ecs
211211
parseCmdId :: T.Text -> Maybe (PluginId, CommandId)
@@ -250,7 +250,7 @@ makeExecuteCommands ecs _lf _params = do
250250
-- "Invalid fallbackCodeAction params"
251251

252252
-- Just an ordinary HIE command
253-
Just (plugin, cmd) -> runPluginCommand pluginMap plugin cmd cmdParams
253+
Just (plugin, cmd) -> runPluginCommand pluginMap lf ide plugin cmd cmdParams
254254

255255
-- Couldn't parse the command identifier
256256
_ -> return (Left $ ResponseError InvalidParams "Invalid command identifier" Nothing, Nothing)
@@ -333,20 +333,33 @@ makeExecuteCommands ecs _lf _params = do
333333

334334
-- | Runs a plugin command given a PluginId, CommandId and
335335
-- arguments in the form of a JSON object.
336-
runPluginCommand :: Map.Map PluginId [PluginCommand] -> PluginId -> CommandId -> J.Value
337-
-> IO (Either ResponseError J.Value,
336+
runPluginCommand :: Map.Map PluginId [PluginCommand]
337+
-> LSP.LspFuncs Config
338+
-> IdeState
339+
-> PluginId
340+
-> CommandId
341+
-> J.Value
342+
-> IO (Either ResponseError J.Value,
338343
Maybe (ServerMethod, ApplyWorkspaceEditParams))
339-
runPluginCommand m p@(PluginId p') com@(CommandId com') arg =
344+
runPluginCommand m lf ide p@(PluginId p') com@(CommandId com') arg =
340345
case Map.lookup p m of
341346
Nothing -> return
342347
(Left $ ResponseError InvalidRequest ("Plugin " <> p' <> " doesn't exist") Nothing, Nothing)
343348
Just xs -> case List.find ((com ==) . commandId) xs of
344349
Nothing -> return (Left $
345-
ResponseError InvalidRequest ("Command " <> com' <> " isn't defined for plugin " <> p' <> ". Legal commands are: " <> T.pack(show $ map commandId xs)) Nothing, Nothing)
350+
ResponseError InvalidRequest ("Command " <> com' <> " isn't defined for plugin " <> p'
351+
<> ". Legal commands are: " <> T.pack(show $ map commandId xs)) Nothing, Nothing)
346352
Just (PluginCommand _ _ f) -> case J.fromJSON arg of
347353
J.Error err -> return (Left $
348-
ResponseError InvalidParams ("error while parsing args for " <> com' <> " in plugin " <> p' <> ": " <> T.pack err) Nothing, Nothing)
349-
J.Success a -> f a
354+
ResponseError InvalidParams ("error while parsing args for " <> com' <> " in plugin " <> p'
355+
<> ": " <> T.pack err
356+
<> "\narg = " <> T.pack (show arg)) Nothing, Nothing)
357+
J.Success a -> f lf ide a
358+
359+
-- lsp-request: error while parsing args for typesignature.add in plugin ghcide:
360+
-- When parsing the record ExecuteCommandParams of type
361+
-- Language.Haskell.LSP.Types.DataTypesJSON.ExecuteCommandParams the key command
362+
-- was not present.
350363

351364
-- -----------------------------------------------------------
352365

src/Ide/Plugin/Example.hs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,8 @@ data AddTodoParams = AddTodoParams
146146
}
147147
deriving (Show, Eq, Generic, ToJSON, FromJSON)
148148

149-
addTodoCmd :: AddTodoParams -> IO (Either ResponseError Value,
150-
Maybe (ServerMethod, ApplyWorkspaceEditParams))
151-
addTodoCmd (AddTodoParams uri todoText) = do
149+
addTodoCmd :: CommandFunction AddTodoParams
150+
addTodoCmd _lf _ide (AddTodoParams uri todoText) = do
152151
let
153152
pos = Position 3 0
154153
textEdits = List

src/Ide/Plugin/Example2.hs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,8 @@ data AddTodoParams = AddTodoParams
143143
}
144144
deriving (Show, Eq, Generic, ToJSON, FromJSON)
145145

146-
addTodoCmd :: AddTodoParams -> IO (Either ResponseError Value,
147-
Maybe (ServerMethod, ApplyWorkspaceEditParams))
148-
addTodoCmd (AddTodoParams uri todoText) = do
146+
addTodoCmd :: CommandFunction AddTodoParams
147+
addTodoCmd _lf _ide (AddTodoParams uri todoText) = do
149148
let
150149
pos = Position 5 0
151150
textEdits = List

src/Ide/Plugin/GhcIde.hs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,22 @@ module Ide.Plugin.GhcIde
55
descriptor
66
) where
77

8+
import Data.Aeson
89
import Development.IDE.Core.Service
910
import Development.IDE.LSP.HoverDefinition
11+
import Development.IDE.Plugin.CodeAction
1012
import Development.IDE.Types.Logger
1113
import Ide.Types
14+
import Language.Haskell.LSP.Types
1215
import Text.Regex.TDFA.Text()
13-
import Development.IDE.Plugin.CodeAction
1416

1517
-- ---------------------------------------------------------------------
1618

1719
descriptor :: PluginId -> PluginDescriptor
1820
descriptor plId = PluginDescriptor
1921
{ pluginId = plId
2022
, pluginRules = mempty
21-
, pluginCommands = []
23+
, pluginCommands = [PluginCommand (CommandId "typesignature.add") "adds a signature" commandAddSignature]
2224
, pluginCodeActionProvider = Just codeAction'
2325
, pluginCodeLensProvider = Just codeLens'
2426
, pluginDiagnosticProvider = Nothing
@@ -37,6 +39,12 @@ hover' ideState params = do
3739

3840
-- ---------------------------------------------------------------------
3941

42+
commandAddSignature :: CommandFunction WorkspaceEdit
43+
commandAddSignature lf ide params
44+
= executeAddSignatureCommand lf ide (ExecuteCommandParams "typesignature.add" (Just (List [toJSON params])) Nothing)
45+
46+
-- ---------------------------------------------------------------------
47+
4048
codeAction' :: CodeActionProvider
4149
codeAction' lf ide _ doc range context = codeAction lf ide doc range context
4250

src/Ide/Plugin/Pragmas.hs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,8 @@ data AddPragmaParams = AddPragmaParams
5656
-- Pragma is added to the first line of the Uri.
5757
-- It is assumed that the pragma name is a valid pragma,
5858
-- thus, not validated.
59-
-- addPragmaCmd :: AddPragmaParams -> IO (Either ResponseError J.WorkspaceEdit)
60-
addPragmaCmd :: AddPragmaParams -> IO (Either ResponseError Value,
61-
Maybe (ServerMethod, ApplyWorkspaceEditParams))
62-
addPragmaCmd (AddPragmaParams uri pragmaName) = do
59+
addPragmaCmd :: CommandFunction AddPragmaParams
60+
addPragmaCmd _lf _ide (AddPragmaParams uri pragmaName) = do
6361
let
6462
pos = J.Position 0 0
6563
textEdits = J.List

src/Ide/Types.hs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module Ide.Types
1616
, HoverProvider
1717
, CodeActionProvider
1818
, CodeLensProvider
19+
, CommandFunction
1920
, ExecuteCommandProvider
2021
, CompletionProvider
2122
, WithSnippets(..)
@@ -80,10 +81,15 @@ instance IsString CommandId where
8081
data PluginCommand = forall a. (FromJSON a) =>
8182
PluginCommand { commandId :: CommandId
8283
, commandDesc :: T.Text
83-
, commandFunc :: a -> IO (Either ResponseError Value, Maybe (ServerMethod, ApplyWorkspaceEditParams))
84+
, commandFunc :: CommandFunction a
8485
}
8586
-- ---------------------------------------------------------------------
8687

88+
type CommandFunction a = LSP.LspFuncs Config
89+
-> IdeState
90+
-> a
91+
-> IO (Either ResponseError Value, Maybe (ServerMethod, ApplyWorkspaceEditParams))
92+
8793
type CodeActionProvider = LSP.LspFuncs Config
8894
-> IdeState
8995
-> PluginId

0 commit comments

Comments
 (0)