@@ -5,11 +5,9 @@ import cc.unitmesh.devti.language.compiler.DevInsCompiler
5
5
import cc.unitmesh.devti.language.psi.DevInFile
6
6
import cc.unitmesh.devti.provider.devins.CustomAgentContext
7
7
import cc.unitmesh.devti.provider.devins.LanguagePromptProcessor
8
- import com.intellij.openapi.editor.Editor
9
8
import com.intellij.openapi.fileEditor.FileEditorManager
10
9
import com.intellij.openapi.project.Project
11
10
import com.intellij.psi.PsiElement
12
- import com.intellij.psi.PsiFile
13
11
import com.intellij.psi.PsiWhiteSpace
14
12
import com.intellij.psi.util.PsiUtilBase
15
13
@@ -18,46 +16,54 @@ class DevInsCustomAgentResponse : LanguagePromptProcessor {
18
16
override val name: String = " DevIn"
19
17
20
18
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)
23
20
24
21
val result = devInsCompiler.compile()
25
22
AutoDevNotifications .notify(project, result.output)
26
23
return result.output
27
24
}
28
25
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)
36
28
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
40
34
}
41
-
42
- return element
43
35
}
44
36
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 {
46
48
val devInFile = DevInFile .fromString(project, text)
47
49
val editor = FileEditorManager .getInstance(project).selectedTextEditor
48
50
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
50
52
getElementAtOffset(psiFile, it)
51
53
}
52
54
53
55
val devInsCompiler = DevInsCompiler (project, devInFile, editor, element)
56
+ return devInsCompiler
57
+ }
54
58
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()
60
64
}
65
+
66
+ return element
61
67
}
62
68
}
63
69
0 commit comments