Skip to content

Commit 05cc3e2

Browse files
committed
SI-10120 ReplReporter handles message indent
Instead of indenting source code to make messages align on output, let the reporter add indentation, only if the source is the console (and not a pastie or a loaded file). Previously, syntax errors were not indented. ``` $ skala Welcome to Scala 2.12.2-20170108-010722-939abf1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_111). Type in expressions for evaluation. Or try :help. scala> 'abc' <console>:1: error: unclosed character literal (or use " for string literal "abc") 'abc' ^ scala> :quit $ scala Welcome to Scala 2.12.1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_111). Type in expressions for evaluation. Or try :help. scala> 'abc' <console>:1: error: unclosed character literal 'abc' ^ ```
1 parent 939abf1 commit 05cc3e2

File tree

12 files changed

+51
-68
lines changed

12 files changed

+51
-68
lines changed

src/repl/scala/tools/nsc/interpreter/Formatting.scala

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/repl/scala/tools/nsc/interpreter/IMain.scala

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,8 @@ class IMain(initialSettings: Settings, protected val out: JPrintWriter) extends
111111
try body finally label = saved
112112
}
113113

114-
// the expanded prompt but without color escapes and without leading newline, for purposes of indenting
115-
lazy val formatting = Formatting.forPrompt(replProps.promptText)
116114
lazy val reporter: ReplReporter = new ReplReporter(this)
117115

118-
import formatting.indentCode
119116
import reporter.{ printMessage, printUntruncatedMessage }
120117

121118
// This exists mostly because using the reporter too early leads to deadlock.
@@ -867,8 +864,8 @@ class IMain(initialSettings: Settings, protected val out: JPrintWriter) extends
867864
|${preambleHeader format lineRep.readName}
868865
|${envLines mkString (" ", ";\n ", ";\n")}
869866
|$importsPreamble
870-
|${indentCode(toCompute)}""".stripMargin
871-
def preambleLength = preamble.length - toCompute.length - 1
867+
|${toCompute}""".stripMargin
868+
def preambleLength = preamble.length - toCompute.length
872869

873870
val generate = (m: MemberHandler) => m extraCodeToEvaluate Request.this
874871

src/repl/scala/tools/nsc/interpreter/ReplReporter.scala

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ package interpreter
99
import reporters._
1010
import IMain._
1111

12-
import scala.reflect.internal.util.Position
12+
import scala.reflect.internal.util.{OffsetPosition, Position}
1313

1414
/** Like ReplGlobal, a layer for ensuring extra functionality.
1515
*/
@@ -40,14 +40,25 @@ class ReplReporter(intp: IMain) extends ConsoleReporter(intp.settings, Console.i
4040
case INFO => RESET
4141
}
4242

43+
private val promptLength = replProps.promptText.lines.toList.last.length
44+
private val indentation = " " * promptLength
45+
46+
// colorized console labels
47+
override protected def clabel(severity: Severity): String = {
48+
val label0 = super.clabel(severity)
49+
if (replProps.colorOk) s"${severityColor(severity)}${label0}${RESET}" else label0
50+
}
51+
52+
// shift indentation for source text entered at prompt
4353
override def print(pos: Position, msg: String, severity: Severity) {
44-
val prefix = (
45-
if (replProps.colorOk)
46-
severityColor(severity) + clabel(severity) + RESET
47-
else
48-
clabel(severity)
49-
)
50-
printMessage(pos, prefix + msg)
54+
val adjusted =
55+
if (pos.source.file.name == "<console>")
56+
new OffsetPosition(pos.source, pos.offset.getOrElse(0)) {
57+
override def lineContent = s"${indentation}${super.lineContent}"
58+
override def lineCaret = s"${indentation}${super.lineCaret}"
59+
}
60+
else pos
61+
super.print(adjusted, msg, severity)
5162
}
5263

