Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use DiffManagerEx and Remove GitMerge view #344

Merged
merged 2 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

group = "com.devoxx.genie"
version = "0.3.0"
version = "0.3.1"

repositories {
mavenCentral()
Expand Down
14 changes: 0 additions & 14 deletions src/main/java/com/devoxx/genie/model/ChatInteraction.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public UserMessage createUserMessage(@NotNull ChatMessageContext chatMessageCont
}

// If git diff is enabled, add special instructions
if (DevoxxGenieStateService.getInstance().getUseDiffMerge()) {
if (DevoxxGenieStateService.getInstance().getUseSimpleDiff()) {
stringBuilder.append("<DiffInstructions>").append(GIT_DIFF_INSTRUCTIONS).append("</DiffInstructions>\n\n");
}

Expand Down Expand Up @@ -137,8 +137,7 @@ public UserMessage createUserMessage(@NotNull ChatMessageContext chatMessageCont
StringBuilder stringBuilder = new StringBuilder();

// If git diff is enabled, add special instructions at the beginning
if (DevoxxGenieStateService.getInstance().getUseDiffMerge() ||
DevoxxGenieStateService.getInstance().getUseSimpleDiff()) {
if (DevoxxGenieStateService.getInstance().getUseSimpleDiff()) {
stringBuilder.append("<DiffInstructions>").append(GIT_DIFF_INSTRUCTIONS).append("</DiffInstructions>\n\n");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
package com.devoxx.genie.service.gitdiff;

import com.devoxx.genie.ui.settings.DevoxxGenieStateService;
import com.devoxx.genie.ui.util.NotificationUtil;
import com.intellij.diff.DiffContentFactory;
import com.intellij.diff.DiffManager;
import com.intellij.diff.DiffManagerImpl;
import com.intellij.diff.DiffManagerEx;
import com.intellij.diff.contents.DiffContent;
import com.intellij.diff.contents.DocumentContent;
import com.intellij.diff.contents.DocumentContentImpl;
import com.intellij.diff.requests.DiffRequest;
import com.intellij.diff.requests.SimpleDiffRequest;
import com.intellij.diff.requests.TextMergeRequestImpl;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Document;
Expand All @@ -30,48 +25,6 @@ public static GitMergeService getInstance() {
return ApplicationManager.getApplication().getService(GitMergeService.class);
}

/**
* Shows a three-way merge view with:
* - Left: Original code
* - Center: Merge result (initially empty)
* - Right: LLM's modified version
*/
public void showMerge(@NotNull Project project,
@NotNull String originalContent,
@NotNull String modifiedContent,
@NotNull String title,
@NotNull Document targetDocument) {
DevoxxGenieStateService instance = DevoxxGenieStateService.getInstance();
if (!instance.getUseSimpleDiff() && !instance.getUseDiffMerge()) {
LOG.info("Diff view is disabled");
return;
}

ApplicationManager.getApplication().invokeLater(() -> {
DiffContentFactory factory = DiffContentFactory.getInstance();

// Create contents for three-way merge
DocumentContent originalContentDoc = factory.create(project, originalContent);

DocumentContent modifiedContent1 = factory.create(project, modifiedContent);
DocumentContent targetDocumentContent = factory.create(project, targetDocument);

Document originalDocument = originalContentDoc.getDocument();

TextMergeRequestImpl request = new TextMergeRequestImpl(
project,
targetDocumentContent,
originalDocument.getCharsSequence(),
List.of(originalContentDoc, modifiedContent1, targetDocumentContent),
title,
List.of("Original Code", "LLM Modified Code", "Merge Result")
);

// Show the merge dialog
DiffManager.getInstance().showMerge(project, request);
});
}

/**
* Git diff view
* @param project the project
Expand Down Expand Up @@ -107,7 +60,7 @@ public void showDiffView(Project project, VirtualFile originalFile, String sugge
);

ApplicationManager.getApplication().invokeLater(() ->
DiffManagerImpl.getInstance().showDiff(project, diffRequest));
DiffManagerEx.getInstance().showDiff(project, diffRequest));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,20 @@
import com.devoxx.genie.ui.settings.DevoxxGenieStateService;
import com.devoxx.genie.ui.topic.AppTopics;
import com.devoxx.genie.ui.util.NotificationUtil;
import com.intellij.diff.DiffManagerImpl;
import com.intellij.diff.contents.DiffContent;
import com.intellij.diff.contents.FileContentImpl;
import com.intellij.diff.requests.DiffRequest;
import com.intellij.diff.requests.SimpleDiffRequest;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.ComboBox;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.ui.Splitter;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.util.NlsContexts;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.ui.OnePixelSplitter;
import com.intellij.ui.components.JBScrollPane;
import com.intellij.util.messages.MessageBusConnection;
import lombok.Getter;
import org.apache.xmlbeans.impl.xb.xsdschema.SimpleContentDocument;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;
Expand Down
99 changes: 2 additions & 97 deletions src/main/java/com/devoxx/genie/ui/panel/ChatResponsePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@
import com.devoxx.genie.ui.processor.NodeProcessorFactory;
import com.devoxx.genie.ui.settings.DevoxxGenieStateService;
import com.devoxx.genie.util.DefaultLLMSettingsUtil;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.ui.JBColor;
import com.knuddels.jtokkit.api.Encoding;
Expand All @@ -33,7 +27,6 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Objects;

import static com.devoxx.genie.ui.util.DevoxxGenieFontsUtil.SourceCodeProFontPlan14;

Expand Down Expand Up @@ -68,9 +61,7 @@ private void addResponsePane(@NotNull ChatMessageContext chatMessageContext) {
DevoxxGenieStateService stateService = DevoxxGenieStateService.getInstance();

// If git diff is enabled, try to extract code blocks and show diff
if (stateService.getUseDiffMerge()) {
processGitMerge(chatMessageContext, document);
} else if (stateService.getUseSimpleDiff()) {
if (stateService.getUseSimpleDiff()) {
processGitDiff(chatMessageContext, document);
}

Expand All @@ -88,65 +79,6 @@ private void addResponsePane(@NotNull ChatMessageContext chatMessageContext) {
}
}

private void processGitMerge(@NotNull ChatMessageContext chatMessageContext, @NotNull Node document) {
// Get original code from context
String originalCode;
EditorInfo editorInfo = chatMessageContext.getEditorInfo();

// Wrap the document access in a read action
originalCode = ApplicationManager
.getApplication()
.runReadAction((Computable<String>) () -> {
if (editorInfo.getSelectedText() != null) {
return editorInfo.getSelectedText();
} else {
List<VirtualFile> selectedFiles = editorInfo.getSelectedFiles();
if (selectedFiles == null || selectedFiles.isEmpty()) {
return null;
}
VirtualFile originalFile = selectedFiles.get(0);
Document originalDoc = FileDocumentManager.getInstance().getDocument(originalFile);
if (originalDoc == null) {
return null;
}
return originalDoc.getText();
}
});

if (originalCode == null) {
return;
}


// Find the first code block in the response
Node node = document.getFirstChild();
while (node != null) {
if (node instanceof FencedCodeBlock codeBlock) {
String modifiedCode = codeBlock.getLiteral();

Document editorDocument = com.intellij.openapi.application.ApplicationManager.getApplication()
.runReadAction((com.intellij.openapi.util.Computable<Document>) () ->
Objects.requireNonNull(FileEditorManager.getInstance(chatMessageContext.getProject())
.getSelectedTextEditor())
.getDocument()
);

// Show merge using our service
GitMergeService.getInstance().showMerge(
chatMessageContext.getProject(),
originalCode,
modifiedCode,
"Merge LLM Changes",
editorDocument
);

// Only show diff for first code block
break;
}
node = node.getNext();
}
}

private void addMetricExecutionInfo(@NotNull ChatMessageContext chatMessageContext) {
JPanel metricExecutionInfoPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
metricExecutionInfoPanel.setOpaque(false);
Expand Down Expand Up @@ -191,34 +123,7 @@ private void processGitDiff(@NotNull ChatMessageContext chatMessageContext, @Not
return;
}

// Handle single file case
if (editorInfo.getSelectedText() != null) {
Editor editor = FileEditorManager.getInstance(chatMessageContext.getProject())
.getSelectedTextEditor();

if (editor != null) {
String originalCode = editorInfo.getSelectedText();

// Find first code block in response
Node node = document.getFirstChild();
while (node != null) {
if (node instanceof FencedCodeBlock codeBlock) {

GitMergeService.getInstance().showMerge(
chatMessageContext.getProject(),
originalCode,
codeBlock.getLiteral(),
"Merge LLM Changes",
editor.getDocument()
);
break;
}
node = node.getNext();
}
}
}
// Handle multiple files case
else if (editorInfo.getSelectedFiles() != null && !editorInfo.getSelectedFiles().isEmpty()) {
if (editorInfo.getSelectedFiles() != null && !editorInfo.getSelectedFiles().isEmpty()) {
List<VirtualFile> files = editorInfo.getSelectedFiles();
List<String> modifiedContents = new ArrayList<>();

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public static DevoxxGenieStateService getInstance() {
private Boolean showExecutionTime = true;

// Git Diff features
private Boolean useDiffMerge = false;
private Boolean useSimpleDiff = false;

// Local LLM URL fields
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,6 @@

public enum GitDiffMode {
DISABLED("Disabled", "", ""),
DIFF_MERGE("Git Diff Merge", "/images/diff_merge.jpg",
"""
<html>
<head>
<style type="text/css">
body {
font-family: 'Source Code Pro', monospace;
zoom: %s;
}
h2 {
margin-bottom: 5px;
}
p {
margin: 0;
}
ul {
margin-bottom: 5px;
}
li {
margin-bottom: 5px;
}
</style>
</head>
<body>
<h3>Three-panel comparison</h2>
<p>Shows three-way comparison between original, suggested, and merged changes</p>
<UL>
<LI>Left panel: Original file labeled "Original code"</LI>
<LI>Center panel: Merge result labeled "Merged"</LI>
<LI>Right panel: LLM's modified version labeled "LLM suggested"</LI>
</UL>
</body></html>
"""),
SIMPLE_DIFF("Simple Git Diff", "/images/simple_diff.jpg",
"""
<html>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ public Component getListCellRendererComponent(JList<?> list, Object value,
}

private GitDiffMode determineCurrentMode() {
if (stateService.getUseDiffMerge()) {
return GitDiffMode.DIFF_MERGE;
} else if (stateService.getUseSimpleDiff()) {
if (stateService.getUseSimpleDiff()) {
return GitDiffMode.SIMPLE_DIFF;
}
return GitDiffMode.DISABLED;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public void apply() {
DevoxxGenieStateService stateService = DevoxxGenieStateService.getInstance();
GitDiffMode selectedMode = (GitDiffMode) diffSettingsComponent.getGitDiffModeComboBox().getSelectedItem();

stateService.setUseDiffMerge(selectedMode == GitDiffMode.DIFF_MERGE);
stateService.setUseSimpleDiff(selectedMode == GitDiffMode.SIMPLE_DIFF);
}

Expand All @@ -61,9 +60,7 @@ public void reset() {
}

private GitDiffMode determineCurrentMode(@NotNull DevoxxGenieStateService stateService) {
if (stateService.getUseDiffMerge()) {
return GitDiffMode.DIFF_MERGE;
} else if (stateService.getUseSimpleDiff()) {
if (stateService.getUseSimpleDiff()) {
return GitDiffMode.SIMPLE_DIFF;
}
return GitDiffMode.DISABLED;
Expand Down
Loading
Loading