Skip to content

Commit

Permalink
Introduce class for setting up debug integration
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolay-egorov committed Feb 6, 2024
1 parent 064f001 commit ded9168
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.jetbrains.kotlinx.jupyter.debug

import kotlin.concurrent.thread

abstract class DebugIntegrationProvider(
protected val debugPort: Int?,
) {
companion object {
const val DEBUG_THREAD_NAME = "NotebookDebugThread"
}

fun provideDebugPort(): Int? = debugPort

fun provideSuspendedThread(runnable: () -> Unit): Thread {
return thread(start = provideDebugPort() != null, name = DEBUG_THREAD_NAME, block = runnable)
}
}

class NotebookDebugIntegrationProvider(
debugPort: Int?,
) : DebugIntegrationProvider(debugPort)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package org.jetbrains.kotlinx.jupyter.debug

object DebugIntegrationProviderMock : DebugIntegrationProvider(null)
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package org.jetbrains.kotlinx.jupyter.repl.creating
import org.jetbrains.kotlinx.jupyter.api.JupyterClientType
import org.jetbrains.kotlinx.jupyter.api.libraries.CommManager
import org.jetbrains.kotlinx.jupyter.config.logger
import org.jetbrains.kotlinx.jupyter.debug.DebugIntegrationProvider
import org.jetbrains.kotlinx.jupyter.debug.NotebookDebugIntegrationProvider
import org.jetbrains.kotlinx.jupyter.libraries.LibraryResolver
import org.jetbrains.kotlinx.jupyter.libraries.ResolutionInfoProvider
import org.jetbrains.kotlinx.jupyter.messaging.DisplayHandler
Expand Down Expand Up @@ -63,8 +65,10 @@ class DefaultReplComponentsProvider(
return _commManager
}

