diff --git a/metals/src/main/scala/scala/meta/internal/metals/MetalsEnrichments.scala b/metals/src/main/scala/scala/meta/internal/metals/MetalsEnrichments.scala index caacc01a12a..83350fbc0e3 100644 --- a/metals/src/main/scala/scala/meta/internal/metals/MetalsEnrichments.scala +++ b/metals/src/main/scala/scala/meta/internal/metals/MetalsEnrichments.scala @@ -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() @@ -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) diff --git a/metals/src/main/scala/scala/meta/internal/metals/MetalsLspService.scala b/metals/src/main/scala/scala/meta/internal/metals/MetalsLspService.scala index ba75dbf032e..2cbdc31b310 100644 --- a/metals/src/main/scala/scala/meta/internal/metals/MetalsLspService.scala +++ b/metals/src/main/scala/scala/meta/internal/metals/MetalsLspService.scala @@ -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._ @@ -1980,7 +1980,7 @@ class MetalsLspService( if ( !buildTools.isAutoConnectable && buildTools.loadSupported.isEmpty - && folder.isScalaProject(extendSearchToScriptsAndSbt = false) + && folder.isScalaProject() ) { scalaCli.setupIDE(folder) } else Future.successful(()) diff --git a/metals/src/main/scala/scala/meta/internal/metals/WorkspaceFolders.scala b/metals/src/main/scala/scala/meta/internal/metals/WorkspaceFolders.scala index e27d3be5b7a..72018c184b1 100644 --- a/metals/src/main/scala/scala/meta/internal/metals/WorkspaceFolders.scala +++ b/metals/src/main/scala/scala/meta/internal/metals/WorkspaceFolders.scala @@ -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 } @@ -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() diff --git a/metals/src/main/scala/scala/meta/internal/metals/WorkspaceLspService.scala b/metals/src/main/scala/scala/meta/internal/metals/WorkspaceLspService.scala index 067f6c12de6..81ac8b7d68f 100644 --- a/metals/src/main/scala/scala/meta/internal/metals/WorkspaceLspService.scala +++ b/metals/src/main/scala/scala/meta/internal/metals/WorkspaceLspService.scala @@ -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 @@ -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, ) @@ -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()) } @@ -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) } } diff --git a/metals/src/main/scala/scala/meta/metals/MetalsLanguageServer.scala b/metals/src/main/scala/scala/meta/metals/MetalsLanguageServer.scala index 4f844381e16..a648fef166c 100644 --- a/metals/src/main/scala/scala/meta/metals/MetalsLanguageServer.scala +++ b/metals/src/main/scala/scala/meta/metals/MetalsLanguageServer.scala @@ -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)) } } @@ -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(