Skip to content

Commit 90966db

Browse files
authored
Fix extend imports regression (#769)
* Update exports map for non FOIs * Fallback to GHC suggestions if exportsMap not accurate * Tests
1 parent 4d33fd1 commit 90966db

File tree

4 files changed

+224
-187
lines changed

4 files changed

+224
-187
lines changed

ghcide/ghcide.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ test-suite ghcide-tests
309309
binary,
310310
bytestring,
311311
containers,
312+
data-default,
312313
directory,
313314
extra,
314315
filepath,
@@ -325,6 +326,7 @@ test-suite ghcide-tests
325326
haddock-library,
326327
haskell-lsp,
327328
haskell-lsp-types,
329+
hls-plugin-api,
328330
network-uri,
329331
lens,
330332
lsp-test >= 0.11.0.6 && < 0.12,

ghcide/src/Development/IDE/Core/OfInterest.hs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ import Development.IDE.Types.Logger
3333
import Development.IDE.Core.RuleTypes
3434
import Development.IDE.Core.Shake
3535
import Data.Maybe (catMaybes)
36+
import Data.List.Extra (nubOrd)
37+
import Development.IDE.Import.DependencyInformation
38+
import Control.Monad.Trans.Maybe
39+
import Control.Monad.Trans.Class
40+
import Development.IDE.Types.Options
3641

3742
newtype OfInterestVar = OfInterestVar (Var (HashMap NormalizedFilePath FileOfInterestStatus))
3843
instance IsIdeGlobal OfInterestVar
@@ -94,11 +99,22 @@ kick = do
9499
ShakeExtras{progressUpdate} <- getShakeExtras
95100
liftIO $ progressUpdate KickStarted
96101

97-
-- Update the exports map for the project
102+
-- Update the exports map for FOIs
98103
(results, ()) <- par (uses GenerateCore files) (void $ uses GetHieAst files)
104+
105+
-- Update the exports map for non FOIs
106+
-- We can skip this if checkProject is True, assuming they never change under our feet.
107+
IdeOptions{ optCheckProject = checkProject } <- getIdeOptions
108+
ifaces <- if checkProject then return Nothing else runMaybeT $ do
109+
deps <- MaybeT $ sequence <$> uses GetDependencies files
110+
hiResults <- lift $ uses GetModIface (nubOrd $ foldMap transitiveModuleDeps deps)
111+
return $ map hirModIface $ catMaybes hiResults
112+
99113
ShakeExtras{exportsMap} <- getShakeExtras
100114
let mguts = catMaybes results
101115
!exportsMap' = createExportsMapMg mguts
102-
liftIO $ modifyVar_ exportsMap $ evaluate . (exportsMap' <>)
116+
!exportsMap'' = maybe mempty createExportsMap ifaces
117+
liftIO $ modifyVar_ exportsMap $ evaluate . (exportsMap'' <>) . (exportsMap' <>)
103118

104119
liftIO $ progressUpdate KickCompleted
120+

ghcide/src/Development/IDE/Plugin/CodeAction.hs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,14 @@ suggestExtendImport exportsMap contents Diagnostic{_range=_range,..}
628628
| Just match <- Map.lookup binding (getExportsMap exportsMap)
629629
, [(ident, _)] <- filter (\(_,m) -> mod == m) (Set.toList match)
630630
= Just ident
631-
| otherwise = Nothing
631+
632+
-- fallback to using GHC suggestion even though it is not always correct
633+
| otherwise
634+
= Just IdentInfo
635+
{ name = binding
636+
, rendered = binding
637+
, parent = Nothing
638+
, isDatacon = False}
632639

633640
suggestFixConstructorImport :: Maybe T.Text -> Diagnostic -> [(T.Text, [TextEdit])]
634641
suggestFixConstructorImport _ Diagnostic{_range=_range,..}
@@ -968,8 +975,8 @@ extractQualifiedModuleName :: T.Text -> Maybe T.Text
968975
extractQualifiedModuleName x
969976
| Just [m] <- matchRegexUnifySpaces x "module named [^‘]*‘([^’]*)’"
970977
= Just m
971-
| otherwise
972-
= Nothing
978+
| otherwise
979+
= Nothing
973980

974981
-------------------------------------------------------------------------------------------------
975982

0 commit comments

Comments
 (0)