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

Rewrite: check BOF/EOF when looking for LF #3278

Merged
merged 2 commits into from
Jul 25, 2022
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 @@ -420,7 +420,7 @@ object Imports extends RewriteFactory {

protected val groups = Array.fill(settings.numGroups + 1)(new Grouping)

protected def addToGroup(
protected final def addToGroup(
group: Grouping,
kw: String,
ref: String,
Expand All @@ -429,7 +429,7 @@ object Imports extends RewriteFactory {
): Unit =
group.add(kw, ref, getSelector(selector, true), importer)

protected def addToGroup(
protected final def addToGroup(
group: Grouping,
kw: String,
ref: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ case class RewriteCtx(

def onlyWhitespaceBefore(index: Int): Boolean =
tokenTraverser
.findAtOrBefore(index - 1)(RewriteCtx.isLFSkipWhitespace)
.findAtOrBefore(index - 1) {
case _: LF | _: Token.BOF => Some(true)
case Whitespace() => None
case _ => Some(false)
}
.isDefined

def findNonWhitespaceWith(
Expand All @@ -78,7 +82,11 @@ case class RewriteCtx(
)(implicit builder: Rewrite.PatchBuilder): Unit =
if (onlyWhitespaceBefore(beg))
tokenTraverser
.findAtOrAfter(end + 1)(RewriteCtx.isLFSkipWhitespace)
.findAtOrAfter(end + 1) {
case _: LF => Some(true)
case Whitespace() => None
case _ => Some(false)
}
.map(TokenPatch.Remove)
.foreach(builder += _)

Expand Down Expand Up @@ -168,15 +176,6 @@ object Rewrite {

object RewriteCtx {

// this is a helper function to be used with the token traverser
// finds a newline not blocked by any non-whitespace characters
private def isLFSkipWhitespace(token: Token): Option[Boolean] =
token match {
case _: LF => Some(true)
case Whitespace() => None
case _ => Some(false)
}

// https://www.scala-lang.org/files/archive/spec/2.13/06-expressions.html#prefix-infix-and-postfix-operations
def isSimpleExprOr(
expr: Tree
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,24 @@ import zio.prelude.` => `
import zio.prelude.{<=>, ` => `}
>>>
import zio.prelude.<=>

import zio.prelude.` => `
import zio.prelude.<=>
import zio.prelude.` => `
<<< #3275 1
runner.parser = source
===
package foo
import scala.collection.mutable
import scala.collection.mutable.AbstractMap
>>>
package foo
import scala.collection.mutable
import scala.collection.mutable.AbstractMap
<<< #3275 2
runner.parser = source
===
import scala.collection.mutable
import scala.collection.mutable.AbstractMap
>>>
import scala.collection.mutable
import scala.collection.mutable.AbstractMap
4 changes: 0 additions & 4 deletions scalafmt-tests/src/test/resources/rewrite/Imports.source
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,6 @@ class B {
import bar._
>>>
import bar._

class A {
import foo._
}
Expand Down Expand Up @@ -771,7 +770,6 @@ class B:
import bar._
>>>
import bar._

class A:
import baz._
import foo._
Expand All @@ -793,7 +791,6 @@ class B:
import bar._
>>>
import bar._

class A:
/* foo */
import baz._
Expand Down Expand Up @@ -822,7 +819,6 @@ class B:
import bar._
>>>
import bar._

class A:
// baz
import baz._
Expand Down