1-- Copyright (c) 2019 The DAML Authors. All rights reserved.
2-- SPDX-License-Identifier: Apache-2.0
4{-# LANGUAGE DuplicateRecordFields #-}
6-- | Go to the definition of a variable.
7module Development.IDE.LSP.CodeAction
8 ( setHandlersCodeAction
11import Language.Haskell.LSP.Types
12import Development.IDE.GHC.Compat
13import Development.IDE.Core.Rules
14import Development.IDE.LSP.Server
15import qualified Data.HashMap.Strict as Map
16import qualified Data.HashSet as Set
17import qualified Language.Haskell.LSP.Core as LSP
18import Language.Haskell.LSP.VFS
19import Language.Haskell.LSP.Messages
20import qualified Data.Rope.UTF16 as Rope
22import qualified Data.Text as T
24-- | Generate code actions.
30codeAction lsp _ CodeActionParams{_textDocument=TextDocumentIdentifier uri,_context=CodeActionContext{_diagnostics=List xs}} = do
31 -- disable logging as its quite verbose
32 -- logInfo (ideLogger ide) $ T.pack $ "Code action req: " ++ show arg
33 contents <- LSP.getVirtualFileFunc lsp $ toNormalizedUri uri
34 let text = Rope.toText . (_text :: VirtualFile -> Rope.Rope) <$> contents
36 [ CACodeAction $ CodeAction title (Just CodeActionQuickFix) (Just $ List [x]) (Just edit) Nothing
37 | x <- xs, (title, tedit) <- suggestAction text x
38 , let edit = WorkspaceEdit (Just $ Map.singleton uri $ List tedit) Nothing
42suggestAction :: Maybe T.Text -> Diagnostic -> [(T.Text, [TextEdit])]
43suggestAction contents Diagnostic{_range=_range@Range{..},..}
45-- File.hs:16:1: warning:
46-- The import of `Data.List' is redundant
47-- except perhaps to import instances from `Data.List'
48-- To import instances alone, use: import Data.List()
49 | "The import of " `T.isInfixOf` _message
50 , " is redundant" `T.isInfixOf` _message
51 , let newlineAfter = maybe False (T.isPrefixOf "\n" . T.dropWhile (\x -> isSpace x && x /= '\n') . snd . textAtPosition _end) contents
52 , let extend = newlineAfter && _character _start == 0 -- takes up an entire line, so remove the whole line
53 = [("Remove import", [TextEdit (if extend then Range _start (Position (_line _end + 1) 0) else _range) ""])]
55-- File.hs:22:8: error:
56-- Illegal lambda-case (use -XLambdaCase)
57-- File.hs:22:6: error:
58-- Illegal view pattern: x -> foo
59-- Use ViewPatterns to enable view patterns
60-- File.hs:26:8: error:
61-- Illegal `..' in record pattern
62-- Use RecordWildCards to permit this
63-- File.hs:53:28: error:
64-- Illegal tuple section: use TupleSections
65-- File.hs:238:29: error:
66-- * Can't make a derived instance of `Data FSATrace':
67-- You need DeriveDataTypeable to derive an instance for this class
68-- * In the data declaration for `FSATrace'
69-- C:\Neil\shake\src\Development\Shake\Command.hs:515:31: error:
70-- * Illegal equational constraint a ~ ()
71-- (Use GADTs or TypeFamilies to permit this)
72-- * In the context: a ~ ()
73-- While checking an instance declaration
74-- In the instance declaration for `Unit (m a)'
75 | exts@(_:_) <- filter (`Set.member` ghcExtensions) $ T.split (not . isAlpha) $ T.replace "-X" "" _message
76 = [("Add " <> x <> " extension", [TextEdit (Range (Position 0 0) (Position 0 0)) $ "{-# LANGUAGE " <> x <> " #-}\n"]) | x <- exts]
81-- | All the GHC extensions
82ghcExtensions :: Set.HashSet T.Text
83ghcExtensions = Set.fromList $ map (T.pack . show) ghcEnumerateExtensions
86textAtPosition :: Position -> T.Text -> (T.Text, T.Text)
87textAtPosition (Position row col) x
88 | (preRow, mid:postRow) <- splitAt row $ T.splitOn "\n" x
89 , (preCol, postCol) <- T.splitAt col mid
90 = (T.intercalate "\n" $ preRow ++ [preCol], T.intercalate "\n" $ postCol : postRow)
91 | otherwise = (x, T.empty)
94setHandlersCodeAction :: PartialHandlers
95setHandlersCodeAction = PartialHandlers $ \WithMessage{..} x -> return x{
96 LSP.codeActionHandler = withResponse RspCodeAction codeAction
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list