From b8829665698c7126902dca20676a7657a9d98210 Mon Sep 17 00:00:00 2001 From: andys8 Date: Thu, 15 Sep 2022 11:38:08 +0200 Subject: [PATCH] Refactor plugin: Prefer code action `isPreferred` can influence the client side order of code actions. The idea is that an unused import is likely to be removed and less likely the warning will be disabled. Therefore actions to remove a single or all redundant imports should be preferred, so that the client can prioritize them higher. Followup of --- .../src/Development/IDE/Plugin/CodeAction.hs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/plugins/hls-refactor-plugin/src/Development/IDE/Plugin/CodeAction.hs b/plugins/hls-refactor-plugin/src/Development/IDE/Plugin/CodeAction.hs index 01c3b555c1..32ea6bf8ce 100644 --- a/plugins/hls-refactor-plugin/src/Development/IDE/Plugin/CodeAction.hs +++ b/plugins/hls-refactor-plugin/src/Development/IDE/Plugin/CodeAction.hs @@ -389,7 +389,7 @@ suggestHideShadow ps fileContents mTcM mHar Diagnostic {_message, _range} | otherwise = [] where L _ HsModule {hsmodImports} = astA ps - + suggests identifier modName s | Just tcM <- mTcM, Just har <- mHar, @@ -458,6 +458,12 @@ suggestRemoveRedundantImport ParsedModule{pm_parsed_source = L _ HsModule{hsmod = [("Remove import", [TextEdit (extendToWholeLineIfPossible contents _range) ""])] | otherwise = [] + +-- Note [Removing imports is preferred] +-- It's good to prefer the remove imports code action because an unused import +-- is likely to be removed and less likely the warning will be disabled. +-- Therefore actions to remove a single or all redundant imports should be +-- preferred, so that the client can prioritize them higher. caRemoveRedundantImports :: Maybe ParsedModule -> Maybe T.Text -> [Diagnostic] -> [Diagnostic] -> Uri -> [Command |? CodeAction] caRemoveRedundantImports m contents digs ctxDigs uri | Just pm <- m, @@ -481,7 +487,8 @@ caRemoveRedundantImports m contents digs ctxDigs uri _diagnostics = Nothing _documentChanges = Nothing _edit = Just WorkspaceEdit{..} - _isPreferred = Nothing + -- See Note [Removing imports is preferred] + _isPreferred = Just True _command = Nothing _disabled = Nothing _xdata = Nothing @@ -520,7 +527,8 @@ caRemoveInvalidExports m contents digs ctxDigs uri _documentChanges = Nothing _edit = Just WorkspaceEdit{..} _command = Nothing - _isPreferred = Nothing + -- See Note [Removing imports is preferred] + _isPreferred = Just True _disabled = Nothing _xdata = Nothing _changeAnnotations = Nothing @@ -534,7 +542,8 @@ caRemoveInvalidExports m contents digs ctxDigs uri _documentChanges = Nothing _edit = Just WorkspaceEdit{..} _command = Nothing - _isPreferred = Nothing + -- See Note [Removing imports is preferred] + _isPreferred = Just True _disabled = Nothing _xdata = Nothing _changeAnnotations = Nothing