Skip to content

Commit

Permalink
Improve action for fixing import typo (#2522)
Browse files Browse the repository at this point in the history
* Improve action for fixing import typo

* Remove unnecessary isInfixOf
  • Loading branch information
jhrcek committed Dec 22, 2021
1 parent 3061ace commit 3de244e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
31 changes: 23 additions & 8 deletions ghcide/src/Development/IDE/Plugin/CodeAction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -700,17 +700,32 @@ suggestFillTypeWildcard Diagnostic{_range=_range,..}
= [("Use type signature: ‘" <> typeSignature <> "", TextEdit _range typeSignature)]
| otherwise = []

{- Handles two variants with different formatting
1. Could not find module ‘Data.Cha’
Perhaps you meant Data.Char (from base-4.12.0.0)
2. Could not find module ‘Data.I’
Perhaps you meant
Data.Ix (from base-4.14.3.0)
Data.Eq (from base-4.14.3.0)
Data.Int (from base-4.14.3.0)
-}
suggestModuleTypo :: Diagnostic -> [(T.Text, TextEdit)]
suggestModuleTypo Diagnostic{_range=_range,..}
-- 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 = let
findSuggestedModules = map (head . T.words) . drop 2 . T.lines
proposeModule mod = ("replace with " <> mod, TextEdit _range mod)
in map proposeModule $ nubOrd $ findSuggestedModules _message
| "Could not find module" `T.isInfixOf` _message =
case T.splitOn "Perhaps you meant" _message of
[_, stuff] ->
[ ("replace with " <> modul, TextEdit _range modul)
| modul <- mapMaybe extractModule (T.lines stuff)
]
_ -> []
| otherwise = []
where
extractModule line = case T.words line of
[modul, "(from", _] -> Just modul
_ -> Nothing


suggestFillHole :: Diagnostic -> [(T.Text, TextEdit)]
suggestFillHole Diagnostic{_range=_range,..}
Expand Down
26 changes: 26 additions & 0 deletions ghcide/test/exe/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,7 @@ codeActionTests = testGroup "code actions"
, suggestHideShadowTests
, suggestImportDisambiguationTests
, fixConstructorImportTests
, fixModuleImportTypoTests
, importRenameActionTests
, fillTypedHoleTests
, addSigActionTests
Expand Down Expand Up @@ -1804,6 +1805,31 @@ extendImportTests = testGroup "extend import actions"
contentAfterAction <- documentContents docB
liftIO $ expectedContentB @=? contentAfterAction

fixModuleImportTypoTests :: TestTree
fixModuleImportTypoTests = testGroup "fix module import typo"
[ testSession "works when single module suggested" $ do
doc <- createDoc "A.hs" "haskell" "import Data.Cha"
_ <- waitForDiagnostics
InR action@CodeAction { _title = actionTitle } : _ <- getCodeActions doc (R 0 0 0 10)
liftIO $ actionTitle @?= "replace with Data.Char"
executeCodeAction action
contentAfterAction <- documentContents doc
liftIO $ contentAfterAction @?= "import Data.Char"
, testSession "works when multiple modules suggested" $ do
doc <- createDoc "A.hs" "haskell" "import Data.I"
_ <- waitForDiagnostics
actions <- sortOn (\(InR CodeAction{_title=x}) -> x) <$> getCodeActions doc (R 0 0 0 10)
let actionTitles = [ title | InR CodeAction{_title=title} <- actions ]
liftIO $ actionTitles @?= [ "replace with Data.Eq"
, "replace with Data.Int"
, "replace with Data.Ix"
]
let InR replaceWithDataEq : _ = actions
executeCodeAction replaceWithDataEq
contentAfterAction <- documentContents doc
liftIO $ contentAfterAction @?= "import Data.Eq"
]

extendImportTestsRegEx :: TestTree
extendImportTestsRegEx = testGroup "regex parsing"
[
Expand Down

0 comments on commit 3de244e

Please sign in to comment.