From 06bc1d5976354d8562bc7957d9e2b0067c60c6c0 Mon Sep 17 00:00:00 2001 From: Krzysztof Romanowski Date: Wed, 23 Mar 2022 12:24:05 +0100 Subject: [PATCH 1/5] Fix macros on Scala 3 --- .gitignore | 2 ++ .../main/scala-3/dependency/literal/Mappings.scala | 5 ++--- .../src/test/scala/dependency/LiteralTests.scala | 12 ++++++++++++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 89f9ac0..8e7c406 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ out/ +.idea +.idea_modules \ No newline at end of file diff --git a/dependency/src/main/scala-3/dependency/literal/Mappings.scala b/dependency/src/main/scala-3/dependency/literal/Mappings.scala index 75d0f16..1b44214 100644 --- a/dependency/src/main/scala-3/dependency/literal/Mappings.scala +++ b/dependency/src/main/scala-3/dependency/literal/Mappings.scala @@ -30,9 +30,8 @@ private[literal] final case class Mappings(mappings: List[(String, QExpr[String] } def Expr(str: String)(using Quotes): QExpr[String] = - mappings match { - case Nil => QExpr(str) - case (id, expr) :: tail => + mappings.find(e => str.contains(e._1)).fold(QExpr(str)){ + case (id, expr) => val indices0 = indices(str, id) insertExpr(str, id.length, expr, indices0) } diff --git a/dependency/src/test/scala/dependency/LiteralTests.scala b/dependency/src/test/scala/dependency/LiteralTests.scala index 178f79f..77f5ae7 100644 --- a/dependency/src/test/scala/dependency/LiteralTests.scala +++ b/dependency/src/test/scala/dependency/LiteralTests.scala @@ -74,6 +74,18 @@ class LiteralTests extends munit.FunSuite { val expected = Dependency("org", "name", "1.2").copy( exclude = Set(Module("fu", "ba")) ) + expect(dep == expected) + } + + test("dependcy interpolation") { + val org = "org.org" + val name = "name-name" + val version = "123-version" + val dep = dep"$org::$name:$version" + val expected = Dependency(org, name, version) + expect(dep.name == expected.name) + expect(dep.organization == expected.organization) + expect(dep.version == expected.version) } } From 8a22ad117e60a9908ba7bd9494be9f1c4ad9ef5d Mon Sep 17 00:00:00 2001 From: Alexandre Archambault Date: Wed, 23 Mar 2022 13:29:47 +0100 Subject: [PATCH 2/5] Add extra test --- .../src/test/scala/dependency/LiteralTests.scala | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/dependency/src/test/scala/dependency/LiteralTests.scala b/dependency/src/test/scala/dependency/LiteralTests.scala index 77f5ae7..1763714 100644 --- a/dependency/src/test/scala/dependency/LiteralTests.scala +++ b/dependency/src/test/scala/dependency/LiteralTests.scala @@ -77,7 +77,7 @@ class LiteralTests extends munit.FunSuite { expect(dep == expected) } - test("dependcy interpolation") { + test("dependency interpolation") { val org = "org.org" val name = "name-name" val version = "123-version" @@ -88,4 +88,15 @@ class LiteralTests extends munit.FunSuite { expect(dep.version == expected.version) } + test("other dependency interpolation") { + val org = "org.org" + val name = "name" + val version = "123-version" + val dep = dep"$org::$name-$name:$version" + val expected = Dependency(org, s"$name-$name", version) + expect(dep.name == expected.name) + expect(dep.organization == expected.organization) + expect(dep.version == expected.version) + } + } From aa07b3370bf1b30ef76c6be3eee1c030af3f60c8 Mon Sep 17 00:00:00 2001 From: Alexandre Archambault Date: Wed, 23 Mar 2022 13:44:11 +0100 Subject: [PATCH 3/5] Fix --- .../dependency/literal/LiteralMacros.scala | 26 ++++++++---------- .../scala-3/dependency/literal/Mappings.scala | 27 ++++++++++++------- 2 files changed, 28 insertions(+), 25 deletions(-) diff --git a/dependency/src/main/scala-2/dependency/literal/LiteralMacros.scala b/dependency/src/main/scala-2/dependency/literal/LiteralMacros.scala index 09a083d..6c80529 100644 --- a/dependency/src/main/scala-2/dependency/literal/LiteralMacros.scala +++ b/dependency/src/main/scala-2/dependency/literal/LiteralMacros.scala @@ -39,34 +39,30 @@ abstract class LiteralMacros(val c: blackbox.Context) { helper(0) } - private def insertExpr(str: String, idLen: Int, insert: c.Expr[Any], indices: List[Int]): c.Tree = + private def insertExpr(str: String, indices: List[(Int, Int, c.Expr[Any])]): c.Tree = indices match { case Nil => q"$str" - case idx :: tail => + case (idx, idLen, insert) :: tail => val (prefix, suffix) = str.splitAt(idx) - val prefixExpr = insertExpr(prefix, idLen, insert, tail) + val prefixExpr = insertExpr(prefix, tail) q"$prefixExpr + $insert + ${suffix.substring(idLen)}" } protected final type Mappings = Seq[(String, c.Expr[Any])] protected def applyMappings(str: String, mappings: Mappings): c.Expr[String] = { - val matchOpt = mappings + val substitutions = mappings .iterator .zipWithIndex - .map { + .flatMap { case ((id, expr), i) => - val idx = str.indexOf(id) - (id, expr, i, idx) + val indices0 = indices(str, id) + indices0.map(idx => (idx, id.length, expr)) } - .find(_._4 >= 0) - matchOpt match { - case None => c.Expr(q"$str") - case Some((id, expr, i, idx)) => - val indices0 = indices(str, id) - val tree = insertExpr(str, id.length, expr, indices0.reverse) - c.Expr(tree) - } + .toList + .sortBy(-_._1) + val tree = insertExpr(str, substitutions) + c.Expr(tree) } def mappings(args: Seq[c.Expr[Any]]): Mappings = diff --git a/dependency/src/main/scala-3/dependency/literal/Mappings.scala b/dependency/src/main/scala-3/dependency/literal/Mappings.scala index 1b44214..c31bbdf 100644 --- a/dependency/src/main/scala-3/dependency/literal/Mappings.scala +++ b/dependency/src/main/scala-3/dependency/literal/Mappings.scala @@ -20,21 +20,28 @@ private[literal] final case class Mappings(mappings: List[(String, QExpr[String] helper(0) } - private def insertExpr(str: String, idLen: Int, insert: QExpr[String], indices: List[Int])(using Quotes): QExpr[String] = - indices match { + private def insertExpr(str: String, substitutions: List[(Int, Int, QExpr[String])])(using Quotes): QExpr[String] = + substitutions match { case Nil => QExpr(str) - case idx :: tail => + case (idx, idLen, insert) :: tail => val (prefix, suffix) = str.splitAt(idx) - val prefixExpr = insertExpr(prefix, idLen, insert, tail) + val prefixExpr = insertExpr(prefix, tail) '{$prefixExpr + $insert + ${QExpr(suffix.substring(idLen))}} } - def Expr(str: String)(using Quotes): QExpr[String] = - mappings.find(e => str.contains(e._1)).fold(QExpr(str)){ - case (id, expr) => - val indices0 = indices(str, id) - insertExpr(str, id.length, expr, indices0) - } + def Expr(str: String)(using Quotes): QExpr[String] = { + + val substitutions = mappings + .flatMap { + case (id, expr) => + val indices0 = indices(str, id) + System.err.println(s"indices0=$indices0") + indices0.map(idx => (idx, id.length, expr)) + } + .sortBy(-_._1) + + insertExpr(str, substitutions) + } def stringOption(opt: Option[String])(using Quotes): QExpr[Option[String]] = opt match { From abd8da55f69b3b326620d5ce979d09beea07ad45 Mon Sep 17 00:00:00 2001 From: Alexandre Archambault Date: Wed, 23 Mar 2022 13:44:45 +0100 Subject: [PATCH 4/5] Revert .gitignore changes --- .gitignore | 2 -- 1 file changed, 2 deletions(-) diff --git a/.gitignore b/.gitignore index 8e7c406..89f9ac0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1 @@ out/ -.idea -.idea_modules \ No newline at end of file From 0293e683a8328a7d5808597e67c8d9c17b94cc86 Mon Sep 17 00:00:00 2001 From: Alexandre Archambault Date: Wed, 23 Mar 2022 13:49:38 +0100 Subject: [PATCH 5/5] clean-up --- dependency/src/main/scala-3/dependency/literal/Mappings.scala | 1 - 1 file changed, 1 deletion(-) diff --git a/dependency/src/main/scala-3/dependency/literal/Mappings.scala b/dependency/src/main/scala-3/dependency/literal/Mappings.scala index c31bbdf..549a2bb 100644 --- a/dependency/src/main/scala-3/dependency/literal/Mappings.scala +++ b/dependency/src/main/scala-3/dependency/literal/Mappings.scala @@ -35,7 +35,6 @@ private[literal] final case class Mappings(mappings: List[(String, QExpr[String] .flatMap { case (id, expr) => val indices0 = indices(str, id) - System.err.println(s"indices0=$indices0") indices0.map(idx => (idx, id.length, expr)) } .sortBy(-_._1)