-- Copyright (c) 2019 The DAML Authors. All rights reserved. -- SPDX-License-Identifier: Apache-2.0 {-# LANGUAGE DuplicateRecordFields #-} -- | Go to the definition of a variable. module Development.IDE.LSP.CodeAction ( setHandlersCodeAction ) where import Language.Haskell.LSP.Types import Development.IDE.GHC.Compat import Development.IDE.Core.Rules import Development.IDE.LSP.Server import qualified Data.HashMap.Strict as Map import qualified Data.HashSet as Set import qualified Language.Haskell.LSP.Core as LSP import Language.Haskell.LSP.VFS import Language.Haskell.LSP.Messages import qualified Data.Rope.UTF16 as Rope import Data.Char import Data.Maybe import qualified Data.Text as T -- | Generate code actions. codeAction :: LSP.LspFuncs () -> IdeState -> CodeActionParams -> IO (List CAResult) codeAction lsp _ CodeActionParams{_textDocument=TextDocumentIdentifier uri,_context=CodeActionContext{_diagnostics=List xs}} = do -- disable logging as its quite verbose -- logInfo (ideLogger ide) $ T.pack $ "Code action req: " ++ show arg contents <- LSP.getVirtualFileFunc lsp $ toNormalizedUri uri let text = Rope.toText . (_text :: VirtualFile -> Rope.Rope) <$> contents pure $ List [ CACodeAction $ CodeAction title (Just CodeActionQuickFix) (Just $ List [x]) (Just edit) Nothing | x <- xs, (title, tedit) <- suggestAction text x , let edit = WorkspaceEdit (Just $ Map.singleton uri $ List tedit) Nothing ] suggestAction :: Maybe T.Text -> Diagnostic -> [(T.Text, [TextEdit])] suggestAction contents Diagnostic{_range=_range@Range{..},..} -- File.hs:16:1: warning: -- The import of `Data.List' is redundant -- except perhaps to import instances from `Data.List' -- To import instances alone, use: import Data.List() | "The import of " `T.isInfixOf` _message , " is redundant" `T.isInfixOf` _message = [("Remove import", [TextEdit (extendToWholeLineIfPossible contents _range) ""])] -- File.hs:52:41: error: -- * Variable not in scope: -- suggestAcion :: Maybe T.Text -> Range -> Range -- * Perhaps you meant ‘suggestAction’ (line 83) -- File.hs:94:37: error: -- Not in scope: ‘T.isPrfixOf’ -- Perhaps you meant one of these: -- ‘T.isPrefixOf’ (imported from Data.Text), -- ‘T.isInfixOf’ (imported from Data.Text), -- ‘T.isSuffixOf’ (imported from Data.Text) -- Module ‘Data.Text’ does not export ‘isPrfixOf’. | renameSuggestions@(_:_) <- extractRenamableTerms _message = [ ("Replace with ‘" <> name <> "’", [mkRenameEdit contents _range name]) | name <- renameSuggestions ] -- Foo.hs:3:8: error: -- * Found type wildcard `_' standing for `p -> p1 -> p' | "Found type wildcard" `T.isInfixOf` _message , " standing for " `T.isInfixOf` _message , typeSignature <- extractWildCardTypeSignature _message = [("Use type signature: ‘" <> typeSignature <> "’", [TextEdit _range typeSignature])] -- File.hs:22:8: error: -- Illegal lambda-case (use -XLambdaCase) -- File.hs:22:6: error: -- Illegal view pattern: x -> foo -- Use ViewPatterns to enable view patterns -- File.hs:26:8: error: -- Illegal `..' in record pattern -- Use RecordWildCards to permit this -- File.hs:53:28: error: -- Illegal tuple section: use TupleSections -- File.hs:238:29: error: -- * Can't make a derived instance of `Data FSATrace': -- You need DeriveDataTypeable to derive an instance for this class -- * In the data declaration for `FSATrace' -- C:\Neil\shake\src\Development\Shake\Command.hs:515:31: error: -- * Illegal equational constraint a ~ () -- (Use GADTs or TypeFamilies to permit this) -- * In the context: a ~ () -- While checking an instance declaration -- In the instance declaration for `Unit (m a)' | exts@(_:_) <- filter (`Set.member` ghcExtensions) $ T.split (not . isAlpha) $ T.replace "-X" "" _message = [("Add " <> x <> " extension", [TextEdit (Range (Position 0 0) (Position 0 0)) $ "{-# LANGUAGE " <> x <> " #-}\n"]) | x <- exts] suggestAction _ _ = [] mkRenameEdit :: Maybe T.Text -> Range -> T.Text -> TextEdit mkRenameEdit contents range name = if fromMaybe False maybeIsInfixFunction then TextEdit range ("`" <> name <> "`") else TextEdit range name where maybeIsInfixFunction = do curr <- textInRange range <$> contents pure $ "`" `T.isPrefixOf` curr && "`" `T.isSuffixOf` curr extractWildCardTypeSignature :: T.Text -> T.Text extractWildCardTypeSignature = -- inferring when parens are actually needed around the type signature would -- require understanding both the precedence of the context of the _ and of -- the signature itself. Inserting them unconditionally is ugly but safe. ("(" `T.append`) . (`T.append` ")") . T.takeWhile (/='’') . T.dropWhile (=='‘') . T.dropWhile (/='‘') . snd . T.breakOnEnd "standing for " extractRenamableTerms :: T.Text -> [T.Text] extractRenamableTerms msg -- Account for both "Variable not in scope" and "Not in scope" | "ot in scope:" `T.isInfixOf` msg = extractSuggestions msg | otherwise = [] where extractSuggestions = map getEnclosed . concatMap singleSuggestions . filter isKnownSymbol . T.lines singleSuggestions = T.splitOn "), " -- Each suggestion is comma delimited isKnownSymbol t = " (imported from" `T.isInfixOf` t || " (line " `T.isInfixOf` t getEnclosed = T.dropWhile (== '‘') . T.dropWhileEnd (== '’') . T.dropAround (\c -> c /= '‘' && c /= '’') -- | If a range takes up a whole line (it begins at the start of the line and there's only whitespace -- between the end of the range and the next newline), extend the range to take up the whole line. extendToWholeLineIfPossible :: Maybe T.Text -> Range -> Range extendToWholeLineIfPossible contents range@Range{..} = let newlineAfter = maybe False (T.isPrefixOf "\n" . T.dropWhile (\x -> isSpace x && x /= '\n') . snd . splitTextAtPosition _end) contents extend = newlineAfter && _character _start == 0 -- takes up an entire line, so remove the whole line in if extend then Range _start (Position (_line _end + 1) 0) else range -- | All the GHC extensions ghcExtensions :: Set.HashSet T.Text ghcExtensions = Set.fromList $ map (T.pack . show) ghcEnumerateExtensions splitTextAtPosition :: Position -> T.Text -> (T.Text, T.Text) splitTextAtPosition (Position row col) x | (preRow, mid:postRow) <- splitAt row $ T.splitOn "\n" x , (preCol, postCol) <- T.splitAt col mid = (T.intercalate "\n" $ preRow ++ [preCol], T.intercalate "\n" $ postCol : postRow) | otherwise = (x, T.empty) textInRange :: Range -> T.Text -> T.Text textInRange (Range (Position startRow startCol) (Position endRow endCol)) text = case compare startRow endRow of LT -> let (linesInRangeBeforeEndLine, endLineAndFurtherLines) = splitAt (endRow - startRow) linesBeginningWithStartLine (textInRangeInFirstLine, linesBetween) = case linesInRangeBeforeEndLine of [] -> ("", []) firstLine:linesInBetween -> (T.drop startCol firstLine, linesInBetween) maybeTextInRangeInEndLine = T.take endCol <$> listToMaybe endLineAndFurtherLines in T.intercalate "\n" (textInRangeInFirstLine : linesBetween ++ maybeToList maybeTextInRangeInEndLine) EQ -> let line = fromMaybe "" (listToMaybe linesBeginningWithStartLine) in T.take (endCol - startCol) (T.drop startCol line) GT -> "" where linesBeginningWithStartLine = drop startRow (T.splitOn "\n" text) setHandlersCodeAction :: PartialHandlers setHandlersCodeAction = PartialHandlers $ \WithMessage{..} x -> return x{ LSP.codeActionHandler = withResponse RspCodeAction codeAction }