From 1ad9f07f4dca8d8dbeaaf117761943da0e30bde1 Mon Sep 17 00:00:00 2001 From: Katarzyna Marek Date: Tue, 10 Oct 2023 17:37:05 +0200 Subject: [PATCH] bugfix: auto-connect to build server --- .../internal/metals/MetalsLspService.scala | 7 +++++- .../metals/UserConfigurationSync.scala | 23 +++++++++++++++---- .../internal/metals/WorkspaceFolders.scala | 4 ++-- .../internal/metals/WorkspaceLspService.scala | 2 +- 4 files changed, 27 insertions(+), 9 deletions(-) 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 c116578ebf7..3b828364078 100644 --- a/metals/src/main/scala/scala/meta/internal/metals/MetalsLspService.scala +++ b/metals/src/main/scala/scala/meta/internal/metals/MetalsLspService.scala @@ -916,9 +916,14 @@ class MetalsLspService( cancel() } - def onUserConfigUpdate(newConfig: UserConfiguration): Future[Unit] = { + def setUserConfig(newConfig: UserConfiguration): UserConfiguration = { val old = userConfig userConfig = newConfig + old + } + + def onUserConfigUpdate(newConfig: UserConfiguration): Future[Unit] = { + val old = setUserConfig(newConfig) if (userConfig.excludedPackages != old.excludedPackages) { excludedPackageHandler = ExcludedPackagesHandler.fromUserConfiguration( userConfig.excludedPackages.getOrElse(Nil) diff --git a/metals/src/main/scala/scala/meta/internal/metals/UserConfigurationSync.scala b/metals/src/main/scala/scala/meta/internal/metals/UserConfigurationSync.scala index 790932de83b..a14d18ab583 100644 --- a/metals/src/main/scala/scala/meta/internal/metals/UserConfigurationSync.scala +++ b/metals/src/main/scala/scala/meta/internal/metals/UserConfigurationSync.scala @@ -23,14 +23,26 @@ class UserConfigurationSync( out <- Option(workspace.getConfiguration()) } yield out.booleanValue()).getOrElse(false) - def syncUserConfiguration(services: List[MetalsLspService]): Future[Unit] = - optSyncUserConfiguration(services).getOrElse(Future.successful(())) + def initSyncUserConfiguration( + services: List[MetalsLspService] + ): Future[Unit] = + optSyncUserConfiguration( + services, + folder => + newConfig => { + folder.setUserConfig(newConfig) + Future.unit + }, + ).getOrElse(Future.successful(())) def onDidChangeConfiguration( params: lsp4j.DidChangeConfigurationParams, services: List[MetalsLspService], ): Future[Unit] = - optSyncUserConfiguration(services) + optSyncUserConfiguration( + services, + folder => newConfig => folder.onUserConfigUpdate(newConfig), + ) .orElse { val fullJson = params.getSettings.asInstanceOf[JsonElement].getAsJsonObject @@ -44,7 +56,8 @@ class UserConfigurationSync( .getOrElse(Future.successful(())) private def optSyncUserConfiguration( - services: List[MetalsLspService] + services: List[MetalsLspService], + consume: MetalsLspService => UserConfiguration => Future[Unit], ): Option[Future[Unit]] = Option.when(supportsConfiguration) { val items = services.map { service => @@ -63,7 +76,7 @@ class UserConfigurationSync( case (item, folder) => val json = item.asInstanceOf[JsonElement].getAsJsonObject() userConfigFrom(json) - .map(folder.onUserConfigUpdate) + .map(consume(folder)) .getOrElse(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 e60d5dc971f..dc21e6bcf44 100644 --- a/metals/src/main/scala/scala/meta/internal/metals/WorkspaceFolders.scala +++ b/metals/src/main/scala/scala/meta/internal/metals/WorkspaceFolders.scala @@ -62,7 +62,7 @@ class WorkspaceFolders( val services = newServices.filterNot(isIn(prev, _)) for { - _ <- userConfigSync.syncUserConfiguration(services) + _ <- userConfigSync.initSyncUserConfiguration(services) _ <- Future.sequence(services.map(_.initialized())) _ <- Future(prev.filter(shouldBeRemoved).foreach(_.onShutdown())) } yield () @@ -86,7 +86,7 @@ class WorkspaceFolders( case None => setupLogger() userConfigSync - .syncUserConfiguration(List(newService)) + .initSyncUserConfiguration(List(newService)) .map(_ => newService.initialized()) newService } 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 df151d659ab..849674a6a39 100644 --- a/metals/src/main/scala/scala/meta/internal/metals/WorkspaceLspService.scala +++ b/metals/src/main/scala/scala/meta/internal/metals/WorkspaceLspService.scala @@ -1112,7 +1112,7 @@ class WorkspaceLspService( def initialized(): Future[Unit] = { statusBar.start(sh, 0, 1, ju.concurrent.TimeUnit.SECONDS) for { - _ <- userConfigSync.syncUserConfiguration(folderServices) + _ <- userConfigSync.initSyncUserConfiguration(folderServices) _ <- Future.sequence(folderServices.map(_.initialized())) _ <- Future(startHttpServer()) } yield ()