Skip to content

Commit

Permalink
improve error message when } is not indented enough
Browse files Browse the repository at this point in the history
  • Loading branch information
evancz committed Jul 26, 2019
1 parent 0cd361d commit 11ffbf5
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 31 deletions.
104 changes: 73 additions & 31 deletions compiler/src/Reporting/Error/Syntax.hs
Expand Up @@ -3977,21 +3977,42 @@ toRecordReport source context record startRow startCol =
)

RecordIndentEnd row col ->
let
surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
region = toRegion row col
in
Report.Report "UNFINISHED RECORD" region [] $
Code.toSnippet source surroundings (Just region)
(
D.reflow $
"I was expecting to see a closing curly brace next:"
,
addNoteForRecordIndentError $
D.fillSep $
["Try","putting","an",D.green "}","and","see","if","that","helps?"
]
)
case Code.nextLineStartsWithCloseCurly source row of
Just (curlyRow, curlyCol) ->
let
surroundings = A.Region (A.Position startRow startCol) (A.Position curlyRow curlyCol)
region = toRegion curlyRow curlyCol
in
Report.Report "NEED MORE INDENTATION" region [] $
Code.toSnippet source surroundings (Just region)
(
D.reflow $
"I was partway through parsing a record, but I got stuck here:"
,
D.stack
[ D.reflow $
"I need this curly brace to be indented more. Try adding some spaces before it!"
, noteForRecordError
]
)

Nothing ->
let
surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
region = toRegion row col
in
Report.Report "UNFINISHED RECORD" region [] $
Code.toSnippet source surroundings (Just region)
(
D.reflow $
"I was partway through parsing a record, but I got stuck here:"
,
addNoteForRecordIndentError $
D.fillSep $
["I","was","expecting","to","see","a","closing","curly","brace","next."
,"Try","putting","a",D.green "}","next","and","see","if","that","helps?"
]
)

RecordIndentField row col ->
let
Expand Down Expand Up @@ -5384,23 +5405,44 @@ toTRecordReport source context record startRow startCol =
)

TRecordIndentEnd row col ->
let
surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
region = toRegion row col
in
Report.Report "UNFINISHED RECORD TYPE" region [] $
Code.toSnippet source surroundings (Just region)
(
D.reflow $
"I was expecting to see a closing curly brace next:"
,
D.stack
[ D.fillSep $
["Try","putting","an",D.green "}","and","see","if","that","helps?"
case Code.nextLineStartsWithCloseCurly source row of
Just (curlyRow, curlyCol) ->
let
surroundings = A.Region (A.Position startRow startCol) (A.Position curlyRow curlyCol)
region = toRegion curlyRow curlyCol
in
Report.Report "NEED MORE INDENTATION" region [] $
Code.toSnippet source surroundings (Just region)
(
D.reflow $
"I was partway through parsing a record type, but I got stuck here:"
,
D.stack
[ D.reflow $
"I need this curly brace to be indented more. Try adding some spaces before it!"
, noteForRecordTypeError
]
, noteForRecordTypeIndentError
]
)
)

Nothing ->
let
surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
region = toRegion row col
in
Report.Report "UNFINISHED RECORD TYPE" region [] $
Code.toSnippet source surroundings (Just region)
(
D.reflow $
"I was partway through parsing a record type, but I got stuck here:"
,
D.stack
[ D.fillSep $
["I","was","expecting","to","see","a","closing","curly","brace","next."
,"Try","putting","a",D.green "}","next","and","see","if","that","helps?"
]
, noteForRecordTypeIndentError
]
)

TRecordIndentField row col ->
let
Expand Down
16 changes: 16 additions & 0 deletions compiler/src/Reporting/Render/Code.hs
Expand Up @@ -8,6 +8,7 @@ module Reporting.Render.Code
, Next(..)
, whatIsNext
, nextLineStartsWithKeyword
, nextLineStartsWithCloseCurly
)
where

Expand Down Expand Up @@ -259,3 +260,18 @@ nextLineStartsWithKeyword keyword (Source sourceLines) row =
Just (row + 1, 1 + fromIntegral (length (takeWhile (==' ') line)))
else
Nothing


nextLineStartsWithCloseCurly :: Source -> Row -> Maybe (Row, Col)
nextLineStartsWithCloseCurly (Source sourceLines) row =
case List.lookup (row + 1) sourceLines of
Nothing ->
Nothing

Just line ->
case dropWhile (==' ') line of
'}':_ ->
Just (row + 1, 1 + fromIntegral (length (takeWhile (==' ') line)))

_ ->
Nothing

0 comments on commit 11ffbf5

Please sign in to comment.