Skip to content

Commit

Permalink
Remove SourceBridge / SourceAPI, and move SourceRuntime to compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
alexarchambault committed Jan 14, 2021
1 parent 5f1386a commit e6559f5
Show file tree
Hide file tree
Showing 12 changed files with 30 additions and 145 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ammonite.repl.tools
package ammonite.compiler.tools

import javassist.{ByteArrayClassPath, CtClass, CtMethod}

Expand Down Expand Up @@ -252,5 +252,23 @@ object SourceRuntime{
}
}

def failLoudly[T](res: Either[String, T]): T = res match{
case Left(s) => throw new Exception(s)
case Right(r) => r
}

def desugarImpl(s: String)(implicit colors: ammonite.util.CodeColors): Desugared = {

new Desugared(
ammonite.compiler.Highlighter.defaultHighlight(
s.toVector,
colors.comment,
colors.`type`,
colors.literal,
colors.keyword,
fansi.Attr.Reset
).mkString
)
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ammonite.repl.tools
package ammonite.compiler.tools

import sourcecode.Compat._
import scala.language.experimental.macros
Expand All @@ -11,7 +11,7 @@ object desugar{
def transformer(c: Context)(expr: c.Expr[Any]): c.Expr[Desugared] = {
import c.universe._
c.Expr[Desugared](
q"ammonite.repl.api.SourceBridge.value.desugarImpl(${c.universe.showCode(expr.tree)})"
q"ammonite.compiler.tools.SourceRuntime.desugarImpl(${c.universe.showCode(expr.tree)})"
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ammonite.repl.tools
package ammonite.compiler.tools

import ammonite.runtime.tools.browse.Strings
import ammonite.util.CodeColors
Expand Down Expand Up @@ -53,7 +53,7 @@ object source{
colors: c.Expr[CodeColors]): c.Expr[Unit] = {
import c.universe._
val defaultBrowseExpr = c.Expr[Int => Strings](
q"_root_.ammonite.repl.api.SourceBridge.value.browseSourceCommand"
q"${prefix(c)}.browseSourceCommand"
)

applyCustomizeCommandMacro(c)(f, defaultBrowseExpr)(pprinter, colors)
Expand Down Expand Up @@ -86,7 +86,7 @@ object source{

def prefix(c: Context) = {
import c.universe._
q"ammonite.repl.api.SourceBridge.value"
q"ammonite.compiler.tools.SourceRuntime"
}
/**
* Attempts to up an expression, into either a LHS + methodcall + rhs. We
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ object Interpreter{
),
ImportData("ammonite.runtime.tools.{browse, grep, time}"),
ImportData("ammonite.runtime.tools.tail", importType = ImportData.TermType),
ImportData("ammonite.repl.tools.{desugar, source}"),
ImportData("ammonite.compiler.tools.{desugar, source}"),
ImportData("mainargs.{arg, main}"),
ImportData("ammonite.repl.tools.Util.PathRead")
)
Expand Down
44 changes: 0 additions & 44 deletions amm/repl/api/src/main/scala/ammonite/repl/api/SourceAPI.scala

This file was deleted.

67 changes: 1 addition & 66 deletions amm/repl/src/main/scala/ammonite/repl/ApiImpls.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package ammonite.repl

import ammonite.ops.Internals
import ammonite.repl.api.{Clipboard, FrontEnd, FrontEndAPI, Session, SourceAPI}
import ammonite.repl.tools.{Desugared, SourceRuntime}
import ammonite.repl.api.{Clipboard, FrontEnd, FrontEndAPI, Session}
import ammonite.runtime._
import ammonite.util.Util._
import ammonite.util.{Frame => _, _}
Expand Down Expand Up @@ -138,70 +137,6 @@ object ClipboardImpl extends Clipboard {
}
}

trait SourceAPIImpl extends SourceAPI {

def loadObjectMemberInfo(symbolOwnerCls: Class[_],
value: Option[Any],
memberName: String,
returnType: Class[_],
argTypes: Class[_]*): Either[String, Location] =
SourceRuntime.loadObjectMemberInfo(
symbolOwnerCls,
value,
memberName,
returnType,
argTypes: _*
)

def loadObjectInfo(value: Any): Either[String, Location] =
SourceRuntime.loadObjectInfo(value)

def browseObjectMember(symbolOwnerCls: Class[_],
value: Option[Any],
memberName: String,
pprinter: pprint.PPrinter,
colors: CodeColors,
command: Int => Seq[String],
returnType: Class[_],
argTypes: Class[_]*): Any =
SourceRuntime.browseObjectMember(
symbolOwnerCls,
value,
memberName,
pprinter,
colors,
n => command(n),
returnType,
argTypes: _*
)

def browseObject(value: Any,
pprinter: pprint.PPrinter,
colors: CodeColors,
command: Int => Seq[String]): Any =
SourceRuntime.browseObject(
value,
pprinter,
colors,
n => command(n)
)

def desugarImpl(s: String)(implicit colors: ammonite.util.CodeColors): Desugared = {

new Desugared(
ammonite.compiler.Highlighter.defaultHighlight(
s.toVector,
colors.comment,
colors.`type`,
colors.literal,
colors.keyword,
fansi.Attr.Reset
).mkString
)
}

}

trait FrontEndAPIImpl extends FrontEndAPI {
def apply(name: String): FrontEnd =
name.toLowerCase(Locale.ROOT) match {
Expand Down
5 changes: 0 additions & 5 deletions amm/repl/src/main/scala/ammonite/repl/Repl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,6 @@ class Repl(input: InputStream,
}
}
),
(
"ammonite.repl.api.SourceBridge",
"source",
new SourceAPIImpl {}
),
(
"ammonite.repl.api.FrontEndBridge",
"frontEnd",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
package ammonite.unit


import ammonite.repl.SourceAPIImpl
import ammonite.repl.api.SourceBridge
import utest._
import ammonite.repl.tools.source.load
import ammonite.compiler.tools.source.load
import ammonite.util.Util
import ammonite.util.Util.Location
//import fastparse.utils.{ElemSetHelper, Generator, IndexedParserInput}

object SourceTests212 extends TestSuite{
val tests = Tests{
if (SourceBridge.value0 == null)
SourceBridge.value0 = new SourceAPIImpl {}
def check(loaded: Location, expectedFileName: String, expected: String, slop: Int = 10) = {


Expand Down
5 changes: 0 additions & 5 deletions amm/repl/src/test/scala/ammonite/DualTestRepl.scala
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
package ammonite

import ammonite.compiler.CodeClassWrapper
import ammonite.repl.api.SourceBridge
import ammonite.repl.SourceAPIImpl
import ammonite.util.{Evaluated, Res}

/**
* Wraps several [[TestRepl]], and runs its tests against all of them.
*/
class DualTestRepl { dual =>

if (SourceBridge.value0 == null)
SourceBridge.value0 = new SourceAPIImpl {}

def predef: (String, Option[os.Path]) = ("", None)

val repls = Seq(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ object AdvancedTests extends TestSuite{
test("desugar"){
check.session("""
@ desugar{1 + 2 max 3}
res0: ammonite.repl.tools.Desugared = scala.Predef.intWrapper(3).max(3)
res0: compiler.tools.Desugared = scala.Predef.intWrapper(3).max(3)
""")
}
test("loadingModulesInPredef"){
Expand Down
7 changes: 1 addition & 6 deletions amm/repl/src/test/scala/ammonite/unit/SourceTests.scala
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
package ammonite.unit


import ammonite.repl.SourceAPIImpl
import ammonite.repl.api.SourceBridge
import utest._
import ammonite.repl.tools.source.load
import ammonite.compiler.tools.source.load
import ammonite.util.Util
import ammonite.util.Util.Location

import java.io.InputStream
object SourceTests extends TestSuite{
val tests = Tests{

if (SourceBridge.value0 == null)
SourceBridge.value0 = new SourceAPIImpl {}

def check(loaded: Location, expectedFileName: String, expected: String, slop: Int = 10) = {


Expand Down
7 changes: 1 addition & 6 deletions amm/src/main/scala/ammonite/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import ammonite.interp.{Watchable, Interpreter, PredefInitialization}
import ammonite.interp.script.AmmoniteBuildServer
import ammonite.runtime.{Frame, Storage}
import ammonite.main._
import ammonite.repl.{FrontEndAPIImpl, Repl, SourceAPIImpl}
import ammonite.repl.{FrontEndAPIImpl, Repl}
import ammonite.util.Util.newLine
import ammonite.util._

Expand Down Expand Up @@ -176,11 +176,6 @@ case class Main(predefCode: String = "",
classPathWhitelist = classPathWhitelist
)
val bridges = Seq(
(
"ammonite.repl.api.SourceBridge",
"source",
new SourceAPIImpl {}
),
(
"ammonite.repl.api.FrontEndBridge",
"frontEnd",
Expand Down

0 comments on commit e6559f5

Please sign in to comment.