mirror of
https://github.com/haskell/ghcide.git
synced 2024-12-02 08:53:07 +03:00
Add code action for GHC's suggestions for not-found module
This commit is contained in:
parent
4aa1821fe2
commit
f53faf8734
@ -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
|
||||
|
@ -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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user