Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
import Development.IDE.Types.Options
import GHC (DeltaPos (..),
EpAnn (..),
LEpaComment)
LEpaComment, EpAnnComments (..), realSrcSpan)
import GHC.Iface.Ext.Types (ContextInfo (..),
IdentifierDetails (..))
import qualified GHC.LanguageExtensions as Lang
Expand Down Expand Up @@ -128,6 +128,7 @@
EpaLocation,
EpaLocation' (..),
HasLoc (..))
import GHC.Types.SrcLoc (combineSrcSpans)
#endif


Expand All @@ -140,7 +141,7 @@
liftIO $ do
let mbFile = toNormalizedFilePath' <$> uriToFilePath uri
allDiags <- atomically $ fmap fdLspDiagnostic . filter (\d -> mbFile == Just (fdFilePath d)) <$> getDiagnostics state
(join -> parsedModule) <- runAction "GhcideCodeActions.getParsedModule" state $ getParsedModule `traverse` mbFile
(join -> parsedModule) <- runAction "GhcideCodeActions.getParsedModule" state $ getParsedModuleWithComments `traverse` mbFile
let
textContents = fmap Rope.toText contents
actions = caRemoveRedundantImports parsedModule textContents allDiags range uri
Expand Down Expand Up @@ -600,29 +601,29 @@
-- Foo.hs:4:1: warning: [-Wunused-binds] Defined but not used: ‘f’
| Just [name] <- matchRegexUnifySpaces _message ".*Defined but not used: ‘([^ ]+)’"
, Just indexedContent <- indexedByPosition . T.unpack <$> contents
= let edits = flip TextEdit "" <$> relatedRanges indexedContent (T.unpack name)
= let edits = flip TextEdit "" <$> mergeRanges (sortOn _start $ relatedRanges indexedContent (T.unpack name))
in ([("Delete ‘" <> name <> "’", edits) | not (null edits)])
| otherwise = []
where
relatedRanges indexedContent name =
concatMap (findRelatedSpans indexedContent name . reLoc) hsmodDecls
concatMap (findRelatedSpans indexedContent name) hsmodDecls
toRange = realSrcSpanToRange
extendForSpaces = extendToIncludePreviousNewlineIfPossible
extendForDeletion indexedContent = extendToIncludePreviousNewlineIfPossible indexedContent

findRelatedSpans :: PositionIndexedString -> String -> Located (HsDecl GhcPs) -> [Range]
findRelatedSpans
indexedContent
name
(L (RealSrcSpan l _) (ValD _ (extractNameAndMatchesFromFunBind -> Just (lname, matches)))) =
case lname of
findRelatedSpans :: PositionIndexedString -> String -> LHsDecl GhcPs -> [Range]
findRelatedSpans indexedContent name decl = case decl of
(L (EpAnn (EpaSpan (RealSrcSpan l _)) _ c) (ValD _ (extractNameAndMatchesFromFunBind -> Just (lname, matches))))

Check failure on line 615 in plugins/hls-refactor-plugin/src/Development/IDE/Plugin/CodeAction.hs

View workflow job for this annotation

GitHub Actions / test (9.6, ubuntu-latest, true)

• The constructor ‘EpaSpan’ should have 2 arguments, but has been given 1

Check failure on line 615 in plugins/hls-refactor-plugin/src/Development/IDE/Plugin/CodeAction.hs

View workflow job for this annotation

GitHub Actions / flags (9.6, ubuntu-latest)

• The constructor ‘EpaSpan’ should have 2 arguments, but has been given 1

Check failure on line 615 in plugins/hls-refactor-plugin/src/Development/IDE/Plugin/CodeAction.hs

View workflow job for this annotation

GitHub Actions / flags (9.8, ubuntu-latest)

• The data constructor ‘EpaSpan’ should have 2 arguments, but has been given 1

Check failure on line 615 in plugins/hls-refactor-plugin/src/Development/IDE/Plugin/CodeAction.hs

View workflow job for this annotation

GitHub Actions / test (9.8, ubuntu-latest, true)

• The data constructor ‘EpaSpan’ should have 2 arguments, but has been given 1

Check failure on line 615 in plugins/hls-refactor-plugin/src/Development/IDE/Plugin/CodeAction.hs

View workflow job for this annotation

