Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix recent regression with comments right before imports #1363

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,13 @@ class DefaultPreprocessor(parse: => String => Either[String, Seq[G#Tree]],

val Import = Processor{
case (name, code, tree: G#Import) =>
val Array(keyword, body) = code.split(" ", 2)
val tq = "\"\"\""
val body = fastparse.parse(code, Parsers.ImportFinder(_)) match {
case s: fastparse.Parsed.Success[String] =>
s.value
case _ =>
val Array(keyword, body) = code.split(" ", 2)
body
}
Expanded(code, Seq(
s"""
_root_.ammonite
Expand Down
24 changes: 14 additions & 10 deletions amm/compiler/src/main/scala-2/ammonite/compiler/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ object Parsers extends IParser {
private def `_`[_: P] = scalaparse.Scala.Underscore


private def ImportSplitter[_: P]: P[Seq[ammonite.util.ImportTree]] = {
private def ImportExpr[_: P]: P[ammonite.util.ImportTree] = {
def IdParser = P( (Id | `_` ).! ).map(
s => if (s(0) == '`') s.drop(1).dropRight(1) else s
)
Expand All @@ -28,18 +28,22 @@ object Parsers extends IParser {
)
def Prefix = P( IdParser.rep(1, sep = ".") )
def Suffix = P( "." ~/ (BulkImport | Selectors) )
def ImportExpr: P[ammonite.util.ImportTree] = {
// Manually use `WL0` parser here, instead of relying on WhitespaceApi, as
// we do not want the whitespace to be consumed even if the WL0 parser parses
// to the end of the input (which is the default behavior for WhitespaceApi)
P( Index ~~ Prefix ~~ (WL0 ~~ Suffix).? ~~ Index).map{
case (start, idSeq, selectors, end) =>
ammonite.util.ImportTree(idSeq, selectors, start, end)
}

// Manually use `WL0` parser here, instead of relying on WhitespaceApi, as
// we do not want the whitespace to be consumed even if the WL0 parser parses
// to the end of the input (which is the default behavior for WhitespaceApi)
P( Index ~~ Prefix ~~ (WL0 ~~ Suffix).? ~~ Index).map{
case (start, idSeq, selectors, end) =>
ammonite.util.ImportTree(idSeq, selectors, start, end)
}
P( `import` ~/ ImportExpr.rep(1, sep = ","./) )
}

def ImportSplitter[_: P]: P[Seq[ammonite.util.ImportTree]] =
P( `import` ~/ ImportExpr.rep(1, sep = ","./) )

def ImportFinder[_: P]: P[String] =
P(WL ~ `import` ~/ ImportExpr.! ~ End)

private def PatVarSplitter[_: P] = {
def Prefixes = P(Prelude ~ (`var` | `val`))
def Lhs = P( Prefixes ~/ BindPattern.rep(1, "," ~/ Pass) ~ (`:` ~/ Type).? )
Expand Down
13 changes: 13 additions & 0 deletions amm/repl/src/test/scala/ammonite/session/AdvancedTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -695,5 +695,18 @@ object AdvancedTests extends TestSuite{
val files = os.walk(dir).filter(os.isFile(_)).map(_.relativeTo(dir))
assert(files.sorted == expectedFiles.sorted)
}
test("comment and import") {
check.session(
"""
@ import $ivy.`org.typelevel::cats-kernel:2.6.1`

@ {
@ // hello
@ import cats.kernel._
@ }
import cats.kernel._
"""
)
}
}
}