-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for GHC JSON diagnostics
- Loading branch information
Showing
23 changed files
with
463 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ other-extensions: | |
|
||
dependencies: | ||
- base >= 4.11 && < 5 | ||
- pretty | ||
- process | ||
- fsnotify == 0.4.* | ||
- time | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
{-# LANGUAGE DeriveAnyClass #-} | ||
module GHC.Diagnostic ( | ||
Diagnostic(..) | ||
, Span(..) | ||
, Location(..) | ||
, Severity(..) | ||
, parse | ||
, format | ||
) where | ||
|
||
import Prelude hiding ((<>), span, unlines) | ||
import Imports hiding (empty, unlines) | ||
import GHC.Generics (Generic) | ||
import Data.Aeson (ToJSON(..), FromJSON(..), decode) | ||
import Data.ByteString.Lazy (fromStrict) | ||
import Text.PrettyPrint | ||
|
||
data Diagnostic = Diagnostic { | ||
version :: String | ||
, ghcVersion :: String | ||
, span :: Maybe Span | ||
, severity :: Severity | ||
, code :: Maybe Int | ||
, message :: [String] | ||
, hints :: [String] | ||
} deriving (Eq, Show, Generic, ToJSON, FromJSON) | ||
|
||
data Span = Span { | ||
file :: FilePath | ||
, start :: Location | ||
, end :: Location | ||
} deriving (Eq, Show, Generic, ToJSON, FromJSON) | ||
|
||
data Location = Location { | ||
line :: Int | ||
, column :: Int | ||
} deriving (Eq, Show, Generic, ToJSON, FromJSON) | ||
|
||
data Severity = Warning | Error | ||
deriving (Eq, Show, Generic, ToJSON, FromJSON) | ||
|
||
parse :: ByteString -> Maybe Diagnostic | ||
parse = decode . fromStrict | ||
|
||
format :: Diagnostic -> ByteString | ||
format diagnostic = encodeUtf8 . render $ unlines [ | ||
hang header 4 messageWithHints | ||
, "" | ||
, "" | ||
] | ||
where | ||
header :: Doc | ||
header = span <> colon <+> severity <> colon <+> code | ||
|
||
span :: Doc | ||
span = case diagnostic.span of | ||
Nothing -> "<no location info>" | ||
Just loc -> text loc.file <> colon <> int loc.start.line <> colon <> int loc.start.column | ||
|
||
severity :: Doc | ||
severity = case diagnostic.severity of | ||
Warning -> "warning" | ||
Error -> "error" | ||
|
||
code :: Doc | ||
code = case diagnostic.code of | ||
Nothing -> empty | ||
Just c -> brackets $ "GHC-" <> int c | ||
|
||
message :: Doc | ||
message = bulleted $ map verbatim diagnostic.message | ||
|
||
hints :: [Doc] | ||
hints = map verbatim diagnostic.hints | ||
|
||
messageWithHints :: Doc | ||
messageWithHints = case hints of | ||
[] -> message | ||
[h] -> message $$ hang (text "Suggested fix:") 2 h | ||
hs -> message $$ hang (text "Suggested fixes:") 2 (bulleted hs) | ||
|
||
bulleted :: [Doc] -> Doc | ||
bulleted = \ case | ||
[] -> empty | ||
[doc] -> doc | ||
docs -> vcat $ map (char '•' <+>) docs | ||
|
||
verbatim :: String -> Doc | ||
verbatim = unlines . map text . lines | ||
|
||
unlines :: [Doc] -> Doc | ||
unlines = foldr ($+$) empty |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.