Skip to content

Commit 7f5a4ef

Browse files
committed
Use diagnostic code prefix "refact:"
to check if a hlint hint should trigger a code action
1 parent e26b23f commit 7f5a4ef

File tree

1 file changed

+8
-6
lines changed
  • plugins/hls-hlint-plugin/src/Ide/Plugin

1 file changed

+8
-6
lines changed

plugins/hls-hlint-plugin/src/Ide/Plugin/Hlint.hs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,13 @@ rules = do
122122
LSP.Diagnostic {
123123
_range = srcSpanToRange $ ideaSpan idea
124124
, _severity = Just LSP.DsInfo
125-
, _code = Just (LSP.StringValue $ T.pack $ ideaHint idea)
125+
, _code = Just $ (LSP.StringValue $ T.pack $ codePre ++ ideaHint idea)
126126
, _source = Just "hlint"
127127
, _message = T.pack $ show idea
128128
, _relatedInformation = Nothing
129129
, _tags = Nothing
130130
}
131+
where codePre = if null $ ideaRefactoring idea then "" else "refact:"
131132

132133
-- This one is defined in Development.IDE.GHC.Error but here
133134
-- the types could come from ghc-lib or ghc
@@ -217,12 +218,12 @@ codeActionProvider _lf ideState plId docId _ context = Right . LSP.List . map CA
217218
let docNfp = toNormalizedFilePath' <$> uriToFilePath' (docId ^. LSP.uri)
218219
numHintsInDoc = length
219220
[d | (nfp, _, d) <- diags
220-
, d ^. LSP.source == Just "hlint"
221+
, validCommand d
221222
, Just nfp == docNfp
222223
]
223224
-- We only want to show the applyAll code action if there is more than 1
224225
-- hint in the current document
225-
if numHintsInDoc >= 2 then do
226+
if numHintsInDoc > 1 then do
226227
applyAll <- applyAllAction
227228
pure $ applyAll:applyOne
228229
else
@@ -238,8 +239,9 @@ codeActionProvider _lf ideState plId docId _ context = Right . LSP.List . map CA
238239

239240
-- |Some hints do not have an associated refactoring
240241
validCommand (LSP.Diagnostic _ _ (Just (LSP.StringValue code)) (Just "hlint") _ _ _) =
241-
code /= "Eta reduce"
242-
validCommand _ = False
242+
"refact:" `T.isPrefixOf` code
243+
validCommand _ =
244+
False
243245

244246
LSP.List diags = context ^. LSP.diagnostics
245247

@@ -248,7 +250,7 @@ codeActionProvider _lf ideState plId docId _ context = Right . LSP.List . map CA
248250
Just . codeAction <$> mkLspCommand plId "applyOne" title (Just args)
249251
where
250252
codeAction cmd = LSP.CodeAction title (Just LSP.CodeActionQuickFix) (Just (LSP.List [diag])) Nothing (Just cmd)
251-
title = "Apply hint: " <> code
253+
title = T.replace code "refact:" "Apply hint: "
252254
-- need 'file', 'start_pos' and hint title (to distinguish between alternative suggestions at the same location)
253255
args = [toJSON (AOP (docId ^. LSP.uri) start code)]
254256
mkHlintAction (LSP.Diagnostic _r _s _c _source _m _ _) = return Nothing

0 commit comments

Comments
 (0)