on master help sign in
Implement Render.Code.renderPair function
commit9809b00480624f9eab90ae4e626c334fef7dee38 authorEvan Czaplicki <evancz@users.noreply.github.com> dateSat 10 Feb 2018, 16:37 (9 years ago) parent5342b58
1 file changed+33−11
src/Reporting/Render/Code.hs+33−11
src/Reporting/Render/Code.hs+33−11
@@ -1,4 +1,5 @@
11{-# OPTIONS_GHC -Wall #-}
2+{-# LANGUAGE OverloadedStrings #-}
23module Reporting.Render.Code
34 ( Source
45 , toSource
@@ -9,13 +10,11 @@ module Reporting.Render.Code
910 where
1011
1112
12−import Data.Text (Text)
13+import qualified Data.List as List
1314import qualified Data.Text as Text
14−import Text.PrettyPrint.ANSI.Leijen
15− ( Doc, (<>), hardline, dullred, empty, text )
15+import Text.PrettyPrint.ANSI.Leijen (Doc, (<>), hardline, dullred, empty, text)
1616
1717import qualified Reporting.Region as R
18−import qualified Reporting.Helpers as H
1918
2019
2120
@@ -84,7 +83,7 @@ makeUnderline width realEndLine (R.Region (R.Position start c1) (R.Position end
8483 Just (text spaces <> dullred (text zigzag))
8584
8685
87−drawLines :: Bool -> Int -> R.Region -> [(Int, Text)] -> Doc -> Doc
86+drawLines :: Bool -> Int -> R.Region -> [(Int, Text.Text)] -> Doc -> Doc
8887drawLines addZigZag width (R.Region start end) sourceLines finalLine =
8988 let
9089 (R.Position startLine _) = start
@@ -94,9 +93,9 @@ drawLines addZigZag width (R.Region start end) sourceLines finalLine =
9493 map (drawLine addZigZag width startLine endLine) sourceLines
9594
9695
97−drawLine :: Bool -> Int -> Int -> Int -> (Int, Text) -> Doc
96+drawLine :: Bool -> Int -> Int -> Int -> (Int, Text.Text) -> Doc
9897drawLine 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))
10099
101100
102101addLineNumber :: Bool -> Int -> Int -> Int -> Int -> Doc -> Doc
@@ -110,9 +109,9 @@ addLineNumber addZigZag width start end n line =
110109
111110 spacer =
112111 if addZigZag && start <= n && n <= end then
113− dullred (text ">")
112+ dullred ">"
114113 else
115− text " "
114+ " "
116115 in
117116 text lineNumber <> spacer <> line
118117
@@ -127,5 +126,28 @@ data CodePair
127126
128127
129128renderPair :: Source -> R.Region -> R.Region -> CodePair
130−renderPair source r1 r2 =
131− error "TODO" source r1 r2
129+renderPair source@(Source sourceLines) region1 region2 =
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)