override fun provideDebugPort(): Int? {
return _settings.kernelConfig.debugPort
override fun provideDebugIntegration(): DebugIntegrationProvider {
return NotebookDebugIntegrationProvider(
_settings.kernelConfig.debugPort,
)
}

override fun provideExplicitClientType(): JupyterClientType? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.jetbrains.kotlinx.jupyter.repl.creating

import org.jetbrains.kotlinx.jupyter.api.JupyterClientType
import org.jetbrains.kotlinx.jupyter.api.libraries.CommManager
import org.jetbrains.kotlinx.jupyter.debug.DebugIntegrationProvider
import org.jetbrains.kotlinx.jupyter.libraries.LibrariesScanner
import org.jetbrains.kotlinx.jupyter.libraries.LibraryResolver
import org.jetbrains.kotlinx.jupyter.libraries.ResolutionInfoProvider
Expand Down Expand Up @@ -29,8 +30,10 @@ abstract class LazilyConstructibleReplComponentsProvider : ReplComponentsProvide
override val homeDir: File? by lazy { provideHomeDir() }
protected abstract fun provideHomeDir(): File?

override val debugPort: Int? by lazy { provideDebugPort() }
protected abstract fun provideDebugPort(): Int?
override val debugPort: Int? by lazy { debugIntegrationProvider.provideDebugPort() }

override val debugIntegrationProvider: DebugIntegrationProvider by lazy { provideDebugIntegration() }
protected abstract fun provideDebugIntegration(): DebugIntegrationProvider

override val mavenRepositories: List<MavenRepositoryCoordinates> by lazy { provideMavenRepositories() }
protected abstract fun provideMavenRepositories(): List<MavenRepositoryCoordinates>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.jetbrains.kotlinx.jupyter.repl.creating

import org.jetbrains.kotlinx.jupyter.api.JupyterClientType
import org.jetbrains.kotlinx.jupyter.api.libraries.CommManager
import org.jetbrains.kotlinx.jupyter.debug.DebugIntegrationProvider
import org.jetbrains.kotlinx.jupyter.libraries.LibrariesScanner
import org.jetbrains.kotlinx.jupyter.libraries.LibraryResolver
import org.jetbrains.kotlinx.jupyter.libraries.ResolutionInfoProvider
Expand All @@ -20,6 +21,7 @@ interface ReplComponentsProvider {
val scriptClasspath: List<File>
val homeDir: File?
val debugPort: Int?
val debugIntegrationProvider: DebugIntegrationProvider?
val mavenRepositories: List<MavenRepositoryCoordinates>
val libraryResolver: LibraryResolver?
val runtimeProperties: ReplRuntimeProperties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package org.jetbrains.kotlinx.jupyter.repl.creating
import org.jetbrains.kotlinx.jupyter.api.JupyterClientType
import org.jetbrains.kotlinx.jupyter.api.libraries.CommManager
import org.jetbrains.kotlinx.jupyter.config.defaultRuntimeProperties
import org.jetbrains.kotlinx.jupyter.debug.DebugIntegrationProvider
import org.jetbrains.kotlinx.jupyter.debug.DebugIntegrationProviderMock
import org.jetbrains.kotlinx.jupyter.libraries.EmptyResolutionInfoProvider
import org.jetbrains.kotlinx.jupyter.libraries.LibrariesScanner
import org.jetbrains.kotlinx.jupyter.libraries.LibraryResolver
Expand All @@ -28,6 +30,7 @@ abstract class ReplComponentsProviderBase : LazilyConstructibleReplComponentsPro
commManager,
explicitClientType,
librariesScanner,
debugIntegrationProvider,
)

override fun provideScriptClasspath() = emptyList<File>()
Expand All @@ -46,5 +49,5 @@ abstract class ReplComponentsProviderBase : LazilyConstructibleReplComponentsPro
override fun provideExplicitClientType(): JupyterClientType? = null

override fun provideCommunicationFacility(): JupyterCommunicationFacility = CommunicationFacilityMock
override fun provideDebugPort(): Int? = null
override fun provideDebugIntegration(): DebugIntegrationProvider = DebugIntegrationProviderMock
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import org.jetbrains.kotlinx.jupyter.api.libraries.Variable
import org.jetbrains.kotlinx.jupyter.codegen.FieldsProcessorInternal
import org.jetbrains.kotlinx.jupyter.codegen.ResultsRenderersProcessor
import org.jetbrains.kotlinx.jupyter.codegen.TextRenderersProcessorWithPreventingRecursion
import org.jetbrains.kotlinx.jupyter.debug.DebugIntegrationProvider
import org.jetbrains.kotlinx.jupyter.libraries.parseLibraryDescriptor
import org.jetbrains.kotlinx.jupyter.repl.EvalData
import org.jetbrains.kotlinx.jupyter.repl.ReplRuntimeProperties
Expand All @@ -37,7 +38,15 @@ class NotebookImpl(
override val commManager: CommManager,
private val explicitClientType: JupyterClientType?,
override val libraryLoader: LibraryLoader,
debugIntegrationProvider: DebugIntegrationProvider,
) : MutableNotebook {
private fun `$debugMethod`() {
while (true) {
Thread.sleep(1000)
}
}
private val debugThread = debugIntegrationProvider.provideSuspendedThread(::`$debugMethod`)

private val cells = hashMapOf<Int, MutableCodeCell>()
override var sharedReplContext: SharedReplContext? = null

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.jetbrains.kotlinx.jupyter.repl.creating

import org.jetbrains.kotlinx.jupyter.config.defaultRuntimeProperties
import org.jetbrains.kotlinx.jupyter.debug.DebugIntegrationProvider
import org.jetbrains.kotlinx.jupyter.debug.NotebookDebugIntegrationProvider
import org.jetbrains.kotlinx.jupyter.libraries.EmptyResolutionInfoProvider
import org.jetbrains.kotlinx.jupyter.libraries.LibraryResolver
import org.jetbrains.kotlinx.jupyter.libraries.ResolutionInfoProvider
Expand Down Expand Up @@ -37,7 +39,9 @@ fun createRepl(
override fun provideIsEmbedded() = isEmbedded
override fun provideDisplayHandler() = displayHandler
override fun provideCommunicationFacility(): JupyterCommunicationFacility = communicationFacility
override fun provideDebugPort(): Int? = debugPort
override fun provideDebugIntegration(): DebugIntegrationProvider {
return NotebookDebugIntegrationProvider(debugPort)
}
}
return componentsProvider.createRepl()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ abstract class AbstractReplTest {
override fun provideIsEmbedded() = false
override fun provideDisplayHandler() = displayHandlerProvider(notebook)
override fun provideCommunicationFacility() = CommunicationFacilityMock
override fun provideDebugPort(): Int? = null
}
return factory.createRepl()
}
Expand Down

0 comments on commit ded9168

Please sign in to comment.