-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Fix for #262 - REPL does not start on windows * Rewrite repl manager * Accurate shutdown and restart
- Loading branch information
Showing
8 changed files
with
148 additions
and
190 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 14 additions & 102 deletions
116
src/main/kotlin/com/jetbrains/micropython/repl/MicroPythonReplManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,112 +1,24 @@ | ||
package com.jetbrains.micropython.repl | ||
|
||
import com.intellij.openapi.components.Service | ||
import com.intellij.openapi.module.Module | ||
import com.jediterm.terminal.TtyConnector | ||
import com.jetbrains.micropython.settings.MicroPythonFacet | ||
import com.jetbrains.micropython.settings.microPythonFacet | ||
import com.pty4j.PtyProcess | ||
import org.jetbrains.plugins.terminal.LocalTerminalDirectRunner | ||
import org.jetbrains.plugins.terminal.ShellStartupOptions | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.util.messages.Topic | ||
|
||
interface CommsEventListener { | ||
fun onProcessStarted(ttyConnector: TtyConnector) | ||
fun onProcessDestroyed() | ||
fun onProcessCreationFailed(reason: String) | ||
} | ||
|
||
@Service | ||
class MicroPythonReplManager(module: Module) { | ||
private val currentModule: Module = module | ||
private var listeners: MutableList<CommsEventListener> = mutableListOf() | ||
private var currentProcess: Process? = null | ||
private var currentConnector: TtyConnector? = null | ||
|
||
companion object { | ||
fun getInstance(module: Module): MicroPythonReplManager = | ||
module.getService(MicroPythonReplManager::class.java) | ||
} | ||
|
||
fun startREPL() { | ||
if (isRunning) { | ||
stopREPL() | ||
} | ||
|
||
val facet = currentModule.microPythonFacet ?: return | ||
val devicePath = facet.getOrDetectDevicePathSynchronously() | ||
|
||
if (facet.pythonPath == null) { | ||
notifyProcessCreationFailed("Valid Python interpreter is needed to start a REPL!") | ||
return | ||
} | ||
|
||
if (devicePath == null) { | ||
notifyProcessCreationFailed("Device path is not specified, please check settings.") | ||
return | ||
} | ||
|
||
val initialShellCommand = mutableListOf( | ||
facet.pythonPath!!, | ||
"${MicroPythonFacet.scriptsPath}/microrepl.py", | ||
devicePath | ||
) | ||
|
||
val terminalRunner = object : LocalTerminalDirectRunner(currentModule.project) { | ||
override fun getInitialCommand(envs: MutableMap<String, String>): MutableList<String> { | ||
return initialShellCommand | ||
} | ||
|
||
fun getTtyConnector(process: PtyProcess): TtyConnector { | ||
return this.createTtyConnector(process) | ||
} | ||
} | ||
|
||
synchronized(this) { | ||
val terminalOptions = ShellStartupOptions.Builder() | ||
.workingDirectory(devicePath) | ||
.shellCommand(initialShellCommand) | ||
.build() | ||
val process = terminalRunner.createProcess(terminalOptions) | ||
val ttyConnector = terminalRunner.getTtyConnector(process) | ||
|
||
currentProcess = process | ||
currentConnector = ttyConnector | ||
notifyProcessStarted(ttyConnector) | ||
} | ||
} | ||
|
||
fun stopREPL() { | ||
synchronized(this) { | ||
currentProcess?.let { | ||
it.destroy() | ||
notifyProcessDestroyed() | ||
} | ||
currentProcess = null | ||
} | ||
} | ||
|
||
val isRunning: Boolean | ||
get() = currentProcess?.isAlive ?: false | ||
|
||
fun addListener(listener: CommsEventListener) { | ||
listeners.add(listener) | ||
interface MicroPythonReplControl { | ||
fun stopRepl() | ||
fun startOrRestartRepl() | ||
} | ||
|
||
currentConnector?.let { listener.onProcessStarted(it) } | ||
} | ||
@Service(Service.Level.PROJECT) | ||
class MicroPythonReplManager(private val project: Project) : MicroPythonReplControl { | ||
override fun stopRepl() = | ||
project.messageBus.syncPublisher(MICROPYTHON_REPL_CONTROL).stopRepl() | ||
|
||
fun removeListener(listener: CommsEventListener) { | ||
listeners.remove(listener) | ||
} | ||
|
||
private fun notifyProcessStarted(connector: TtyConnector) { | ||
listeners.forEach { it.onProcessStarted(connector) } | ||
} | ||
override fun startOrRestartRepl() = | ||
project.messageBus.syncPublisher(MICROPYTHON_REPL_CONTROL).startOrRestartRepl() | ||
|
||
private fun notifyProcessDestroyed() { | ||
listeners.forEach { it.onProcessDestroyed() } | ||
} | ||
} | ||
|
||
private fun notifyProcessCreationFailed(reason: String) { | ||
listeners.forEach { it.onProcessCreationFailed(reason) } | ||
} | ||
} | ||
val MICROPYTHON_REPL_CONTROL = Topic(MicroPythonReplControl::class.java) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.