Skip to content

Commit de7295a

Browse files
authored
Merge pull request #52 from jacg/module-not-found-action
Code action for 'module not found'
2 parents 855c18e + a58751e commit de7295a

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/Development/IDE/LSP/CodeAction.hs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import Language.Haskell.LSP.Messages
2020
import qualified Data.Rope.UTF16 as Rope
2121
import Data.Char
2222
import Data.Maybe
23+
import Data.List.Extra
2324
import qualified Data.Text as T
2425

2526
-- | Generate code actions.
@@ -97,6 +98,15 @@ suggestAction contents Diagnostic{_range=_range@Range{..},..}
9798
| exts@(_:_) <- filter (`Set.member` ghcExtensions) $ T.split (not . isAlpha) $ T.replace "-X" "" _message
9899
= [("Add " <> x <> " extension", [TextEdit (Range (Position 0 0) (Position 0 0)) $ "{-# LANGUAGE " <> x <> " #-}\n"]) | x <- exts]
99100

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 $ nubOrd $ findSuggestedModules _message where
107+
findSuggestedModules = map (head . T.words) . drop 2 . T.lines
108+
proposeModule mod = ("replace with " <> mod, [TextEdit _range mod])
109+
100110
suggestAction _ _ = []
101111

102112
mkRenameEdit :: Maybe T.Text -> Range -> T.Text -> TextEdit

test/exe/Main.hs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ codeActionTests = testGroup "code actions"
231231
[ renameActionTests
232232
, typeWildCardActionTests
233233
, removeImportTests
234+
, importRenameActionTests
234235
]
235236

236237
renameActionTests :: TestTree
@@ -431,6 +432,27 @@ removeImportTests = testGroup "remove import actions"
431432
liftIO $ expectedContentAfterAction @=? contentAfterAction
432433
]
433434

435+
importRenameActionTests :: TestTree
436+
importRenameActionTests = testGroup "import rename actions"
437+
[ testSession "Data.Mape -> Data.Map" $ check "Map"
438+
, testSession "Data.Mape -> Data.Maybe" $ check "Maybe" ] where
439+
check modname = do
440+
let content = T.unlines
441+
[ "module Testing where"
442+
, "import Data.Mape"
443+
]
444+
doc <- openDoc' "Testing.hs" "haskell" content
445+
_ <- waitForDiagnostics
446+
actionsOrCommands <- getCodeActions doc (Range (Position 2 8) (Position 2 16))
447+
let [changeToMap] = [action | CACodeAction action@CodeAction{ _title = actionTitle } <- actionsOrCommands, ("Data." <> modname) `T.isInfixOf` actionTitle ]
448+
executeCodeAction changeToMap
449+
contentAfterAction <- documentContents doc
450+
let expectedContentAfterAction = T.unlines
451+
[ "module Testing where"
452+
, "import Data." <> modname
453+
]
454+
liftIO $ expectedContentAfterAction @=? contentAfterAction
455+
434456
----------------------------------------------------------------------
435457
-- Utils
436458

0 commit comments

Comments
 (0)