Skip to content

Commit d2b6205

Browse files
committed
Insert message "inline" into multiline code at point
1 parent 29d19ba commit d2b6205

File tree

6 files changed

+38
-36
lines changed

6 files changed

+38
-36
lines changed

src/dotty/tools/dotc/printing/Formatting.scala

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import scala.annotation.switch
1010
import scala.util.control.NonFatal
1111
import reporting.diagnostic.MessageContainer
1212
import util.DiffUtil
13-
import Highlighting.{ highlightToString => _, _ }
13+
import Highlighting._
1414
import SyntaxHighlighting._
1515

1616
object Formatting {
@@ -165,7 +165,11 @@ object Formatting {
165165
}
166166
}
167167

168-
/** Turns a `Seen => String` to produce a `where: T is...` clause */
168+
/** Turns a `Seen` into a `String` to produce an explanation for types on the
169+
* form `where: T is...`
170+
*
171+
* @return string disambiguating types
172+
*/
169173
private def explanations(seen: Seen)(implicit ctx: Context): String = {
170174
def needsExplanation(entry: Recorded) = entry match {
171175
case param: PolyParam => ctx.typerState.constraint.contains(param)
@@ -245,7 +249,7 @@ object Formatting {
245249
val exp = wrapNonSensical(expected, expected.show)
246250

247251
(found, expected) match {
248-
case (_: RefinedType, _: RefinedType) =>
252+
case (_: RefinedType, _: RefinedType) if ctx.settings.color.value != "never" =>
249253
DiffUtil.mkColoredTypeDiff(fnd, exp)
250254
case _ =>
251255
(hl"$fnd", hl"$exp")

src/dotty/tools/dotc/printing/Highlighting.scala

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ object Highlighting {
99

1010
implicit def highlightShow(h: Highlight)(implicit ctx: Context): String =
1111
h.show
12-
implicit def highlightToString(h: Highlight): String =
13-
h.toString
14-
implicit def hbufToString(hb: HighlightBuffer): String =
15-
hb.toString
1612

1713
abstract class Highlight(private val highlight: String) {
1814
def text: String

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ class ConsoleReporter(
3232
def stripColor(str: String): String =
3333
str.replaceAll("\u001B\\[[;\\d]*m", "")
3434

35-
def sourceLines(pos: SourcePosition)(implicit ctx: Context): (List[String], Int) = {
35+
def sourceLines(pos: SourcePosition)(implicit ctx: Context): (List[String], List[String], Int) = {
3636
var maxLen = Int.MinValue
37-
val lines = pos.lines
37+
def render(xs: List[Int]) =
38+
xs.map(pos.source.offsetToLine(_))
3839
.map { lineNbr =>
3940
val prefix = s"${lineNbr + 1} |"
4041
maxLen = math.max(maxLen, prefix.length)
@@ -45,7 +46,8 @@ class ConsoleReporter(
4546
hl"$lnum$line"
4647
}
4748

48-
(lines, maxLen)
49+
val (before, after) = pos.beforeAndAfterPoint
50+
(render(before), render(after), maxLen)
4951
}
5052

5153
def columnMarker(pos: SourcePosition, offset: Int)(implicit ctx: Context) = {
@@ -95,11 +97,11 @@ class ConsoleReporter(
9597
def printMessageAndPos(msg: Message, pos: SourcePosition, diagnosticLevel: String)(implicit ctx: Context): Unit = {
9698
printMessage(posStr(pos, diagnosticLevel, msg))
9799
if (pos.exists) {
98-
val (srcLines, offset) = sourceLines(pos)
100+
val (srcBefore, srcAfter, offset) = sourceLines(pos)
99101
val marker = columnMarker(pos, offset)
100102
val err = errorMsg(pos, msg.msg, offset)
101103

102-
printMessage((srcLines ::: marker :: err :: Nil).mkString("\n"))
104+
printMessage((srcBefore ::: marker :: err :: srcAfter).mkString("\n"))
103105
} else printMessage(msg.msg)
104106
}
105107

src/dotty/tools/dotc/reporting/diagnostic/Message.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ object Message {
1515
new NoExplanation(str)
1616
}
1717

18+
/** A `Message` contains all semantic information necessary to easily
19+
* comprehend what caused the message to be logged. Each message can be turned
20+
* into a `MessageContainer` which contains the log level and can later be
21+
* consumed by a subclass of `Reporter`.
22+
*
23+
* @param errorId a unique number identifying the message, this will later be
24+
* used to reference documentation online
25+
*/
1826
abstract class Message(val errorId: Int) { self =>
1927
import messages._
2028

src/dotty/tools/dotc/reporting/diagnostic/messages.scala

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,10 @@ object messages {
9090
}
9191

9292
val code1 =
93-
s"""|try $tryString catch {
94-
| case t: Throwable => ???
93+
s"""|import scala.util.control.NonFatal
94+
|
95+
|try $tryString catch {
96+
| case NonFatal(e) => ???
9597
|}""".stripMargin
9698

9799
val code2 =
@@ -108,7 +110,10 @@ object messages {
108110
|It is also possible to follow a ${"try"} immediately by a ${"finally"} - letting the
109111
|exception propagate - but still allowing for some clean up in ${"finally"}:
110112
|
111-
|$code2""".stripMargin
113+
|$code2
114+
|
115+
|It is recommended to use the ${"NonFatal"} extractor to catch all exceptions as it
116+
|correctly handles transfer functions like ${"return"}.""".stripMargin
112117
}
113118
}
114119

@@ -133,29 +138,10 @@ object messages {
133138
val kind = "Syntax"
134139
val msg =
135140
hl"""${"with"} as a type operator has been deprecated; use `&' instead"""
136-
val explanation = {
137-
val codeBlock1 =
138-
"""|trait A {
139-
| type T = Int
140-
|}
141-
|
142-
|trait B {
143-
| type T = Double
144-
|}""".stripMargin
145-
141+
val explanation =
146142
hl"""|Dotty introduces intersection types - `&' types. These replace the
147143
|use of the ${"with"} keyword. There are a few differences in
148-
|semantics between intersection types and using `${"with"}'.
149-
|
150-
|`${"A with B"}' is ordered, `${"A & B"}' is not.
151-
|
152-
|In:
153-
|
154-
|$codeBlock1
155-
|
156-
|The type of `${"T"}' in `${"A with B"}' is ${"Int"} whereas in `${"A & B"}'
157-
|the type of `${"T"}' is ${"Int & Double"}.""".stripMargin
158-
}
144+
|semantics between intersection types and using `${"with"}'.""".stripMargin
159145
}
160146

161147
case class CaseClassMissingParamList(cdef: untpd.TypeDef)(implicit ctx: Context)

src/dotty/tools/dotc/util/SourcePosition.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,15 @@ extends interfaces.SourcePosition {
2222
case xs => xs
2323
}
2424

25+
def lineOffsets: List[Int] =
26+
lines.map(source.lineToOffset(_))
27+
2528
def lineContent(lineNumber: Int): String =
2629
source.lineContent(source.lineToOffset(lineNumber))
2730

31+
def beforeAndAfterPoint: (List[Int], List[Int]) =
32+
lineOffsets.partition(_ < point)
33+
2834
/** The column of the position, starting at 0 */
2935
def column: Int = source.column(point)
3036

0 commit comments

Comments
 (0)