on master help sign in
Add "Remove import" code action (#1945)
9a45c0dNeil Mitchell
hie-core.cabal+1
@@ -87,6 +87,7 @@ library
87 Development.IDE.GHC.Warnings
87 Development.IDE.GHC.Warnings
88 Development.IDE.Import.DependencyInformation
88 Development.IDE.Import.DependencyInformation
89 Development.IDE.Import.FindImports
89 Development.IDE.Import.FindImports
90+ Development.IDE.LSP.CodeAction
90 Development.IDE.LSP.Definition
91 Development.IDE.LSP.Definition
91 Development.IDE.LSP.Hover
92 Development.IDE.LSP.Hover
92 Development.IDE.LSP.LanguageServer
93 Development.IDE.LSP.LanguageServer
src/Development/IDE/LSP/CodeAction.hsnew file+50
@@ -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+ }
src/Development/IDE/LSP/LanguageServer.hs+2−1
@@ -27,6 +27,7 @@ import Control.Monad.Extra
27
27
28import Development.IDE.LSP.Definition
28import Development.IDE.LSP.Definition
29import Development.IDE.LSP.Hover
29import Development.IDE.LSP.Hover
30+import Development.IDE.LSP.CodeAction
30import Development.IDE.LSP.Notifications
31import Development.IDE.LSP.Notifications
31import Development.IDE.Core.Service
32import Development.IDE.Core.Service
32import Development.IDE.Types.Logger
33import Development.IDE.Types.Logger
@@ -68,7 +69,7 @@ runLanguageServer options userHandlers getIdeState = do
68 let withNotification old f = Just $ \r -> writeChan clientMsgChan $ Notification r (\ide x -> f ide x >> whenJust old ($ r))
69 let withNotification old f = Just $ \r -> writeChan clientMsgChan $ Notification r (\ide x -> f ide x >> whenJust old ($ r))
69 let PartialHandlers parts =
70 let PartialHandlers parts =
70 setHandlersIgnore <> -- least important
71 setHandlersIgnore <> -- least important
71− setHandlersDefinition <> setHandlersHover <> -- useful features someone may override
72+ setHandlersDefinition <> setHandlersHover <> setHandlersCodeAction <> -- useful features someone may override
72 userHandlers <>
73 userHandlers <>
73 setHandlersNotifications -- absolutely critical, join them with user notifications
74 setHandlersNotifications -- absolutely critical, join them with user notifications
74 handlers <- parts WithMessage{withResponse, withNotification} def
75 handlers <- parts WithMessage{withResponse, withNotification} def