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
@@ -15,25 +15,26 @@ module Ide.Plugin.Example2
1515
1616import Control.DeepSeq ( NFData )
1717import Control.Monad.Trans.Maybe
18- import Data.Aeson.Types ( toJSON )
18+ import Data.Aeson
1919import Data.Binary
2020import Data.Functor
2121import qualified Data.HashMap.Strict as Map
22- import Data.Hashable
2322import qualified Data.HashSet as HashSet
23+ import Data.Hashable
2424import qualified Data.Text as T
2525import Data.Typeable
2626import Development.IDE.Core.OfInterest
27- import Development.IDE.Core.Rules
2827import Development.IDE.Core.RuleTypes
28+ import Development.IDE.Core.Rules
2929import Development.IDE.Core.Service
3030import Development.IDE.Core.Shake
3131import Development.IDE.Types.Diagnostics as D
3232import Development.IDE.Types.Location
3333import Development.IDE.Types.Logger
3434import Development.Shake hiding ( Diagnostic )
35- import Ide.Types
3635import GHC.Generics
36+ import Ide.Plugin
37+ import Ide.Types
3738import Language.Haskell.LSP.Types
3839import Text.Regex.TDFA.Text ()
3940
@@ -43,12 +44,12 @@ descriptor :: PluginId -> PluginDescriptor
4344descriptor plId = PluginDescriptor
4445 { pluginId = plId
4546 , pluginRules = exampleRules
46- , pluginCommands = []
47+ , pluginCommands = [PluginCommand " codelens.todo " " example adding " addTodoCmd ]
4748 , pluginCodeActionProvider = Just codeAction
4849 , pluginCodeLensProvider = Just codeLens
4950 , pluginDiagnosticProvider = Nothing
50- , pluginHoverProvider = Just hover
51- , pluginSymbolProvider = Nothing
51+ , pluginHoverProvider = Just hover
52+ , pluginSymbolProvider = Nothing
5253 , pluginFormattingProvider = Nothing
5354 , pluginCompletionProvider = Nothing
5455 }
@@ -63,6 +64,8 @@ blah _ (Position line col)
6364 = return $ Just (Just (Range (Position line col) (Position (line+ 1 ) 0 )), [" example hover 2\n " ])
6465
6566-- ---------------------------------------------------------------------
67+ -- Generating Diagnostics via rules
68+ -- ---------------------------------------------------------------------
6669
6770data Example2 = Example2
6871 deriving (Eq , Show , Typeable , Generic )
@@ -100,6 +103,8 @@ mkDiag file diagSource sev loc msg = (file, D.ShowDiag,)
100103 }
101104
102105-- ---------------------------------------------------------------------
106+ -- code actions
107+ -- ---------------------------------------------------------------------
103108
104109-- | Generate code actions.
105110codeAction
@@ -125,24 +130,43 @@ codeLens
125130 -> PluginId
126131 -> CodeLensParams
127132 -> IO (Either ResponseError (List CodeLens ))
128- codeLens ideState _plId CodeLensParams {_textDocument= TextDocumentIdentifier uri} =
133+ codeLens ideState plId CodeLensParams {_textDocument= TextDocumentIdentifier uri} = do
134+ logInfo (ideLogger ideState) " Example2.codeLens entered (ideLogger)" -- AZ
129135 case uriToFilePath' uri of
130136 Just (toNormalizedFilePath -> filePath) -> do
131137 _ <- runAction ideState $ runMaybeT $ useE TypeCheck filePath
132138 _diag <- getDiagnostics ideState
133139 _hDiag <- getHiddenDiagnostics ideState
134140 let
135141 title = " Add TODO2 Item via Code Lens"
136- tedit = [TextEdit (Range (Position 3 0 ) (Position 3 0 ))
137- " -- TODO2 added by Example2 Plugin via code lens action\n " ]
138- edit = WorkspaceEdit (Just $ Map. singleton uri $ List tedit) Nothing
139142 range = Range (Position 3 0 ) (Position 4 0 )
140- pure $ Right $ List
141- -- [ CodeLens range (Just (Command title "codelens.do" (Just $ List [toJSON edit]))) Nothing
142- [ CodeLens range (Just (Command title " codelens.todo" (Just $ List [toJSON edit]))) Nothing
143- ]
143+ let cmdParams = AddTodoParams uri " do abc"
144+ cmd <- mkLspCommand plId " codelens.todo" title (Just [toJSON cmdParams])
145+ pure $ Right $ List [ CodeLens range (Just cmd) Nothing ]
144146 Nothing -> pure $ Right $ List []
145147
148+ -- ---------------------------------------------------------------------
149+ -- | Parameters for the addTodo PluginCommand.
150+ data AddTodoParams = AddTodoParams
151+ { file :: Uri -- ^ Uri of the file to add the pragma to
152+ , todoText :: T. Text
153+ }
154+ deriving (Show , Eq , Generic , ToJSON , FromJSON )
155+
156+ addTodoCmd :: AddTodoParams -> IO (Either ResponseError Value ,
157+ Maybe (ServerMethod , ApplyWorkspaceEditParams ))
158+ addTodoCmd (AddTodoParams uri todoText) = do
159+ let
160+ pos = Position 0 0
161+ textEdits = List
162+ [TextEdit (Range pos pos)
163+ (" -- TODO2:" <> todoText <> " \n " )
164+ ]
165+ res = WorkspaceEdit
166+ (Just $ Map. singleton uri textEdits)
167+ Nothing
168+ return (Right Null , Just (WorkspaceApplyEdit , ApplyWorkspaceEditParams res))
169+
146170-- ---------------------------------------------------------------------
147171
148172foundHover :: (Maybe Range , [T. Text ]) -> Either ResponseError (Maybe Hover )
@@ -166,7 +190,8 @@ request label getResults notFound found ide (TextDocumentPositionParams (TextDoc
166190 Nothing -> pure Nothing
167191 pure $ maybe notFound found mbResult
168192
169- logAndRunRequest :: T. Text -> (NormalizedFilePath -> Position -> Action b ) -> IdeState -> Position -> String -> IO b
193+ logAndRunRequest :: T. Text -> (NormalizedFilePath -> Position -> Action b )
194+ -> IdeState -> Position -> String -> IO b
170195logAndRunRequest label getResults ide pos path = do
171196 let filePath = toNormalizedFilePath path
172197 logInfo (ideLogger ide) $
0 commit comments