Skip to content

Commit 4ce87a2

Browse files
committed
feat(debug): better synchronization between test view and execution in intellij-client, part 2
1 parent ec2422c commit 4ce87a2

File tree

4 files changed

+39
-28
lines changed

4 files changed

+39
-28
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package dev.robotcode.robotcode4ij.debugging
2+
3+
import org.eclipse.lsp4j.debug.CancelArguments
4+
import org.eclipse.lsp4j.debug.services.IDebugProtocolServer
5+
import org.eclipse.lsp4j.jsonrpc.services.JsonRequest
6+
import java.util.concurrent.CompletableFuture
7+
8+
interface IRobotCodeDebugProtocolServer: IDebugProtocolServer {
9+
@JsonRequest("robot/sync") fun robotSync(): CompletableFuture<Void?>? {
10+
throw UnsupportedOperationException()
11+
}
12+
}

intellij-client/src/main/kotlin/dev/robotcode/robotcode4ij/debugging/RobotCodeDebugProtocolClient.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package dev.robotcode.robotcode4ij.debugging
22

33
import com.intellij.openapi.diagnostic.thisLogger
44
import com.jetbrains.rd.util.reactive.Signal
5+
import kotlinx.coroutines.future.await
56
import org.eclipse.lsp4j.debug.ExitedEventArguments
67
import org.eclipse.lsp4j.debug.OutputEventArguments
78
import org.eclipse.lsp4j.debug.StoppedEventArguments
@@ -142,6 +143,8 @@ data class RobotLogMessageEventArguments(
142143
)
143144

