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