Skip to content

Commit

Permalink
refactor(service): rename and update syntax error handling methods
Browse files Browse the repository at this point in the history
Rename and update the methods related to syntax error handling in the auto test service. The changes include:
- Renaming `tryFixSyntax` to `tryFixSyntaxError` for clarity.
- Renaming `syntaxAnalysis` to `collectSyntaxError` to better reflect the method's purpose.
- Updating the method invocations and comments accordingly.
  • Loading branch information
phodal committed May 24, 2024
1 parent b70de19 commit 0066505
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,15 +168,15 @@ class JavaAutoTestService : AutoTestService() {
}
}

override fun tryFixSyntax(outputFile: VirtualFile, project: Project) {
override fun tryFixSyntaxError(outputFile: VirtualFile, project: Project) {
val sourceFile =
runReadAction { PsiManager.getInstance(project).findFile(outputFile) as? PsiJavaFile } ?: return

val editor = FileEditorManager.getInstance(project).selectedTextEditor ?: return
DaemonCodeAnalyzer.getInstance(project).autoImportReferenceAtCursor(editor, sourceFile)
}

override fun syntaxAnalysis(outputFile: VirtualFile, project: Project, runAction: ((errors: List<String>) -> Unit)?) {
override fun collectSyntaxError(outputFile: VirtualFile, project: Project, runAction: ((errors: List<String>) -> Unit)?) {
val sourceFile = runReadAction { PsiManager.getInstance(project).findFile(outputFile) as? PsiJavaFile } ?: return
val collectPsiError = sourceFile.collectPsiError()
if (collectPsiError.isNotEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ class TestCodeGenTask(val request: TestCodeGenRequest) :

navigateTestFile(testContext.outputFile, request.project)

autoTestService?.syntaxAnalysis(testContext.outputFile, request.project) {
autoTestService.tryFixSyntax(testContext.outputFile, request.project)
autoTestService?.collectSyntaxError(testContext.outputFile, request.project) {
autoTestService.tryFixSyntaxError(testContext.outputFile, request.project)

if (it.isNotEmpty()) {
AutoDevNotifications.error(request.project, "Test has error: ${it.joinToString("\n")}")
Expand Down
20 changes: 18 additions & 2 deletions src/main/kotlin/cc/unitmesh/devti/provider/AutoTestService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,27 @@ abstract class AutoTestService : LazyExtensionInstance<AutoTestService>(), RunSe
*/
abstract fun lookupRelevantClass(project: Project, element: PsiElement): List<ClassContext>

open fun syntaxAnalysis(outputFile: VirtualFile, project: Project, runAction: ((errors: List<String>) -> Unit)?) {
/**
* This method is used to collect syntax errors from a given project and write them to an output file.
* It takes the output file, the project to check, and an optional action to be executed with the list of errors.
*
* @param outputFile The virtual file where the syntax errors will be written to.
* @param project The project to be analyzed for syntax errors.
* @param runAction An optional lambda function that takes a list of strings as its parameter, representing the syntax errors.
* If provided, this action is invoked with an empty list of errors, indicating no syntax errors were found.
*/
open fun collectSyntaxError(outputFile: VirtualFile, project: Project, runAction: ((errors: List<String>) -> Unit)?) {
runAction?.invoke(emptyList())
}

open fun tryFixSyntax(outputFile: VirtualFile, project: Project) {
/**
* Attempts to fix syntax errors in the given Kotlin file within the project.
* This method is designed to be overridden by subclasses to provide custom syntax error fixing logic.
*
* @param outputFile The virtual file that needs to have its syntax errors fixed.
* @param project The current project in which the file resides.
*/
open fun tryFixSyntaxError(outputFile: VirtualFile, project: Project) {

}

Expand Down

0 comments on commit 0066505

Please sign in to comment.