on master help sign in
Add code action for GHC's suggestions for not-found module
f53faf8Jacek Generowicz
src/Development/IDE/LSP/CodeAction.hs+10
@@ -20,6 +20,7 @@ import Language.Haskell.LSP.Messages
20import qualified Data.Rope.UTF16 as Rope
20import qualified Data.Rope.UTF16 as Rope
21import Data.Char
21import Data.Char
22import Data.Maybe
22import Data.Maybe
23+import Data.List
23import qualified Data.Text as T
24import qualified Data.Text as T
24
25
25-- | Generate code actions.
26-- | Generate code actions.
@@ -97,6 +98,15 @@ suggestAction contents Diagnostic{_range=_range@Range{..},..}
97 | exts@(_:_) <- filter (`Set.member` ghcExtensions) $ T.split (not . isAlpha) $ T.replace "-X" "" _message
98 | exts@(_:_) <- filter (`Set.member` ghcExtensions) $ T.split (not . isAlpha) $ T.replace "-X" "" _message
98 = [("Add " <> x <> " extension", [TextEdit (Range (Position 0 0) (Position 0 0)) $ "{-# LANGUAGE " <> x <> " #-}\n"]) | x <- exts]
99 = [("Add " <> x <> " extension", [TextEdit (Range (Position 0 0) (Position 0 0)) $ "{-# LANGUAGE " <> x <> " #-}\n"]) | x <- exts]
99
100
101+-- src/Development/IDE/Core/Compile.hs:58:1: error:
102+-- Could not find module ‘Data.Cha’
103+-- Perhaps you meant Data.Char (from base-4.12.0.0)
104+ | "Could not find module" `T.isInfixOf` _message
105+ , "Perhaps you meant" `T.isInfixOf` _message
106+ = map proposeModule $ nub $ findSuggestedModules _message where
107+ findSuggestedModules = (map (head . T.words) . drop 2 . T.lines)
108+ proposeModule mod = ("replace with " <> mod, [TextEdit _range mod])
109+
100suggestAction _ _ = []
110suggestAction _ _ = []
101
111
102mkRenameEdit :: Maybe T.Text -> Range -> T.Text -> TextEdit
112mkRenameEdit :: Maybe T.Text -> Range -> T.Text -> TextEdit
test/exe/Main.hs+22
@@ -213,6 +213,7 @@ codeActionTests = testGroup "code actions"
213 [ renameActionTests
213 [ renameActionTests
214 , typeWildCardActionTests
214 , typeWildCardActionTests
215 , removeImportTests
215 , removeImportTests
216+ , importRenameActionTests
216 ]
217 ]
217
218
218renameActionTests :: TestTree
219renameActionTests :: TestTree
@@ -413,6 +414,27 @@ removeImportTests = testGroup "remove import actions"
413 liftIO $ expectedContentAfterAction @=? contentAfterAction
414 liftIO $ expectedContentAfterAction @=? contentAfterAction
414 ]
415 ]
415
416
417+importRenameActionTests :: TestTree
418+importRenameActionTests = testGroup "import rename actions"
419+ [ testSession "Data.Mape -> Data.Map" $ check "Map"
420+ , testSession "Data.Mape -> Data.Maybe" $ check "Maybe" ] where
421+ check modname = do
422+ let content = T.unlines
423+ [ "module Testing where"
424+ , "import Data.Mape"
425+ ]
426+ doc <- openDoc' "Testing.hs" "haskell" content
427+ _ <- waitForDiagnostics
428+ actionsOrCommands <- getCodeActions doc (Range (Position 2 8) (Position 2 16))
429+ let [changeToMap] = [action | CACodeAction action@CodeAction{ _title = actionTitle } <- actionsOrCommands, ("Data." <> modname) `T.isInfixOf` actionTitle ]
430+ executeCodeAction changeToMap
431+ contentAfterAction <- documentContents doc
432+ let expectedContentAfterAction = T.unlines
433+ [ "module Testing where"
434+ , "import Data." <> modname
435+ ]
436+ liftIO $ expectedContentAfterAction @=? contentAfterAction
437+
416----------------------------------------------------------------------
438----------------------------------------------------------------------
417-- Utils
439-- Utils
418
440