Skip to content

Commit f0c360d

Browse files
committed
Use highlighting in explanation classes
1 parent 0994041 commit f0c360d

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

src/dotty/tools/dotc/reporting/Examples.scala

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package dotc
33
package reporting
44

55
import dotc.core.Contexts.Context
6+
import dotc.printing.SyntaxHighlighting._
67

78
object ErrorExplanations {
89
import dotc.ast.Trees._
@@ -36,32 +37,40 @@ object ErrorExplanations {
3637
case Block(Nil, untpd.EmptyTree) => "{}"
3738
case _ => tryBody.show
3839
}
39-
s"""|Explanation:
40-
|============
41-
|A try expression should be followed by some mechanism to handle any exceptions
42-
|thrown. Typically a `catch` expression follows the `try` and pattern matches
43-
|on any expected exceptions. For example:
44-
|
45-
|try $tryString catch {
46-
| case t: Throwable => ???
47-
|}
48-
|
49-
|It is also possible to follow a `try` immediately by a finally - letting the
50-
|exception propagate - but still allowing for some clean up in `finally`:
51-
|
52-
|try $tryString finally {
53-
| // perform your cleanup here!
54-
|}""".stripMargin
40+
41+
val code1 =
42+
s"""|try $tryString catch {
43+
| case t: Throwable => ???
44+
|}""".stripMargin
45+
46+
val code2 =
47+
s"""|try $tryString finally {
48+
| // perform your cleanup here!
49+
|}""".stripMargin
50+
51+
hl"""|Explanation:
52+
|============
53+
|A ${"try"} expression should be followed by some mechanism to handle any exceptions
54+
|thrown. Typically a ${"catch"} expression follows the ${"try"} and pattern matches
55+
|on any expected exceptions. For example:
56+
|
57+
|$code1
58+
|
59+
|It is also possible to follow a ${"try"} immediately by a ${"finally"} - letting the
60+
|exception propagate - but still allowing for some clean up in ${"finally"}:
61+
|
62+
|$code2
63+
""".stripMargin
5564
}
5665
}
5766

5867
case class EmptyCatchBlock(tryBody: untpd.Tree)(implicit ctx: Context) extends EmptyCatchOrFinallyBlock(tryBody) {
5968
val msg =
60-
"`catch` block does not contain a valid expression, try adding a case like - `case e: Exception =>` to the block"
69+
hl"""The ${"catch"} block does not contain a valid expression, try adding a case like - `${"case e: Exception =>"}` to the block"""
6170
}
6271

6372
case class EmptyCatchAndFinallyBlock(tryBody: untpd.Tree)(implicit ctx: Context) extends EmptyCatchOrFinallyBlock(tryBody) {
6473
val msg =
65-
"A try without `catch` or `finally` is equivalent to putting its body in a block; no exceptions are handled."
74+
hl"""A ${"try"} without ${"catch"} or ${"finally"} is equivalent to putting its body in a block; no exceptions are handled."""
6675
}
6776
}

0 commit comments

Comments
 (0)