From ef7642f3d4864c45a9c8783ecb12efcfcbfdb498 Mon Sep 17 00:00:00 2001 From: "Simeon H.K. Fitch" Date: Wed, 20 Jul 2016 12:26:23 -0400 Subject: [PATCH] Proposed fix to bug #17 --- .../src/main/scala/sourcecode/SourceContext.scala | 9 +++++++-- .../src/test/scala/sourcecode/Regressions.scala | 11 +++++++++++ .../shared/src/test/scala/sourcecode/Tests.scala | 2 ++ 3 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 sourcecode/shared/src/test/scala/sourcecode/Regressions.scala diff --git a/sourcecode/shared/src/main/scala/sourcecode/SourceContext.scala b/sourcecode/shared/src/main/scala/sourcecode/SourceContext.scala index 6ba86c5..66d67f7 100644 --- a/sourcecode/shared/src/main/scala/sourcecode/SourceContext.scala +++ b/sourcecode/shared/src/main/scala/sourcecode/SourceContext.scala @@ -128,7 +128,12 @@ object Impls{ def text[T: c.WeakTypeTag](c: Compat.Context)(v: c.Expr[T]): c.Expr[sourcecode.Text[T]] = { import c.universe._ val fileContent = new String(v.tree.pos.source.content) - val start = v.tree.collect{case tree => tree.pos.startOrPoint}.min + val start = v.tree.collect { + case treeVal => treeVal.pos match { + case NoPosition ⇒ Int.MaxValue + case p ⇒ p.startOrPoint + } + }.min val g = c.asInstanceOf[reflect.macros.runtime.Context].global val parser = g.newUnitParser(fileContent.drop(start)) parser.expr() @@ -185,4 +190,4 @@ object Impls{ }.mkString.dropRight(1) c.Expr[T](q"""${c.prefix}($renderedPath)""") } -} \ No newline at end of file +} diff --git a/sourcecode/shared/src/test/scala/sourcecode/Regressions.scala b/sourcecode/shared/src/test/scala/sourcecode/Regressions.scala new file mode 100644 index 0000000..0b73e9e --- /dev/null +++ b/sourcecode/shared/src/test/scala/sourcecode/Regressions.scala @@ -0,0 +1,11 @@ +package sourcecode + +object Regressions { + def bug17() = { + val text = sourcecode.Text(Seq(1).map(_+1)) + assert(text.source == "Seq(1).map(_+1)") + } + def main() = { + bug17() + } +} diff --git a/sourcecode/shared/src/test/scala/sourcecode/Tests.scala b/sourcecode/shared/src/test/scala/sourcecode/Tests.scala index 546e576..4bfd38b 100644 --- a/sourcecode/shared/src/test/scala/sourcecode/Tests.scala +++ b/sourcecode/shared/src/test/scala/sourcecode/Tests.scala @@ -30,6 +30,8 @@ object Tests{ DebugName.main() println("================Debug Lite================") DebugLite.main() + println("================Regressions===============") + Regressions.main() println("================Test Ended================") } }