on 6a96e21 help sign in
188 lines 3.8 kB history raw
1{-# LANGUAGE OverloadedStrings #-}
2module Reporting.Exit.Help
3 ( Report
4 , report
5 , docReport
6 , jsonReport
7 , compilerReport
8 , reportToDoc
9 , reportToJson
10 , hintLink
11 , stack
12 , reflow
13 , note
14 , toString
15 , toStdout
16 , toStderr
17 )
18 where
21import qualified Data.List as List
22import qualified Data.Text as Text
23import GHC.IO.Handle (hIsTerminalDevice)
24import System.IO (Handle, hPutStr, stderr, stdout)
25import qualified Text.PrettyPrint.ANSI.Leijen as P
26import Text.PrettyPrint.ANSI.Leijen ((<>),(<+>))
28import qualified Elm.Compiler as Compiler
29import qualified Elm.Package as Pkg
30import qualified Json.Encode as Encode
31import qualified Reporting.Exit.Compile as Compile
35-- REPORT
38data Report
39 = CompilerReport Compile.Exit [Compile.Exit]
40 | Report
41 { _title :: String
42 , _path :: Maybe FilePath
43 , _message :: P.Doc
44 }
47report :: String -> Maybe FilePath -> String -> [P.Doc] -> Report
48report title path startString others =
49 Report title path $ stack (reflow startString:others)
52docReport :: String -> Maybe FilePath -> P.Doc -> [P.Doc] -> Report
53docReport title path startDoc others =
54 Report title path $ stack (startDoc:others)
57jsonReport :: String -> Maybe FilePath -> P.Doc -> Report
58jsonReport =
59 Report
62compilerReport :: Compile.Exit -> [Compile.Exit] -> Report
63compilerReport =
64 CompilerReport
68-- TO DOC
71reportToDoc :: Report -> P.Doc
72reportToDoc report_ =
73 case report_ of
74 CompilerReport e es ->
75 Compile.toDoc e es
77 Report title maybePath message ->
78 let
79 makeDashes n =
80 replicate (max 1 (80 - n)) '-'
82 errorBarEnd =
83 case maybePath of
84 Nothing ->
85 makeDashes (4 + length title)
87 Just path ->
88 makeDashes (5 + length title + length path) ++ " " ++ path
90 errorBar =
91 P.dullcyan $
92 "--" <+> P.text title <+> P.text errorBarEnd
93 in
94 stack [errorBar, message, ""]
98-- TO JSON
101reportToJson :: Report -> Encode.Value
102reportToJson report_ =
103 case report_ of
104 CompilerReport e es ->
105 Encode.object
106 [ ("type", Encode.text "compile-errors")
107 , ("errors", Encode.list Compile.toJson (e:es))
108 ]
110 Report title maybePath message ->
111 let
112 messageString =
113 P.displayS (P.renderPretty 1 80 message) ""
114 in
115 Encode.object
116 [ ("type", Encode.text "error")
117 , ("path", maybe Encode.null (Encode.text . Text.pack) maybePath)
118 , ("title", Encode.text (Text.pack title))
119 , ("message", Encode.text (Text.pack messageString))
120 ]
124-- HINT LINKS
127hintLink :: String -> String
128hintLink hintName =
129 "<https://github.com/elm-lang/elm-compiler/blob/"
130 ++ Pkg.versionToString Compiler.version
131 ++ "/hints/" ++ hintName ++ ".md>"
135-- HELPERS
138stack :: [P.Doc] -> P.Doc
139stack allDocs =
140 case allDocs of
141 [] ->
142 error "Do not use `stack` on empty lists."
144 doc : docs ->
145 List.foldl' verticalAppend doc docs
148verticalAppend :: P.Doc -> P.Doc -> P.Doc
149verticalAppend a b =
150 a <> P.hardline <> P.hardline <> b
153reflow :: String -> P.Doc
154reflow paragraph =
155 P.fillSep (map P.text (words paragraph))
158note :: String -> P.Doc
159note details =
160 P.fillSep $
161 (P.underline "Note" <> ":") : map P.text (words details)
165-- OUTPUT
168toString :: P.Doc -> String
169toString doc =
170 P.displayS (P.renderPretty 1 80 (P.plain doc)) ""
173toStdout :: P.Doc -> IO ()
174toStdout doc =
175 toHandle stdout doc
178toStderr :: P.Doc -> IO ()
179toStderr doc =
180 toHandle stderr doc
183toHandle :: Handle -> P.Doc -> IO ()
184toHandle handle doc =
185 do isTerminal <- hIsTerminalDevice handle
186 if isTerminal
187 then P.displayIO handle (P.renderPretty 1 80 doc)
188 else hPutStr handle (toString doc)