Scratch: finish execution if no new lines was executed
^KT-32062
This commit is contained in:
@@ -87,8 +87,6 @@ abstract class SequentialScratchExecutor(file: ScratchFile) : ScratchExecutor(fi
|
|||||||
|
|
||||||
protected abstract fun needProcessToStart(): Boolean
|
protected abstract fun needProcessToStart(): Boolean
|
||||||
|
|
||||||
private var lastExecuted = 0
|
|
||||||
|
|
||||||
fun start() {
|
fun start() {
|
||||||
EditorFactory.getInstance().eventMulticaster.addDocumentListener(listener, file.project.messageBus.connect())
|
EditorFactory.getInstance().eventMulticaster.addDocumentListener(listener, file.project.messageBus.connect())
|
||||||
|
|
||||||
@@ -102,18 +100,22 @@ abstract class SequentialScratchExecutor(file: ScratchFile) : ScratchExecutor(fi
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun executeNew() {
|
fun executeNew() {
|
||||||
|
val expressions = file.getExpressions()
|
||||||
|
if (wasExpressionExecuted(expressions.size)) return
|
||||||
|
|
||||||
handler.onStart(file)
|
handler.onStart(file)
|
||||||
|
|
||||||
for ((index, expression) in file.getExpressions().withIndex()) {
|
for ((index, expression) in expressions.withIndex()) {
|
||||||
if (index + 1 <= lastExecuted) continue
|
if (wasExpressionExecuted(index)) continue
|
||||||
|
|
||||||
executeStatement(expression)
|
executeStatement(expression)
|
||||||
lastExecuted = index + 1
|
lastExecuted = index
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun execute() {
|
override fun execute() {
|
||||||
if (needToRestartProcess()) {
|
if (needToRestartProcess()) {
|
||||||
lastExecuted = 0
|
resetLastExecutedIndex()
|
||||||
handler.clear(file)
|
handler.clear(file)
|
||||||
|
|
||||||
handler.onStart(file)
|
handler.onStart(file)
|
||||||
@@ -129,16 +131,13 @@ abstract class SequentialScratchExecutor(file: ScratchFile) : ScratchExecutor(fi
|
|||||||
|
|
||||||
fun getFirstNewExpression(): ScratchExpression? {
|
fun getFirstNewExpression(): ScratchExpression? {
|
||||||
val expressions = runReadAction { file.getExpressions() }
|
val expressions = runReadAction { file.getExpressions() }
|
||||||
if (lastExecuted in expressions.indices) {
|
val firstNewExpressionIndex = lastExecuted + 1
|
||||||
return expressions[lastExecuted]
|
if (firstNewExpressionIndex in expressions.indices) {
|
||||||
|
return expressions[firstNewExpressionIndex]
|
||||||
}
|
}
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun needToRestartProcess(): Boolean {
|
|
||||||
return lastExecuted > 0
|
|
||||||
}
|
|
||||||
|
|
||||||
private val listener = object : DocumentListener {
|
private val listener = object : DocumentListener {
|
||||||
override fun documentChanged(event: DocumentEvent) {
|
override fun documentChanged(event: DocumentEvent) {
|
||||||
if (event.newFragment.isBlank() && event.oldFragment.isBlank()) return
|
if (event.newFragment.isBlank() && event.oldFragment.isBlank()) return
|
||||||
@@ -154,13 +153,27 @@ abstract class SequentialScratchExecutor(file: ScratchFile) : ScratchExecutor(fi
|
|||||||
|
|
||||||
val changedLine = document.getLineNumber(event.offset)
|
val changedLine = document.getLineNumber(event.offset)
|
||||||
val changedExpression = file.getExpressionAtLine(changedLine) ?: return
|
val changedExpression = file.getExpressionAtLine(changedLine) ?: return
|
||||||
val changedExpressionIndex = file.getExpressions().indexOf(changedExpression) + 1
|
val changedExpressionIndex = file.getExpressions().indexOf(changedExpression)
|
||||||
if (changedExpressionIndex <= lastExecuted) {
|
if (wasExpressionExecuted(changedExpressionIndex)) {
|
||||||
lastExecuted = 0
|
resetLastExecutedIndex()
|
||||||
handler.clear(file)
|
handler.clear(file)
|
||||||
|
|
||||||
stopExecution()
|
stopExecution()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private var lastExecuted = -1
|
||||||
|
|
||||||
|
private fun needToRestartProcess(): Boolean {
|
||||||
|
return lastExecuted > -1
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun resetLastExecutedIndex() {
|
||||||
|
lastExecuted = -1
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun wasExpressionExecuted(index: Int): Boolean {
|
||||||
|
return index <= lastExecuted
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user