Skip to content

Fix 'gp open' command to open files in JetBrains Client instead of the backend IDE #14182

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 1 commit into from
Oct 26, 2022
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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