Skip to content

Commit af5cd2d

Browse files
IAmPara0xmichaelpj
andauthored
Fix issue: HLS HLint plugin doesn't preserve HLint's severities #3881 (#3902)
* Fix issue: HLS HLint plugin doesn't preserve HLint's severities #3881 preserve severity from HLint * Fix tests * Only preserve error serverity from hlint * Add comment explaining the propogation of error level serverity --------- Co-authored-by: Michael Peyton Jones <[email protected]>
1 parent be31326 commit af5cd2d

File tree

1 file changed

+32
-18
lines changed
  • plugins/hls-hlint-plugin/src/Ide/Plugin

1 file changed

+32
-18
lines changed

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

+32-18
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,7 @@ import Ide.Plugin.Resolve
125125
import Ide.PluginUtils
126126
import Ide.Types hiding
127127
(Config)
128-
import Language.Haskell.HLint as Hlint hiding
129-
(Error)
128+
import Language.Haskell.HLint as Hlint
130129
import qualified Language.LSP.Protocol.Lens as LSP
131130
import Language.LSP.Protocol.Message
132131
import Language.LSP.Protocol.Types hiding
@@ -242,25 +241,40 @@ rules recorder plugin = do
242241

243242
diagnostics :: NormalizedFilePath -> Either ParseError [Idea] -> [FileDiagnostic]
244243
diagnostics file (Right ideas) =
245-
[(file, ShowDiag, ideaToDiagnostic i) | i <- ideas, ideaSeverity i /= Ignore]
244+
(file, ShowDiag,) <$> catMaybes [ideaToDiagnostic i | i <- ideas]
246245
diagnostics file (Left parseErr) =
247246
[(file, ShowDiag, parseErrorToDiagnostic parseErr)]
248247

249-
ideaToDiagnostic :: Idea -> Diagnostic
250-
ideaToDiagnostic idea =
251-
LSP.Diagnostic {
252-
_range = srcSpanToRange $ ideaSpan idea
253-
, _severity = Just LSP.DiagnosticSeverity_Information
254-
-- we are encoding the fact that idea has refactorings in diagnostic code
255-
, _code = Just (InR $ T.pack $ codePre ++ ideaHint idea)
256-
, _source = Just "hlint"
257-
, _message = idea2Message idea
258-
, _relatedInformation = Nothing
259-
, _tags = Nothing
260-
, _codeDescription = Nothing
261-
, _data_ = Nothing
262-
}
263-
where codePre = if null $ ideaRefactoring idea then "" else "refact:"
248+
249+
ideaToDiagnostic :: Idea -> Maybe Diagnostic
250+
ideaToDiagnostic idea = do
251+
diagnosticSeverity <- ideaSeverityToDiagnosticSeverity (ideaSeverity idea)
252+
pure $
253+
LSP.Diagnostic {
254+
_range = srcSpanToRange $ ideaSpan idea
255+
, _severity = Just diagnosticSeverity
256+
-- we are encoding the fact that idea has refactorings in diagnostic code
257+
, _code = Just (InR $ T.pack $ codePre ++ ideaHint idea)
258+
, _source = Just "hlint"
259+
, _message = idea2Message idea
260+
, _relatedInformation = Nothing
261+
, _tags = Nothing
262+
, _codeDescription = Nothing
263+
, _data_ = Nothing
264+
}
265+
266+
where
267+
codePre = if null $ ideaRefactoring idea then "" else "refact:"
268+
269+
-- We only propogate error severity of hlint and downgrade other severities to Info.
270+
-- Currently, there are just 2 error level serverities present in hlint by default: https://github.com/ndmitchell/hlint/issues/1549#issuecomment-1892701824.
271+
-- And according to ndmitchell: The default error level severities of the two hints are justified and it's fairly uncommon to happen.
272+
-- GH Issue about discussion on this: https://github.com/ndmitchell/hlint/issues/1549
273+
ideaSeverityToDiagnosticSeverity :: Hlint.Severity -> Maybe LSP.DiagnosticSeverity
274+
ideaSeverityToDiagnosticSeverity Hlint.Ignore = Nothing
275+
ideaSeverityToDiagnosticSeverity Hlint.Suggestion = Just LSP.DiagnosticSeverity_Information
276+
ideaSeverityToDiagnosticSeverity Hlint.Warning = Just LSP.DiagnosticSeverity_Information
277+
ideaSeverityToDiagnosticSeverity Hlint.Error = Just LSP.DiagnosticSeverity_Error
264278

265279
idea2Message :: Idea -> T.Text
266280
idea2Message idea = T.unlines $ [T.pack $ ideaHint idea, "Found:", " " <> T.pack (ideaFrom idea)]

0 commit comments

Comments
 (0)