Skip to content

Commit eeb62e0

Browse files
committed
Return hlint diags if no ghc parse errors
1 parent 7f5a4ef commit eeb62e0

File tree

1 file changed

+24
-9
lines changed
  • plugins/hls-hlint-plugin/src/Ide/Plugin

1 file changed

+24
-9
lines changed

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

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import Data.Typeable
3535
import Development.IDE
3636
import Development.IDE.Core.Rules (defineNoFile)
3737
import Development.IDE.Core.Shake (getDiagnostics)
38-
import Development.Shake
3938

4039
#ifdef GHC_LIB
4140
import Development.IDE.Core.RuleTypes (GhcSession(..))
@@ -113,9 +112,8 @@ rules = do
113112
diagnostics :: NormalizedFilePath -> Either ParseError [Idea] -> [FileDiagnostic]
114113
diagnostics file (Right ideas) =
115114
[(file, ShowDiag, ideaToDiagnostic i) | i <- ideas, ideaSeverity i /= Ignore]
116-
-- We don't return parse errors as diagnostics cause they match the emitted ones
117-
-- by ghc and they would be duplicated
118-
diagnostics file (Left parseErr) = []
115+
diagnostics file (Left parseErr) =
116+
[(file, ShowDiag, parseErrorToDiagnostic parseErr)]
119117

120118
ideaToDiagnostic :: Idea -> Diagnostic
121119
ideaToDiagnostic idea =
@@ -130,6 +128,18 @@ rules = do
130128
}
131129
where codePre = if null $ ideaRefactoring idea then "" else "refact:"
132130

131+
parseErrorToDiagnostic :: ParseError -> Diagnostic
132+
parseErrorToDiagnostic (Hlint.ParseError l msg contents) =
133+
LSP.Diagnostic {
134+
_range = srcSpanToRange l
135+
, _severity = Just LSP.DsInfo
136+
, _code = Just (LSP.StringValue "parser")
137+
, _source = Just "hlint"
138+
, _message = T.unlines [T.pack msg,T.pack contents]
139+
, _relatedInformation = Nothing
140+
, _tags = Nothing
141+
}
142+
133143
-- This one is defined in Development.IDE.GHC.Error but here
134144
-- the types could come from ghc-lib or ghc
135145
srcSpanToRange :: SrcSpan -> LSP.Range
@@ -157,11 +167,16 @@ getIdeas nfp = do
157167
where moduleEx :: ParseFlags -> Action (Maybe (Either ParseError ModuleEx))
158168
#ifdef GHC_LIB
159169
moduleEx flags = do
160-
flags' <- setExtensions flags
161-
(_, contents) <- getFileContents nfp
162-
let fp = fromNormalizedFilePath nfp
163-
let contents' = T.unpack <$> contents
164-
Just <$> (liftIO $ parseModuleEx flags' fp contents')
170+
mbpm <- getParsedModule nfp
171+
-- If ghc was not able to parse the module, we disable hlint diagnostics
172+
if isNothing mbpm
173+
then return Nothing
174+
else do
175+
flags' <- setExtensions flags
176+
(_, contents) <- getFileContents nfp
177+
let fp = fromNormalizedFilePath nfp
178+
let contents' = T.unpack <$> contents
179+
Just <$> (liftIO $ parseModuleEx flags' fp contents')
165180

166181
setExtensions flags = do
167182
hsc <- hscEnv <$> use_ GhcSession nfp

0 commit comments

Comments
 (0)