diff --git a/metals/src/main/scala/scala/meta/internal/metals/ActiveFiles.scala b/metals/src/main/scala/scala/meta/internal/metals/ActiveFiles.scala index 46e714819d9..e588e0e8302 100644 --- a/metals/src/main/scala/scala/meta/internal/metals/ActiveFiles.scala +++ b/metals/src/main/scala/scala/meta/internal/metals/ActiveFiles.scala @@ -27,4 +27,9 @@ final class ActiveFiles(time: Time) { def isRecentlyActive(path: AbsolutePath): Boolean = { paths.asScala.exists(p => p.isActive && p.path == path) } + + def pollRecent(): Option[AbsolutePath] = { + paths.removeIf(_.isStale) + Option(paths.poll()).map(_.path) + } } diff --git a/metals/src/main/scala/scala/meta/internal/metals/MetalsLanguageServer.scala b/metals/src/main/scala/scala/meta/internal/metals/MetalsLanguageServer.scala index 100f7dc2ca9..ba9f7ae4599 100644 --- a/metals/src/main/scala/scala/meta/internal/metals/MetalsLanguageServer.scala +++ b/metals/src/main/scala/scala/meta/internal/metals/MetalsLanguageServer.scala @@ -146,6 +146,7 @@ class MetalsLanguageServer( Option.empty[BspSession] private val savedFiles = new ActiveFiles(time) private val openedFiles = new ActiveFiles(time) + private val recentlyFocusedFiles = new ActiveFiles(time) private val languageClient = new DelegatingLanguageClient(NoopLanguageClient) var userConfig: UserConfiguration = UserConfiguration() val excludedPackageHandler: ExcludedPackagesHandler = @@ -928,6 +929,9 @@ class MetalsLanguageServer( @JsonNotification("textDocument/didOpen") def didOpen(params: DidOpenTextDocumentParams): CompletableFuture[Unit] = { val path = params.getTextDocument.getUri.toAbsolutePath + // In some cases like peeking definition didOpen might be followed up by close + // and we would lose the notion of the focused document + focusedDocument.foreach(recentlyFocusedFiles.add) focusedDocument = Some(path) openedFiles.add(path) @@ -1050,6 +1054,9 @@ class MetalsLanguageServer( @JsonNotification("textDocument/didClose") def didClose(params: DidCloseTextDocumentParams): Unit = { val path = params.getTextDocument.getUri.toAbsolutePath + if (focusedDocument.contains(path)) { + focusedDocument = recentlyFocusedFiles.pollRecent() + } buffers.remove(path) compilers.didClose(path) trees.didClose(path)