on ca2be8a help sign in
elk/src/Reporting/Report.hs
83 lines 1.7 kB history raw
1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Reporting.Report
4 ( Report(..)
5 , toDoc
6 , toCodeSnippet
7 , toCodePair
8 )
9 where
12import Text.PrettyPrint.ANSI.Leijen (Doc, (<>), hardline, dullcyan, text)
14import qualified Reporting.Region as R
15import qualified Reporting.Render.Code as Code
19-- BUILD REPORTS
22data Report =
23 Report
24 { _title :: String
25 , _region :: R.Region
26 , _sgstns :: [String]
27 , _message :: Doc
28 }
31toDoc :: FilePath -> Report -> Doc
32toDoc filePath (Report title _ _ message) =
33 messageBar title filePath
34 <> hardline <> hardline <>
35 message
38messageBar :: String -> FilePath -> Doc
39messageBar title filePath =
40 let
41 usedSpace =
42 4 + length title + 1 + length filePath
43 in
44 dullcyan $ text $
45 "-- " ++ title
46 ++ " " ++ replicate (max 1 (80 - usedSpace)) '-'
47 ++ " " ++ filePath
51-- CODE FORMATTING
54toCodeSnippet :: Code.Source -> R.Region -> Maybe R.Region -> (Doc, Doc) -> Doc
55toCodeSnippet source region highlight (preHint, postHint) =
56 preHint
57 <> hardline <> hardline <>
58 Code.render source region highlight
59 <> hardline <>
60 postHint
61 <> hardline <> hardline
64toCodePair :: Code.Source -> R.Region -> R.Region -> (Doc, Doc) -> (Doc, Doc, Doc) -> Doc
65toCodePair source r1 r2 (oneStart, oneEnd) (twoStart, twoMiddle, twoEnd) =
66 case Code.renderPair source r1 r2 of
67 Code.OneLine codeDocs ->
68 oneStart
69 <> hardline <> hardline <>
70 codeDocs
71 <> hardline <>
72 oneEnd
74 Code.TwoChunks code1 code2 ->
75 twoStart
76 <> hardline <> hardline <>
77 code1
78 <> hardline <>
79 twoMiddle
80 <> hardline <> hardline <>
81 code2
82 <> hardline <>
83 twoEnd