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

feat: custom project root setting #5769

Merged
merged 1 commit into from
Oct 25, 2023
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
42 changes: 29 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 @@ -121,21 +121,37 @@ final class BuildTools(
)
def isBazel: Boolean = bazelProject.isDefined

private def customProjectRoot =
userConfig().customProjectRoot
.map(relativePath => workspace.resolve(relativePath.trim()))
.filter { projectRoot =>
val exists = projectRoot.exists
if (!exists) {
scribe.error(s"custom project root $projectRoot does not exist")
}
exists
}

private def searchForBuildTool(
isProjectRoot: AbsolutePath => Boolean
): 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())
}
): Option[AbsolutePath] = {
customProjectRoot match {
case Some(projectRoot) => Some(projectRoot).filter(isProjectRoot)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
case Some(projectRoot) => Some(projectRoot).filter(isProjectRoot)
case Some(projectRoot) if isProjectRoot(projectRoot) => Some(projectRoot)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's on purpose like this. So if you choose a custom root it should look only in that root. We had a problem discussed on discord, where a person had a custom bsp and an unused sbt somewhere lower file hierarchy.

case None =>
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 @@ -957,6 +957,13 @@ class MetalsLspService(
compilers.restartAll()
}

val slowConnect =
if (userConfig.customProjectRoot != old.customProjectRoot) {
tables.buildTool.reset()
tables.buildServers.reset()
slowConnectToBuildServer(false).ignoreValue
} else Future.successful(())

val resetDecorations =
if (
userConfig.showImplicitArguments != old.showImplicitArguments ||
Expand Down Expand Up @@ -1008,11 +1015,10 @@ class MetalsLspService(
}
.getOrElse(Future.successful(()))

Future
.sequence(
List(restartBuildServer, resetDecorations)
)
.map(_ => ())
for {
_ <- slowConnect
_ <- Future.sequence(List(restartBuildServer, resetDecorations))
} yield ()
}

override def didOpen(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ case class UserConfiguration(
testUserInterface: TestUserInterfaceKind = TestUserInterfaceKind.CodeLenses,
javaFormatConfig: Option[JavaFormatConfig] = None,
scalafixRulesDependencies: List[String] = Nil,
customProjectRoot: Option[String] = None,
scalaCliLauncher: Option[String] = None,
) {

Expand Down Expand Up @@ -313,6 +314,14 @@ object UserConfiguration {
|launcher, not available in PATH.
|""".stripMargin,
),
UserConfigurationOption(
"custom-project-root",
"""empty string `""`.""",
""""backend/scalaProject/"""",
"Custom project root",
"""Optional relative path to your project's root.
|If you want your project root to be the workspace/workspace root set it to "." .""".stripMargin,
),
)

def fromJson(
Expand Down Expand Up @@ -525,6 +534,8 @@ object UserConfiguration {
val scalafixRulesDependencies =
getStringListKey("scalafix-rules-dependencies").getOrElse(Nil)

val customProjectRoot = getStringKey("custom-project-root")

if (errors.isEmpty) {
Right(
UserConfiguration(
Expand Down Expand Up @@ -555,6 +566,7 @@ object UserConfiguration {
disableTestCodeLenses,
javaFormatConfig,
scalafixRulesDependencies,
customProjectRoot,
)
)
} else {
Expand Down
37 changes: 37 additions & 0 deletions tests/slow/src/test/scala/tests/CustomProjectRootLspSuite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package tests
import scala.meta.internal.metals.UserConfiguration
import scala.meta.internal.metals.{BuildInfo => V}

class CustomProjectRootLspSuite
extends BaseLspSuite("custom-project-root", BloopImportInitializer) {
override def userConfig: UserConfiguration =
UserConfiguration().copy(customProjectRoot = Some("inner/inner/"))

test("basic") {
cleanWorkspace()
for {
_ <- initialize(
s"""|/build.sbt
|scalaVersion := "${V.scala213}"
|/inner/inner/project.scala
|//> using scala ${V.scala3}
|/inner/inner/Main.scala
|package a
|
|val k = 1
|val m: Int = "aaa"
|""".stripMargin
)
_ <- server.didOpen("inner/inner/Main.scala")
_ = assert(server.server.bspSession.exists(_.main.isScalaCLI))
_ = assertEquals(
client.workspaceDiagnostics,
"""|inner/inner/Main.scala:4:14: error: Found: ("aaa" : String)
|Required: Int
|val m: Int = "aaa"
| ^^^^^
|""".stripMargin,
)
} yield ()
}
}
Loading