1{-# OPTIONS_GHC -Wall #-}
2module Reporting.Render.Code (render) where
5import qualified Data.Text as Text
6import Text.PrettyPrint.ANSI.Leijen
7 ( Doc, (<>), hardline, dullred, empty, text )
9import qualified Reporting.Region as R
10import qualified Reporting.Helpers as H
13(|>) :: a -> (a -> b) -> b
18(<==>) :: Doc -> Doc -> Doc
23render :: Maybe R.Region -> R.Region -> Text -> Doc
24render maybeSubRegion region@(R.Region start end) source =
26 (R.Position startLine _) = start
27 (R.Position endLine _) = end
30 (Text.lines source ++ [Text.empty])
32 |> drop (startLine - 1)
33 |> zip [startLine .. endLine + 1]
36 length (show (fst (last relevantLines)))
39 maybe region id maybeSubRegion
41 case makeUnderline width endLine smallerRegion of
43 drawLines True width smallerRegion relevantLines empty
46 drawLines False width smallerRegion relevantLines underline
49makeUnderline :: Int -> Int -> R.Region -> Maybe Doc
50makeUnderline width realEndLine (R.Region (R.Position start c1) (R.Position end c2)) =
51 if start /= end || end < realEndLine then
56 spaces = replicate (c1 + width + 1) ' '
57 zigzag = replicate (max 1 (c2 - c1)) '^'
59 Just (text spaces <> dullred (text zigzag))
62drawLines :: Bool -> Int -> R.Region -> [(Int, Text)] -> Doc -> Doc
63drawLines addZigZag width (R.Region start end) sourceLines finalLine =
65 (R.Position startLine _) = start
66 (R.Position endLine _) = end
68 foldr (<==>) finalLine $
69 map (drawLine addZigZag width startLine endLine) sourceLines
72drawLine :: Bool -> Int -> Int -> Int -> (Int, Text) -> Doc
73drawLine addZigZag width startLine endLine (n, line) =
74 addLineNumber addZigZag width startLine endLine n (H.text line)
77addLineNumber :: Bool -> Int -> Int -> Int -> Int -> Doc -> Doc
78addLineNumber addZigZag width start end n line =
81 if n < 0 then " " else show n
84 replicate (width - length number) ' ' ++ number ++ "|"
87 if addZigZag && start <= n && n <= end then
92 text lineNumber <> spacer <> line
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list