-
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.
- Loading branch information
Showing
8 changed files
with
208 additions
and
154 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,106 +1,46 @@ | ||
{-# LANGUAGE DeriveAnyClass #-} | ||
module GHC.Diagnostic ( | ||
Diagnostic(..) | ||
, Span(..) | ||
, Location(..) | ||
, Severity(..) | ||
, parse | ||
, format | ||
module Diagnostic | ||
, Action(..) | ||
, analyze | ||
, apply | ||
) 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 | ||
import Imports | ||
|
||
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) | ||
import System.IO | ||
import Data.Text (Text) | ||
import qualified Data.Text as T | ||
import qualified Data.Text.Encoding as T | ||
import qualified Data.ByteString as B | ||
import Data.ByteString.Builder (hPutBuilder) | ||
|
||
data Span = Span { | ||
file :: FilePath | ||
, start :: Location | ||
, end :: Location | ||
} deriving (Eq, Show, Generic, ToJSON, FromJSON) | ||
import GHC.Diagnostic.Type as Diagnostic | ||
|
||
data Location = Location { | ||
line :: Int | ||
, column :: Int | ||
} deriving (Eq, Show, Generic, ToJSON, FromJSON) | ||
data Action = AddExtension FilePath Text | ||
deriving (Eq, Show) | ||
|
||
data Severity = Warning | Error | ||
deriving (Eq, Show, Generic, ToJSON, FromJSON) | ||
|
||
parse :: ByteString -> Maybe Diagnostic | ||
parse = fmap removeGhciSpecificHints . 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 | ||
|
||
removeGhciSpecificHints :: Diagnostic -> Diagnostic | ||
removeGhciSpecificHints diagnostic = diagnostic { hints = map processHint diagnostic.hints } | ||
analyze :: Diagnostic -> Maybe Action | ||
analyze diagnostic = listToMaybe $ mapMaybe analyzeHint diagnostic.hints | ||
where | ||
isSetLanguageExtension :: String -> Bool | ||
isSetLanguageExtension = isPrefixOf " :set -X" | ||
|
||
processHint :: String -> String | ||
processHint input = case lines input of | ||
[hint, "You may enable this language extension in GHCi with:", ghciHint] | ||
| isSetLanguageExtension ghciHint -> hint | ||
hint : "You may enable these language extensions in GHCi with:" : ghciHints | ||
| all isSetLanguageExtension ghciHints -> hint | ||
_ -> input | ||
analyzeHint :: String -> Maybe Action | ||
analyzeHint (T.pack -> hint) = | ||
perhapsYouIntendedToUse | ||
<|> enableAnyOfTheFollowingExtensions | ||
where | ||
perhapsYouIntendedToUse :: Maybe Action | ||
perhapsYouIntendedToUse = do | ||
AddExtension . (.file) <$> diagnostic.span <*> T.stripPrefix "Perhaps you intended to use " hint | ||
|
||
enableAnyOfTheFollowingExtensions :: Maybe Action | ||
enableAnyOfTheFollowingExtensions = do | ||
file <- (.file) <$> diagnostic.span | ||
T.stripPrefix "Enable any of the following extensions: " hint | ||
>>= listToMaybe . reverse . map (AddExtension file) . T.splitOn ", " | ||
|
||
apply :: Action -> IO () | ||
apply = \ case | ||
AddExtension file name -> do | ||
old <- B.readFile file | ||
withFile file WriteMode $ \ h -> do | ||
hPutBuilder h $ "{-# LANGUAGE " <> T.encodeUtf8Builder name <> " #-}\n" | ||
B.hPutStr h old |
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,106 @@ | ||
{-# LANGUAGE DeriveAnyClass #-} | ||
module GHC.Diagnostic.Type ( | ||
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 = fmap removeGhciSpecificHints . 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 | ||
|
||
removeGhciSpecificHints :: Diagnostic -> Diagnostic | ||
removeGhciSpecificHints diagnostic = diagnostic { hints = map processHint diagnostic.hints } | ||
where | ||
isSetLanguageExtension :: String -> Bool | ||
isSetLanguageExtension = isPrefixOf " :set -X" | ||
|
||
processHint :: String -> String | ||
processHint input = case lines input of | ||
[hint, "You may enable this language extension in GHCi with:", ghciHint] | ||
| isSetLanguageExtension ghciHint -> hint | ||
hint : "You may enable these language extensions in GHCi with:" : ghciHints | ||
| all isSetLanguageExtension ghciHints -> hint | ||
_ -> input |
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.