Skip to content

Commit

Permalink
refactor: simplify searchForBuildTool function
Browse files Browse the repository at this point in the history
  • Loading branch information
kasiaMarek committed Sep 8, 2023
1 parent abd3537 commit 6694a54
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 96 deletions.
18 changes: 15 additions & 3 deletions metals/src/main/scala/scala/meta/internal/bsp/BspConnector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,12 @@ class BspConnector(
Future.successful(None)
case ResolvedBloop =>
bloopServers
.newServer(projectRoot, bspTraceRoot, userConfiguration, addLivenessMonitor)
.newServer(
projectRoot,
bspTraceRoot,
userConfiguration,
addLivenessMonitor,
)
.map(Some(_))
case ResolvedBspOne(details)
if details.getName() == SbtBuildTool.name =>
Expand All @@ -114,7 +119,9 @@ class BspConnector(
.map(Some(_))
case ResolvedBspOne(details) =>
tables.buildServers.chooseServer(details.getName())
bspServers.newServer(projectRoot, bspTraceRoot, details, addLivenessMonitor).map(Some(_))
bspServers
.newServer(projectRoot, bspTraceRoot, details, addLivenessMonitor)
.map(Some(_))
case ResolvedMultiple(_, availableServers) =>
val distinctServers = availableServers
.groupBy(_.getName())
Expand Down Expand Up @@ -146,7 +153,12 @@ class BspConnector(
)
)
_ = tables.buildServers.chooseServer(item.getName())
conn <- bspServers.newServer(projectRoot, bspTraceRoot, item, addLivenessMonitor)
conn <- bspServers.newServer(
projectRoot,
bspTraceRoot,
item,
addLivenessMonitor,
)
} yield Some(conn)
}
}
Expand Down
36 changes: 13 additions & 23 deletions metals/src/main/scala/scala/meta/internal/builds/BuildTools.scala
Original file line number Diff line number Diff line change
Expand Up @@ -117,29 +117,19 @@ final class BuildTools(

private def searchForBuildTool(
isProjectRoot: AbsolutePath => Boolean
): Option[AbsolutePath] = {
def recIsProjectRoot(
path: AbsolutePath,
level: Int = 0,
): Option[AbsolutePath] =
if (
path.isDirectory &&
!path.toNIO.filename.startsWith(".")
) {
if (isProjectRoot(path)) Some(path)
else if (level < 1)
path.toNIO
.toFile()
.listFiles()
.collectFirst(root =>
recIsProjectRoot(AbsolutePath(root), level + 1) match {
case Some(root) => root
}
)
else None
} else None
recIsProjectRoot(workspace)
}
): Option[AbsolutePath] =
if (isProjectRoot(workspace)) Some(workspace)
else
workspace.toNIO
.toFile()
.listFiles()
.collectFirst {
case file
if file.isDirectory &&
!file.getName.startsWith(".") &&
isProjectRoot(AbsolutePath(file.toPath())) =>
AbsolutePath(file.toPath())
}

def allAvailable: List[BuildTool] = {
List(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,7 @@ object Messages {
params
}
}

}