GitHub Actions / test (9.6, macOS-latest, false)

• The constructor ‘EpaSpan’ should have 2 arguments, but has been given 1

Check failure on line 615 in plugins/hls-refactor-plugin/src/Development/IDE/Plugin/CodeAction.hs

View workflow job for this annotation

GitHub Actions / test (9.8, macOS-latest, false)

• The data constructor ‘EpaSpan’ should have 2 arguments, but has been given 1

Check failure on line 615 in plugins/hls-refactor-plugin/src/Development/IDE/Plugin/CodeAction.hs

View workflow job for this annotation

GitHub Actions / test (9.8, windows-latest, true)

• The data constructor ‘EpaSpan’ should have 2 arguments, but has been given 1

Check failure on line 615 in plugins/hls-refactor-plugin/src/Development/IDE/Plugin/CodeAction.hs

View workflow job for this annotation

GitHub Actions / test (9.6, windows-latest, true)

• The constructor ‘EpaSpan’ should have 2 arguments, but has been given 1
-> c `seq` decl `seq` case lname of
(L nLoc _name) | isTheBinding nLoc ->
let findSig (L (RealSrcSpan l _) (SigD _ sig)) = findRelatedSigSpan indexedContent name l sig
findSig _ = []
in
extendForSpaces indexedContent (toRange l) :
extendForDeletion indexedContent (toRange l) :
concatMap (findSig . reLoc) hsmodDecls
_ -> concatMap (findRelatedSpanForMatch indexedContent name) matches
findRelatedSpans _ _ _ = []
(L (EpAnn (EpaSpan (RealSrcSpan lname _)) _ c) (SigD _ bind))
-> [extendForDeletion indexedContent $ bind `seq` toRange $ joinComms (commsToList c) lname]
_ -> decl `seq` []

extractNameAndMatchesFromFunBind
:: HsBind GhcPs
Expand All @@ -634,11 +635,26 @@
} = Just (reLoc lname, matches)
extractNameAndMatchesFromFunBind _ = Nothing

joinComms :: [LEpaComment] -> RealSrcSpan -> RealSrcSpan
joinComms comms idL =
let locL (L l _) = l
epaL = \case
EpaSpan l -> l

Check failure on line 642 in plugins/hls-refactor-plugin/src/Development/IDE/Plugin/CodeAction.hs

View workflow job for this annotation

GitHub Actions / test (9.6, ubuntu-latest, true)

• The constructor ‘EpaSpan’ should have 2 arguments, but has been given 1

Check failure on line 642 in plugins/hls-refactor-plugin/src/Development/IDE/Plugin/CodeAction.hs

View workflow job for this annotation

GitHub Actions / flags (9.6, ubuntu-latest)

• The constructor ‘EpaSpan’ should have 2 arguments, but has been given 1

Check failure on line 642 in plugins/hls-refactor-plugin/src/Development/IDE/Plugin/CodeAction.hs

View workflow job for this annotation

GitHub Actions / flags (9.8, ubuntu-latest)

• The data constructor ‘EpaSpan’ should have 2 arguments, but has been given 1

Check failure on line 642 in plugins/hls-refactor-plugin/src/Development/IDE/Plugin/CodeAction.hs

View workflow job for this annotation

GitHub Actions / test (9.8, ubuntu-latest, true)

• The data constructor ‘EpaSpan’ should have 2 arguments, but has been given 1

Check failure on line 642 in plugins/hls-refactor-plugin/src/Development/IDE/Plugin/CodeAction.hs

View workflow job for this annotation

GitHub Actions / test (9.6, macOS-latest, false)

• The constructor ‘EpaSpan’ should have 2 arguments, but has been given 1

Check failure on line 642 in plugins/hls-refactor-plugin/src/Development/IDE/Plugin/CodeAction.hs

View workflow job for this annotation

GitHub Actions / test (9.8, macOS-latest, false)

• The data constructor ‘EpaSpan’ should have 2 arguments, but has been given 1

Check failure on line 642 in plugins/hls-refactor-plugin/src/Development/IDE/Plugin/CodeAction.hs

View workflow job for this annotation

GitHub Actions / test (9.8, windows-latest, true)

• The data constructor ‘EpaSpan’ should have 2 arguments, but has been given 1

