Skip to content

Commit 98b963c

Browse files
committed
detect when there really is an else branch next
Fixes elm/error-message-catalog#14
1 parent 5eddcb2 commit 98b963c

File tree

2 files changed

+50
-17
lines changed

2 files changed

+50
-17
lines changed

compiler/src/Reporting/Error/Syntax.hs

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3747,24 +3747,43 @@ toIfReport source context if_ startRow startCol =
37473747
)
37483748

37493749
IfIndentElse row col ->
3750-
let
3751-
surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3752-
region = toRegion row col
3753-
in
3754-
Report.Report "UNFINISHED IF" region [] $
3755-
Code.toSnippet source surroundings (Just region)
3756-
(
3757-
D.reflow $
3758-
"I was expecting to see an `else` branch after this:"
3759-
,
3760-
D.stack
3761-
[ D.fillSep
3762-
["I","know","what","to","do","when","the","condition","is","True,"
3763-
,"but","what","happens","when","it","is","False?"
3764-
,"Add","an",D.cyan "else","branch","to","handle","that","scenario!"
3750+
case Code.nextLineStartsWithKeyword "else" source row of
3751+
Just (elseRow, elseCol) ->
3752+
let
3753+
surroundings = A.Region (A.Position startRow startCol) (A.Position elseRow elseCol)
3754+
region = toWiderRegion elseRow elseCol 4
3755+
in
3756+
Report.Report "WEIRD ELSE BRANCH" region [] $
3757+
Code.toSnippet source surroundings (Just region)
3758+
(
3759+
D.reflow $
3760+
"I was partway through an `if` expression when I got stuck here:"
3761+
,
3762+
D.fillSep $
3763+
["I","think","this",D.cyan "else","keyword","needs","to","be","indented","more."
3764+
,"Try","adding","some","spaces","before","it."
37653765
]
3766-
]
3767-
)
3766+
)
3767+
3768+
Nothing ->
3769+
let
3770+
surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3771+
region = toRegion row col
3772+
in
3773+
Report.Report "UNFINISHED IF" region [] $
3774+
Code.toSnippet source surroundings (Just region)
3775+
(
3776+
D.reflow $
3777+
"I was expecting to see an `else` branch after this:"
3778+
,
3779+
D.stack
3780+
[ D.fillSep
3781+
["I","know","what","to","do","when","the","condition","is","True,"
3782+
,"but","what","happens","when","it","is","False?"
3783+
,"Add","an",D.cyan "else","branch","to","handle","that","scenario!"
3784+
]
3785+
]
3786+
)
37683787

37693788

37703789

compiler/src/Reporting/Render/Code.hs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module Reporting.Render.Code
77
, toPair
88
, Next(..)
99
, whatIsNext
10+
, nextLineStartsWithKeyword
1011
)
1112
where
1213

@@ -245,3 +246,16 @@ startsWithKeyword restOfLine keyword =
245246

246247
c:_ ->
247248
not (Char.isAlphaNum c || c == '_')
249+
250+
251+
nextLineStartsWithKeyword :: [Char] -> Source -> Row -> Maybe (Row, Col)
252+
nextLineStartsWithKeyword keyword (Source sourceLines) row =
253+
case List.lookup (row + 1) sourceLines of
254+
Nothing ->
255+
Nothing
256+
257+
Just line ->
258+
if startsWithKeyword (dropWhile (==' ') line) keyword then
259+
Just (row + 1, 1 + fromIntegral (length (takeWhile (==' ') line)))
260+
else
261+
Nothing

0 commit comments

Comments
 (0)