@@ -0,0 +1,50 @@
1+-- Copyright (c) 2019 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
2+-- SPDX-License-Identifier: Apache-2.0
3+
4+{-# LANGUAGE OverloadedStrings #-}
5+{-# LANGUAGE DuplicateRecordFields #-}
6+
7+-- | Go to the definition of a variable.
8+module Development.IDE.LSP.CodeAction
9+ ( setHandlersCodeAction
10+ ) where
11+
12+import Language.Haskell.LSP.Types
13+
14+import Development.IDE.Core.Rules
15+import Development.IDE.LSP.Server
16+import qualified Data.HashMap.Strict as Map
17+import qualified Language.Haskell.LSP.Core as LSP
18+import qualified Language.Haskell.LSP.Types as LSP
19+import Language.Haskell.LSP.Messages
20+
21+import qualified Data.Text as T
22+
23+-- | Generate code actions.
24+codeAction
25+ :: IdeState
26+ -> CodeActionParams
27+ -> IO (List CAResult)
28+codeAction _ CodeActionParams{_textDocument=TextDocumentIdentifier uri,_context=CodeActionContext{_diagnostics=List xs}} = do
29+ -- disable logging as its quite verbose
30+ -- logInfo (ideLogger ide) $ T.pack $ "Code action req: " ++ show arg
31+ pure $ List
32+ [ CACodeAction $ CodeAction title (Just CodeActionQuickFix) (Just $ List [x]) (Just edit) Nothing
33+ | x <- xs, (title, edit) <- suggestAction uri x]
34+
35+
36+suggestAction :: Uri -> Diagnostic -> [(T.Text, LSP.WorkspaceEdit)]
37+suggestAction uri Diagnostic{..}
38+-- File.hs:16:1: warning:
39+-- The import of `Data.List' is redundant
40+-- except perhaps to import instances from `Data.List'
41+-- To import instances alone, use: import Data.List()
42+ | "The import of " `T.isInfixOf` _message
43+ , " is redundant" `T.isInfixOf` _message
44+ = [("Remove import", WorkspaceEdit (Just $ Map.singleton uri $ List [TextEdit _range ""]) Nothing)]
45+suggestAction _ _ = []
46+
47+setHandlersCodeAction :: PartialHandlers
48+setHandlersCodeAction = PartialHandlers $ \WithMessage{..} x -> return x{
49+ LSP.codeActionHandler = withResponse RspCodeAction codeAction
50+ }