Skip to content

Commit f693e04

Browse files
authored
Merge pull request #5102 from dotty-staging/topic/worksheets
Worksheet mode in Dotty IDE
2 parents 7463afe + 9a19df7 commit f693e04

36 files changed

+1437
-124
lines changed

compiler/src/dotty/tools/dotc/core/StdNames.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,7 @@ object StdNames {
548548
val wait_ : N = "wait"
549549
val withFilter: N = "withFilter"
550550
val withFilterIfRefutable: N = "withFilterIfRefutable$"
551+
val WorksheetWrapper: N = "WorksheetWrapper"
551552
val wrap: N = "wrap"
552553
val zero: N = "zero"
553554
val zip: N = "zip"

compiler/src/dotty/tools/dotc/interactive/Interactive.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -265,12 +265,12 @@ object Interactive {
265265
namedTrees(trees, (include & Include.references) != 0, matchSymbol(_, sym, include))
266266

267267
/** Find named trees with a non-empty position whose name contains `nameSubstring` in `trees`.
268-
*
269-
* @param includeReferences If true, include references and not just definitions
270268
*/
271-
def namedTrees(trees: List[SourceTree], includeReferences: Boolean, nameSubstring: String)
272-
(implicit ctx: Context): List[SourceTree] =
273-
namedTrees(trees, includeReferences, _.show.toString.contains(nameSubstring))
269+
def namedTrees(trees: List[SourceTree], nameSubstring: String)
270+
(implicit ctx: Context): List[SourceTree] = {
271+
val predicate: NameTree => Boolean = _.name.toString.contains(nameSubstring)
272+
namedTrees(trees, includeReferences = false, predicate)
273+
}
274274

275275
/** Find named trees with a non-empty position satisfying `treePredicate` in `trees`.
276276
*
@@ -322,7 +322,7 @@ object Interactive {
322322
val includeLinkedClass = (includes & Include.linkedClass) != 0
323323
val predicate: NameTree => Boolean = tree =>
324324
( tree.pos.isSourceDerived
325-
&& !tree.symbol.isConstructor
325+
&& !tree.symbol.isPrimaryConstructor
326326
&& (includeDeclaration || !Interactive.isDefinition(tree))
327327
&& ( Interactive.matchSymbol(tree, symbol, includes)
328328
|| ( includeDeclaration

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1404,7 +1404,8 @@ object Parsers {
14041404
val (impl, missingBody) = template(emptyConstructor)
14051405
impl.parents match {
14061406
case parent :: Nil if missingBody =>
1407-
if (parent.isType) ensureApplied(wrapNew(parent)) else parent
1407+
if (parent.isType) ensureApplied(wrapNew(parent))
1408+
else parent.withPos(Position(start, in.lastOffset))
14081409
case _ =>
14091410
New(impl.withPos(Position(start, in.lastOffset)))
14101411
}

compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ public enum ErrorMessageID {
135135
CaseClassCannotExtendEnumID,
136136
ValueClassParameterMayNotBeCallByNameID,
137137
NotAnExtractorID,
138-
MemberWithSameNameAsStaticID
138+
MemberWithSameNameAsStaticID,
139+
PureExpressionInStatementPositionID
139140
;
140141

141142
public int errorNumber() {

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2139,4 +2139,14 @@ object messages {
21392139
override def kind: String = "Syntax"
21402140
override def explanation: String = ""
21412141
}
2142+
2143+
case class PureExpressionInStatementPosition(stat: untpd.Tree, exprOwner: Symbol)(implicit ctx: Context)
2144+
extends Message(PureExpressionInStatementPositionID) {
2145+
2146+
val kind = "Potential Issue"
2147+
val msg = "a pure expression does nothing in statement position; you may be omitting necessary parentheses"
2148+
val explanation =
2149+
hl"""The pure expression `$stat` doesn't have any side effect and its result is not assigned elsewhere.
2150+
|It can be removed without changing the semantics of the program. This may indicate an error.""".stripMargin
2151+
}
21422152
}

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2013,7 +2013,7 @@ class Typer extends Namer
20132013
val stat1 = typed(stat)(ctx.exprContext(stat, exprOwner))
20142014
if (!ctx.isAfterTyper && isPureExpr(stat1) &&
20152015
!stat1.tpe.isRef(defn.UnitClass) && !isSelfOrSuperConstrCall(stat1))
2016-
ctx.warning(em"a pure expression does nothing in statement position", stat.pos)
2016+
ctx.warning(PureExpressionInStatementPosition(stat, exprOwner), stat.pos)
20172017
buf += stat1
20182018
traverse(rest)
20192019
case nil =>

compiler/src/dotty/tools/repl/JLineTerminal.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ import org.jline.reader.impl.history.DefaultHistory
1313
import org.jline.terminal.TerminalBuilder
1414
import org.jline.utils.AttributedString
1515

16-
final class JLineTerminal extends java.io.Closeable {
16+
final class JLineTerminal(needsTerminal: Boolean) extends java.io.Closeable {
1717
// import java.util.logging.{Logger, Level}
1818
// Logger.getLogger("org.jline").setLevel(Level.FINEST)
1919

20-
private val terminal = TerminalBuilder.builder()
21-
.dumb(false) // fail early if not able to create a terminal
20+
private val terminal =
21+
TerminalBuilder.builder()
22+
.dumb(!needsTerminal) // fail early if we need a terminal and can't create one
2223
.build()
2324
private val history = new DefaultHistory
2425

compiler/src/dotty/tools/repl/Main.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,10 @@ package dotty.tools.repl
33
/** Main entry point to the REPL */
44
object Main {
55
def main(args: Array[String]): Unit =
6-
new ReplDriver(args).runUntilQuit()
6+
new ReplDriver(args).runUntilQuit(needsTerminal = true)
7+
}
8+
9+
object WorksheetMain {
10+
def main(args: Array[String]): Unit =
11+
new ReplDriver(args).runUntilQuit(needsTerminal = false)
712
}

compiler/src/dotty/tools/repl/ReplDriver.scala

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ class ReplDriver(settings: Array[String],
102102
* observable outside of the CLI, for this reason, most helper methods are
103103
* `protected final` to facilitate testing.
104104
*/
105-
final def runUntilQuit(initialState: State = initialState): State = {
106-
val terminal = new JLineTerminal()
105+
final def runUntilQuit(needsTerminal: Boolean, initialState: State = initialState): State = {
106+
val terminal = new JLineTerminal(needsTerminal)
107107

108108
/** Blockingly read a line, getting back a parse result */
109109
def readLine(state: State): ParseResult = {
@@ -285,7 +285,12 @@ class ReplDriver(settings: Array[String],
285285
.foreach { sym =>
286286
// FIXME syntax highlighting on comment is currently not working
287287
// out.println(SyntaxHighlighting.highlight("// defined " + sym.showUser))
288-
out.println(SyntaxHighlighting.CommentColor + "// defined " + sym.showUser + SyntaxHighlighting.NoColor)
288+
val message = "// defined " + sym.showUser
289+
if (ctx.settings.color.value != "never") {
290+
println(SyntaxHighlighting.CommentColor + message + SyntaxHighlighting.NoColor)
291+
} else {
292+
println(message)
293+
}
289294
}
290295

291296

compiler/test-resources/repl/errmsgs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ scala> val z: (List[String], List[Int]) = (List(1), List("a"))
2525
|
2626
scala> val a: Inv[String] = new Inv(new Inv(1))
2727
1 | val a: Inv[String] = new Inv(new Inv(1))
28-
| ^^^^^^
29-
| found: Inv[Int]
30-
| required: String
28+
| ^^^^^^^^^^
29+
| found: Inv[Int]
30+
| required: String
3131
|
3232
scala> val b: Inv[String] = new Inv(1)
3333
1 | val b: Inv[String] = new Inv(1)

0 commit comments

Comments
 (0)