11{-# LANGUAGE ViewPatterns #-}
2+ {-# LANGUAGE DeriveAnyClass #-}
23{-# LANGUAGE DeriveGeneric #-}
34{-# LANGUAGE DuplicateRecordFields #-}
45{-# LANGUAGE FlexibleContexts #-}
56{-# LANGUAGE FlexibleInstances #-}
67{-# LANGUAGE OverloadedStrings #-}
7- {-# LANGUAGE RecordWildCards #-}
88{-# LANGUAGE TupleSections #-}
99{-# LANGUAGE TypeFamilies #-}
1010
1111module Ide.Plugin.Example
1212 (
1313 descriptor
14- , plugin
15- , hover
16- , codeAction
1714 ) where
1815
1916import Control.DeepSeq ( NFData )
2017import Control.Monad.Trans.Maybe
21- import Data.Aeson.Types ( toJSON , fromJSON , Value ( .. ), Result ( .. ))
18+ import Data.Aeson
2219import Data.Binary
2320import Data.Functor
2421import qualified Data.HashMap.Strict as Map
@@ -31,16 +28,13 @@ import Development.IDE.Core.RuleTypes
3128import Development.IDE.Core.Rules
3229import Development.IDE.Core.Service
3330import Development.IDE.Core.Shake
34- import Development.IDE.LSP.Server
35- import Development.IDE.Plugin
3631import Development.IDE.Types.Diagnostics as D
3732import Development.IDE.Types.Location
3833import Development.IDE.Types.Logger
3934import Development.Shake hiding ( Diagnostic )
4035import GHC.Generics
36+ import Ide.Plugin
4137import Ide.Types
42- import qualified Language.Haskell.LSP.Core as LSP
43- import Language.Haskell.LSP.Messages
4438import Language.Haskell.LSP.Types
4539import Text.Regex.TDFA.Text ()
4640
@@ -50,8 +44,9 @@ descriptor :: PluginId -> PluginDescriptor
5044descriptor plId = PluginDescriptor
5145 { pluginId = plId
5246 , pluginRules = exampleRules
53- , pluginCommands = []
47+ , pluginCommands = [PluginCommand " codelens.todo " " example adding " addTodoCmd ]
5448 , pluginCodeActionProvider = Just codeAction
49+ , pluginCodeLensProvider = Just codeLens
5550 , pluginDiagnosticProvider = Nothing
5651 , pluginHoverProvider = Just hover
5752 , pluginSymbolProvider = Nothing
@@ -61,23 +56,15 @@ descriptor plId = PluginDescriptor
6156
6257-- ---------------------------------------------------------------------
6358
64- plugin :: Plugin c
65- plugin = Plugin mempty exampleRules handlersExample
66- -- <> codeActionPlugin codeAction
67- <> Plugin mempty mempty handlersCodeLens
68-
6959hover :: IdeState -> TextDocumentPositionParams -> IO (Either ResponseError (Maybe Hover ))
7060hover = request " Hover" blah (Right Nothing ) foundHover
7161
7262blah :: NormalizedFilePath -> Position -> Action (Maybe (Maybe Range , [T. Text ]))
7363blah _ (Position line col)
7464 = return $ Just (Just (Range (Position line col) (Position (line+ 1 ) 0 )), [" example hover 1\n " ])
7565
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
8168-- ---------------------------------------------------------------------
8269
8370data Example = Example
@@ -116,6 +103,8 @@ mkDiag file diagSource sev loc msg = (file, D.ShowDiag,)
116103 }
117104
118105-- ---------------------------------------------------------------------
106+ -- code actions
107+ -- ---------------------------------------------------------------------
119108
120109-- | Generate code actions.
121110codeAction
@@ -136,49 +125,50 @@ codeAction _state _pid (TextDocumentIdentifier uri) _range CodeActionContext{_di
136125
137126-- ---------------------------------------------------------------------
138127
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-
146128codeLens
147- :: LSP. LspFuncs c
148- -> IdeState
129+ :: IdeState
130+ -> PluginId
149131 -> CodeLensParams
150132 -> 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
152135 case uriToFilePath' uri of
153136 Just (toNormalizedFilePath -> filePath) -> do
154137 _ <- runAction ideState $ runMaybeT $ useE TypeCheck filePath
155138 _diag <- getDiagnostics ideState
156139 _hDiag <- getHiddenDiagnostics ideState
157140 let
158141 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 ]
167149 Nothing -> pure $ Right $ List []
168150
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))
182172
183173-- ---------------------------------------------------------------------
184174
@@ -203,7 +193,8 @@ request label getResults notFound found ide (TextDocumentPositionParams (TextDoc
203193 Nothing -> pure Nothing
204194 pure $ maybe notFound found mbResult
205195
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
207198logAndRunRequest label getResults ide pos path = do
208199 let filePath = toNormalizedFilePath path
209200 logInfo (ideLogger ide) $
0 commit comments