Skip to content

Fix threading issues with showDocument #2431

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions src/main/kotlin/com/sourcegraph/cody/agent/CodyAgentClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,19 @@ class CodyAgentClient(private val project: Project, private val webview: NativeW

@JsonRequest("textDocument/show")
fun textDocument_show(params: TextDocument_ShowParams): CompletableFuture<Boolean> {
return acceptOnEventThreadAndGet {
val selection = params.options?.selection
val preserveFocus = params.options?.preserveFocus
val vf = CodyEditorUtil.findFileOrScratch(project, params.uri)
if (vf != null) {
CodyEditorUtil.showDocument(project, vf, selection, preserveFocus)
true
} else {
false
}
}
val vf =
acceptOnEventThreadAndGet { CodyEditorUtil.findFileOrScratch(project, params.uri) }.get()

val result =
if (vf != null) {
val selection = params.options?.selection
val preserveFocus = params.options?.preserveFocus
CodyEditorUtil.showDocument(project, vf, selection, preserveFocus)
} else {
false
}

return CompletableFuture.completedFuture(result)
}

@JsonRequest("textDocument/openUntitledDocument")
Expand Down
15 changes: 8 additions & 7 deletions src/main/kotlin/com/sourcegraph/utils/CodyEditorUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import com.intellij.util.withScheme
import com.sourcegraph.cody.agent.protocol_extensions.toOffsetRange
import com.sourcegraph.cody.agent.protocol_generated.Range
import com.sourcegraph.config.ConfigUtil
import com.sourcegraph.utils.ThreadingUtil.runInEdtAndGet
import java.net.URI
import java.net.URISyntaxException
import kotlin.io.path.createDirectories
Expand Down Expand Up @@ -166,24 +167,24 @@ object CodyEditorUtil {
FileDocumentManager.getInstance().getFile(editor.document)

@JvmStatic
@RequiresEdt
fun showDocument(
project: Project,
vf: VirtualFile,
selection: Range? = null,
preserveFocus: Boolean? = false
): Boolean {
try {
if (selection == null) {
OpenFileDescriptor(project, vf).navigate(/* requestFocus= */ preserveFocus != true)
} else {
OpenFileDescriptor(
val descriptor =
if (selection == null) {
OpenFileDescriptor(project, vf)
} else {
OpenFileDescriptor(
project,
vf,
selection.start.line.toInt(),
/* logicalColumn= */ selection.start.character.toInt())
.navigate(/* requestFocus= */ preserveFocus != true)
}
}
runInEdtAndGet { descriptor.navigate(/* requestFocus= */ preserveFocus != true) }
return true
} catch (e: Exception) {
logger.error("Cannot switch view to file ${vf.path}", e)
Expand Down
7 changes: 6 additions & 1 deletion src/main/kotlin/com/sourcegraph/utils/ThreadingUtil.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package com.sourcegraph.utils

import com.intellij.openapi.application.ApplicationManager
import java.awt.EventQueue.invokeAndWait
import java.util.concurrent.CompletableFuture
import javax.swing.SwingUtilities.invokeAndWait

object ThreadingUtil {

fun <T> runInEdtAndGet(task: () -> T): T {
val app = ApplicationManager.getApplication()
if (app.isDispatchThread) {
return task()
}
val future = CompletableFuture<T>()
invokeAndWait {
try {
Expand Down
Loading