Skip to content

Commit 1e47c75

Browse files
authored
fix unwanted import refinement (#1801)
1 parent ef0e9fd commit 1e47c75

File tree

6 files changed

+36
-10
lines changed

6 files changed

+36
-10
lines changed

plugins/hls-refine-imports-plugin/src/Ide/Plugin/RefineImports.hs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import Data.IORef (readIORef)
2222
import Data.List (intercalate)
2323
import qualified Data.Map.Strict as Map
2424
import Data.Maybe (catMaybes, fromMaybe)
25+
import qualified Data.Set as S
2526
import qualified Data.Text as T
2627
import Data.Traversable (forM)
2728
import Development.IDE
@@ -197,13 +198,23 @@ refineImportsRule = define $ \RefineImports nfp -> do
197198
let filterByImport
198199
:: LImportDecl GhcRn
199200
-> Map.Map ModuleName [AvailInfo]
200-
-> Map.Map ModuleName [AvailInfo]
201+
-> Maybe (Map.Map ModuleName [AvailInfo])
201202
filterByImport (L _ ImportDecl{ideclHiding = Just (_, L _ names)}) avails =
202-
let importedNames = map (ieName . unLoc) names
203-
in flip Map.filter avails $ \a ->
204-
any (`elem` importedNames)
205-
$ concatMap availNamesWithSelectors a
206-
filterByImport _ _ = mempty
203+
let importedNames = S.fromList $ map (ieName . unLoc) names
204+
res = flip Map.filter avails $ \a ->
205+
any (`S.member` importedNames)
206+
$ concatMap availNamesWithSelectors a
207+
allFilteredAvailsNames = S.fromList
208+
$ concatMap availNamesWithSelectors
209+
$ mconcat
210+
$ Map.elems res
211+
-- if there is a function defined in the current module and is used
212+
-- i.e. if a function is not reexported but defined in current
213+
-- module then this import cannot be refined
214+
in if importedNames `S.isSubsetOf` allFilteredAvailsNames
215+
then Just res
216+
else Nothing
217+
filterByImport _ _ = Nothing
207218
let constructImport
208219
:: LImportDecl GhcRn
209220
-> (ModuleName, [AvailInfo])
@@ -229,7 +240,7 @@ refineImportsRule = define $ \RefineImports nfp -> do
229240
-- we check for the inner imports
230241
, Just innerImports <- [Map.lookup mn import2Map]
231242
-- and only get those symbols used
232-
, filteredInnerImports <- [filterByImport i innerImports]
243+
, Just filteredInnerImports <- [filterByImport i innerImports]
233244
-- if no symbols from this modules then don't need to generate new import
234245
, not $ null filteredInnerImports
235246
]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module F (module F, module G) where
2+
3+
import G
4+
5+
f1 :: String
6+
f1 = "f1"
7+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module G where
2+
3+
g1 :: String
4+
g1 = "g1"

plugins/hls-refine-imports-plugin/test/testdata/WithOverride.expected.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ module Main where
33
import B ( b1 )
44
import C ( c1 )
55
import D
6+
import F
67
import Data.List (intercalate)
78

89
main :: IO ()
910
main = putStrLn
1011
$ "hello "
11-
<> intercalate ", " [b1, c1, e1]
12+
<> intercalate ", " [b1, c1, e1, f1, g1]

plugins/hls-refine-imports-plugin/test/testdata/WithOverride.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ module Main where
22

33
import A
44
import D
5+
import F
56
import Data.List (intercalate)
67

78
main :: IO ()
89
main = putStrLn
910
$ "hello "
10-
<> intercalate ", " [b1, c1, e1]
11+
<> intercalate ", " [b1, c1, e1, f1, g1]

plugins/hls-refine-imports-plugin/test/testdata/hie.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ cradle:
77
- B.hs
88
- C.hs
99
- D.hs
10-
- E.hs
10+
- E.hs
11+
- F.hs
12+
- G.hs

0 commit comments

Comments
 (0)