on master help sign in
Allow subregion underlines on single lines
commitb7842c03ffd754845ab598bd135e9f7301a7384d authorEvan Czaplicki <info@elm-lang.org> dateThu 7 May 2015, 14:02 (11 years ago) parent37a5687
1 file changed+33−15
src/Reporting/Report.hs+33−15
src/Reporting/Report.hs+33−15
@@ -58,21 +58,14 @@ grabRegion maybeSubRegion (R.Region start end) source =
5858 [] ->
5959 error "something has gone badly wrong with reporting source locations"
6060
61− [theLine] ->
62− let
63− n = startLine
64− w = length (show (n + 1))
65− underline =
66− replicate (startColumn - 1) ' '
67− ++ replicate (max 1 (endColumn - startColumn)) '^'
68− number =
69− addLineNumber Nothing w
70− in
71− unlines
72− [ number (n-1) ""
73− , number n theLine
74− , number (n+1) underline
75− ]
61+ [sourceLine] ->
62+ singleLineRegion startLine sourceLine $
63+ case maybeSubRegion of
64+ Nothing ->
65+ (0, startColumn, endColumn, length sourceLine)
66+
67+ Just (R.Region s e) ->
68+ (startColumn, R.column s, R.column e, endColumn)
7669
7770 firstLine : rest ->
7871 let
@@ -112,3 +105,28 @@ addLineNumber maybeSubRegion width n line =
112105 replicate (width - length number) ' ' ++ number
113106 ++ "|" ++ maybe " " spacer maybeSubRegion
114107 ++ line
108+
109+
110+singleLineRegion :: Int -> String -> (Int, Int, Int, Int) -> String
111+singleLineRegion lineNum sourceLine (start, innerStart, innerEnd, end) =
112+ let
113+ w = length (show (lineNum + 1))
114+
115+ number =
116+ addLineNumber Nothing w
117+
118+ underline =
119+ replicate (innerStart - 1) ' '
120+ ++ replicate (max 1 (innerEnd - innerStart)) '^'
121+
122+ trimmedSourceLine =
123+ sourceLine
124+ |> drop (start - 1)
125+ |> take (end - start + 1)
126+ |> (++) (replicate (start - 1) ' ')
127+ in
128+ unlines
129+ [ number (lineNum-1) ""
130+ , number lineNum trimmedSourceLine
131+ , number (lineNum+1) underline
132+ ]