144145
@Suppress("unused") class RobotCodeDebugProtocolClient : IDebugProtocolClient {
146+
var server: IRobotCodeDebugProtocolServer? = null
147+
145148
var onStopped = Signal<StoppedEventArguments>()
146149
val onTerminated = Signal<TerminatedEventArguments?>()
147150
val onExited = Signal<ExitedEventArguments?>()
@@ -170,39 +173,46 @@ data class RobotLogMessageEventArguments(
170173
onStopped.fire(args)
171174
}
172175

173-
@JsonNotification("robotEnqueued") fun robotEnqueued(args: RobotEnqueuedArguments) {
176+
@JsonNotification("robotEnqueued") fun robotEnqueued(args: RobotEnqueuedArguments) {
174177
thisLogger().trace("robotEnqueued")
175178
onRobotEnqueued.fire(args)
179+
server?.robotSync()
176180
}
177181

178182
@JsonNotification("robotStarted") fun robotStarted(args: RobotExecutionEventArguments) {
179183
thisLogger().trace("robotStarted $args")
180184
onRobotStarted.fire(args)
185+
server?.robotSync()
181186
}
182187

183188
@JsonNotification("robotEnded") fun robotEnded(args: RobotExecutionEventArguments) {
184189
thisLogger().trace("robotEnded $args")
185190
onRobotEnded.fire(args)
191+
server?.robotSync()
186192
}
187193

188194
@JsonNotification("robotSetFailed") fun robotSetFailed(args: RobotExecutionEventArguments) {
189195
thisLogger().trace("robotSetFailed $args")
190196
onRobotSetFailed.fire(args)
197+
server?.robotSync()
191198
}
192199

193200
@JsonNotification("robotExited") fun robotExited(args: RobotExitedEventArguments) {
194201
thisLogger().trace("robotExited")
195202
onRobotExited.fire(args)
203+
server?.robotSync()
196204
}
197205

198206
@JsonNotification("robotLog") fun robotLog(args: RobotLogMessageEventArguments) {
199207
thisLogger().trace("robotLog")
200208
onRobotLog.fire(args)
209+
server?.robotSync()
201210
}
202211

203212
@JsonNotification("robotMessage") fun robotMessage(args: RobotLogMessageEventArguments) {
204213
thisLogger().trace("robotMessage")
205214
onRobotMessage.fire(args)
215+
server?.robotSync()
206216
}
207217

208218
override fun output(args: OutputEventArguments) {

intellij-client/src/main/kotlin/dev/robotcode/robotcode4ij/execution/RobotCodeRunProfileState.kt

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import com.intellij.openapi.util.Ref
2323
import com.jetbrains.rd.util.reactive.Signal
2424
import com.jetbrains.rd.util.reactive.adviseEternal
2525
import dev.robotcode.robotcode4ij.buildRobotCodeCommandLine
26+
import dev.robotcode.robotcode4ij.debugging.IRobotCodeDebugProtocolServer
2627
import dev.robotcode.robotcode4ij.debugging.RobotCodeDebugProgramRunner
2728
import dev.robotcode.robotcode4ij.debugging.RobotCodeDebugProtocolClient
2829
import dev.robotcode.robotcode4ij.utils.NetUtils.findFreePort
@@ -35,9 +36,7 @@ import kotlinx.coroutines.withContext
3536
import kotlinx.coroutines.withTimeout
3637
import org.eclipse.lsp4j.debug.ConfigurationDoneArguments
3738
import org.eclipse.lsp4j.debug.InitializeRequestArguments
38-
import org.eclipse.lsp4j.debug.launch.DSPLauncher
39-
import org.eclipse.lsp4j.debug.services.IDebugProtocolServer
40-
import org.eclipse.lsp4j.jsonrpc.Launcher
39+
import org.eclipse.lsp4j.jsonrpc.debug.DebugLauncher
4140
import java.net.Socket
4241
import java.net.SocketTimeoutException
4342
import kotlin.uuid.ExperimentalUuidApi
@@ -54,7 +53,7 @@ class RobotCodeRunProfileState(private val config: RobotCodeRunConfiguration, en
5453
}
5554

5655
val debugClient = RobotCodeDebugProtocolClient()
57-
lateinit var debugServer: IDebugProtocolServer
56+
lateinit var debugServer: IRobotCodeDebugProtocolServer
5857
var isInitialized = false
5958
private set
6059
var isConfigurationDone = false
@@ -63,7 +62,6 @@ class RobotCodeRunProfileState(private val config: RobotCodeRunConfiguration, en
6362
val afterInitialize = Signal<Unit>()
6463
val afterConfigurationDone = Signal<Unit>()
6564

66-
6765
init {
6866
debugClient.onTerminated.adviseEternal {
6967
if (socket.isConnected) socket.close()
@@ -142,8 +140,8 @@ class RobotCodeRunProfileState(private val config: RobotCodeRunConfiguration, en
142140
consoleProperties.state = this
143141
}
144142

145-
var splitterPropertyName = SMTestRunnerConnectionUtil.getSplitterPropertyName(TESTFRAMEWORK_NAME)
146-
var consoleView = RobotCodeRunnerConsoleView(consoleProperties, splitterPropertyName)
143+
val splitterPropertyName = SMTestRunnerConnectionUtil.getSplitterPropertyName(TESTFRAMEWORK_NAME)
144+
val consoleView = RobotCodeRunnerConsoleView(consoleProperties, splitterPropertyName)
147145
SMTestRunnerConnectionUtil.initConsoleView(consoleView, TESTFRAMEWORK_NAME)
148146
consoleView.attachToProcess(processHandler)
149147
consoleRef.set(consoleView)
@@ -192,17 +190,22 @@ class RobotCodeRunProfileState(private val config: RobotCodeRunConfiguration, en
192190
@OptIn(ExperimentalUuidApi::class) override fun startNotified(event: ProcessEvent) {
193191
runBlocking(Dispatchers.IO) {
194192

195-
var port = event.processHandler.getUserData(DEBUG_PORT) ?: throw CantRunException("No debug port found.")
193+
val port = event.processHandler.getUserData(DEBUG_PORT) ?: throw CantRunException("No debug port found.")
196194

197195
socket = tryConnectToServerWithTimeout("127.0.0.1", port, 10000, retryIntervalMillis = 100)
198196
?: throw CantRunException("Unable to establish connection to debug server.")
199197

200-
val launcher: Launcher<IDebugProtocolServer> =
201-
DSPLauncher.createClientLauncher(debugClient, socket.getInputStream(), socket.getOutputStream())
198+
val launcher = DebugLauncher.createLauncher(
199+
debugClient,
200+
IRobotCodeDebugProtocolServer::class.java,
201+
socket.getInputStream(),
202+
socket.getOutputStream()
203+
);
202204

203205
launcher.startListening()
204206

205207
debugServer = launcher.remoteProxy
208+
debugClient.server = debugServer
206209

207210
val arguments = InitializeRequestArguments().apply {
208211
clientID = Uuid.random().toString()

intellij-client/src/main/kotlin/dev/robotcode/robotcode4ij/execution/RobotOutputToGeneralTestEventsConverter.kt

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import com.intellij.util.Urls.newLocalFileUrl
99
import com.intellij.util.Urls.newUrl
1010
import com.jetbrains.rd.util.lifetime.Lifetime
1111
import com.jetbrains.rd.util.reactive.adviseEternal
12-
import com.jetbrains.rd.util.threading.coroutines.adviseSuspend
1312
import dev.robotcode.robotcode4ij.debugging.RobotExecutionEventArguments
1413
import jetbrains.buildServer.messages.serviceMessages.ServiceMessage
1514
import jetbrains.buildServer.messages.serviceMessages.ServiceMessageVisitor
@@ -111,24 +110,11 @@ import org.eclipse.lsp4j.debug.OutputEventArgumentsCategory
111110
}
112111
}
113112

114-
consoleProperties.state?.debugClient?.onRobotStarted?.adviseSuspend(
115-
Lifetime.Eternal,
116-
myContext,
117-
this::robotStarted
113+
consoleProperties.state?.debugClient?.onRobotStarted?.advise( Lifetime.Eternal, this::robotStarted
118114
)
119-
consoleProperties.state?.debugClient?.onRobotEnded?.adviseSuspend(Lifetime.Eternal, myContext, this::robotEnded)
115+
consoleProperties.state?.debugClient?.onRobotEnded?.advise(Lifetime.Eternal, this::robotEnded)
120116

121-
// TODO: Implement this
122-
// consoleProperties.state?.debugClient?.onRobotLog?.adviseEternal { args ->
123-
// val msg = ServiceMessageBuilder.testStdOut("blah")
124-
//
125-
// msg.addAttribute("nodeId", args.itemId ?: "0").addAttribute(
126-
// "out", "[${args.level}] ${args.message}\n"
127-
// )
128-
// this.processServiceMessageFromRobot(msg)
129-
// }
130-
131-
consoleProperties.state?.debugClient?.onOutput?.adviseSuspend(Lifetime.Eternal, myContext) { args ->
117+
consoleProperties.state?.debugClient?.onOutput?.advise(Lifetime.Eternal) { args ->
132118
val msg =
133119
if (args.category == OutputEventArgumentsCategory.STDERR) ServiceMessageBuilder.testStdErr(args.category)
134120
else ServiceMessageBuilder.testStdOut(args.category)

0 commit comments

Comments
 (0)