Skip to content

Commit

Permalink
scala213Source3 - allow infix op after NL
Browse files Browse the repository at this point in the history
Original PR in compiler - scala/scala#8419
Fixes scalameta#2739.
  • Loading branch information
dos65 committed Apr 26, 2022
1 parent 1a9ec6f commit 31d1632
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 9 deletions.
18 changes: 13 additions & 5 deletions scalameta/dialects/shared/src/main/scala/scala/meta/Dialect.scala
Expand Up @@ -150,7 +150,9 @@ final class Dialect private (
// import a.b.c.{ given, _} used for -X:source3
val allowGivenImports: Boolean,
// Scala 3 uses proper precedence rules for infix types, unlike Scala 2
val useInfixTypePrecedence: Boolean
val useInfixTypePrecedence: Boolean,
// Scala213Source3 and Scala3 allow infix operator being placed after nl
val allowInfixOperatorAfterNL: Boolean
) extends Product with Serializable {

// NOTE(olafur) checklist for adding a new dialect field in a binary compatible way:
Expand Down Expand Up @@ -239,7 +241,8 @@ final class Dialect private (
allowPlusMinusUnderscoreAsIdent = false,
allowPlusMinusUnderscoreAsPlaceholder = false,
allowGivenImports = false,
useInfixTypePrecedence = false
useInfixTypePrecedence = false,
allowInfixOperatorAfterNL = false
// NOTE(olafur): declare the default value for new fields above this comment.
)
}
Expand Down Expand Up @@ -429,6 +432,10 @@ final class Dialect private (
privateCopy(useInfixTypePrecedence = newValue)
}

def withAllowInfixOperatorAfterNL(newValue: Boolean): Dialect = {
privateCopy(allowInfixOperatorAfterNL = newValue)
}

// NOTE(olafur): add the next `withX()` method above this comment. Please try
// to use consistent formatting, use `newValue` as the parameter name and wrap
// the body inside curly braces.
Expand Down Expand Up @@ -491,8 +498,8 @@ final class Dialect private (
allowPlusMinusUnderscoreAsIdent: Boolean = this.allowPlusMinusUnderscoreAsIdent,
allowPlusMinusUnderscoreAsPlaceholder: Boolean = this.allowPlusMinusUnderscoreAsPlaceholder,
allowGivenImports: Boolean = this.allowGivenImports,
useInfixTypePrecedence: Boolean = this.useInfixTypePrecedence

useInfixTypePrecedence: Boolean = this.useInfixTypePrecedence,
allowInfixOperatorAfterNL: Boolean = this.allowInfixOperatorAfterNL
// NOTE(olafur): add the next parameter above this comment.
): Dialect = {
new Dialect(
Expand Down Expand Up @@ -552,7 +559,8 @@ final class Dialect private (
allowPlusMinusUnderscoreAsIdent,
allowPlusMinusUnderscoreAsPlaceholder,
allowGivenImports,
useInfixTypePrecedence
useInfixTypePrecedence,
allowInfixOperatorAfterNL
// NOTE(olafur): add the next argument above this comment.
)
}
Expand Down
Expand Up @@ -65,6 +65,7 @@ package object dialects {
.withAllowPostfixStarVarargSplices(true)
.withAllowPlusMinusUnderscoreAsIdent(true)
.withAllowGivenImports(true)
.withAllowInfixOperatorAfterNL(true)

/**
* Dialect starting with Scala 2.12.14 for `-Xsource:3` option
Expand Down Expand Up @@ -145,6 +146,7 @@ package object dialects {
.withAllowDoWhile(false)
.withAllowPlusMinusUnderscoreAsPlaceholder(true)
.withUseInfixTypePrecedence(true)
.withAllowInfixOperatorAfterNL(true)

@deprecated("Use Scala3 instead", "4.4.2")
implicit val Dotty = Scala3
Expand Down
Expand Up @@ -360,7 +360,7 @@ private[parsers] class LazyTokenIterator private (
case _: RegionIndent | _: RegionIndentEnum => true
case x: RegionParen => x.canProduceLF
case _ => false
}
} && !next.isLeadingInfixOperator
}

def getIfCanProduceLF =
Expand Down
Expand Up @@ -106,7 +106,7 @@ class ScannerTokens(tokens: Tokens, input: Input)(implicit dialect: Dialect) {
@inline
def strictNext: Token = getStrictAfterSafe(nextSafe)

def isLeadingInfixOperator: Boolean = dialect.allowSignificantIndentation && {
def isLeadingInfixOperator: Boolean = dialect.allowInfixOperatorAfterNL && {
val text = token.text
@tailrec
def iter(idx: Int, nonEmpty: Boolean): Boolean = {
Expand Down
@@ -1,6 +1,8 @@
package scala.meta.tests
package parsers

import scala.meta.Dialect
import scala.meta.dialects
import scala.meta.dialects.Scala213

class Scala213Suite extends ParseSuite {
Expand Down Expand Up @@ -78,9 +80,18 @@ class Scala213Suite extends ParseSuite {
runAssert("try ()", """Term.Try(Lit.Unit(()), Nil, None)""")
}

private def runAssert(code: String, expected: String): Unit = {
test("infix-at-line-start") {
runAssert(
"""|val x = "hello"
| ++ "world"
|""".stripMargin,
"""Defn.Val(Nil, List(Pat.Var(Term.Name("x"))), None, Term.ApplyInfix(Lit.String("hello"), Term.Name("++"), Nil, List(Lit.String("world"))))"""
)(dialects.Scala213Source3)
}

private def runAssert(code: String, expected: String)(implicit d: Dialect): Unit = {
assertNoDiff(
templStat(code).structure,
templStat(code)(d).structure,
expected
)
}
Expand Down

0 comments on commit 31d1632

Please sign in to comment.