Check failure on line 642 in plugins/hls-refactor-plugin/src/Development/IDE/Plugin/CodeAction.hs

View workflow job for this annotation

GitHub Actions / test (9.6, windows-latest, true)

• The constructor ‘EpaSpan’ should have 2 arguments, but has been given 1
EpaDelta l _ _ -> l

Check failure on line 643 in plugins/hls-refactor-plugin/src/Development/IDE/Plugin/CodeAction.hs

View workflow job for this annotation

GitHub Actions / flags (9.10, ubuntu-latest)

• The data constructor ‘EpaDelta’ should have 2 arguments, but has been given 3

Check failure on line 643 in plugins/hls-refactor-plugin/src/Development/IDE/Plugin/CodeAction.hs

View workflow job for this annotation

GitHub Actions / test (9.10, ubuntu-latest, true)

• The data constructor ‘EpaDelta’ should have 2 arguments, but has been given 3

Check failure on line 643 in plugins/hls-refactor-plugin/src/Development/IDE/Plugin/CodeAction.hs

View workflow job for this annotation

GitHub Actions / test (9.10, macOS-latest, false)

• The data constructor ‘EpaDelta’ should have 2 arguments, but has been given 3

Check failure on line 643 in plugins/hls-refactor-plugin/src/Development/IDE/Plugin/CodeAction.hs

View workflow job for this annotation

GitHub Actions / test (windows-latest, true, 9.10.2)

• The data constructor ‘EpaDelta’ should have 2 arguments, but has been given 3
commsSrc = realSrcSpan . epaL . locL <$> comms
in foldl' combineRealSrcSpans idL commsSrc

commsToList :: EpAnnComments -> [LEpaComment]
commsToList = \case
EpaComments prior -> prior
EpaCommentsBalanced prior following -> prior <> following


findRelatedSigSpan :: PositionIndexedString -> String -> RealSrcSpan -> Sig GhcPs -> [Range]
findRelatedSigSpan indexedContent name l sig =
let maybeSpan = findRelatedSigSpan1 name sig
in case maybeSpan of
Just (_span, True) -> pure $ extendForSpaces indexedContent $ toRange l -- a :: Int
Just (_span, True) -> pure $ extendForDeletion indexedContent $ toRange l -- a :: Int
Just (RealSrcSpan span _, False) -> pure $ toRange span -- a, b :: Int, a is unused
_ -> []

Expand Down Expand Up @@ -701,7 +717,7 @@
then
let findSig (L (RealSrcSpan l _) sig) = findRelatedSigSpan indexedContent name l sig
findSig _ = []
in extendForSpaces indexedContent (toRange l) : concatMap (findSig . reLoc) lsigs
in extendForDeletion indexedContent (toRange l) : concatMap (findSig . reLoc) lsigs
else concatMap (findRelatedSpanForMatch indexedContent name) matches
findRelatedSpanForHsBind _ _ _ _ = []

Expand Down
39 changes: 39 additions & 0 deletions plugins/hls-refactor-plugin/test/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2533,6 +2533,45 @@ deleteUnusedDefinitionTests = testGroup "delete unused definition action"
, ""
, "some = ()"
]
, testSession "delete unused top level binding with Haddock comment" $
testFor
[ "{-# OPTIONS_GHC -Wunused-top-binds #-}"
, "module A (some) where"
, ""
, "-- | docs for f"
, "f :: Int"
, "f = 1"
, ""
, "some = ()"
]
(5, 0)
1
"Delete ‘f’"
[ "{-# OPTIONS_GHC -Wunused-top-binds #-}"
, "module A (some) where"
, ""
, "some = ()"
]
, testSession "delete unused top level binding with block Haddock comment" $
testFor
[ "{-# OPTIONS_GHC -Wunused-top-binds #-}"
, "module A (some) where"
, ""
, "{-| docs for f"
, "-}"
, "f :: Int"
, "f = 1"
, ""
, "some = ()"
]
(6, 0)
1
"Delete ‘f’"
[ "{-# OPTIONS_GHC -Wunused-top-binds #-}"
, "module A (some) where"
, ""
, "some = ()"
]
, testSession "delete unused binding in where clause" $
testFor
[ "{-# OPTIONS_GHC -Wunused-binds #-}"
Expand Down
Loading