Skip to content

Commit

Permalink
refactor(middleware): improve RunCodeProcessor execution logic
Browse files Browse the repository at this point in the history
Update the RunCodeProcessor's execute function to handle code execution more effectively. The changes include:
- Refactor argument list to multiline for better readability.
- Add a condition to check if the code contains new lines before creating a new file.
- Use ReadAction.compute for finding the PsiFile to handle it in a read-safe context.
  • Loading branch information
phodal committed Oct 10, 2024
1 parent 6087c9b commit c4066f6
Showing 1 changed file with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ class RunCodeProcessor : PostProcessor {

override fun isApplicable(context: PostProcessorContext): Boolean = true

override fun execute(project: Project, context: PostProcessorContext, console: ConsoleView?, args: List<Any>): String {
override fun execute(
project: Project,
context: PostProcessorContext,
console: ConsoleView?,
args: List<Any>,
): String {
when (val code = context.pipeData["output"]) {
is VirtualFile -> {
LocalFileSystem.getInstance().refreshAndFindFileByPath(code.path)
Expand All @@ -38,11 +43,24 @@ class RunCodeProcessor : PostProcessor {
is String -> {
val ext = context.genTargetLanguage?.associatedFileType?.defaultExtension ?: "txt"
ApplicationManager.getApplication().invokeAndWait {
PsiFileFactory.getInstance(project).createFileFromText("temp.$ext", code).let { psiFile ->
if (psiFile.virtualFile == null) {
console?.print("Failed to create file for run\n", ERROR_OUTPUT)
} else {
doExecute(console, project, psiFile.virtualFile, psiFile)
if (code.contains("\n")) {
PsiFileFactory.getInstance(project).createFileFromText("temp.$ext", code).let { psiFile ->
if (psiFile.virtualFile == null) {
console?.print("Failed to create file for run\n", ERROR_OUTPUT)
} else {
doExecute(console, project, psiFile.virtualFile, psiFile)
}
}
} else {
val file = LocalFileSystem.getInstance().refreshAndFindFileByPath(code)
if (file != null) {
val psiFile = ReadAction.compute<PsiFile?, Throwable> {
PsiManager.getInstance(project).findFile(file)
}

psiFile?.let {
doExecute(console, project, file, psiFile)
}
}
}
}
Expand Down

0 comments on commit c4066f6

Please sign in to comment.