Add quoteCode for printing errors

This both colorizes and wraps code in backticks, in order to separate it from surrounding context.
This commit is contained in:
Greg Pfeil 2024-06-20 21:40:07 -04:00
parent bb4f39fb2f
commit 6f26a16401
No known key found for this signature in database
GPG Key ID: 1193ACD196ED61F2
3 changed files with 30 additions and 25 deletions

View File

@ -126,6 +126,10 @@ styleAnnotated sty a = (,sty) <$> rangeForAnnotated a
style :: s -> String -> Pretty (AnnotatedText s)
style sty str = Pr.lit . AT.annotate sty $ fromString str
-- | Applies the color highlighting for `Code`, but also quotes the code, to separate it from the containing context.
quoteCode :: String -> Pretty ColorText
quoteCode = Pr.backticked . style Code
stylePretty :: Color -> Pretty ColorText -> Pretty ColorText
stylePretty = Pr.map . AT.annotate
@ -1368,21 +1372,21 @@ renderParseErrors s = \case
<> excerpt
L.InvalidWordyId id ->
Pr.lines
[ "The identifier " <> style Code id <> " isn't valid syntax: ",
[ "The identifier, " <> quoteCode id <> ", isn't valid syntax: ",
"",
excerpt,
"Here's a few examples of valid syntax: "
<> style Code "abba1'"
<> quoteCode "abba1'"
<> ", "
<> style Code "snake_case"
<> quoteCode "snake_case"
<> ", "
<> style Code "Foo.zoink!"
<> quoteCode "Foo.zoink!"
<> ", and "
<> style Code "🌻"
<> quoteCode "🌻"
]
L.ReservedWordyId id ->
Pr.lines
[ "The identifier " <> style Code id <> " used here is a reserved keyword: ",
[ "The identifier, " <> quoteCode id <> ", used here is a reserved keyword: ",
"",
excerpt,
Pr.wrap $
@ -1392,19 +1396,19 @@ renderParseErrors s = \case
]
L.InvalidSymbolyId id ->
Pr.lines
[ "The infix identifier " <> style Code id <> " isnt valid syntax: ",
[ "The infix identifier, " <> quoteCode id <> ", isnt valid syntax: ",
"",
excerpt,
"Here are a few valid examples: "
<> style Code "++"
<> quoteCode "++"
<> ", "
<> style Code "Float./"
<> quoteCode "Float./"
<> ", and "
<> style Code "`List.map`"
<> quoteCode "List.map"
]
L.ReservedSymbolyId id ->
Pr.lines
[ "The identifier " <> style Code id <> " is reserved by Unison and can't be used as an operator: ",
[ "The identifier, " <> quoteCode id <> ", is reserved by Unison and can't be used as an operator: ",
"",
excerpt
]
@ -1458,11 +1462,12 @@ renderParseErrors s = \case
"",
excerpt,
Pr.wrap $
"I was expecting some digits after the '.',"
<> "for example: "
<> style Code (n <> "0")
"I was expecting some digits after the "
<> quoteCode "."
<> ", for example: "
<> quoteCode (n <> "0")
<> "or"
<> Pr.group (style Code (n <> "1e37") <> ".")
<> Pr.group (quoteCode (n <> "1e37") <> ".")
]
L.MissingExponent n ->
Pr.lines
@ -1472,7 +1477,7 @@ renderParseErrors s = \case
Pr.wrap $
"I was expecting some digits for the exponent,"
<> "for example: "
<> Pr.group (style Code (n <> "37") <> ".")
<> Pr.group (quoteCode (n <> "37") <> ".")
]
L.TextLiteralMissingClosingQuote _txt ->
Pr.lines
@ -1488,7 +1493,7 @@ renderParseErrors s = \case
"",
"I only know about the following escape characters:",
"",
let s ch = style Code (fromString $ "\\" <> [ch])
let s ch = quoteCode (fromString $ "\\" <> [ch])
in Pr.indentN 2 $ intercalateMap "," s (fst <$> L.escapeChars)
]
L.LayoutError ->
@ -1719,7 +1724,7 @@ renderParseErrors s = \case
let msg =
mconcat
[ "This looks like the start of an expression here but I was expecting a binding.",
"\nDid you mean to use a single " <> style Code ":",
"\nDid you mean to use a single " <> quoteCode ":",
" here for a type signature?",
"\n\n",
tokenAsErrorSite s t

View File

@ -19,8 +19,8 @@ x = 1. -- missing some digits after the decimal
1 | x = 1. -- missing some digits after the decimal
I was expecting some digits after the '.', for example: 1.0 or
1.1e37.
I was expecting some digits after the `.` , for example: `1.0`
or `1.1e37`.
```
```unison
@ -36,7 +36,7 @@ x = 1e -- missing an exponent
1 | x = 1e -- missing an exponent
I was expecting some digits for the exponent, for example:
1e37.
`1e37`.
```
```unison
@ -52,7 +52,7 @@ x = 1e- -- missing an exponent
1 | x = 1e- -- missing an exponent
I was expecting some digits for the exponent, for example:
1e-37.
`1e-37`.
```
```unison
@ -68,7 +68,7 @@ x = 1E+ -- missing an exponent
1 | x = 1E+ -- missing an exponent
I was expecting some digits for the exponent, for example:
1e+37.
`1e+37`.
```
### Hex, octal, and bytes literals
@ -343,7 +343,7 @@ use.keyword.in.namespace = 1
Loading changes detected in scratch.u.
The identifier namespace used here is a reserved keyword:
The identifier, `namespace`, used here is a reserved keyword:
1 | use.keyword.in.namespace = 1

View File

@ -30,7 +30,7 @@ namespace.blah = 1
Loading changes detected in scratch.u.
The identifier namespace used here is a reserved keyword:
The identifier, `namespace`, used here is a reserved keyword:
1 | namespace.blah = 1