Skip to content

Commit

Permalink
fix(devins-lang): remove unused imports and refactor compiler creatio…
Browse files Browse the repository at this point in the history
…n logic

The commit removes unused `com.intellij.openapi.editor.Editor` and `com.intellij.psi.PsiFile` imports and refactors the compiler creation logic into a private function `createCompiler` for better code readability and maintainability.
  • Loading branch information
phodal committed Mar 29, 2024
1 parent d9a4762 commit 2229789
Showing 1 changed file with 29 additions and 23 deletions.
Expand Up @@ -5,11 +5,9 @@ import cc.unitmesh.devti.language.compiler.DevInsCompiler
import cc.unitmesh.devti.language.psi.DevInFile
import cc.unitmesh.devti.provider.devins.CustomAgentContext
import cc.unitmesh.devti.provider.devins.LanguagePromptProcessor
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiWhiteSpace
import com.intellij.psi.util.PsiUtilBase

Expand All @@ -18,46 +16,54 @@ class DevInsCustomAgentResponse : LanguagePromptProcessor {
override val name: String = "DevIn"

override fun execute(project: Project, context: CustomAgentContext): String {
val devInFile = DevInFile.fromString(project, context.response)
val devInsCompiler = DevInsCompiler(project, devInFile)
val devInsCompiler = createCompiler(project, context.response)

val result = devInsCompiler.compile()
AutoDevNotifications.notify(project, result.output)
return result.output
}

private fun getCurrentPsiFile(project: Project, editor: Editor): PsiFile? {
return PsiUtilBase.getPsiFileInEditor(editor, project)
}

private fun getElementAtOffset(psiFile: PsiElement, offset: Int): PsiElement? {
// 获取偏移量对应的元素
var element = psiFile.findElementAt(offset) ?: return null
override fun compile(project: Project, text: String): String {
val devInsCompiler = createCompiler(project, text)

// 如果元素是空白元素,尝试获取其父元素
if (element is PsiWhiteSpace) {
element = element.getParent()
val result = devInsCompiler.compile()
return if (result.hasError || result.isLocalCommand) {
text
} else {
result.output
}

return element
}

override fun compile(project: Project, text: String): String {
/**
* Creates a new instance of `DevInsCompiler`.
*
* @param project The current project.
* @param text The source code text.
* @return A new instance of `DevInsCompiler`.
*/
private fun createCompiler(
project: Project,
text: String
): DevInsCompiler {
val devInFile = DevInFile.fromString(project, text)
val editor = FileEditorManager.getInstance(project).selectedTextEditor
val element: PsiElement? = editor?.caretModel?.currentCaret?.offset?.let {
val psiFile = getCurrentPsiFile(project, editor) ?: return@let null
val psiFile = PsiUtilBase.getPsiFileInEditor(editor, project) ?: return@let null
getElementAtOffset(psiFile, it)
}

val devInsCompiler = DevInsCompiler(project, devInFile, editor, element)
return devInsCompiler
}

val result = devInsCompiler.compile()
return if (result.hasError || result.isLocalCommand) {
text
} else {
result.output
private fun getElementAtOffset(psiFile: PsiElement, offset: Int): PsiElement? {
var element = psiFile.findElementAt(offset) ?: return null

if (element is PsiWhiteSpace) {
element = element.getParent()
}

return element
}
}

0 comments on commit 2229789

Please sign in to comment.