on master help sign in
Add "Remove import" code action (#1945)

* Add a code action * Update comments * Remove logging, since its too verbose * Fix a few warnings, add a final case * Add an example of what the code action matches

commit9a45c0d17e120d064a47d84c939e412f8e9f1e89 authorNeil Mitchell <35463327+neil-da@users.noreply.github.com> dateFri 28 Jun 2019, 18:37 (7 years ago) parenteddf78a
3 files changed+53−1
hie-core.cabal+1 src/Development/IDE/LSP/CodeAction.hs+50 src/Development/IDE/LSP/LanguageServer.hs+2−1
hie-core.cabal+1
@@ -87,6 +87,7 @@ library
8787 Development.IDE.GHC.Warnings
8888 Development.IDE.Import.DependencyInformation
8989 Development.IDE.Import.FindImports
90+ Development.IDE.LSP.CodeAction
9091 Development.IDE.LSP.Definition
9192 Development.IDE.LSP.Hover
9293 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
2727
2828import Development.IDE.LSP.Definition
2929import Development.IDE.LSP.Hover
30+import Development.IDE.LSP.CodeAction
3031import Development.IDE.LSP.Notifications
3132import Development.IDE.Core.Service
3233import Development.IDE.Types.Logger
@@ -68,7 +69,7 @@ runLanguageServer options userHandlers getIdeState = do
6869 let withNotification old f = Just $ \r -> writeChan clientMsgChan $ Notification r (\ide x -> f ide x >> whenJust old ($ r))
6970 let PartialHandlers parts =
7071 setHandlersIgnore <> -- least important
71− setHandlersDefinition <> setHandlersHover <> -- useful features someone may override
72+ setHandlersDefinition <> setHandlersHover <> setHandlersCodeAction <> -- useful features someone may override
7273 userHandlers <>
7374 setHandlersNotifications -- absolutely critical, join them with user notifications
7475 handlers <- parts WithMessage{withResponse, withNotification} def