Skip to content

Commit

Permalink
Fix 'gp open' command to open files in JetBrains Client instead of th…
Browse files Browse the repository at this point in the history
…e backend IDE

(cherry picked from commit 9e1dac1)
  • Loading branch information
felladrin committed Oct 26, 2022
1 parent 9599a02 commit 9d00de1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 25 deletions.
3 changes: 2 additions & 1 deletion components/ide/jetbrains/backend-plugin/launch-dev-server.sh
Expand Up @@ -81,7 +81,8 @@ export IJ_HOST_SYSTEM_BASE_DIR=/workspace/.cache/JetBrains
export CWM_HOST_STATUS_OVER_HTTP_TOKEN=gitpod

# Build and move idea-cli, then overwrite environment variables initially defined by `components/ide/jetbrains/image/leeway.Dockerfile`
IDEA_CLI_DEV_PATH=$TEST_BACKEND_DIR/bin/idea-cli-dev
# Note: IDEA_CLI_DEV_PATH path needs to be the same string used in components/ide/jetbrains/cli/cmd/root.go
IDEA_CLI_DEV_PATH=/ide-desktop/bin/idea-cli-dev
(cd ../cli && go build -o $IDEA_CLI_DEV_PATH)
export EDITOR="$IDEA_CLI_DEV_PATH open"
export VISUAL="$EDITOR"
Expand Down
Expand Up @@ -6,7 +6,6 @@ package io.gitpod.jetbrains.remote

import com.intellij.codeWithMe.ClientId
import com.intellij.ide.BrowserUtil
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.client.ClientSession
import com.intellij.openapi.client.ClientSessionsManager
import com.intellij.openapi.components.service
Expand All @@ -24,10 +23,7 @@ import io.netty.channel.ChannelHandlerContext
import io.netty.handler.codec.http.FullHttpRequest
import io.netty.handler.codec.http.QueryStringDecoder
import io.prometheus.client.exporter.common.TextFormat
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlinx.coroutines.*
import org.jetbrains.ide.RestService
import org.jetbrains.io.response
import java.io.OutputStreamWriter
Expand Down Expand Up @@ -69,11 +65,7 @@ class GitpodCLIService : RestService() {
val file = parseFilePath(fileStr) ?: return "invalid file"
val shouldWait = getBooleanParameter("wait", urlDecoder)
return withClient(request, context) {
GlobalScope.launch {
withContext(Dispatchers.IO) {
cliHelperService.open(file, shouldWait)
}
}
cliHelperService.open(file, shouldWait)
}
}
if (operation == "preview") {
Expand Down Expand Up @@ -105,8 +97,8 @@ class GitpodCLIService : RestService() {
}
}

private fun withClient(request: FullHttpRequest, context: ChannelHandlerContext, action: (project: Project?) -> Unit): String? {
ApplicationManager.getApplication().executeOnPooledThread {
private fun withClient(request: FullHttpRequest, context: ChannelHandlerContext, action: suspend (project: Project?) -> Unit): String? {
GlobalScope.launch {
getClientSessionAndProjectAsync().let { (session, project) ->
ClientId.withClientId(session.clientId) {
action(project)
Expand All @@ -119,21 +111,21 @@ class GitpodCLIService : RestService() {

private data class ClientSessionAndProject(val session: ClientSession, val project: Project?)

private tailrec fun getClientSessionAndProjectAsync(): ClientSessionAndProject {
private suspend fun getClientSessionAndProjectAsync(): ClientSessionAndProject {
val project = getLastFocusedOrOpenedProject()
var session: ClientSession? = null
if (project != null) {
session = ClientSessionsManager.getProjectSessions(project, false).firstOrNull()
}
if (session == null) {
session = ClientSessionsManager.getAppSessions(false).firstOrNull()
}
return if (session != null) {
ClientSessionAndProject (session, project)
} else {
Thread.sleep(1000L)
getClientSessionAndProjectAsync()
while (session == null) {
if (project != null) {
session = ClientSessionsManager.getProjectSessions(project, false).firstOrNull()
}
if (session == null) {
session = ClientSessionsManager.getAppSessions(false).firstOrNull()
}
if (session == null) {
delay(1000L)
}
}
return ClientSessionAndProject(session, project)
}

private fun parseFilePath(path: String): Path? {
Expand Down

0 comments on commit 9d00de1

Please sign in to comment.