@@ -75,7 +75,7 @@ import Development.IDE.Types.Location
7575import Development.IDE.Types.Options
7676import GHC (DeltaPos (.. ),
7777 EpAnn (.. ),
78- LEpaComment )
78+ LEpaComment , EpAnnComments ( .. ), realSrcSpan )
7979import GHC.Iface.Ext.Types (ContextInfo (.. ),
8080 IdentifierDetails (.. ))
8181import qualified GHC.LanguageExtensions as Lang
@@ -128,6 +128,7 @@ import GHC (AnnsModule (
128128 EpaLocation ,
129129 EpaLocation' (.. ),
130130 HasLoc (.. ))
131+ import GHC.Types.SrcLoc (combineSrcSpans )
131132#endif
132133
133134
@@ -140,7 +141,7 @@ codeAction state _ (CodeActionParams _ _ (TextDocumentIdentifier uri) range _) =
140141 liftIO $ do
141142 let mbFile = toNormalizedFilePath' <$> uriToFilePath uri
142143 allDiags <- atomically $ fmap fdLspDiagnostic . filter (\ d -> mbFile == Just (fdFilePath d)) <$> getDiagnostics state
143- (join -> parsedModule) <- runAction " GhcideCodeActions.getParsedModule" state $ getParsedModule `traverse` mbFile
144+ (join -> parsedModule) <- runAction " GhcideCodeActions.getParsedModule" state $ getParsedModuleWithComments `traverse` mbFile
144145 let
145146 textContents = fmap Rope. toText contents
146147 actions = caRemoveRedundantImports parsedModule textContents allDiags range uri
@@ -600,29 +601,29 @@ suggestDeleteUnusedBinding
600601-- Foo.hs:4:1: warning: [-Wunused-binds] Defined but not used: ‘f’
601602 | Just [name] <- matchRegexUnifySpaces _message " .*Defined but not used: ‘([^ ]+)’"
602603 , Just indexedContent <- indexedByPosition . T. unpack <$> contents
603- = let edits = flip TextEdit " " <$> relatedRanges indexedContent (T. unpack name)
604+ = let edits = flip TextEdit " " <$> mergeRanges (sortOn _start $ relatedRanges indexedContent (T. unpack name) )
604605 in ([(" Delete ‘" <> name <> " ’" , edits) | not (null edits)])
605606 | otherwise = []
606607 where
607608 relatedRanges indexedContent name =
608- concatMap (findRelatedSpans indexedContent name . reLoc ) hsmodDecls
609+ concatMap (findRelatedSpans indexedContent name) hsmodDecls
609610 toRange = realSrcSpanToRange
610- extendForSpaces = extendToIncludePreviousNewlineIfPossible
611+ extendForDeletion indexedContent = extendToIncludePreviousNewlineIfPossible indexedContent
611612
612- findRelatedSpans :: PositionIndexedString -> String -> Located (HsDecl GhcPs ) -> [Range ]
613- findRelatedSpans
614- indexedContent
615- name
616- (L (RealSrcSpan l _) (ValD _ (extractNameAndMatchesFromFunBind -> Just (lname, matches)))) =
617- case lname of
613+ findRelatedSpans :: PositionIndexedString -> String -> LHsDecl GhcPs -> [Range ]
614+ findRelatedSpans indexedContent name decl = case decl of
615+ (L (EpAnn (EpaSpan (RealSrcSpan l _)) _ c) (ValD _ (extractNameAndMatchesFromFunBind -> Just (lname, matches))))
616+ -> c `seq` decl `seq` case lname of
618617 (L nLoc _name) | isTheBinding nLoc ->
619618 let findSig (L (RealSrcSpan l _) (SigD _ sig)) = findRelatedSigSpan indexedContent name l sig
620619 findSig _ = []
621620 in
622- extendForSpaces indexedContent (toRange l) :
621+ extendForDeletion indexedContent (toRange l) :
623622 concatMap (findSig . reLoc) hsmodDecls
624623 _ -> concatMap (findRelatedSpanForMatch indexedContent name) matches
625- findRelatedSpans _ _ _ = []
624+ (L (EpAnn (EpaSpan (RealSrcSpan lname _)) _ c) (SigD _ bind))
625+ -> bind `seq` [toRange $ joinComms (commsToList c) lname]
626+ _ -> decl `seq` []
626627
627628 extractNameAndMatchesFromFunBind
628629 :: HsBind GhcPs
@@ -634,11 +635,26 @@ suggestDeleteUnusedBinding
634635 } = Just (reLoc lname, matches)
635636 extractNameAndMatchesFromFunBind _ = Nothing
636637
638+ joinComms :: [LEpaComment ] -> RealSrcSpan -> RealSrcSpan
639+ joinComms comms idL =
640+ let locL (L l _) = l
641+ epaL = \ case
642+ EpaSpan l -> l
643+ EpaDelta l _ _ -> l
644+ commsSrc = realSrcSpan . epaL . locL <$> comms
645+ in foldl' combineRealSrcSpans idL commsSrc
646+
647+ commsToList :: EpAnnComments -> [LEpaComment ]
648+ commsToList = \ case
649+ EpaComments prior -> prior
650+ EpaCommentsBalanced prior following -> prior <> following
651+
652+
637653 findRelatedSigSpan :: PositionIndexedString -> String -> RealSrcSpan -> Sig GhcPs -> [Range ]
638654 findRelatedSigSpan indexedContent name l sig =
639655 let maybeSpan = findRelatedSigSpan1 name sig
640656 in case maybeSpan of
641- Just (_span, True ) -> pure $ extendForSpaces indexedContent $ toRange l -- a :: Int
657+ Just (_span, True ) -> pure $ extendForDeletion indexedContent $ toRange l -- a :: Int
642658 Just (RealSrcSpan span _, False ) -> pure $ toRange span -- a, b :: Int, a is unused
643659 _ -> []
644660
@@ -701,7 +717,7 @@ suggestDeleteUnusedBinding
701717 then
702718 let findSig (L (RealSrcSpan l _) sig) = findRelatedSigSpan indexedContent name l sig
703719 findSig _ = []
704- in extendForSpaces indexedContent (toRange l) : concatMap (findSig . reLoc) lsigs
720+ in extendForDeletion indexedContent (toRange l) : concatMap (findSig . reLoc) lsigs
705721 else concatMap (findRelatedSpanForMatch indexedContent name) matches
706722 findRelatedSpanForHsBind _ _ _ _ = []
707723
0 commit comments