Skip to content

Commit

Permalink
start MetalsLspService also for java projects
Browse files Browse the repository at this point in the history
  • Loading branch information
kasiaMarek committed Sep 6, 2023
1 parent 6e56778 commit 2634e82
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,14 @@ object MetalsEnrichments

implicit class XtensionAbsolutePathBuffers(path: AbsolutePath) {

def isScalaProject(extendSearchToScriptsAndSbt: Boolean = true): Boolean = {
val isScala: String => Boolean =
if (extendSearchToScriptsAndSbt) _.isScalaFilename else _.isScala
def isScalaProject(): Boolean =
containsProjectFilesSatisfying(_.isScala)
def isMetalsProject(): Boolean =
containsProjectFilesSatisfying(_.isScalaOrJavaFilename)

private def containsProjectFilesSatisfying(
fileNamePredicate: String => Boolean
): Boolean = {
val directoriesToCheck = Set("test", "src", "it")
def dirFilter(f: File) = directoriesToCheck(f.getName()) || f
.listFiles()
Expand All @@ -316,7 +321,7 @@ object MetalsEnrichments
): Boolean = {
file.listFiles().exists { file =>
if (file.isDirectory()) dirFilter(file) && isScalaDir(file)
else isScala(file.getName())
else fileNamePredicate(file.getName())
}
}
path.isDirectory && isScalaDir(path.toFile, dirFilter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class MetalsLspService(
folder: AbsolutePath,
folderVisibleName: Option[String],
headDoctor: HeadDoctor,
) extends Folder(folder, folderVisibleName, isKnownScalaProject = true)
) extends Folder(folder, folderVisibleName, isKnownMetalsProject = true)
with Cancelable
with TextDocumentService {
import serverInputs._
Expand Down Expand Up @@ -1980,7 +1980,7 @@ class MetalsLspService(
if (
!buildTools.isAutoConnectable
&& buildTools.loadSupported.isEmpty
&& folder.isScalaProject(extendSearchToScriptsAndSbt = false)
&& folder.isScalaProject()
) {
scalaCli.setupIDE(folder)
} else Future.successful(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class WorkspaceFolders(

private val folderServices: AtomicReference[WorkspaceFoldersServices] = {
val (scalaProjects, nonScalaProjects) =
initialFolders.partition(_.isScalaProject) match {
initialFolders.partition(_.isMetalsProject) match {
case (Nil, nonScala) => (List(nonScala.head), nonScala.tail)
case t => t
}
Expand All @@ -37,7 +37,7 @@ class WorkspaceFolders(
def shouldBeRemoved(folder: Folder) =
actualToRemove.exists(_.path == folder.path)

val (newScala, newNonScala) = toAdd.partition(_.isScalaProject)
val (newScala, newNonScala) = toAdd.partition(_.isMetalsProject)
val newServices = newScala.map(createService(_))
if (newServices.isEmpty && getFolderServices.forall(shouldBeRemoved)) {
shutdownMetals()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ class WorkspaceLspService(
focusedDocument = Some(path)
val service = getServiceForOpt(path)
.orElse {
if (path.isScalaFilename) {
if (path.filename.isScalaOrJavaFilename) {
getFolderForOpt(path, nonScalaProjects)
.map(workspaceFolders.convertToScalaProject)
} else None
Expand Down Expand Up @@ -542,13 +542,13 @@ class WorkspaceLspService(
params
.getEvent()
.getRemoved()
.map(Folder(_, isKnownScalaProject = false))
.map(Folder(_, isKnownMetalsProject = false))
.asScala
.toList,
params
.getEvent()
.getAdded()
.map(Folder(_, isKnownScalaProject = false))
.map(Folder(_, isKnownMetalsProject = false))
.asScala
.toList,
)
Expand Down Expand Up @@ -1271,11 +1271,11 @@ class WorkspaceLspService(
class Folder(
val path: AbsolutePath,
val visibleName: Option[String],
isKnownScalaProject: Boolean,
isKnownMetalsProject: Boolean,
) {
lazy val isScalaProject: Boolean =
isKnownScalaProject || path.resolve(".metals").exists || path
.isScalaProject()
lazy val isMetalsProject: Boolean =
isKnownMetalsProject || path.resolve(".metals").exists || path
.isMetalsProject()
def nameOrUri: String = visibleName.getOrElse(path.toString())
}

Expand All @@ -1285,12 +1285,12 @@ object Folder {
)
def apply(
folder: lsp4j.WorkspaceFolder,
isKnownScalaProject: Boolean,
isKnownMetalsProject: Boolean,
): Folder = {
val name = Option(folder.getName()) match {
case Some("") => None
case maybeValue => maybeValue
}
new Folder(folder.getUri().toAbsolutePath, name, isKnownScalaProject)
new Folder(folder.getUri().toAbsolutePath, name, isKnownMetalsProject)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,12 @@ class MetalsLanguageServer(
new Folder(
root.toAbsolutePath,
Some("root"),
isKnownScalaProject = true,
isKnownMetalsProject = true,
)
)
.toList
case head :: Nil => List(Folder(head, isKnownScalaProject = true))
case many => many.map(Folder(_, isKnownScalaProject = false))
case head :: Nil => List(Folder(head, isKnownMetalsProject = true))
case many => many.map(Folder(_, isKnownMetalsProject = false))
}
}

Expand Down Expand Up @@ -174,7 +174,7 @@ class MetalsLanguageServer(

val folderPathsWithScala =
folders.collect {
case folder if folder.isScalaProject => folder.path
case folder if folder.isMetalsProject => folder.path
} match {
case Nil =>
scribe.warn(
Expand Down

0 comments on commit 2634e82

Please sign in to comment.