From e6559f59e07d25a57dd9c2eb9915617597f477af Mon Sep 17 00:00:00 2001 From: Alexandre Archambault Date: Thu, 14 Jan 2021 12:11:07 +0100 Subject: [PATCH] Remove SourceBridge / SourceAPI, and move SourceRuntime to compiler --- .../compiler}/tools/SourceRuntime.scala | 20 +++++- .../ammonite/compiler}/tools/desugar.scala | 4 +- .../ammonite/compiler}/tools/source.scala | 6 +- .../scala/ammonite/interp/Interpreter.scala | 2 +- .../scala/ammonite/repl/api/SourceAPI.scala | 44 ------------ .../main/scala/ammonite/repl/ApiImpls.scala | 67 +------------------ .../src/main/scala/ammonite/repl/Repl.scala | 5 -- .../ammonite/unit/SourceTests212.scala | 6 +- .../test/scala/ammonite/DualTestRepl.scala | 5 -- .../ammonite/session/AdvancedTests.scala | 2 +- .../scala/ammonite/unit/SourceTests.scala | 7 +- amm/src/main/scala/ammonite/Main.scala | 7 +- 12 files changed, 30 insertions(+), 145 deletions(-) rename amm/{repl/src/main/scala/ammonite/repl => compiler/src/main/scala/ammonite/compiler}/tools/SourceRuntime.scala (95%) rename amm/{repl/api/src/main/scala/ammonite/repl => compiler/src/main/scala/ammonite/compiler}/tools/desugar.scala (72%) rename amm/{repl/api/src/main/scala/ammonite/repl => compiler/src/main/scala/ammonite/compiler}/tools/source.scala (97%) delete mode 100644 amm/repl/api/src/main/scala/ammonite/repl/api/SourceAPI.scala diff --git a/amm/repl/src/main/scala/ammonite/repl/tools/SourceRuntime.scala b/amm/compiler/src/main/scala/ammonite/compiler/tools/SourceRuntime.scala similarity index 95% rename from amm/repl/src/main/scala/ammonite/repl/tools/SourceRuntime.scala rename to amm/compiler/src/main/scala/ammonite/compiler/tools/SourceRuntime.scala index af3b4cd7a..57aaf3773 100644 --- a/amm/repl/src/main/scala/ammonite/repl/tools/SourceRuntime.scala +++ b/amm/compiler/src/main/scala/ammonite/compiler/tools/SourceRuntime.scala @@ -1,4 +1,4 @@ -package ammonite.repl.tools +package ammonite.compiler.tools import javassist.{ByteArrayClassPath, CtClass, CtMethod} @@ -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 + ) + } } diff --git a/amm/repl/api/src/main/scala/ammonite/repl/tools/desugar.scala b/amm/compiler/src/main/scala/ammonite/compiler/tools/desugar.scala similarity index 72% rename from amm/repl/api/src/main/scala/ammonite/repl/tools/desugar.scala rename to amm/compiler/src/main/scala/ammonite/compiler/tools/desugar.scala index 4aea3f1e0..88150b6eb 100644 --- a/amm/repl/api/src/main/scala/ammonite/repl/tools/desugar.scala +++ b/amm/compiler/src/main/scala/ammonite/compiler/tools/desugar.scala @@ -1,4 +1,4 @@ -package ammonite.repl.tools +package ammonite.compiler.tools import sourcecode.Compat._ import scala.language.experimental.macros @@ -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)})" ) } diff --git a/amm/repl/api/src/main/scala/ammonite/repl/tools/source.scala b/amm/compiler/src/main/scala/ammonite/compiler/tools/source.scala similarity index 97% rename from amm/repl/api/src/main/scala/ammonite/repl/tools/source.scala rename to amm/compiler/src/main/scala/ammonite/compiler/tools/source.scala index 3af654f17..89c694f0d 100644 --- a/amm/repl/api/src/main/scala/ammonite/repl/tools/source.scala +++ b/amm/compiler/src/main/scala/ammonite/compiler/tools/source.scala @@ -1,4 +1,4 @@ -package ammonite.repl.tools +package ammonite.compiler.tools import ammonite.runtime.tools.browse.Strings import ammonite.util.CodeColors @@ -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) @@ -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 diff --git a/amm/interp/src/main/scala/ammonite/interp/Interpreter.scala b/amm/interp/src/main/scala/ammonite/interp/Interpreter.scala index 8c76218f0..ad30d9931 100644 --- a/amm/interp/src/main/scala/ammonite/interp/Interpreter.scala +++ b/amm/interp/src/main/scala/ammonite/interp/Interpreter.scala @@ -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") ) diff --git a/amm/repl/api/src/main/scala/ammonite/repl/api/SourceAPI.scala b/amm/repl/api/src/main/scala/ammonite/repl/api/SourceAPI.scala deleted file mode 100644 index 58bede351..000000000 --- a/amm/repl/api/src/main/scala/ammonite/repl/api/SourceAPI.scala +++ /dev/null @@ -1,44 +0,0 @@ -package ammonite.repl.api - -import ammonite.interp.api.APIHolder -import ammonite.repl.tools.Desugared -import ammonite.util.CodeColors -import ammonite.util.Util.Location - -trait SourceAPI { - - def browseSourceCommand(targetLine: Int) = Seq("less", "+" + targetLine,"-RMN") - - def failLoudly[T](res: Either[String, T]): T = res match{ - case Left(s) => throw new Exception(s) - case Right(r) => r - } - - def loadObjectMemberInfo(symbolOwnerCls: Class[_], - value: Option[Any], - memberName: String, - returnType: Class[_], - argTypes: Class[_]*): Either[String, Location] - - def loadObjectInfo(value: Any): Either[String, Location] - - def browseObjectMember(symbolOwnerCls: Class[_], - value: Option[Any], - memberName: String, - pprinter: pprint.PPrinter, - colors: CodeColors, - command: Int => Seq[String], - returnType: Class[_], - argTypes: Class[_]*): Any - - def browseObject(value: Any, - pprinter: pprint.PPrinter, - colors: CodeColors, - command: Int => Seq[String]): Any - - def desugarImpl(s: String)(implicit colors: ammonite.util.CodeColors): Desugared - -} - -object SourceBridge extends APIHolder[SourceAPI] - diff --git a/amm/repl/src/main/scala/ammonite/repl/ApiImpls.scala b/amm/repl/src/main/scala/ammonite/repl/ApiImpls.scala index a8b85b55c..00925be15 100644 --- a/amm/repl/src/main/scala/ammonite/repl/ApiImpls.scala +++ b/amm/repl/src/main/scala/ammonite/repl/ApiImpls.scala @@ -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 => _, _} @@ -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 { diff --git a/amm/repl/src/main/scala/ammonite/repl/Repl.scala b/amm/repl/src/main/scala/ammonite/repl/Repl.scala index a412436ad..b36253510 100644 --- a/amm/repl/src/main/scala/ammonite/repl/Repl.scala +++ b/amm/repl/src/main/scala/ammonite/repl/Repl.scala @@ -128,11 +128,6 @@ class Repl(input: InputStream, } } ), - ( - "ammonite.repl.api.SourceBridge", - "source", - new SourceAPIImpl {} - ), ( "ammonite.repl.api.FrontEndBridge", "frontEnd", diff --git a/amm/repl/src/test/scala-2.12/ammonite/unit/SourceTests212.scala b/amm/repl/src/test/scala-2.12/ammonite/unit/SourceTests212.scala index 225870ea6..d40050427 100644 --- a/amm/repl/src/test/scala-2.12/ammonite/unit/SourceTests212.scala +++ b/amm/repl/src/test/scala-2.12/ammonite/unit/SourceTests212.scala @@ -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) = { diff --git a/amm/repl/src/test/scala/ammonite/DualTestRepl.scala b/amm/repl/src/test/scala/ammonite/DualTestRepl.scala index 109f78453..8726204eb 100644 --- a/amm/repl/src/test/scala/ammonite/DualTestRepl.scala +++ b/amm/repl/src/test/scala/ammonite/DualTestRepl.scala @@ -1,8 +1,6 @@ package ammonite import ammonite.compiler.CodeClassWrapper -import ammonite.repl.api.SourceBridge -import ammonite.repl.SourceAPIImpl import ammonite.util.{Evaluated, Res} /** @@ -10,9 +8,6 @@ import ammonite.util.{Evaluated, Res} */ class DualTestRepl { dual => - if (SourceBridge.value0 == null) - SourceBridge.value0 = new SourceAPIImpl {} - def predef: (String, Option[os.Path]) = ("", None) val repls = Seq( diff --git a/amm/repl/src/test/scala/ammonite/session/AdvancedTests.scala b/amm/repl/src/test/scala/ammonite/session/AdvancedTests.scala index fff94baef..0de5a9f81 100644 --- a/amm/repl/src/test/scala/ammonite/session/AdvancedTests.scala +++ b/amm/repl/src/test/scala/ammonite/session/AdvancedTests.scala @@ -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"){ diff --git a/amm/repl/src/test/scala/ammonite/unit/SourceTests.scala b/amm/repl/src/test/scala/ammonite/unit/SourceTests.scala index 67dbedda0..be6df8e3e 100644 --- a/amm/repl/src/test/scala/ammonite/unit/SourceTests.scala +++ b/amm/repl/src/test/scala/ammonite/unit/SourceTests.scala @@ -1,10 +1,8 @@ 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 @@ -12,9 +10,6 @@ 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) = { diff --git a/amm/src/main/scala/ammonite/Main.scala b/amm/src/main/scala/ammonite/Main.scala index fafec0bbb..e193e57dc 100644 --- a/amm/src/main/scala/ammonite/Main.scala +++ b/amm/src/main/scala/ammonite/Main.scala @@ -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._ @@ -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",