@@ -1,5 +1,13 @@
11{-# OPTIONS_GHC -Wall #-}
2−module Reporting.Render.Code (render) where
2+module Reporting.Render.Code
3+ ( Source
4+ , toSource
5+ , render
6+ , CodePair(..)
7+ , renderPair
8+ )
9+ where
10+
311
412import Data.Text (Text)
513import qualified Data.Text as Text
@@ -10,6 +18,24 @@ import qualified Reporting.Region as R
1018import qualified Reporting.Helpers as H
1119
1220
21+
22+-- CODE
23+
24+
25+newtype Source =
26+ Source [(Int, Text.Text)]
27+
28+
29+toSource :: Text.Text -> Source
30+toSource source =
31+ Source $ zip [1..] $
32+ Text.lines source ++ [Text.empty]
33+
34+
35+
36+-- RENDER
37+
38+
1339(|>) :: a -> (a -> b) -> b
1440(|>) a f =
1541 f a
@@ -20,17 +46,16 @@ import qualified Reporting.Helpers as H
2046 a <> hardline <> b
2147
2248
23−render :: Maybe R.Region -> R.Region -> Text -> Doc
24−render maybeSubRegion region@(R.Region start end) source =
49+render :: Source -> R.Region -> Maybe R.Region -> Doc
50+render (Source sourceLines) region@(R.Region start end) maybeSubRegion =
2551 let
2652 (R.Position startLine _) = start
2753 (R.Position endLine _) = end
2854
2955 relevantLines =
30− (Text.lines source ++ [Text.empty])
31− |> take endLine
56+ sourceLines
3257 |> drop (startLine - 1)
33− |> zip [startLine .. endLine + 1]
58+ |> take (1 + endLine - startLine)
3459
3560 width =
3661 length (show (fst (last relevantLines)))
@@ -71,7 +96,7 @@ drawLines addZigZag width (R.Region start end) sourceLines finalLine =
7196
7297drawLine :: Bool -> Int -> Int -> Int -> (Int, Text) -> Doc
7398drawLine addZigZag width startLine endLine (n, line) =
74− addLineNumber addZigZag width startLine endLine n (H.text line)
99+ addLineNumber addZigZag width startLine endLine n (H.textToDoc line)
75100
76101
77102addLineNumber :: Bool -> Int -> Int -> Int -> Int -> Doc -> Doc
@@ -90,3 +115,17 @@ addLineNumber addZigZag width start end n line =
90115 text " "
91116 in
92117 text lineNumber <> spacer <> line
118+
119+
120+
121+-- RENDER PAIR
122+
123+
124+data CodePair
125+ = OneLine Doc
126+ | TwoChunks Doc Doc
127+
128+
129+renderPair :: Source -> R.Region -> R.Region -> CodePair
130+renderPair source r1 r2 =
131+ error "TODO" source r1 r2