Skip to content

Commit

Permalink
Merge pull request #347 from xuwei-k/update-scalafmt
Browse files Browse the repository at this point in the history
update scalafmt
  • Loading branch information
eed3si9n authored Jun 25, 2023
2 parents d3d690b + b3001e4 commit 1b49318
Show file tree
Hide file tree
Showing 28 changed files with 237 additions and 231 deletions.
10 changes: 6 additions & 4 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
version = 2.0.0
version = 3.7.4
maxColumn = 100
project.git = true
project.excludeFilters = [ /sbt-test/, /input_sources/, /contraband-scala/ ]
lineEndings = preserve

# http://docs.scala-lang.org/style/scaladoc.html recommends the JavaDoc style.
# scala/scala is written that way too https://github.com/scala/scala/blob/v2.12.2/src/library/scala/Predef.scala
docstrings = JavaDoc
docstrings.style = Asterisk
docstrings.wrap = no

# This also seems more idiomatic to include whitespace in import x.{ yyy }
spaces.inImportCurlyBraces = true
Expand All @@ -17,6 +17,8 @@ align.openParenCallSite = false
align.openParenDefnSite = false

# For better code clarity
danglingParentheses = true
danglingParentheses.preset = true

trailingCommas = preserve

runner.dialect = Scala212Source3
19 changes: 10 additions & 9 deletions io/src/main/scala/sbt/internal/io/MacOSXWatchService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,16 @@ private[sbt] class MacOSXWatchService extends WatchService with Unregisterable {
val parent = path.getParent
if (!keys.contains(parent)) {
// workaround for https://github.com/sbt/sbt/issues/4603
if (keys.keys.exists(
p =>
p.getParent == parent && {
val leftFileName = p.getFileName.toString
val rightFileName = path.getFileName.toString
leftFileName != rightFileName && (leftFileName
.startsWith(rightFileName) || rightFileName.startsWith(leftFileName))
}
)) {
if (
keys.keys.exists(p =>
p.getParent == parent && {
val leftFileName = p.getFileName.toString
val rightFileName = path.getFileName.toString
leftFileName != rightFileName && (leftFileName
.startsWith(rightFileName) || rightFileName.startsWith(leftFileName))
}
)
) {
parentKeys.put(parent, underlying.register(parent, events: _*))
}
}
Expand Down
27 changes: 14 additions & 13 deletions io/src/main/scala/sbt/internal/io/Milli.scala
Original file line number Diff line number Diff line change
Expand Up @@ -303,19 +303,20 @@ private object WinMilli extends MilliNative[FILETIME] {
FILE_READ_ATTRIBUTES,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE
)
val mtime = try {
val modifiedTime = new FILETIME.ByReference()
if (!GetFileTime(hFile, /*creationTime*/ null, /*accessTime*/ null, modifiedTime))
throw new IOException(
"GetFileTime() failed with error " + GetLastError() + " for file " + filePath
)
modifiedTime
} finally {
if (!CloseHandle(hFile))
throw new IOException(
"CloseHandle() after GetFileTime() failed with error " + GetLastError() + " for file " + filePath
)
}
val mtime =
try {
val modifiedTime = new FILETIME.ByReference()
if (!GetFileTime(hFile, /*creationTime*/ null, /*accessTime*/ null, modifiedTime))
throw new IOException(
"GetFileTime() failed with error " + GetLastError() + " for file " + filePath
)
modifiedTime
} finally {
if (!CloseHandle(hFile))
throw new IOException(
"CloseHandle() after GetFileTime() failed with error " + GetLastError() + " for file " + filePath
)
}
mtime
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ private[sbt] final class WatchState private (
/** Retrieve events from the `WatchService` */
private[sbt] def pollEvents(): Iterable[(Path, WatchEvent[_])] = {
val events = service.pollEvents()
events.toIterable.flatMap {
case (k, evs) => evs.map((k.watchable().asInstanceOf[Path], _))
events.toIterable.flatMap { case (k, evs) =>
evs.map((k.watchable().asInstanceOf[Path], _))
}
}

Expand Down Expand Up @@ -235,7 +235,7 @@ final class Source(
override def equals(o: Any): Boolean = o match {
case that: Source =>
this.base == that.base && this.includeFilter == that.includeFilter &&
this.excludeFilter == that.excludeFilter && this.recursive == that.recursive
this.excludeFilter == that.excludeFilter && this.recursive == that.recursive
case _ => false
}
override lazy val hashCode: Int = Seq[Any](base, includeFilter, excludeFilter, recursive).hashCode
Expand Down
45 changes: 21 additions & 24 deletions io/src/main/scala/sbt/internal/nio/FileCache.scala
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,19 @@ private[nio] class FileCache[+T](converter: Path => T, globs: mutable.Set[Glob])
FileAttributes(path).foreach(add(glob, _))
val current = subMap.asScala.toMap
val result = new util.ArrayList[FileEvent[T]].asScala
previous.foreach {
case (p, prev) =>
current.get(p) match {
case Some(newPair) if prev != newPair =>
result += Update(p, prev, newPair)
case None => result += Deletion(p, prev)
case _ =>
}
previous.foreach { case (p, prev) =>
current.get(p) match {
case Some(newPair) if prev != newPair =>
result += Update(p, prev, newPair)
case None => result += Deletion(p, prev)
case _ =>
}
}
current.foreach {
case (p, newAttributes) =>
previous.get(p) match {
case None => result += Creation(p, newAttributes)
case _ =>
}
current.foreach { case (p, newAttributes) =>
previous.get(p) match {
case None => result += Creation(p, newAttributes)
case _ =>
}
}
result.toVector
} else {
Expand Down Expand Up @@ -132,16 +130,15 @@ private[nio] class FileCache[+T](converter: Path => T, globs: mutable.Set[Glob])
}
private[this] def updateGlob(path: Path): Glob = {
val depth = globs.toIndexedSeq.view
.map(
g =>
if (path.startsWith(g.base)) {
if (path == g.base) g.range._2
else
g.range._2 match {
case Int.MaxValue => Int.MaxValue
case d => d - g.base.relativize(path).getNameCount
}
} else Int.MinValue
.map(g =>
if (path.startsWith(g.base)) {
if (path == g.base) g.range._2
else
g.range._2 match {
case Int.MaxValue => Int.MaxValue
case d => d - g.base.relativize(path).getNameCount
}
} else Int.MinValue
)
.min
depth match {
Expand Down
4 changes: 2 additions & 2 deletions io/src/main/scala/sbt/internal/nio/FileEvent.scala
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ private[sbt] object FileEvent {
override def exists: Boolean = true
}
private[sbt] object Update {
def apply[T](path: Path, previousAttributes: T, attributes: T)(
implicit timeSource: TimeSource
def apply[T](path: Path, previousAttributes: T, attributes: T)(implicit
timeSource: TimeSource
): Update[T] =
new Update(path, previousAttributes, attributes) {
override val occurredAt: Deadline = timeSource.now
Expand Down
19 changes: 10 additions & 9 deletions io/src/main/scala/sbt/internal/nio/FileTreeRepositoryImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,16 @@ private[sbt] class FileTreeRepositoryImpl[T] extends FileTreeRepository[FileAttr
// workaround for https://github.com/sbt/sbt/issues/4603
val parent = glob.base.getParent
if (!registered.contains(parent)) {
if (registered.exists(
path =>
path.getParent == parent && {
val leftFileName = path.getFileName.toString
val rightFileName = base.getFileName.toString
leftFileName != rightFileName && (leftFileName
.startsWith(rightFileName) || rightFileName.startsWith(leftFileName))
}
)) {
if (
registered.exists(path =>
path.getParent == parent && {
val leftFileName = path.getFileName.toString
val rightFileName = base.getFileName.toString
leftFileName != rightFileName && (leftFileName
.startsWith(rightFileName) || rightFileName.startsWith(leftFileName))
}
)
) {
register(Glob(parent))
}
}
Expand Down
6 changes: 3 additions & 3 deletions io/src/main/scala/sbt/internal/nio/Globs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ private[sbt] object Globs {
case NothingFilter => Some(AnyPath)
case AllPassFilter => Some(NoPath)
case f =>
fileFilterToRelativeGlob(f).collect {
case m: Matcher => Matcher.not(m)
fileFilterToRelativeGlob(f).collect { case m: Matcher =>
Matcher.not(m)
}
}
case of: OrFilter =>
Expand Down Expand Up @@ -90,7 +90,7 @@ private[sbt] object Globs {
case ef: ExtensionFilter =>
ef.extensions match {
case extensions if extensions.length == 1 => Some(Matcher(s"*.${extensions.head}"))
case extensions => Some(Matcher(s"*.${extensions.mkString("{", ",", "}")}"))
case extensions => Some(Matcher(s"*.${extensions.mkString("{", ",", "}")}"))
}
case NothingFilter => Some(NoPath)
case nf: NotNameFilter =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,8 @@ private[sbt] class WatchServiceBackedObservable(
(event match {
case Creation(path, attrs) if attrs.isDirectory =>
s.register(path)
event +: view.list(Glob(path, RecursiveGlob)).flatMap {
case (p, a) =>
process(Creation(p, a))
event +: view.list(Glob(path, RecursiveGlob)).flatMap { case (p, a) =>
process(Creation(p, a))
}
case Deletion(p, attrs) if attrs.isDirectory =>
val events = fileCache.refresh(Glob(p, RecursiveGlob))
Expand Down
14 changes: 7 additions & 7 deletions io/src/main/scala/sbt/io/Hash.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ import java.net.{ URI, URL }
object Hash {
private val BufferSize = 8192

/** Converts an array of `bytes` to a hexadecimal representation String.*/
/** Converts an array of `bytes` to a hexadecimal representation String. */
def toHex(bytes: Array[Byte]): String = {
val buffer = new StringBuilder(bytes.length * 2)
for (i <- bytes.indices) {
val b = bytes(i)
val bi: Int = if (b < 0) b + 256 else b.toInt
buffer append toHex((bi >>> 4).asInstanceOf[Byte])
buffer append toHex((bi & 0x0F).asInstanceOf[Byte])
buffer append toHex((bi & 0x0f).asInstanceOf[Byte])
}
buffer.toString
}
Expand Down Expand Up @@ -52,18 +52,18 @@ object Hash {
/** Computes the SHA-1 hash of `s` and truncates the hexadecimal representation of the hash via [[halve]]. */
def halfHashString(s: String): String = halve(toHex(apply(s)))

/** Calculates the SHA-1 hash of the given String.*/
/** Calculates the SHA-1 hash of the given String. */
def apply(s: String): Array[Byte] = apply(s.getBytes("UTF-8"))

/** Calculates the SHA-1 hash of the given Array[Byte].*/
/** Calculates the SHA-1 hash of the given Array[Byte]. */
def apply(as: Array[Byte]): Array[Byte] = apply(new ByteArrayInputStream(as))

/** Calculates the SHA-1 hash of the given file.*/
/** Calculates the SHA-1 hash of the given file. */
def apply(file: File): Array[Byte] =
try apply(new BufferedInputStream(new FileInputStream(file))) // apply closes the stream
catch { case _: FileNotFoundException => apply("") }

/** Calculates the SHA-1 hash of the given resource.*/
/** Calculates the SHA-1 hash of the given resource. */
def apply(url: URL): Array[Byte] = Using.urlInputStream(url)(apply)

/**
Expand All @@ -74,7 +74,7 @@ object Hash {
def contentsIfLocal(uri: URI): Array[Byte] =
if (uri.getScheme == "file") apply(uri.toURL) else apply(uri.normalize.toString)

/** Calculates the SHA-1 hash of the given stream, closing it when finished.*/
/** Calculates the SHA-1 hash of the given stream, closing it when finished. */
def apply(stream: InputStream): Array[Byte] = {
import java.security.{ DigestInputStream, MessageDigest }
val digest = MessageDigest.getInstance("SHA")
Expand Down
Loading

0 comments on commit 1b49318

Please sign in to comment.