Skip to content

Commit 2229789

Browse files
committed
fix(devins-lang): remove unused imports and refactor compiler creation 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.
1 parent d9a4762 commit 2229789

File tree

1 file changed

+29
-23
lines changed

1 file changed

+29
-23
lines changed

exts/devins-lang/src/main/kotlin/cc/unitmesh/devti/language/provider/DevInsCustomAgentResponse.kt

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@ import cc.unitmesh.devti.language.compiler.DevInsCompiler
55
import cc.unitmesh.devti.language.psi.DevInFile
66
import cc.unitmesh.devti.provider.devins.CustomAgentContext
77
import cc.unitmesh.devti.provider.devins.LanguagePromptProcessor
8-
import com.intellij.openapi.editor.Editor
98
import com.intellij.openapi.fileEditor.FileEditorManager
109
import com.intellij.openapi.project.Project
1110
import com.intellij.psi.PsiElement
12-
import com.intellij.psi.PsiFile
1311
import com.intellij.psi.PsiWhiteSpace
1412
import com.intellij.psi.util.PsiUtilBase
1513

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

2018
override fun execute(project: Project, context: CustomAgentContext): String {
21-
val devInFile = DevInFile.fromString(project, context.response)
22-
val devInsCompiler = DevInsCompiler(project, devInFile)
19+
val devInsCompiler = createCompiler(project, context.response)
2320

2421
val result = devInsCompiler.compile()
2522
AutoDevNotifications.notify(project, result.output)
2623
return result.output
2724
}
2825

29-
private fun getCurrentPsiFile(project: Project, editor: Editor): PsiFile? {
30-
return PsiUtilBase.getPsiFileInEditor(editor, project)
31-
}
32-
33-
private fun getElementAtOffset(psiFile: PsiElement, offset: Int): PsiElement? {
34-
// 获取偏移量对应的元素
35-
var element = psiFile.findElementAt(offset) ?: return null
26+
override fun compile(project: Project, text: String): String {
27+
val devInsCompiler = createCompiler(project, text)
3628

37-
// 如果元素是空白元素,尝试获取其父元素
38-
if (element is PsiWhiteSpace) {
39-
element = element.getParent()
29+
val result = devInsCompiler.compile()
30+
return if (result.hasError || result.isLocalCommand) {
31+
text
32+
} else {
33+
result.output
4034
}
41-
42-
return element
4335
}
4436

45-
override fun compile(project: Project, text: String): String {
37+
/**
38+
* Creates a new instance of `DevInsCompiler`.
39+
*
40+
* @param project The current project.
41+
* @param text The source code text.
42+
* @return A new instance of `DevInsCompiler`.
43+
*/
44+
private fun createCompiler(
45+
project: Project,
46+
text: String
47+
): DevInsCompiler {
4648
val devInFile = DevInFile.fromString(project, text)
4749
val editor = FileEditorManager.getInstance(project).selectedTextEditor
4850
val element: PsiElement? = editor?.caretModel?.currentCaret?.offset?.let {
49-
val psiFile = getCurrentPsiFile(project, editor) ?: return@let null
51+
val psiFile = PsiUtilBase.getPsiFileInEditor(editor, project) ?: return@let null
5052
getElementAtOffset(psiFile, it)
5153
}
5254

5355
val devInsCompiler = DevInsCompiler(project, devInFile, editor, element)
56+
return devInsCompiler
57+
}
5458

55-
val result = devInsCompiler.compile()
56-
return if (result.hasError || result.isLocalCommand) {
57-
text
58-
} else {
59-
result.output
59+
private fun getElementAtOffset(psiFile: PsiElement, offset: Int): PsiElement? {
60+
var element = psiFile.findElementAt(offset) ?: return null
61+
62+
if (element is PsiWhiteSpace) {
63+
element = element.getParent()
6064
}
65+
66+
return element
6167
}
6268
}
6369

0 commit comments

Comments
 (0)