Skip to content

Commit 6ce7391

Browse files
committed
refactor(custom): extract for range
1 parent 9af81d0 commit 6ce7391

File tree

3 files changed

+38
-12
lines changed

3 files changed

+38
-12
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
package cc.unitmesh.devti.custom
22

3+
import cc.unitmesh.devti.gui.chat.ChatActionType
34
import cc.unitmesh.devti.intentions.AbstractChatIntention
5+
import com.intellij.openapi.editor.Editor
6+
import com.intellij.openapi.project.Project
7+
import com.intellij.psi.PsiFile
48

5-
class CustomIntention(val intentionConfig: CustomIntentionConfig) : AbstractChatIntention() {
9+
class CustomIntention(private val intentionConfig: CustomIntentionConfig) : AbstractChatIntention() {
610

711
override fun getText(): String = intentionConfig.title
812

913
override fun getFamilyName(): String = "Custom Intention"
1014

15+
override fun getActionType(): ChatActionType = super.getActionType()
16+
17+
override fun invoke(project: Project, editor: Editor?, file: PsiFile?) {
18+
19+
}
20+
1121
companion object {
1222
fun create(intentionConfig: CustomIntentionConfig): CustomIntention {
1323
return CustomIntention(intentionConfig)

src/main/kotlin/cc/unitmesh/devti/gui/chat/ChatActionType.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ enum class ChatActionType {
1717
FIX_ISSUE,
1818
CREATE_DDL,
1919
CREATE_CHANGELOG,
20-
CUSTOM_COMPLETE;
20+
CUSTOM_COMPLETE,
21+
CUSTOM_ACTION
22+
;
2123

2224
override fun toString(): String {
2325
return instruction()
@@ -83,6 +85,7 @@ $diff
8385
CREATE_CHANGELOG -> "generate release note"
8486
CHAT -> ""
8587
CUSTOM_COMPLETE -> ""
88+
CUSTOM_ACTION -> ""
8689
EXPLAIN_BUSINESS -> "Recover the original business scene and functionality of the provided code by describing it in a User Story format. Ensure that your explanation avoids any technical jargon or technology-related terms."
8790
}
8891
}

src/main/kotlin/cc/unitmesh/devti/intentions/AbstractChatIntention.kt

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import com.intellij.codeInsight.intention.IntentionAction
1010
import com.intellij.lang.injection.InjectedLanguageManager
1111
import com.intellij.openapi.editor.Editor
1212
import com.intellij.openapi.project.Project
13+
import com.intellij.openapi.util.NlsSafe
1314
import com.intellij.openapi.util.TextRange
1415
import com.intellij.psi.ElementDescriptionUtil
1516
import com.intellij.psi.PsiElement
@@ -36,27 +37,39 @@ abstract class AbstractChatIntention : IntentionAction {
3637
*/
3738
override fun invoke(project: Project, editor: Editor?, file: PsiFile?) {
3839
if (editor == null || file == null) return
40+
val withRange = elementWithRange(editor, file, project) ?: return
3941

42+
val selectedText = withRange.first
43+
val psiElement = withRange.second
44+
45+
val actionType = getActionType()
46+
47+
val prompter = ContextPrompter.prompter(file.language.displayName)
48+
prompter.initContext(actionType, selectedText, file, project, editor.caretModel.offset, psiElement)
49+
sendToChatPanel(project, actionType, prompter)
50+
}
51+
52+
fun elementWithRange(
53+
editor: Editor,
54+
file: PsiFile,
55+
project: Project,
56+
): Pair<@NlsSafe String, PsiElement?>? {
4057
var selectedText = editor.selectionModel.selectedText
41-
val elementToExplain = getElementToAction(project, editor)
58+
val psiElement = getElementToAction(project, editor)
4259

4360
if (selectedText == null) {
44-
if (elementToExplain == null) {
45-
return
61+
if (psiElement == null) {
62+
return null
4663
}
47-
selectElement(elementToExplain, editor)
64+
selectElement(psiElement, editor)
4865
selectedText = editor.selectionModel.selectedText
4966
}
5067

5168
if (selectedText == null) {
52-
return
69+
return null
5370
}
5471

55-
val actionType = getActionType()
56-
57-
val prompter = ContextPrompter.prompter(file.language.displayName)
58-
prompter.initContext(actionType, selectedText, file, project, editor.caretModel.offset, elementToExplain)
59-
sendToChatPanel(project, actionType, prompter)
72+
return Pair(selectedText, psiElement)
6073
}
6174

6275
protected fun selectElement(elementToExplain: PsiElement, editor: Editor) {

0 commit comments

Comments
 (0)