-
Notifications
You must be signed in to change notification settings - Fork 97
Delete unused top level binding code action #657
Changes from 4 commits
c13cf60
ef5e923
9685ac3
da17a08
56a4740
3465d66
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -476,6 +476,7 @@ codeActionTests = testGroup "code actions" | |
, fillTypedHoleTests | ||
, addSigActionTests | ||
, insertNewDefinitionTests | ||
, deleteUnusedDefinitionTests | ||
] | ||
|
||
codeLensesTests :: TestTree | ||
|
@@ -1144,6 +1145,68 @@ insertNewDefinitionTests = testGroup "insert new definition actions" | |
++ txtB') | ||
] | ||
|
||
|
||
deleteUnusedDefinitionTests :: TestTree | ||
deleteUnusedDefinitionTests = testGroup "delete unused definition action" | ||
[ testSession "delete unused top level binding" $ | ||
testFor | ||
(T.unlines [ "{-# OPTIONS_GHC -Wunused-top-binds #-}" | ||
, "module A (some) where" | ||
, "" | ||
, "f :: Int -> Int" | ||
, "f 1 = let a = 1" | ||
, " in a" | ||
, "f 2 = 2" | ||
, "" | ||
, "some = ()" | ||
]) | ||
(4, 0) | ||
"Delete ‘f’" | ||
(T.unlines [ | ||
"{-# OPTIONS_GHC -Wunused-top-binds #-}" | ||
, "module A (some) where" | ||
, "" | ||
, "some = ()" | ||
]) | ||
|
||
, testSession "delete unused top level binding defined in infix form" $ | ||
testFor | ||
(T.unlines [ "{-# OPTIONS_GHC -Wunused-top-binds #-}" | ||
, "module A (some) where" | ||
, "" | ||
, "myPlus :: Int -> Int -> Int" | ||
, "a `myPlus` b = a + b" | ||
, "" | ||
, "some = ()" | ||
]) | ||
(4, 2) | ||
"Delete ‘myPlus’" | ||
(T.unlines [ | ||
"{-# OPTIONS_GHC -Wunused-top-binds #-}" | ||
, "module A (some) where" | ||
, "" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A very minor remark, but beforehand they had 1-line gaps, now they have 2-line gaps. Should we expand one line after if it's blank? Or maybe this is getting ahead of ourselves and there is no need. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense to me. What do you think @serhiip? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For sure expanding the text edit until the next binding in a file is a good thing. Now the code works by finding the following binding after the current one and expands the deletion until that subsequent binding (if any). To make that happen, I rearranged my code a bit. I hope it makes more sense now. Thanks for your suggestions |
||
, "some = ()" | ||
]) | ||
] | ||
where | ||
testFor source pos expectedTitle expectedResult = do | ||
docId <- createDoc "A.hs" "haskell" source | ||
expectDiagnostics [ ("A.hs", [(DsWarning, pos, "not used")]) ] | ||
|
||
(action, title) <- extractCodeAction docId "Delete" | ||
|
||
liftIO $ title @?= expectedTitle | ||
executeCodeAction action | ||
contentAfterAction <- documentContents docId | ||
liftIO $ contentAfterAction @?= expectedResult | ||
|
||
extractCodeAction docId actionPrefix = do | ||
Just (CACodeAction action@CodeAction { _title = actionTitle }) | ||
<- find (\(CACodeAction CodeAction{_title=x}) -> actionPrefix `T.isPrefixOf` x) | ||
<$> getCodeActions docId (R 0 0 0 0) | ||
return (action, actionTitle) | ||
|
||
|
||
fixConstructorImportTests :: TestTree | ||
fixConstructorImportTests = testGroup "fix import actions" | ||
[ testSession "fix constructor import" $ template | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
hsmodDecls
guaranteed to be sorted by ranges? Looking at haddock’scollectDocs
it always seems to be called aftersortLocated
suggesting that we probably need this here as well.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Order in
hsmodDecls
doesn't matter really - I usefind
to locate next binding within all the top level bindings. I might have missed the point whyhsmodDecls
must be sortedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
find
will give you the first match. where the condition is true, consider what happens if you have 3 bindings. You want to delete the first. AssumehsModDecls
has the order1, 3, 2
. Then yourfind
here will find3
and you will end up deleting2
as wellThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My bad. That is indeed an issue - I've pushed an update. Thanks for catching this