From 98c3d5073ca7772ef4e8f77aa5b837f164b8bd8d Mon Sep 17 00:00:00 2001 From: Carl-Robert Linnupuu Date: Mon, 17 Jun 2024 16:54:06 +0300 Subject: [PATCH] fix: commit message placeholders --- .../GenerateGitCommitMessageAction.java | 25 +++------- .../generate-commit-message-system-prompt.txt | 3 ++ .../CodeCompletionServiceTest.kt | 46 +++++++++---------- .../testsupport/mixin/ShortcutsTestMixin.kt | 3 +- 4 files changed, 35 insertions(+), 42 deletions(-) diff --git a/src/main/java/ee/carlrobert/codegpt/actions/GenerateGitCommitMessageAction.java b/src/main/java/ee/carlrobert/codegpt/actions/GenerateGitCommitMessageAction.java index 82efba4a..2fb3afbf 100644 --- a/src/main/java/ee/carlrobert/codegpt/actions/GenerateGitCommitMessageAction.java +++ b/src/main/java/ee/carlrobert/codegpt/actions/GenerateGitCommitMessageAction.java @@ -1,7 +1,6 @@ package ee.carlrobert.codegpt.actions; import static com.intellij.openapi.ui.Messages.OK; -import static com.intellij.util.ObjectUtils.tryCast; import static ee.carlrobert.codegpt.settings.service.ServiceType.YOU; import static java.util.stream.Collectors.joining; @@ -13,14 +12,10 @@ import com.intellij.openapi.actionSystem.AnAction; import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.command.WriteCommandAction; -import com.intellij.openapi.editor.Document; -import com.intellij.openapi.editor.Editor; -import com.intellij.openapi.editor.ex.EditorEx; import com.intellij.openapi.project.Project; import com.intellij.openapi.vcs.FilePath; import com.intellij.openapi.vcs.VcsDataKeys; import com.intellij.openapi.vcs.changes.Change; -import com.intellij.openapi.vcs.ui.CommitMessage; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.vcs.commit.CommitWorkflowUi; import ee.carlrobert.codegpt.CodeGPTBundle; @@ -89,14 +84,13 @@ public class GenerateGitCommitMessageAction extends AnAction { return; } - var editor = getCommitMessageEditor(event); - if (editor != null) { - ((EditorEx) editor).setCaretVisible(false); + var commitWorkflowUi = event.getData(VcsDataKeys.COMMIT_WORKFLOW_UI); + if (commitWorkflowUi != null) { CompletionRequestService.getInstance() .generateCommitMessageAsync( project.getService(CommitMessageTemplate.class).getSystemPrompt(), gitDiff, - getEventListener(project, editor.getDocument())); + getEventListener(project, commitWorkflowUi)); } } @@ -105,7 +99,9 @@ public class GenerateGitCommitMessageAction extends AnAction { return ActionUpdateThread.EDT; } - private CompletionEventListener getEventListener(Project project, Document document) { + private CompletionEventListener getEventListener( + Project project, + CommitWorkflowUi commitWorkflowUi) { return new CompletionEventListener<>() { private final StringBuilder messageBuilder = new StringBuilder(); @@ -116,7 +112,7 @@ public class GenerateGitCommitMessageAction extends AnAction { application.invokeLater(() -> application.runWriteAction(() -> WriteCommandAction.runWriteCommandAction(project, () -> - document.setText(messageBuilder)))); + commitWorkflowUi.getCommitMessageUi().setText(messageBuilder.toString())))); } @Override @@ -130,13 +126,6 @@ public class GenerateGitCommitMessageAction extends AnAction { }; } - private Editor getCommitMessageEditor(AnActionEvent event) { - var commitMessage = tryCast( - event.getData(VcsDataKeys.COMMIT_MESSAGE_CONTROL), - CommitMessage.class); - return commitMessage != null ? commitMessage.getEditorField().getEditor() : null; - } - private String getGitDiff(AnActionEvent event, Project project) { var commitWorkflowUi = Optional.ofNullable(event.getData(VcsDataKeys.COMMIT_WORKFLOW_UI)) .orElseThrow(() -> new IllegalStateException("Could not retrieve commit workflow ui.")); diff --git a/src/main/resources/prompts/generate-commit-message-system-prompt.txt b/src/main/resources/prompts/generate-commit-message-system-prompt.txt index 78d7cb09..88e05fce 100644 --- a/src/main/resources/prompts/generate-commit-message-system-prompt.txt +++ b/src/main/resources/prompts/generate-commit-message-system-prompt.txt @@ -1,3 +1,6 @@ +Branch: {BRANCH_NAME} +Date: {DATE_ISO_8601} + Write a short and descriptive git commit message for the following git diff. Use imperative mood, present tense, active voice and verbs. Your entire response will be passed directly into git commit. \ No newline at end of file diff --git a/src/test/kotlin/ee/carlrobert/codegpt/codecompletions/CodeCompletionServiceTest.kt b/src/test/kotlin/ee/carlrobert/codegpt/codecompletions/CodeCompletionServiceTest.kt index 71f02c20..334e0d15 100644 --- a/src/test/kotlin/ee/carlrobert/codegpt/codecompletions/CodeCompletionServiceTest.kt +++ b/src/test/kotlin/ee/carlrobert/codegpt/codecompletions/CodeCompletionServiceTest.kt @@ -13,38 +13,38 @@ import testsupport.IntegrationTest class CodeCompletionServiceTest : IntegrationTest() { - private val cursorPosition = VisualPosition(3, 0) + private val cursorPosition = VisualPosition(3, 0) - fun testFetchCodeCompletionLlama() { - useLlamaService() - LlamaSettings.getCurrentState().isCodeCompletionsEnabled = true - myFixture.configureByText( - "CompletionTest.java", - getResourceContent("/codecompletions/code-completion-file.txt") - ) - myFixture.editor.caretModel.moveToVisualPosition(cursorPosition) - val expectedCompletion = "TEST_OUTPUT" - val prefix = """ + fun testFetchCodeCompletionLlama() { + useLlamaService() + LlamaSettings.getCurrentState().isCodeCompletionsEnabled = true + myFixture.configureByText( + "CompletionTest.java", + getResourceContent("/codecompletions/code-completion-file.txt") + ) + myFixture.editor.caretModel.moveToVisualPosition(cursorPosition) + val expectedCompletion = "TEST_OUTPUT" + val prefix = """ ${"z".repeat(245)} [INPUT] c """.trimIndent() // 128 tokens - val suffix = """ + val suffix = """ [\INPUT] ${"z".repeat(247)} """.trimIndent() // 128 tokens - expectLlama(StreamHttpExchange { request: RequestEntity -> - assertThat(request.uri.path).isEqualTo("/completion") - assertThat(request.method).isEqualTo("POST") - assertThat(request.body) - .extracting("prompt") - .isEqualTo(InfillPromptTemplate.CODE_LLAMA.buildPrompt(prefix, suffix)) - listOf(jsonMapResponse(e("content", expectedCompletion), e("stop", true))) - }) + expectLlama(StreamHttpExchange { request: RequestEntity -> + assertThat(request.uri.path).isEqualTo("/completion") + assertThat(request.method).isEqualTo("POST") + assertThat(request.body) + .extracting("prompt") + .isEqualTo(InfillPromptTemplate.CODE_LLAMA.buildPrompt(prefix, suffix)) + listOf(jsonMapResponse(e("content", expectedCompletion), e("stop", true))) + }) - myFixture.type('c') + myFixture.type('c') - waitExpecting { "TEST_OUTPUT" == PREVIOUS_INLAY_TEXT[myFixture.editor] } - } + waitExpecting { "TEST_OUTPUT" == PREVIOUS_INLAY_TEXT[myFixture.editor] } + } } diff --git a/src/test/kotlin/testsupport/mixin/ShortcutsTestMixin.kt b/src/test/kotlin/testsupport/mixin/ShortcutsTestMixin.kt index 2273ca08..1e325475 100644 --- a/src/test/kotlin/testsupport/mixin/ShortcutsTestMixin.kt +++ b/src/test/kotlin/testsupport/mixin/ShortcutsTestMixin.kt @@ -45,9 +45,10 @@ interface ShortcutsTestMixin { GeneralSettings.getCurrentState().selectedService = ServiceType.YOU } - fun useLlamaService() { + fun useLlamaService(codeCompletionsEnabled: Boolean = false) { GeneralSettings.getCurrentState().selectedService = ServiceType.LLAMA_CPP LlamaSettings.getCurrentState().serverPort = null + LlamaSettings.getCurrentState().isCodeCompletionsEnabled = codeCompletionsEnabled } fun useGoogleService() {