Fix deleting unused bindings with Haddock docs#4881
Fix deleting unused bindings with Haddock docs#4881agam263 wants to merge 1 commit intohaskell:masterfrom
Conversation
| , 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) | ||
| "Delete ‘f’" | ||
| [ "{-# OPTIONS_GHC -Wunused-top-binds #-}" | ||
| , "module A (some) where" | ||
| , "" | ||
| , "some = ()" | ||
| ] |
There was a problem hiding this comment.
20260402-0820-18.0224737.mp4
It seems weird, i cannot replicate the Behavior
But the tests do Pass.
|
Hi, thank you for your contribution! It looks to me like you are manually computing the haddock comment range. If we do it stringly typed, I feel like there are corner cases we might miss. For example: -- | Comment
foo = ()
some = ()If we delete What about {-
-- | Commented out
-}
some = ()Would that delete What about some = ()
-- ^ CommentDo we perform arbitrary look ahead to find trailing haddock comments? Similar and complicated: some = ()
-- Comment
-- ^ Haddock commentIf we remove I imagine there are many more corner cases, and I think we need to look into the AST and figure out the correct haddock comment to compute the Range to delete. |
Closes #4876
What changed
This fixes the refactor plugin's
Delete ‘…’code action so it removes attached Haddock documentation when deleting an unused binding.The change covers:
-- |and-- ^{-|and{-^It also adds regression tests for both forms.
Why
When an unused top-level binding had Haddock immediately above it, the delete-unused-binding quick fix removed the binding but left the documentation behind. That produced orphaned docs and invalid or misleading source after applying the code action.
Root cause
The delete action built text edits from declaration and signature spans, but those spans did not account for attached Haddock comments. As a result, the edit range started too late and excluded the documentation block.
There was also a follow-up issue with overlapping deletion ranges once the Haddock span was included. That is now handled by sorting and merging the related ranges before producing text edits.
Implementation details
Validation
Ran locally with: