@@ -31,6 +31,8 @@ import Language.Haskell.GHC.ExactPrint.Types (DeltaPos (DP), KeywordId (G), mkAn
31
31
import Language.Haskell.LSP.Types
32
32
import OccName
33
33
import Outputable (ppr , showSDocUnsafe )
34
+ import Data.Generics (listify )
35
+ import Development.IDE.Spans.Common (showName )
34
36
35
37
------------------------------------------------------------------------------
36
38
@@ -211,17 +213,23 @@ extendImportTopLevel df idnetifier (L l it@ImportDecl {..})
211
213
src <- uniqueSrcSpanT
212
214
top <- uniqueSrcSpanT
213
215
rdr <- liftParseAST df idnetifier
216
+
217
+ let alreadyImported = showName rdr `elem` map (showName @ RdrName ) (listify (const True ) lies)
218
+ when alreadyImported $
219
+ lift (Left $ idnetifier <> " already imported" )
220
+
214
221
let lie = L src $ IEName rdr
215
222
x = L top $ IEVar noExtField lie
216
- when hasSibling $
217
- addTrailingCommaT (last lies)
218
- addSimpleAnnT x (DP (0 , if hasSibling then 1 else 0 )) []
219
- addSimpleAnnT rdr dp00 $ unqalDP $ hasParen idnetifier
220
- -- Parens are attachted to `lies`, so if `lies` was empty previously,
221
- -- we need change the ann key from `[]` to `:` to keep parens and other anns.
222
- unless hasSibling $
223
- transferAnn (L l' lies) (L l' [x]) id
224
- return $ L l it {ideclHiding = Just (hide, L l' $ lies ++ [x])}
223
+ if x `elem` lies then lift (Left $ idnetifier <> " already imported" ) else do
224
+ when hasSibling $
225
+ addTrailingCommaT (last lies)
226
+ addSimpleAnnT x (DP (0 , if hasSibling then 1 else 0 )) []
227
+ addSimpleAnnT rdr dp00 $ unqalDP $ hasParen idnetifier
228
+ -- Parens are attachted to `lies`, so if `lies` was empty previously,
229
+ -- we need change the ann key from `[]` to `:` to keep parens and other anns.
230
+ unless hasSibling $
231
+ transferAnn (L l' lies) (L l' [x]) id
232
+ return $ L l it {ideclHiding = Just (hide, L l' $ lies ++ [x])}
225
233
extendImportTopLevel _ _ _ = lift $ Left " Unable to extend the import list"
226
234
227
235
-- | Add an identifier with its parent to import list
@@ -255,6 +263,11 @@ extendImportViaParent df parent child (L l it@ImportDecl {..})
255
263
do
256
264
srcChild <- uniqueSrcSpanT
257
265
childRdr <- liftParseAST df child
266
+
267
+ let alreadyImported = showName childRdr `elem` map (showName @ RdrName ) (listify (const True ) lies')
268
+ when alreadyImported $
269
+ lift (Left $ child <> " already included in " <> parent <> " imports" )
270
+
258
271
when hasSibling $
259
272
addTrailingCommaT (last lies')
260
273
let childLIE = L srcChild $ IEName childRdr
0 commit comments