Add code action for GHC's suggestions for not-found module

This commit is contained in:
Jacek Generowicz 2019-09-13 01:08:57 +02:00
parent 4aa1821fe2
commit f53faf8734
2 changed files with 32 additions and 0 deletions

View File

@ -20,6 +20,7 @@ import Language.Haskell.LSP.Messages
import qualified Data.Rope.UTF16 as Rope
import Data.Char
import Data.Maybe
import Data.List
import qualified Data.Text as T
-- | Generate code actions.
@ -97,6 +98,15 @@ suggestAction contents Diagnostic{_range=_range@Range{..},..}
| 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]
-- src/Development/IDE/Core/Compile.hs:58:1: error:
-- Could not find module Data.Cha
-- Perhaps you meant Data.Char (from base-4.12.0.0)
| "Could not find module" `T.isInfixOf` _message
, "Perhaps you meant" `T.isInfixOf` _message
= map proposeModule $ nub $ findSuggestedModules _message where
findSuggestedModules = (map (head . T.words) . drop 2 . T.lines)
proposeModule mod = ("replace with " <> mod, [TextEdit _range mod])
suggestAction _ _ = []
mkRenameEdit :: Maybe T.Text -> Range -> T.Text -> TextEdit

View File

@ -213,6 +213,7 @@ codeActionTests = testGroup "code actions"
[ renameActionTests
, typeWildCardActionTests
, removeImportTests
, importRenameActionTests
]
renameActionTests :: TestTree
@ -413,6 +414,27 @@ removeImportTests = testGroup "remove import actions"
liftIO $ expectedContentAfterAction @=? contentAfterAction
]
importRenameActionTests :: TestTree
importRenameActionTests = testGroup "import rename actions"
[ testSession "Data.Mape -> Data.Map" $ check "Map"
, testSession "Data.Mape -> Data.Maybe" $ check "Maybe" ] where
check modname = do
let content = T.unlines
[ "module Testing where"
, "import Data.Mape"
]
doc <- openDoc' "Testing.hs" "haskell" content
_ <- waitForDiagnostics
actionsOrCommands <- getCodeActions doc (Range (Position 2 8) (Position 2 16))
let [changeToMap] = [action | CACodeAction action@CodeAction{ _title = actionTitle } <- actionsOrCommands, ("Data." <> modname) `T.isInfixOf` actionTitle ]
executeCodeAction changeToMap
contentAfterAction <- documentContents doc
let expectedContentAfterAction = T.unlines
[ "module Testing where"
, "import Data." <> modname
]
liftIO $ expectedContentAfterAction @=? contentAfterAction
----------------------------------------------------------------------
-- Utils