5364
override def printMessage(msg: String) {
@@ -63,12 +74,8 @@ class ReplReporter(intp: IMain) extends ConsoleReporter(intp.settings, Console.i
6374
else Console.println("[init] " + msg)
6475
}
6576

66-
override def displayPrompt() {
67-
if (intp.totalSilence) ()
68-
else super.displayPrompt()
69-
}
77+
override def displayPrompt() = if (!intp.totalSilence) super.displayPrompt()
7078

7179
override def rerunWithDetails(setting: reflect.internal.settings.MutableSettings#Setting, name: String) =
7280
s"; for details, enable `:setting $name' or `:replay $name'"
73-
7481
}

test/files/jvm/interpreter.check

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,13 +278,13 @@ scala> // both of the following should abort immediately:
278278

279279
scala> def x => y => z
280280
<console>:1: error: '=' expected but '=>' found.
281-
def x => y => z
282-
^
281+
def x => y => z
282+
^
283283

284284
scala> [1,2,3]
285285
<console>:1: error: illegal start of definition
286-
[1,2,3]
287-
^
286+
[1,2,3]
287+
^
288288

289289
scala>
290290

test/files/run/reify_newimpl_22.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ scala> {
1515
}
1616
println(code.eval)
1717
}
18-
<console>:19: free term: Ident(TermName("x")) defined by res0 in <console>:18:14
18+
<console>:19: free term: Ident(TermName("x")) defined by res0 in <console>:18:7
1919
val code = reify {
2020
^
2121
2

test/files/run/reify_newimpl_23.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ scala> def foo[T]{
1414
}
1515
println(code.eval)
1616
}
17-
<console>:17: free type: Ident(TypeName("T")) defined by foo in <console>:16:16
17+
<console>:17: free type: Ident(TypeName("T")) defined by foo in <console>:16:9
1818
val code = reify {
1919
^
2020
foo: [T]=> Unit

test/files/run/reify_newimpl_25.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ scala> {
55
val tt = implicitly[TypeTag[x.type]]
66
println(tt)
77
}
8-
<console>:15: free term: Ident(TermName("x")) defined by res0 in <console>:14:14
8+
<console>:15: free term: Ident(TermName("x")) defined by res0 in <console>:14:7
99
val tt = implicitly[TypeTag[x.type]]
1010
^
1111
TypeTag[x.type]

test/files/run/reify_newimpl_26.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ scala> def foo[T]{
44
val tt = implicitly[WeakTypeTag[List[T]]]
55
println(tt)
66
}
7-
<console>:13: free type: Ident(TypeName("T")) defined by foo in <console>:11:16
7+
<console>:13: free type: Ident(TypeName("T")) defined by foo in <console>:11:9
88
val tt = implicitly[WeakTypeTag[List[T]]]
99
^
1010
foo: [T]=> Unit

test/files/run/repl-colon-type.check

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11

22
scala> :type List[1, 2, 3]
33
<console>:1: error: identifier expected but integer literal found.
4-
List[1, 2, 3]
5-
^
4+
List[1, 2, 3]
5+
^
66

77
scala> :type List(1, 2, 3)
88
List[Int]
@@ -38,8 +38,8 @@ scala> :type protected lazy val f = 5
3838
Access to protected lazy value f not permitted because
3939
enclosing object $eval in package $line13 is not a subclass of
4040
object $iw where target is defined
41-
lazy val $result = f
42-
^
41+
lazy val $result = f
42+
^
4343

4444
scala> :type def f = 5
4545
=> Int

test/files/run/t8918-unary-ids.check

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,17 @@ Expected 41 lines, got 39
55
-Type in expressions to have them evaluated.
66
-Type :help for more information.
77

8+
@@ -14,4 +12,4 @@
9+
<console>:1: error: illegal start of simple expression
10+
-- if (true) 1 else 2
11+
- ^
12+
+ - if (true) 1 else 2
13+
+ ^
14+
15+
@@ -19,4 +17,4 @@
16+
<console>:1: error: ';' expected but integer literal found.
17+
-- - 1
18+
- ^
19+
+ - - 1
20+
+ ^
21+

test/files/run/t9170.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ object Y {
4848
def f[A](a: => A): Int at line 12 and
4949
def f[A](a: => Either[Exception,A]): Int at line 13
5050
have same type after erasure: (a: Function0)Int
51-
def f[A](a: => Either[Exception, A]) = 2
52-
^
51+
def f[A](a: => Either[Exception, A]) = 2
52+
^
5353
5454
scala> :quit"""
5555
}

test/junit/scala/tools/nsc/interpreter/ScriptedTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class ScriptedTest {
9696
}
9797
@Test def `on compile error`(): Unit = {
9898
val engine = scripted
99-
val err = "not found: value foo in def f = foo at line number 11 at column number 16"
99+
val err = "not found: value foo in def f = foo at line number 11 at column number 9"
100100
assertThrows[ScriptException](engine.compile("def f = foo"), _ == err)
101101
}
102102
}

0 commit comments

Comments
 (0)