on master help sign in
Implement Render.Code.renderPair function
9809b00Evan Czaplicki
src/Reporting/Render/Code.hs+33−11
@@ -1,4 +1,5 @@
1{-# OPTIONS_GHC -Wall #-}
1{-# OPTIONS_GHC -Wall #-}
2+{-# LANGUAGE OverloadedStrings #-}
2module Reporting.Render.Code
3module Reporting.Render.Code
3 ( Source
4 ( Source
4 , toSource
5 , toSource
@@ -9,13 +10,11 @@ module Reporting.Render.Code
9 where
10 where
10
11
11
12
12−import Data.Text (Text)
13+import qualified Data.List as List
13import qualified Data.Text as Text
14import qualified Data.Text as Text
14−import Text.PrettyPrint.ANSI.Leijen
15+import Text.PrettyPrint.ANSI.Leijen (Doc, (<>), hardline, dullred, empty, text)
15− ( Doc, (<>), hardline, dullred, empty, text )
16
16
17import qualified Reporting.Region as R
17import qualified Reporting.Region as R
18−import qualified Reporting.Helpers as H
19
18
20
19
21
20
@@ -84,7 +83,7 @@ makeUnderline width realEndLine (R.Region (R.Position start c1) (R.Position end
84 Just (text spaces <> dullred (text zigzag))
83 Just (text spaces <> dullred (text zigzag))
85
84
86
85
87−drawLines :: Bool -> Int -> R.Region -> [(Int, Text)] -> Doc -> Doc
86+drawLines :: Bool -> Int -> R.Region -> [(Int, Text.Text)] -> Doc -> Doc
88drawLines addZigZag width (R.Region start end) sourceLines finalLine =
87drawLines addZigZag width (R.Region start end) sourceLines finalLine =
89 let
88 let
90 (R.Position startLine _) = start
89 (R.Position startLine _) = start
@@ -94,9 +93,9 @@ drawLines addZigZag width (R.Region start end) sourceLines finalLine =
94 map (drawLine addZigZag width startLine endLine) sourceLines
93 map (drawLine addZigZag width startLine endLine) sourceLines
95
94
96
95
97−drawLine :: Bool -> Int -> Int -> Int -> (Int, Text) -> Doc
96+drawLine :: Bool -> Int -> Int -> Int -> (Int, Text.Text) -> Doc
98drawLine addZigZag width startLine endLine (n, line) =
97drawLine addZigZag width startLine endLine (n, line) =
99− addLineNumber addZigZag width startLine endLine n (H.textToDoc line)
98+ addLineNumber addZigZag width startLine endLine n (text (Text.unpack line))
100
99
101
100
102addLineNumber :: Bool -> Int -> Int -> Int -> Int -> Doc -> Doc
101addLineNumber :: Bool -> Int -> Int -> Int -> Int -> Doc -> Doc
@@ -110,9 +109,9 @@ addLineNumber addZigZag width start end n line =
110
109
111 spacer =
110 spacer =
112 if addZigZag && start <= n && n <= end then
111 if addZigZag && start <= n && n <= end then
113− dullred (text ">")
112+ dullred ">"
114 else
113 else
115− text " "
114+ " "
116 in
115 in
117 text lineNumber <> spacer <> line
116 text lineNumber <> spacer <> line
118
117
@@ -127,5 +126,28 @@ data CodePair
127
126
128
127
129renderPair :: Source -> R.Region -> R.Region -> CodePair
128renderPair :: Source -> R.Region -> R.Region -> CodePair
130−renderPair source r1 r2 =
129+renderPair source@(Source sourceLines) region1 region2 =
131− error "TODO" source r1 r2
130+ let
131+ (R.Region (R.Position startRow1 startCol1) (R.Position endRow1 endCol1)) = region1
132+ (R.Region (R.Position startRow2 startCol2) (R.Position endRow2 endCol2)) = region2
133+ in
134+ if startRow1 == endRow1 && endRow1 == startRow2 && startRow2 == endRow2 then
135+ let
136+ lineNumber = show startRow1
137+ spaces1 = replicate (startCol1 + length lineNumber + 1) ' '
138+ zigzag1 = replicate (endCol1 - startCol1) '^'
139+ spaces2 = replicate (startCol2 - endCol1) ' '
140+ zigzag2 = replicate (endCol2 - startCol2) '^'
141+
142+ (Just line) = List.lookup startRow1 sourceLines
143+ in
144+ OneLine $
145+ text lineNumber <> "|" <> text (Text.unpack line)
146+ <> hardline
147+ <> text spaces1 <> dullred (text zigzag1)
148+ <> text spaces2 <> dullred (text zigzag2)
149+
150+ else
151+ TwoChunks
152+ (render source region1 Nothing)
153+ (render source region2 Nothing)