Skip to content

Commit

Permalink
Merge pull request #938 from marek1840/migrate-java-streams-to-genera…
Browse files Browse the repository at this point in the history
…tors

replace java streams with generators
  • Loading branch information
Marek Żarnowski authored Sep 23, 2019
2 parents 87a59a3 + 78875e9 commit e551ab8
Show file tree
Hide file tree
Showing 18 changed files with 107 additions and 205 deletions.
6 changes: 6 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ lazy val interfaces = project
)
)

val genyVersion = Def.setting {
if (scalaVersion.value.startsWith("2.11")) "0.1.6"
else "0.1.8"
}

lazy val mtags = project
.settings(
moduleName := "mtags",
Expand All @@ -189,6 +194,7 @@ lazy val mtags = project
"com.thoughtworks.qdox" % "qdox" % "2.0-M9", // for java mtags
"org.jsoup" % "jsoup" % "1.11.3", // for extracting HTML from javadocs
"org.lz4" % "lz4-java" % "1.6.0", // for streaming hashing when indexing classpaths
"com.lihaoyi" %% "geny" % genyVersion.value,
"org.scalameta" % "semanticdb-scalac-core" % V.scalameta cross CrossVersion.full
),
libraryDependencies ++= {
Expand Down
15 changes: 2 additions & 13 deletions metals/src/main/scala/scala/meta/internal/builds/BuildTools.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package scala.meta.internal.builds

import java.nio.file.Files
import java.util.Properties
import scala.meta.internal.jdk.CollectionConverters._
import scala.meta.io.AbsolutePath
import scala.meta.internal.metals.MetalsEnrichments._

/**
* Detects what build tool is used in this workspace.
Expand Down Expand Up @@ -31,18 +31,7 @@ final class BuildTools(
bspGlobalDirectories.exists(hasJsonFile)
}
private def hasJsonFile(dir: AbsolutePath): Boolean = {
dir.isDirectory && {
val ls = Files.list(dir.toNIO)
val hasJsonFile =
try {
ls.iterator()
.asScala
.exists(_.getFileName.toString.endsWith(".json"))
} finally {
ls.close()
}
hasJsonFile
}
dir.list.exists(_.extension == "json")
}
// Returns true if there's a build.sbt file or project/build.properties with sbt.version
def isSbt: Boolean = {
Expand Down
9 changes: 3 additions & 6 deletions metals/src/main/scala/scala/meta/internal/builds/Digest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import scala.meta.internal.metals.MetalsEnrichments._
import scala.meta.io.AbsolutePath
import scala.util.control.NonFatal
import scala.xml.Node
import scala.meta.internal.mtags.ListFiles

case class Digest(
md5: String,
Expand Down Expand Up @@ -61,11 +60,9 @@ object Digest {
): Boolean = {
if (!path.isDirectory) true
else {
var success = true
ListFiles.foreach(path) { file =>
success &= digestFile(file, digest)
path.list.forall { file =>
digestFile(file, digest)
}
success
}
}

Expand Down Expand Up @@ -122,7 +119,7 @@ object Digest {
chldrenSuccessful.forall(p => p)
}
try {
val xml = XML.loadFile(file.toNIO.toFile())
val xml = XML.loadFile(file.toNIO.toFile)
digestElement(xml)
xml.text.split("\\s+").foreach(word => digest.update(word.getBytes))
true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package scala.meta.internal.builds

import scala.meta.io.AbsolutePath
import scala.meta.internal.mtags.WalkFiles
import java.security.MessageDigest
import scala.meta.internal.mtags.ListFiles
import scala.meta.internal.metals.MetalsEnrichments._
import scala.meta.io.AbsolutePath

object GradleDigest extends Digestable {
override protected def digestWorkspace(
Expand All @@ -23,33 +22,28 @@ object GradleDigest extends Digestable {
}

def digestBuildSrc(path: AbsolutePath, digest: MessageDigest): Boolean = {
WalkFiles.foreach(path) { file =>
path.listRecursive.forall { file =>
Digest.digestFile(file, digest)
}
true
}

def digestSubProjects(
workspace: AbsolutePath,
digest: MessageDigest
): Boolean = {
val directories = ListFiles(workspace).filter(_.isDirectory)
val directories = workspace.list.filter(_.isDirectory).toList

val (subprojects, dirs) = directories.partition { file =>
ListFiles
.exists(file) { path =>
val stringPath = path.toString
stringPath.endsWith(".gradle") || stringPath.endsWith("gradle.kts")
}
file.list.exists { path =>
val stringPath = path.toString
stringPath.endsWith(".gradle") || stringPath.endsWith("gradle.kts")
}
}
/*
If a dir contains a gradle file we need to treat is as a workspace
*/
val isSuccessful = subprojects.forall { file =>
digestWorkspace(
file,
digest
)
digestWorkspace(file, digest)
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@ package scala.meta.internal.builds

import java.security.MessageDigest
import scala.meta.io.AbsolutePath
import scala.meta.internal.mtags.WalkFiles
import scala.meta.internal.mtags.MtagsEnrichments._
import scala.meta.internal.metals.MetalsEnrichments._

object MavenDigest extends Digestable {
override protected def digestWorkspace(
workspace: AbsolutePath,
digest: MessageDigest
): Boolean = {
Digest.digestFile(workspace.resolve("pom.xml"), digest)
WalkFiles.foreach(workspace) { file =>
if (file.filename == "pom.xml") {
workspace.listRecursive.forall {
case file if file.filename == "pom.xml" =>
Digest.digestFile(file, digest)
}
case _ =>
true
}
true
}
}
12 changes: 4 additions & 8 deletions metals/src/main/scala/scala/meta/internal/builds/SbtDigest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import java.security.MessageDigest
import scala.meta.internal.builds.Digest.digestScala
import scala.meta.internal.metals.MetalsEnrichments._
import scala.meta.io.AbsolutePath
import scala.meta.internal.mtags.ListFiles

object SbtDigest extends Digestable {

Expand All @@ -20,14 +19,11 @@ object SbtDigest extends Digestable {
Digest.digestDirectory(project.resolve("project"), digest)
}

def digestSbtFiles(
path: AbsolutePath,
digest: MessageDigest
): Boolean = {
if (!path.isDirectory) {
true
def digestSbtFiles(path: AbsolutePath, digest: MessageDigest): Boolean = {
if (path.isDirectory) {
path.list.forall(file => digestSbtFile(digest)(file))
} else {
ListFiles.forall(path)(file => digestSbtFile(digest)(file))
true
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ final class BloopServers(
maxRetries: Int = defaultRetries
): Future[Option[BuildServerConnection]] = {
newServerUnsafe().map(Option(_)).recoverWith {
case NonFatal(_) if maxRetries > 0 =>
scribe.warn(s"BSP retry ${defaultRetries - maxRetries + 1}")
case NonFatal(e) if maxRetries > 0 =>
val retry = defaultRetries - maxRetries + 1
val cause = e.getMessage
scribe.warn(s"BSP retry $retry due to: $cause", e)
newServer(maxRetries - 1)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import scala.meta.internal.metals.MetalsEnrichments._
import scala.meta.internal.mtags.MD5
import scala.meta.io.AbsolutePath
import scala.util.Try
import scala.meta.internal.mtags.ListFiles

/**
* Implements BSP server discovery, named "BSP Connection Protocol" in the spec.
Expand Down Expand Up @@ -124,11 +123,9 @@ final class BspServers(
private def findJsonFiles(): List[AbsolutePath] = {
val buf = List.newBuilder[AbsolutePath]
def visit(dir: AbsolutePath): Unit =
if (dir.isDirectory) {
ListFiles.foreach(dir) { p =>
if (p.extension == "json") {
buf += p
}
dir.list.foreach { p =>
if (p.extension == "json") {
buf += p
}
}
visit(workspace.resolve(".bsp"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,21 +294,13 @@ object MetalsEnrichments
}
}

def exists: Boolean = {
Files.exists(path.toNIO)
}

def touch(): Unit = {
if (!path.exists) {
parent.createDirectories()
path.parent.createDirectories()
Files.createFile(path.toNIO)
}
}

def parent: AbsolutePath = {
AbsolutePath(path.toNIO.getParent)
}

def createDirectories(): AbsolutePath = {
AbsolutePath(Files.createDirectories(dealias.toNIO))
}
Expand All @@ -318,10 +310,9 @@ object MetalsEnrichments
}

def writeText(text: String): Unit = {
parent.createDirectories()
path.parent.createDirectories()
Files.write(path.toNIO, text.getBytes(StandardCharsets.UTF_8))
}

}

implicit class XtensionString(value: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1316,7 +1316,7 @@ class MetalsLanguageServer(
private def indexWorkspaceSources(): Unit = {
for {
(sourceItem, targets) <- buildTargets.sourceItemsToBuildTargets
source <- WalkFiles(sourceItem)
source <- sourceItem.listRecursive
if source.isScalaOrJava
} {
targets.asScala.foreach { target =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import scala.tools.scalap.scalax.rules.scalasig.SymbolInfoSymbol
import scala.meta.internal.scalacp.SymlinkChildren
import scala.meta.internal.scalacp.Synthetics
import scala.util.control.NonFatal
import java.util.stream.Collectors
import scala.meta.internal.javacp.Javacp
import scala.meta.internal.metals.Time
import scala.meta.internal.metals.Timer
Expand Down Expand Up @@ -95,17 +94,16 @@ class ClasspathSymbols(isStatisticsEnabled: Boolean = false) {
def list(root: AbsolutePath): Unit = {
val dir =
if (symbol == Scala.Symbols.RootPackage) root
else {
root.resolve(Symbol(symbol).enclosingPackage.value)
}
listPath(dir.toNIO).foreach { path =>
if (Files.isDirectory(path)) {
buf ++= dummyClassfiles(root.toNIO, path)
} else if (isClassfile(path)) {
else root.resolve(Symbol(symbol).enclosingPackage.value)

dir.list.foreach {
case path if path.isDirectory =>
buf ++= dummyClassfiles(root.toNIO, path.toNIO)

case path if isClassfile(path.toNIO) =>
try {
val abspath = AbsolutePath(path)
val node = abspath.toClassNode
classfileSymbols(path, node, index, { i =>
val node = path.toClassNode
classfileSymbols(path.toNIO, node, index, { i =>
if (isRelevant(i)) {
buf += TreeViewSymbolInformation(i.symbol, i.kind, i.properties)
}
Expand All @@ -114,7 +112,8 @@ class ClasspathSymbols(isStatisticsEnabled: Boolean = false) {
case NonFatal(ex) =>
scribe.warn(s"error: can't convert $path in $in", ex)
}
}

case _ =>
}

}
Expand Down Expand Up @@ -174,15 +173,6 @@ class ClasspathSymbols(isStatisticsEnabled: Boolean = false) {
private def isClassfile(path: Path): Boolean = {
PathIO.extension(path) == "class" && Files.size(path) > 0
}
private def listPath(path: Path): Seq[Path] = {
if (Files.isDirectory(path)) {
val ls = Files.list(path)
try ls.collect(Collectors.toList()).asScala
finally ls.close()
} else {
Nil
}
}

private def classfileSymbols(
path: Path,
Expand Down Expand Up @@ -224,7 +214,6 @@ class ClasspathSymbols(isStatisticsEnabled: Boolean = false) {
}
}
}
Nil
}

}
Loading

0 comments on commit e551ab8

Please sign in to comment.