@@ -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 (showNameWithoutUniques )
34
36
35
37
------------------------------------------------------------------------------
36
38
@@ -211,17 +213,25 @@ 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 =
218
+ showNameWithoutUniques rdr `elem`
219
+ map (showNameWithoutUniques @ RdrName ) (listify (const True ) lies)
220
+ when alreadyImported $
221
+ lift (Left $ idnetifier <> " already imported" )
222
+
214
223
let lie = L src $ IEName rdr
215
224
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])}
225
+ if x `elem` lies then lift (Left $ idnetifier <> " already imported" ) else do
226
+ when hasSibling $
227
+ addTrailingCommaT (last lies)
228
+ addSimpleAnnT x (DP (0 , if hasSibling then 1 else 0 )) []
229
+ addSimpleAnnT rdr dp00 $ unqalDP $ hasParen idnetifier
230
+ -- Parens are attachted to `lies`, so if `lies` was empty previously,
231
+ -- we need change the ann key from `[]` to `:` to keep parens and other anns.
232
+ unless hasSibling $
233
+ transferAnn (L l' lies) (L l' [x]) id
234
+ return $ L l it {ideclHiding = Just (hide, L l' $ lies ++ [x])}
225
235
extendImportTopLevel _ _ _ = lift $ Left " Unable to extend the import list"
226
236
227
237
-- | Add an identifier with its parent to import list
@@ -255,6 +265,13 @@ extendImportViaParent df parent child (L l it@ImportDecl {..})
255
265
do
256
266
srcChild <- uniqueSrcSpanT
257
267
childRdr <- liftParseAST df child
268
+
269
+ let alreadyImported =
270
+ showNameWithoutUniques childRdr `elem`
271
+ map (showNameWithoutUniques @ RdrName ) (listify (const True ) lies')
272
+ when alreadyImported $
273
+ lift (Left $ child <> " already included in " <> parent <> " imports" )
274
+
258
275
when hasSibling $
259
276
addTrailingCommaT (last lies')
260
277
let childLIE = L srcChild $ IEName childRdr
0 commit comments