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

Feat: Implement Diff View #8

Merged
merged 1 commit into from
Nov 28, 2023
Merged
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
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
package ai.devchat.devchat.handler;

import ai.devchat.common.Log;
import ai.devchat.devchat.ActionHandler;
import ai.devchat.devchat.DevChatActionHandler;
import com.alibaba.fastjson.JSONObject;
import com.intellij.psi.PsiFileFactory;
import com.intellij.diff.DiffContentFactory;
import com.intellij.diff.DiffManager;
import com.intellij.diff.requests.SimpleDiffRequest;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.SelectionModel;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;

public class ViewDiffRequestHandler implements ActionHandler {
private JSONObject metadata;
Expand All @@ -15,14 +25,42 @@
this.devChatActionHandler = devChatActionHandler;
}


@Override
public void executeAction() {
Log.info("Handling view diff request");
String callbackFunc = metadata.getString("callback");
String diffContent = payload.getString("content");
Project project = devChatActionHandler.getProject();

ApplicationManager.getApplication().invokeLater(() -> {
Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor();
if (editor == null) {
// Handle the case when no editor is opened
return;
}
Document document = editor.getDocument();
VirtualFile file = FileDocumentManager.getInstance().getFile(document);
if (file == null) {
// Handle the case when no file corresponds to the document
return;
}

FileType fileType = file.getFileType();
SelectionModel selectionModel = editor.getSelectionModel();
String localContent = selectionModel.hasSelection()
? selectionModel.getSelectedText()
: document.getText();

DiffContentFactory contentFactory = DiffContentFactory.getInstance();
SimpleDiffRequest diffRequest = new SimpleDiffRequest(
"Code Diff",
contentFactory.create(localContent, fileType),

Check warning on line 56 in src/main/java/ai/devchat/devchat/handler/ViewDiffRequestHandler.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Nullability and data flow problems

Argument `localContent` might be null
contentFactory.create(diffContent, fileType),
"Current Code",

Check warning on line 58 in src/main/java/ai/devchat/devchat/handler/ViewDiffRequestHandler.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Incorrect string capitalization

String 'Current Code' is not properly capitalized. It should have sentence capitalization
"New Code"

Check warning on line 59 in src/main/java/ai/devchat/devchat/handler/ViewDiffRequestHandler.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Incorrect string capitalization

String 'New Code' is not properly capitalized. It should have sentence capitalization
);

PsiFileFactory psiFileFactory = PsiFileFactory.getInstance(devChatActionHandler.getProject());
// PsiFile psiFile = psiFileFactory.createFileFromText("yourFileName.java", , diffContent);
DiffManager.getInstance().showDiff(project, diffRequest);
});
}

@Override
Expand Down
Loading