Skip to content

Commit

Permalink
fix: fix commit message gen issues
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Dec 31, 2023
1 parent 97a793a commit 149069a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 30 deletions.
Expand Up @@ -2,7 +2,63 @@ package cc.unitmesh.devti.actions.vcs

import cc.unitmesh.devti.actions.chat.base.ChatBaseAction
import cc.unitmesh.devti.gui.chat.ChatActionType
import cc.unitmesh.devti.gui.chat.ChatContext
import cc.unitmesh.devti.gui.sendToChatPanel
import cc.unitmesh.devti.prompting.VcsPrompting
import cc.unitmesh.devti.provider.ContextPrompter
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.components.service
import com.intellij.openapi.project.ProjectManager
import com.intellij.temporary.getElementToAction

class CommitMessageSuggestionAction : ChatBaseAction() {
override fun getActionType(): ChatActionType = ChatActionType.GEN_COMMIT_MESSAGE

override fun executeAction(event: AnActionEvent) {
val project = event.project ?: return
val prompt = generateCommitMessage(prepareVcsContext())

sendToChatPanel(project) { panel, service ->
val chatContext = ChatContext(
getReplaceableAction(event),
prompt,
""
)

service.handlePromptAndResponse(panel, object : ContextPrompter() {
override fun displayPrompt(): String = prompt
override fun requestPrompt(): String = prompt
}, chatContext, newChatContext = true)
}
}

private fun prepareVcsContext(): String {
val project = ProjectManager.getInstance().openProjects.firstOrNull() ?: return ""
val prompting = project.service<VcsPrompting>()

return prompting.prepareContext()
}

private fun generateCommitMessage(diff: String): String {
return """Write a cohesive yet descriptive commit message for a given diff.
Make sure to include both information What was changed and Why.
Start with a short sentence in imperative form, no more than 50 characters long.
Then leave an empty line and continue with a more detailed explanation, if necessary.
Explanation should have less than 200 characters.
examples:
- fix(authentication): add password regex pattern
- feat(storage): add new test cases
Diff:
```diff
$diff
```
"""
}


}
30 changes: 1 addition & 29 deletions src/main/kotlin/cc/unitmesh/devti/gui/chat/ChatActionType.kt
Expand Up @@ -24,42 +24,14 @@ enum class ChatActionType {
override fun toString(): String {
return instruction()
}

private fun prepareVcsContext(): String {
val project = ProjectManager.getInstance().openProjects.firstOrNull() ?: return ""
val prompting = project.service<VcsPrompting>()

return prompting.prepareContext()
}

private fun generateCommitMessage(diff: String): String {
return """Write a cohesive yet descriptive commit message for a given diff.
Make sure to include both information What was changed and Why.
Start with a short sentence in imperative form, no more than 50 characters long.
Then leave an empty line and continue with a more detailed explanation, if necessary.
Explanation should have less than 200 characters.
examples:
- fix(authentication): add password regex pattern
- feat(storage): add new test cases
Diff:
```diff
$diff
```
"""
}

fun instruction(lang: String = ""): String {
return when (this) {
EXPLAIN -> "Explain selected $lang code"
REFACTOR -> "Refactor the given $lang code"
CODE_COMPLETE -> "Complete $lang code, return rest code, no explaining"
GENERATE_TEST -> "Write unit test for given $lang code"
FIX_ISSUE -> "Help me fix this issue"
GEN_COMMIT_MESSAGE -> generateCommitMessage(prepareVcsContext())
GEN_COMMIT_MESSAGE -> ""
CREATE_CHANGELOG -> "generate release note"
CHAT -> ""
CUSTOM_COMPLETE -> ""
Expand Down
Expand Up @@ -76,7 +76,6 @@ class VcsPrompting(private val project: Project) {
)



UnifiedDiffWriter.write(
project,
project.stateStore.projectBasePath,
Expand Down

0 comments on commit 149069a

Please sign in to comment.