Skip to content

Commit 61b01bd

Browse files
committed
extendImport - catch duplicates
1 parent 573d184 commit 61b01bd

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

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

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ import Language.Haskell.GHC.ExactPrint.Types (DeltaPos (DP), KeywordId (G), mkAn
3131
import Language.Haskell.LSP.Types
3232
import OccName
3333
import Outputable (ppr, showSDocUnsafe)
34+
import Data.Generics (listify)
35+
import Development.IDE.Spans.Common (showName)
3436

3537
------------------------------------------------------------------------------
3638

@@ -211,17 +213,23 @@ extendImportTopLevel df idnetifier (L l it@ImportDecl {..})
211213
src <- uniqueSrcSpanT
212214
top <- uniqueSrcSpanT
213215
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+
214221
let lie = L src $ IEName rdr
215222
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])}
225233
extendImportTopLevel _ _ _ = lift $ Left "Unable to extend the import list"
226234

227235
-- | Add an identifier with its parent to import list
@@ -255,6 +263,11 @@ extendImportViaParent df parent child (L l it@ImportDecl {..})
255263
do
256264
srcChild <- uniqueSrcSpanT
257265
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+
258271
when hasSibling $
259272
addTrailingCommaT (last lies')
260273
let childLIE = L srcChild $ IEName childRdr

0 commit comments

Comments
 (0)