1{-# OPTIONS_GHC -Wall #-}
2module Reporting.Render.Code
12import Data.Text (Text)
13import qualified Data.Text as Text
14import Text.PrettyPrint.ANSI.Leijen
15 ( Doc, (<>), hardline, dullred, empty, text )
17import qualified Reporting.Region as R
18import qualified Reporting.Helpers as H
26 Source [(Int, Text.Text)]
29toSource :: Text.Text -> Source
32 Text.lines source ++ [Text.empty]
39(|>) :: a -> (a -> b) -> b
44(<==>) :: Doc -> Doc -> Doc
49render :: Source -> R.Region -> Maybe R.Region -> Doc
50render (Source sourceLines) region@(R.Region start end) maybeSubRegion =
52 (R.Position startLine _) = start
53 (R.Position endLine _) = end
57 |> drop (startLine - 1)
58 |> take (1 + endLine - startLine)
61 length (show (fst (last relevantLines)))
64 maybe region id maybeSubRegion
66 case makeUnderline width endLine smallerRegion of
68 drawLines True width smallerRegion relevantLines empty
71 drawLines False width smallerRegion relevantLines underline
74makeUnderline :: Int -> Int -> R.Region -> Maybe Doc
75makeUnderline width realEndLine (R.Region (R.Position start c1) (R.Position end c2)) =
76 if start /= end || end < realEndLine then
81 spaces = replicate (c1 + width + 1) ' '
82 zigzag = replicate (max 1 (c2 - c1)) '^'
84 Just (text spaces <> dullred (text zigzag))
87drawLines :: Bool -> Int -> R.Region -> [(Int, Text)] -> Doc -> Doc
88drawLines addZigZag width (R.Region start end) sourceLines finalLine =
90 (R.Position startLine _) = start
91 (R.Position endLine _) = end
93 foldr (<==>) finalLine $
94 map (drawLine addZigZag width startLine endLine) sourceLines
97drawLine :: Bool -> Int -> Int -> Int -> (Int, Text) -> Doc
98drawLine addZigZag width startLine endLine (n, line) =
99 addLineNumber addZigZag width startLine endLine n (H.textToDoc line)
102addLineNumber :: Bool -> Int -> Int -> Int -> Int -> Doc -> Doc
103addLineNumber addZigZag width start end n line =
106 if n < 0 then " " else show n
109 replicate (width - length number) ' ' ++ number ++ "|"
112 if addZigZag && start <= n && n <= end then
117 text lineNumber <> spacer <> line
129renderPair :: Source -> R.Region -> R.Region -> CodePair
130renderPair source r1 r2 =
131 error "TODO" source r1 r2
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