Skip to content

Commit 149069a

Browse files
committed
fix: fix commit message gen issues
1 parent 97a793a commit 149069a

File tree

3 files changed

+57
-30
lines changed

3 files changed

+57
-30
lines changed

src/main/kotlin/cc/unitmesh/devti/actions/vcs/CommitMessageSuggestionAction.kt

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,63 @@ package cc.unitmesh.devti.actions.vcs
22

33
import cc.unitmesh.devti.actions.chat.base.ChatBaseAction
44
import cc.unitmesh.devti.gui.chat.ChatActionType
5+
import cc.unitmesh.devti.gui.chat.ChatContext
6+
import cc.unitmesh.devti.gui.sendToChatPanel
7+
import cc.unitmesh.devti.prompting.VcsPrompting
8+
import cc.unitmesh.devti.provider.ContextPrompter
9+
import com.intellij.openapi.actionSystem.AnActionEvent
10+
import com.intellij.openapi.actionSystem.CommonDataKeys
11+
import com.intellij.openapi.components.service
12+
import com.intellij.openapi.project.ProjectManager
13+
import com.intellij.temporary.getElementToAction
514

615
class CommitMessageSuggestionAction : ChatBaseAction() {
716
override fun getActionType(): ChatActionType = ChatActionType.GEN_COMMIT_MESSAGE
17+
18+
override fun executeAction(event: AnActionEvent) {
19+
val project = event.project ?: return
20+
val prompt = generateCommitMessage(prepareVcsContext())
21+
22+
sendToChatPanel(project) { panel, service ->
23+
val chatContext = ChatContext(
24+
getReplaceableAction(event),
25+
prompt,
26+
""
27+
)
28+
29+
service.handlePromptAndResponse(panel, object : ContextPrompter() {
30+
override fun displayPrompt(): String = prompt
31+
override fun requestPrompt(): String = prompt
32+
}, chatContext, newChatContext = true)
33+
}
34+
}
35+
36+
private fun prepareVcsContext(): String {
37+
val project = ProjectManager.getInstance().openProjects.firstOrNull() ?: return ""
38+
val prompting = project.service<VcsPrompting>()
39+
40+
return prompting.prepareContext()
41+
}
42+
43+
private fun generateCommitMessage(diff: String): String {
44+
return """Write a cohesive yet descriptive commit message for a given diff.
45+
Make sure to include both information What was changed and Why.
46+
Start with a short sentence in imperative form, no more than 50 characters long.
47+
Then leave an empty line and continue with a more detailed explanation, if necessary.
48+
Explanation should have less than 200 characters.
49+
50+
examples:
51+
- fix(authentication): add password regex pattern
52+
- feat(storage): add new test cases
53+
54+
Diff:
55+
56+
```diff
57+
$diff
58+
```
59+
60+
"""
61+
}
62+
63+
864
}

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

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,42 +24,14 @@ enum class ChatActionType {
2424
override fun toString(): String {
2525
return instruction()
2626
}
27-
28-
private fun prepareVcsContext(): String {
29-
val project = ProjectManager.getInstance().openProjects.firstOrNull() ?: return ""
30-
val prompting = project.service<VcsPrompting>()
31-
32-
return prompting.prepareContext()
33-
}
34-
35-
private fun generateCommitMessage(diff: String): String {
36-
return """Write a cohesive yet descriptive commit message for a given diff.
37-
Make sure to include both information What was changed and Why.
38-
Start with a short sentence in imperative form, no more than 50 characters long.
39-
Then leave an empty line and continue with a more detailed explanation, if necessary.
40-
Explanation should have less than 200 characters.
41-
42-
examples:
43-
- fix(authentication): add password regex pattern
44-
- feat(storage): add new test cases
45-
46-
Diff:
47-
48-
```diff
49-
$diff
50-
```
51-
52-
"""
53-
}
54-
5527
fun instruction(lang: String = ""): String {
5628
return when (this) {
5729
EXPLAIN -> "Explain selected $lang code"
5830
REFACTOR -> "Refactor the given $lang code"
5931
CODE_COMPLETE -> "Complete $lang code, return rest code, no explaining"
6032
GENERATE_TEST -> "Write unit test for given $lang code"
6133
FIX_ISSUE -> "Help me fix this issue"
62-
GEN_COMMIT_MESSAGE -> generateCommitMessage(prepareVcsContext())
34+
GEN_COMMIT_MESSAGE -> ""
6335
CREATE_CHANGELOG -> "generate release note"
6436
CHAT -> ""
6537
CUSTOM_COMPLETE -> ""

src/main/kotlin/cc/unitmesh/devti/prompting/VcsPrompting.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ class VcsPrompting(private val project: Project) {
7676
)
7777

7878

79-
8079
UnifiedDiffWriter.write(
8180
project,
8281
project.stateStore.projectBasePath,

0 commit comments

Comments
 (0)