Skip to content

Commit 72b298c

Browse files
committed
extendImport - catch duplicates
1 parent 755fc37 commit 72b298c

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

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

Lines changed: 26 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 (showNameWithoutUniques)
3436

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

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

227237
-- | Add an identifier with its parent to import list
@@ -255,6 +265,13 @@ extendImportViaParent df parent child (L l it@ImportDecl {..})
255265
do
256266
srcChild <- uniqueSrcSpanT
257267
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+
258275
when hasSibling $
259276
addTrailingCommaT (last lies')
260277
let childLIE = L srcChild $ IEName childRdr

0 commit comments

Comments
 (0)