object FileOutOfScalaCliBspScope {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,7 @@ class MetalsLspService(

def connectTables(): Connection = tables.connect()

def initialized(): Future[Unit] = {
def initialized(): Future[Unit] =
for {
_ <- maybeSetupScalaCli()
_ <-
Expand All @@ -902,7 +902,6 @@ class MetalsLspService(
)
)
} yield ()
}

def onShutdown(): Unit = {
tables.fingerprints.save(fingerprints.getAllFingerprints())
Expand Down Expand Up @@ -1656,7 +1655,7 @@ class MetalsLspService(
} yield bloopServers.shutdownServer()
case Some(session) if session.main.isSbt =>
for {
currentBuildTool <- supportedBuildTool
currentBuildTool <- supportedBuildTool()
res <- currentBuildTool match {
case Some(sbt: SbtBuildTool) =>
for {
Expand Down Expand Up @@ -1948,7 +1947,9 @@ class MetalsLspService(
})
}

def supportedBuildTool(): Future[Option[BuildTool]] = {
def supportedBuildTool(
withStartFileWatcher: Boolean = false
): Future[Option[BuildTool]] = {
def isCompatibleVersion(buildTool: BuildTool) = {
val isCompatibleVersion = SemVer.isCompatibleVersion(
buildTool.minimumVersion,
Expand All @@ -1970,8 +1971,10 @@ class MetalsLspService(
if (!buildTools.isAutoConnectable()) {
warnings.noBuildTool()
}
// wait for a bsp file to show up
fileWatcher.start(Set(folder.resolve(".bsp")))
if (withStartFileWatcher) {
// wait for a bsp file to show up
fileWatcher.start(Set(folder.resolve(".bsp")))
}
Future(None)
}
case buildTools =>
Expand All @@ -1987,7 +1990,7 @@ class MetalsLspService(
forceImport: Boolean
): Future[BuildChange] =
for {
possibleBuildTool <- supportedBuildTool
possibleBuildTool <- supportedBuildTool(withStartFileWatcher = true)
chosenBuildServer = tables.buildServers.selectedServer()
isBloopOrEmpty = chosenBuildServer.isEmpty || chosenBuildServer.exists(
_ == BloopServers.name
Expand Down Expand Up @@ -2078,7 +2081,7 @@ class MetalsLspService(

def calculateOptProjectRoot(): Future[Option[AbsolutePath]] =
for {
possibleBuildTool <- supportedBuildTool
possibleBuildTool <- supportedBuildTool()
} yield possibleBuildTool.map(_.projectRoot).orElse(buildTools.bloopProject)

def quickConnectToBuildServer(): Future[BuildChange] =
Expand Down
29 changes: 0 additions & 29 deletions metals/src/main/scala/scala/meta/internal/metals/ProjectRoot.scala

This file was deleted.

2 changes: 0 additions & 2 deletions metals/src/main/scala/scala/meta/internal/metals/Tables.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ final class Tables(
new ChosenBuildTool(() => connection)
val fingerprints =
new Fingerprints(() => connection)
val projectRoot =
new ProjectRoot(() => connection)

private val ref: AtomicReference[ConnectionState] =
new AtomicReference(ConnectionState.Empty)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,19 +313,6 @@ object UserConfiguration {
|launcher, not available in PATH.
|""".stripMargin,
),
UserConfigurationOption(
"projects-roots",
"""empty map""",
""""{
"project1": "backend",
"project2": "backend",
}"""",
"Project roots",
"""Optional map of Scala projects' roots as relative path from workspace folder root.
|The project root should contain the build tool configuration if not passed workspace root is assumed.
|For a single root project a the key should be set to "root" or simply a plain string can be passed.
|""".stripMargin,
),
)

def fromJson(
Expand Down Expand Up @@ -365,16 +352,12 @@ object UserConfiguration {
)
},
)
def getStringKey(
key: String,
customError: JsonElement => Option[String] = _ => None,
): Option[String] =
getStringKeyOnObj(key, json, customError)
def getStringKey(key: String): Option[String] =
getStringKeyOnObj(key, json)

def getStringKeyOnObj(
key: String,
currentObject: JsonObject,
customError: JsonElement => Option[String] = _ => None,
): Option[String] =
getKey(
key,
Expand All @@ -383,9 +366,7 @@ object UserConfiguration {
Try(value.getAsString)
.fold(
_ => {
errors += customError(value).getOrElse(
s"json error: key '$key' should have value of type string but obtained $value"
)
errors += s"json error: key '$key' should have value of type string but obtained $value"
None
},
Some(_),
Expand Down Expand Up @@ -443,10 +424,7 @@ object UserConfiguration {
},
)

def getStringMap(
key: String,
collectError: Boolean = true,
): Option[Map[String, String]] =
def getStringMap(key: String): Option[Map[String, String]] =
getKey(
key,
json,
Expand All @@ -461,9 +439,7 @@ object UserConfiguration {
}
}.fold(
_ => {
if (collectError) {
errors += s"json error: key '$key' should have be object with string values but obtained $value"
}
errors += s"json error: key '$key' should have be object with string values but obtained $value"
None
},
entries => Some(entries.toMap),
Expand Down
3 changes: 1 addition & 2 deletions tests/unit/src/test/scala/tests/FileWatcherLspSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,7 @@ class FileWatcherLspSuite extends BaseLspSuite("file-watcher") {
.asScala
_ = assertNoDiff(
client.workspaceMessageRequests,
s"""|${Messages.ImportBuild.params("sbt").getMessage()}
|""".stripMargin,
Messages.ImportBuild.params("sbt").getMessage(),
)
} yield ()
}
Expand Down

0 comments on commit 6694a54

Please sign in to comment.