fix: commit message placeholders

This commit is contained in:
Carl-Robert Linnupuu 2024-06-17 16:54:06 +03:00
parent 58e17c0819
commit 98c3d5073c
4 changed files with 35 additions and 42 deletions

View file

@ -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<String> getEventListener(Project project, Document document) {
private CompletionEventListener<String> 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."));

View file

@ -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.

View file

@ -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] }
}
}

View file

@ -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() {