Skip to content

Commit

Permalink
feat(middleware): enhance code execution, file saving and verification
Browse files Browse the repository at this point in the history
…#24

Enhanced the RunCodeProcessor to handle both VirtualFile and String types of code. Improved SaveFileProcessor to throw an exception when file saving fails and refresh the project directory after saving. Updated VerifyCodeProcessor to handle code verification for VirtualFile type of code and print syntax errors if any.
  • Loading branch information
phodal committed Jun 27, 2024
1 parent 55f7495 commit ed7ca3b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package com.phodal.shirecore.middleware.builtin

import com.intellij.execution.ui.ConsoleView
import com.intellij.execution.ui.ConsoleViewContentType.*
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.LocalFileSystem
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiFileFactory
import com.intellij.psi.PsiManager
import com.phodal.shirecore.middleware.BuiltinPostHandler
import com.phodal.shirecore.middleware.PostCodeHandleContext
import com.phodal.shirecore.middleware.PostProcessor
import com.phodal.shirecore.provider.shire.FileRunService

class RunCodeProcessor : PostProcessor {
override val processorName: String = BuiltinPostHandler.RunCode.handleName
Expand All @@ -14,10 +21,45 @@ class RunCodeProcessor : PostProcessor {
}

override fun execute(project: Project, context: PostCodeHandleContext, console: ConsoleView?): String {
// return context.currentFile?.virtualFile?.let {
// FileRunService.provider(project, it)?.runFile(project, it, context.currentElement)
// } ?: ""
when (val code = context.pipeData["output"]) {
is VirtualFile -> {
LocalFileSystem.getInstance().refreshAndFindFileByPath(code.path)
PsiManager.getInstance(project).findFile(code)?.let { psiFile ->
doExecute(console, project, code, psiFile)
return ""
}
}

is String -> {
val ext = context.targetLanguage?.associatedFileType?.defaultExtension ?: "txt"
PsiFileFactory.getInstance(project).createFileFromText("temp.$ext", code).let { psiFile ->
val file = psiFile.virtualFile

doExecute(console, project, file, psiFile)

return ""
}
}
}

console?.print("No code to run\n", ERROR_OUTPUT)
return ""
}

private fun doExecute(
console: ConsoleView?,
project: Project,
file: VirtualFile,
psiFile: PsiFile,
) {
val fileRunService = FileRunService.provider(project, file)
if (fileRunService == null) {
console?.print("No run service found\n", ERROR_OUTPUT)
return
}

console?.print("Running code...\n", SYSTEM_OUTPUT)
val output = fileRunService.runFile(project, file, psiFile)
console?.print(output ?: "", NORMAL_OUTPUT)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@ class SaveFileProcessor : PostProcessor {
outputFile?.setBinaryContent(content?.toByteArray() ?: ByteArray(0))

outputFile
}
} ?: throw IllegalStateException("Failed to save file")

context.pipeData["output"] = outputFile?.canonicalPath ?: ""
context.pipeData["output"] = outputFile

console?.print("Saved to ${outputFile?.canonicalPath}\n", ConsoleViewContentType.SYSTEM_OUTPUT)
return outputFile?.path ?: ""
// refresh index
project.guessProjectDir()?.refresh(true, true)

console?.print("Saved to ${outputFile.canonicalPath}\n", ConsoleViewContentType.SYSTEM_OUTPUT)
return outputFile.path ?: ""
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.phodal.shirecore.middleware.builtin
import com.intellij.codeInsight.daemon.DaemonCodeAnalyzer
import com.intellij.codeInsight.daemon.impl.DaemonCodeAnalyzerEx
import com.intellij.execution.ui.ConsoleView
import com.intellij.execution.ui.ConsoleViewContentType
import com.intellij.lang.annotation.HighlightSeverity
import com.intellij.openapi.Disposable
import com.intellij.openapi.application.runReadAction
Expand All @@ -26,15 +27,29 @@ class VerifyCodeProcessor : PostProcessor {
}

override fun execute(project: Project, context: PostCodeHandleContext, console: ConsoleView?): String {
if (context.currentFile == null) {
val code = context.pipeData["output"]
if (code !is VirtualFile) {
console?.print("No code to verify\n", ConsoleViewContentType.ERROR_OUTPUT)
return ""
}

val psiFile = PsiManager.getInstance(project).findFile(code)
if (psiFile == null) {
console?.print("No code to verify\n", ConsoleViewContentType.ERROR_OUTPUT)
return ""
}

var errors: List<String> = listOf()
collectSyntaxError<PsiFile>(context.currentFile!!.virtualFile, project) {
collectSyntaxError<PsiFile>(psiFile.virtualFile, project) {
errors = it
}

if (errors.isNotEmpty()) {
console?.print("Syntax errors found:\n${errors.joinToString("\n")}\n", ConsoleViewContentType.ERROR_OUTPUT)
} else {
console?.print("No syntax errors found\n", ConsoleViewContentType.SYSTEM_OUTPUT)
}

return errors.joinToString("\n")
}

Expand Down

0 comments on commit ed7ca3b

Please sign in to comment.