Scratch can be modified during execution (KT-30200)

^KT-30200 Fixed
This commit is contained in:
Natalia Selezneva
2019-03-01 13:28:49 +03:00
parent 0dbbd8e08c
commit 4a0574dc5f
2 changed files with 15 additions and 15 deletions
@@ -58,15 +58,16 @@ class KtCompilingExecutor(file: ScratchFile) : ScratchExecutor(file) {
private var backgroundProcessIndicator: ProgressIndicator? = null private var backgroundProcessIndicator: ProgressIndicator? = null
private val expressions = file.getExpressions()
override fun execute() { override fun execute() {
handler.onStart(file) handler.onStart(file)
val module = file.getModule()
val psiFile = file.getPsiFile() as? KtFile ?: return errorOccurs("Couldn't find KtFile for current editor", isFatal = true) val psiFile = file.getPsiFile() as? KtFile ?: return errorOccurs("Couldn't find KtFile for current editor", isFatal = true)
if (!checkForErrors(psiFile)) return if (!checkForErrors(psiFile)) return
val result = runReadAction { KtScratchSourceFileProcessor().process(file) } val result = runReadAction { KtScratchSourceFileProcessor().process(expressions) }
when (result) { when (result) {
is KtScratchSourceFileProcessor.Result.Error -> return errorOccurs(result.message, isFatal = true) is KtScratchSourceFileProcessor.Result.Error -> return errorOccurs(result.message, isFatal = true)
is KtScratchSourceFileProcessor.Result.OK -> { is KtScratchSourceFileProcessor.Result.OK -> {
@@ -88,7 +89,7 @@ class KtCompilingExecutor(file: ScratchFile) : ScratchExecutor(file) {
) ?: return ) ?: return
try { try {
val commandLine = createCommandLine(psiFile, module, result.mainClassName, tempDir.path) val commandLine = createCommandLine(psiFile, file.getModule(), result.mainClassName, tempDir.path)
LOG.printDebugMessage(commandLine.commandLineString) LOG.printDebugMessage(commandLine.commandLineString)
@@ -219,7 +220,7 @@ class KtCompilingExecutor(file: ScratchFile) : ScratchExecutor(file) {
val errorText = DefaultErrorMessages.render(diagnostic) val errorText = DefaultErrorMessages.render(diagnostic)
if (psiFile == scratchPsiFile) { if (psiFile == scratchPsiFile) {
if (diagnostic.psiElement.containingFile == psiFile) { if (diagnostic.psiElement.containingFile == psiFile) {
val scratchExpression = file.findExpression(diagnostic.psiElement) val scratchExpression = expressions.findExpression(diagnostic.psiElement)
if (scratchExpression == null) { if (scratchExpression == null) {
LOG.error("Couldn't find expression to report error: ${diagnostic.psiElement.getElementTextWithContext()}") LOG.error("Couldn't find expression to report error: ${diagnostic.psiElement.getElementTextWithContext()}")
handler.error(file, errorText) handler.error(file, errorText)
@@ -241,13 +242,13 @@ class KtCompilingExecutor(file: ScratchFile) : ScratchExecutor(file) {
} }
} }
private fun ScratchFile.findExpression(psiElement: PsiElement): ScratchExpression? { private fun List<ScratchExpression>.findExpression(psiElement: PsiElement): ScratchExpression? {
val elementLine = psiElement.getLineNumber() val elementLine = psiElement.getLineNumber()
return runReadAction { getExpressions().firstOrNull { elementLine in it.lineStart..it.lineEnd } } return runReadAction { firstOrNull { elementLine in it.lineStart..it.lineEnd } }
} }
private fun ScratchFile.findExpression(lineStart: Int, lineEnd: Int): ScratchExpression? { private fun List<ScratchExpression>.findExpression(lineStart: Int, lineEnd: Int): ScratchExpression? {
return runReadAction { getExpressions().firstOrNull { it.lineStart == lineStart && it.lineEnd == lineEnd } } return runReadAction { firstOrNull { it.lineStart == lineStart && it.lineEnd == lineEnd } }
} }
private inner class ProcessOutputParser { private inner class ProcessOutputParser {
@@ -278,11 +279,11 @@ class KtCompilingExecutor(file: ScratchFile) : ScratchExecutor(file) {
val extractedLineInfo = extractLineInfoFrom(lineWoPrefix) val extractedLineInfo = extractLineInfoFrom(lineWoPrefix)
?: return errorOccurs("Couldn't extract line info from line: $lineWoPrefix", isFatal = true) ?: return errorOccurs("Couldn't extract line info from line: $lineWoPrefix", isFatal = true)
val (startLine, endLine) = extractedLineInfo val (startLine, endLine) = extractedLineInfo
val scratchExpression = file.findExpression(startLine, endLine) val scratchExpression = expressions.findExpression(startLine, endLine)
if (scratchExpression == null) { if (scratchExpression == null) {
LOG.error( LOG.error(
"Couldn't find expression with start line = $startLine, end line = $endLine.\n" + "Couldn't find expression with start line = $startLine, end line = $endLine.\n" +
file.getExpressions().joinToString("\n") expressions.joinToString("\n")
) )
} else { } else {
userOutput.forEach { output -> userOutput.forEach { output ->
@@ -20,7 +20,6 @@ import org.jetbrains.kotlin.diagnostics.rendering.Renderers
import org.jetbrains.kotlin.diagnostics.rendering.RenderingContext import org.jetbrains.kotlin.diagnostics.rendering.RenderingContext
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
import org.jetbrains.kotlin.idea.scratch.ScratchExpression import org.jetbrains.kotlin.idea.scratch.ScratchExpression
import org.jetbrains.kotlin.idea.scratch.ScratchFile
import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.*
class KtScratchSourceFileProcessor { class KtScratchSourceFileProcessor {
@@ -35,11 +34,11 @@ class KtScratchSourceFileProcessor {
const val GET_RES_FUN_NAME_PREFIX = "generated_get_instance_res" const val GET_RES_FUN_NAME_PREFIX = "generated_get_instance_res"
} }
fun process(file: ScratchFile): Result { fun process(expressions: List<ScratchExpression>): Result {
val sourceProcessor = KtSourceProcessor() val sourceProcessor = KtSourceProcessor()
file.getExpressions().forEach { expressions.forEach {
sourceProcessor.process(it) sourceProcessor.process(it)
} }
val codeResult = val codeResult =
""" """