1
1
{-# LANGUAGE ViewPatterns #-}
2
+ {-# LANGUAGE DeriveAnyClass #-}
2
3
{-# LANGUAGE DeriveGeneric #-}
3
4
{-# LANGUAGE DuplicateRecordFields #-}
4
5
{-# LANGUAGE FlexibleContexts #-}
5
6
{-# LANGUAGE FlexibleInstances #-}
6
7
{-# LANGUAGE OverloadedStrings #-}
7
- {-# LANGUAGE RecordWildCards #-}
8
8
{-# LANGUAGE TupleSections #-}
9
9
{-# LANGUAGE TypeFamilies #-}
10
10
11
11
module Ide.Plugin.Example
12
12
(
13
13
descriptor
14
- , plugin
15
- , hover
16
- , codeAction
17
14
) where
18
15
19
16
import Control.DeepSeq ( NFData )
20
17
import Control.Monad.Trans.Maybe
21
- import Data.Aeson.Types ( toJSON , fromJSON , Value ( .. ), Result ( .. ))
18
+ import Data.Aeson
22
19
import Data.Binary
23
20
import Data.Functor
24
21
import qualified Data.HashMap.Strict as Map
@@ -31,16 +28,13 @@ import Development.IDE.Core.RuleTypes
31
28
import Development.IDE.Core.Rules
32
29
import Development.IDE.Core.Service
33
30
import Development.IDE.Core.Shake
34
- import Development.IDE.LSP.Server
35
- import Development.IDE.Plugin
36
31
import Development.IDE.Types.Diagnostics as D
37
32
import Development.IDE.Types.Location
38
33
import Development.IDE.Types.Logger
39
34
import Development.Shake hiding ( Diagnostic )
40
35
import GHC.Generics
36
+ import Ide.Plugin
41
37
import Ide.Types
42
- import qualified Language.Haskell.LSP.Core as LSP
43
- import Language.Haskell.LSP.Messages
44
38
import Language.Haskell.LSP.Types
45
39
import Text.Regex.TDFA.Text ()
46
40
@@ -50,8 +44,9 @@ descriptor :: PluginId -> PluginDescriptor
50
44
descriptor plId = PluginDescriptor
51
45
{ pluginId = plId
52
46
, pluginRules = exampleRules
53
- , pluginCommands = []
47
+ , pluginCommands = [PluginCommand " codelens.todo " " example adding " addTodoCmd ]
54
48
, pluginCodeActionProvider = Just codeAction
49
+ , pluginCodeLensProvider = Just codeLens
55
50
, pluginDiagnosticProvider = Nothing
56
51
, pluginHoverProvider = Just hover
57
52
, pluginSymbolProvider = Nothing
@@ -61,23 +56,15 @@ descriptor plId = PluginDescriptor
61
56
62
57
-- ---------------------------------------------------------------------
63
58
64
- plugin :: Plugin c
65
- plugin = Plugin mempty exampleRules handlersExample
66
- -- <> codeActionPlugin codeAction
67
- <> Plugin mempty mempty handlersCodeLens
68
-
69
59
hover :: IdeState -> TextDocumentPositionParams -> IO (Either ResponseError (Maybe Hover ))
70
60
hover = request " Hover" blah (Right Nothing ) foundHover
71
61
72
62
blah :: NormalizedFilePath -> Position -> Action (Maybe (Maybe Range , [T. Text ]))
73
63
blah _ (Position line col)
74
64
= return $ Just (Just (Range (Position line col) (Position (line+ 1 ) 0 )), [" example hover 1\n " ])
75
65
76
- handlersExample :: PartialHandlers c
77
- handlersExample = mempty
78
- -- handlersExample = PartialHandlers $ \WithMessage{..} x ->
79
- -- return x{LSP.hoverHandler = withResponse RspHover $ const hover}
80
-
66
+ -- ---------------------------------------------------------------------
67
+ -- Generating Diagnostics via rules
81
68
-- ---------------------------------------------------------------------
82
69
83
70
data Example = Example
@@ -116,6 +103,8 @@ mkDiag file diagSource sev loc msg = (file, D.ShowDiag,)
116
103
}
117
104
118
105
-- ---------------------------------------------------------------------
106
+ -- code actions
107
+ -- ---------------------------------------------------------------------
119
108
120
109
-- | Generate code actions.
121
110
codeAction
@@ -136,49 +125,50 @@ codeAction _state _pid (TextDocumentIdentifier uri) _range CodeActionContext{_di
136
125
137
126
-- ---------------------------------------------------------------------
138
127
139
- -- | Generate code lenses.
140
- handlersCodeLens :: PartialHandlers c
141
- handlersCodeLens = PartialHandlers $ \ WithMessage {.. } x -> return x{
142
- LSP. codeLensHandler = withResponse RspCodeLens codeLens,
143
- LSP. executeCommandHandler = withResponseAndRequest RspExecuteCommand ReqApplyWorkspaceEdit executeAddSignatureCommand
144
- }
145
-
146
128
codeLens
147
- :: LSP. LspFuncs c
148
- -> IdeState
129
+ :: IdeState
130
+ -> PluginId
149
131
-> CodeLensParams
150
132
-> IO (Either ResponseError (List CodeLens ))
151
- codeLens _lsp ideState CodeLensParams {_textDocument= TextDocumentIdentifier uri} = do
133
+ codeLens ideState plId CodeLensParams {_textDocument= TextDocumentIdentifier uri} = do
134
+ logInfo (ideLogger ideState) " Example.codeLens entered (ideLogger)" -- AZ
152
135
case uriToFilePath' uri of
153
136
Just (toNormalizedFilePath -> filePath) -> do
154
137
_ <- runAction ideState $ runMaybeT $ useE TypeCheck filePath
155
138
_diag <- getDiagnostics ideState
156
139
_hDiag <- getHiddenDiagnostics ideState
157
140
let
158
141
title = " Add TODO Item via Code Lens"
159
- tedit = [TextEdit (Range (Position 3 0 ) (Position 3 0 ))
160
- " -- TODO added by Example Plugin via code lens action\n " ]
161
- edit = WorkspaceEdit (Just $ Map. singleton uri $ List tedit) Nothing
162
- range = (Range (Position 3 0 ) (Position 4 0 ))
163
- pure $ Right $ List
164
- -- [ CodeLens range (Just (Command title "codelens.do" (Just $ List [toJSON edit]))) Nothing
165
- [ CodeLens range (Just (Command title " codelens.todo" (Just $ List [toJSON edit]))) Nothing
166
- ]
142
+ -- tedit = [TextEdit (Range (Position 3 0) (Position 3 0))
143
+ -- "-- TODO added by Example Plugin via code lens action\n"]
144
+ -- edit = WorkspaceEdit (Just $ Map.singleton uri $ List tedit) Nothing
145
+ range = Range (Position 3 0 ) (Position 4 0 )
146
+ let cmdParams = AddTodoParams uri " do abc"
147
+ cmd <- mkLspCommand plId " codelens.todo" title (Just [(toJSON cmdParams)])
148
+ pure $ Right $ List [ CodeLens range (Just cmd) Nothing ]
167
149
Nothing -> pure $ Right $ List []
168
150
169
- -- | Execute the "codelens.todo" command.
170
- executeAddSignatureCommand
171
- :: LSP. LspFuncs c
172
- -> IdeState
173
- -> ExecuteCommandParams
174
- -> IO (Either ResponseError Value , Maybe (ServerMethod , ApplyWorkspaceEditParams ))
175
- executeAddSignatureCommand _lsp _ideState ExecuteCommandParams {.. }
176
- | _command == " codelens.todo"
177
- , Just (List [edit]) <- _arguments
178
- , Success wedit <- fromJSON edit
179
- = return (Right Null , Just (WorkspaceApplyEdit , ApplyWorkspaceEditParams wedit))
180
- | otherwise
181
- = return (Right Null , Nothing )
151
+ -- ---------------------------------------------------------------------
152
+ -- | Parameters for the addTodo PluginCommand.
153
+ data AddTodoParams = AddTodoParams
154
+ { file :: Uri -- ^ Uri of the file to add the pragma to
155
+ , todoText :: T. Text
156
+ }
157
+ deriving (Show , Eq , Generic , ToJSON , FromJSON )
158
+
159
+ addTodoCmd :: AddTodoParams -> IO (Either ResponseError Value ,
160
+ Maybe (ServerMethod , ApplyWorkspaceEditParams ))
161
+ addTodoCmd (AddTodoParams uri todoText) = do
162
+ let
163
+ pos = Position 0 0
164
+ textEdits = List
165
+ [TextEdit (Range pos pos)
166
+ (" -- TODO:" <> todoText)
167
+ ]
168
+ res = WorkspaceEdit
169
+ (Just $ Map. singleton uri textEdits)
170
+ Nothing
171
+ return (Right Null , Just (WorkspaceApplyEdit , ApplyWorkspaceEditParams res))
182
172
183
173
-- ---------------------------------------------------------------------
184
174
@@ -203,7 +193,8 @@ request label getResults notFound found ide (TextDocumentPositionParams (TextDoc
203
193
Nothing -> pure Nothing
204
194
pure $ maybe notFound found mbResult
205
195
206
- logAndRunRequest :: T. Text -> (NormalizedFilePath -> Position -> Action b ) -> IdeState -> Position -> String -> IO b
196
+ logAndRunRequest :: T. Text -> (NormalizedFilePath -> Position -> Action b )
197
+ -> IdeState -> Position -> String -> IO b
207
198
logAndRunRequest label getResults ide pos path = do
208
199
let filePath = toNormalizedFilePath path
209
200
logInfo (ideLogger ide) $
0 commit comments