Skip to content

Commit 263e1a8

Browse files
committed
feat(custom): Refactor variable resolver and prompt service in CustomActionBaseIntention.kt to improve efficiency and readability #136
1 parent 7ee34df commit 263e1a8

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/main/kotlin/cc/unitmesh/devti/custom/CustomActionBaseIntention.kt

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,13 @@ class CustomActionBaseIntention(private val intentionConfig: CustomIntentionConf
4343
val withRange = elementWithRange(editor, file, project) ?: return
4444
val selectedText = withRange.first
4545
val psiElement = withRange.second
46-
val prompt: CustomIntentionPrompt = constructCustomPrompt(psiElement!!, selectedText)
46+
val beforeCursor = editor.document.text.substring(0, editor.caretModel.offset)
47+
val afterCursor = editor.document.text.substring(editor.caretModel.offset)
48+
49+
val prompt: CustomIntentionPrompt = constructCustomPrompt(
50+
psiElement!!, selectedText,
51+
beforeCursor, afterCursor
52+
)
4753

4854
if (intentionConfig.autoInvoke) {
4955
sendToChatPanel(project, getActionType(), object : ContextPrompter() {
@@ -62,7 +68,12 @@ class CustomActionBaseIntention(private val intentionConfig: CustomIntentionConf
6268
}
6369
}
6470

65-
private fun constructCustomPrompt(psiElement: PsiElement, selectedText: @NlsSafe String): CustomIntentionPrompt {
71+
private fun constructCustomPrompt(
72+
psiElement: PsiElement,
73+
selectedText: @NlsSafe String,
74+
beforeCursor: String,
75+
afterCursor: String
76+
): CustomIntentionPrompt {
6677
val velocityContext = VelocityContext()
6778

6879
val variableResolvers = arrayOf(
@@ -81,6 +92,11 @@ class CustomActionBaseIntention(private val intentionConfig: CustomIntentionConf
8192
velocityContext.put(variableType, value)
8293
}
8394

95+
velocityContext.put("beforeCursor", beforeCursor)
96+
velocityContext.put("afterCursor", afterCursor)
97+
val filePath = psiElement.containingFile.virtualFile.path
98+
velocityContext.put("filePath", filePath)
99+
84100
val oldContextClassLoader = Thread.currentThread().getContextClassLoader()
85101
Thread.currentThread().setContextClassLoader(CustomActionBaseIntention::class.java.getClassLoader())
86102

0 commit comments

Comments
 (0)