Skip to content

Commit

Permalink
Move the sbt-pamflet ConsoleInterface API to Interactive*
Browse files Browse the repository at this point in the history
  • Loading branch information
dwijnand committed May 4, 2017
1 parent fd323ab commit baad700
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

package xsbti;

public interface ConsoleFactory {
ConsoleInterface createConsole(
public interface InteractiveConsoleFactory {
InteractiveConsoleInterface createConsole(
String[] args,
String bootClasspathString,
String classpathString,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

package xsbti;

public interface ConsoleInterface {
public interface InteractiveConsoleInterface {
void reset();
ConsoleResponse interpret(String line, boolean synthetic);
InteractiveConsoleResponse interpret(String line, boolean synthetic);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
package xsbti;

/** Public interface for repl responses. */
public interface ConsoleResponse {
ConsoleResult result();
public interface InteractiveConsoleResponse {
InteractiveConsoleResult result();

String output();
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

package xsbti;

public enum ConsoleResult {
public enum InteractiveConsoleResult {
Success,
Incomplete,
Error
Expand Down
20 changes: 0 additions & 20 deletions internal/compiler-bridge/src/main/scala/xsbt/ConsoleHelper.scala

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ package xsbt

import xsbti.Logger

class ConsoleFactory extends xsbti.ConsoleFactory {
class InteractiveConsoleFactory extends xsbti.InteractiveConsoleFactory {
def createConsole(
args: Array[String],
bootClasspathString: String,
Expand All @@ -20,8 +20,8 @@ class ConsoleFactory extends xsbti.ConsoleFactory {
bindNames: Array[String],
bindValues: Array[AnyRef],
log: Logger
): xsbti.ConsoleInterface =
new ConsoleInterface(
): xsbti.InteractiveConsoleInterface =
new InteractiveConsoleInterface(
args,
bootClasspathString,
classpathString,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Zinc - The incremental compiler for Scala.
* Copyright 2011 - 2017, Lightbend, Inc.
* Copyright 2008 - 2010, Mark Harrah
* This software is released under the terms written in LICENSE.
*/

package xsbt

import scala.tools.nsc.interpreter.IR
import xsbti.InteractiveConsoleResult

object InteractiveConsoleHelper {
implicit def toConsoleResult(ir: IR.Result): InteractiveConsoleResult =
ir match {
case IR.Success => InteractiveConsoleResult.Success
case IR.Incomplete => InteractiveConsoleResult.Incomplete
case IR.Error => InteractiveConsoleResult.Error
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import scala.tools.nsc.{ GenericRunnerCommand, Settings }

import xsbti.Logger

import ConsoleHelper._
import InteractiveConsoleHelper._

class ConsoleInterface(
class InteractiveConsoleInterface(
args: Array[String],
bootClasspathString: String,
classpathString: String,
Expand All @@ -26,14 +26,14 @@ class ConsoleInterface(
bindNames: Array[String],
bindValues: Array[AnyRef],
log: Logger
) extends xsbti.ConsoleInterface {
) extends xsbti.InteractiveConsoleInterface {

lazy val interpreterSettings: Settings = MakeSettings.sync(args.toList, onError)
lazy val interpreterSettings: Settings = InteractiveMakeSettings.sync(args.toList, onError)

val useJavaCp = "-usejavacp" // we need rt.jar from JDK, so java classpath is required

val compilerSettings: Settings =
MakeSettings.sync(args :+ useJavaCp, bootClasspathString, classpathString, onError)
InteractiveMakeSettings.sync(args :+ useJavaCp, bootClasspathString, classpathString, onError)

val outWriter: StringWriter = new StringWriter
val poutWriter: PrintWriter = new PrintWriter(outWriter)
Expand All @@ -42,10 +42,10 @@ class ConsoleInterface(
def lastReq: Request = prevRequestList.last
}

def interpret(line: String, synthetic: Boolean): ConsoleResponse = {
def interpret(line: String, synthetic: Boolean): InteractiveConsoleResponse = {
clearBuffer()
val r = interpreter.interpret(line, synthetic)
ConsoleResponse(r, outWriter.toString)
InteractiveConsoleResponse(r, outWriter.toString)
}

def clearBuffer(): Unit = {
Expand All @@ -61,7 +61,7 @@ class ConsoleInterface(
private def onError(str: String) = log error Message(str)
}

object MakeSettings {
object InteractiveMakeSettings {
def apply(args: List[String], onError: String => Unit): Settings = {
val command = new GenericRunnerCommand(args, onError)
if (command.ok) command.settings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

package xsbt

import xsbti.ConsoleResult
import xsbti.InteractiveConsoleResult

case class ConsoleResponse(result: ConsoleResult, output: String) extends xsbti.ConsoleResponse
case class InteractiveConsoleResponse(result: InteractiveConsoleResult, output: String)
extends xsbti.InteractiveConsoleResponse
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package xsbt

import sbt.internal.util.UnitSpec
import sbt.util.Logger
import xsbti.ConsoleResult
import xsbti.InteractiveConsoleResult

// This is a specification to check the REPL block parsing.
class ConsoleInterfaceSpecification extends UnitSpec {
class InteractiveConsoleInterfaceSpecification extends UnitSpec {

private val consoleFactory = new ConsoleFactory
private val consoleFactory = new InteractiveConsoleFactory

def consoleWithArgs(args: String*) = consoleFactory.createConsole(
args = args.toArray,
Expand All @@ -26,45 +26,45 @@ class ConsoleInterfaceSpecification extends UnitSpec {
"Scala interpreter" should "evaluate arithmetic expression" in {
val response = consoleWithoutArgs.interpret("1+1", false)
response.output.trim shouldBe "res0: Int = 2"
response.result shouldBe ConsoleResult.Success
response.result shouldBe InteractiveConsoleResult.Success
}

it should "evaluate list constructor" in {
val response = consoleWithoutArgs.interpret("List(1,2)", false)
response.output.trim shouldBe "res1: List[Int] = List(1, 2)"
response.result shouldBe ConsoleResult.Success
response.result shouldBe InteractiveConsoleResult.Success
}

it should "evaluate import" in {
val response = consoleWithoutArgs.interpret("import xsbt._", false)
response.output.trim shouldBe "import xsbt._"
response.result shouldBe ConsoleResult.Success
response.result shouldBe InteractiveConsoleResult.Success
}

it should "mark partial expression as incomplete" in {
val response = consoleWithoutArgs.interpret("val a =", false)
response.result shouldBe ConsoleResult.Incomplete
response.result shouldBe InteractiveConsoleResult.Incomplete
}

it should "not evaluate incorrect expression" in {
val response = consoleWithoutArgs.interpret("1 ++ 1", false)
response.result shouldBe ConsoleResult.Error
response.result shouldBe InteractiveConsoleResult.Error
}

val postfixOpExpression = "import scala.concurrent.duration._\nval t = 1 second"

it should "evaluate postfix op with a warning" in {
val response = consoleWithoutArgs.interpret(postfixOpExpression, false)
response.output.trim should startWith("warning")
response.result shouldBe ConsoleResult.Success
response.result shouldBe InteractiveConsoleResult.Success
}

private val consoleWithPostfixOps = consoleWithArgs("-language:postfixOps")

it should "evaluate postfix op without warning when -language:postfixOps arg passed" in {
val response = consoleWithPostfixOps.interpret(postfixOpExpression, false)
response.output.trim should not startWith "warning"
response.result shouldBe ConsoleResult.Success
response.result shouldBe InteractiveConsoleResult.Success
}

}

0 comments on commit baad700

Please sign in to comment.