1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Reporting.Render.Code
13import qualified Data.List as List
14import qualified Data.Text as Text
16import Reporting.Doc (Doc, (<>))
17import qualified Reporting.Doc as D
18import qualified Reporting.Region as R
26 Source [(Int, Text.Text)]
29toSource :: Text.Text -> Source
32 Text.lines source ++ [Text.empty]
39(|>) :: a -> (a -> b) -> b
44render :: Source -> R.Region -> Maybe R.Region -> Doc
45render (Source sourceLines) region@(R.Region start end) maybeSubRegion =
47 (R.Position startLine _) = start
48 (R.Position endLine _) = end
52 |> drop (startLine - 1)
53 |> take (1 + endLine - startLine)
56 length (show (fst (last relevantLines)))
59 maybe region id maybeSubRegion
61 case makeUnderline width endLine smallerRegion of
63 drawLines True width smallerRegion relevantLines D.empty
66 drawLines False width smallerRegion relevantLines underline
69makeUnderline :: Int -> Int -> R.Region -> Maybe Doc
70makeUnderline width realEndLine (R.Region (R.Position start c1) (R.Position end c2)) =
71 if start /= end || end < realEndLine then
76 spaces = replicate (c1 + width + 1) ' '
77 zigzag = replicate (max 1 (c2 - c1)) '^'
79 Just (D.fromString spaces <> D.dullred (D.fromString zigzag))
82drawLines :: Bool -> Int -> R.Region -> [(Int, Text.Text)] -> Doc -> Doc
83drawLines addZigZag width (R.Region start end) sourceLines finalLine =
85 (R.Position startLine _) = start
86 (R.Position endLine _) = end
89 map (drawLine addZigZag width startLine endLine) sourceLines
93drawLine :: Bool -> Int -> Int -> Int -> (Int, Text.Text) -> Doc
94drawLine addZigZag width startLine endLine (n, line) =
95 addLineNumber addZigZag width startLine endLine n (D.fromText line)
98addLineNumber :: Bool -> Int -> Int -> Int -> Int -> Doc -> Doc
99addLineNumber addZigZag width start end n line =
102 if n < 0 then " " else show n
105 replicate (width - length number) ' ' ++ number ++ "|"
108 if addZigZag && start <= n && n <= end then
113 D.fromString lineNumber <> spacer <> line
125renderPair :: Source -> R.Region -> R.Region -> CodePair
126renderPair source@(Source sourceLines) region1 region2 =
128 (R.Region (R.Position startRow1 startCol1) (R.Position endRow1 endCol1)) = region1
129 (R.Region (R.Position startRow2 startCol2) (R.Position endRow2 endCol2)) = region2
131 if startRow1 == endRow1 && endRow1 == startRow2 && startRow2 == endRow2 then
133 lineNumber = show startRow1
134 spaces1 = replicate (startCol1 + length lineNumber + 1) ' '
135 zigzag1 = replicate (endCol1 - startCol1) '^'
136 spaces2 = replicate (startCol2 - endCol1) ' '
137 zigzag2 = replicate (endCol2 - startCol2) '^'
139 (Just line) = List.lookup startRow1 sourceLines
143 [ D.fromString lineNumber <> "| " <> D.fromText line
144 , D.fromString spaces1 <> D.dullred (D.fromString zigzag1) <>
145 D.fromString spaces2 <> D.dullred (D.fromString zigzag2)
150 (render source region1 Nothing)
151 (render source region2 Nothing)
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