Skip to content

Commit

Permalink
Revert to recently focused document if the current one was immedietal…
Browse files Browse the repository at this point in the history
…ly closed

Some clients when using peek functionality might send didOpen and didClose notifications immediately one after the other. In this case we don't actually want to change the focused document so we revert to the one that was most recently focused.
  • Loading branch information
tgodzik committed Feb 17, 2021
1 parent 3e6cedf commit e1921d4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit e1921d4

Please sign in to comment.