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: add support for resolving Java runtime errors #243

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package ee.carlrobert.codegpt.filter;

import com.intellij.execution.filters.Filter;
import com.intellij.execution.filters.JvmExceptionOccurrenceFilter;
import com.intellij.execution.impl.InlayProvider;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.EditorCustomElementRenderer;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiClass;
import java.util.List;
import org.jetbrains.annotations.NotNull;

public class CodeGPTJvmExceptionFilter implements JvmExceptionOccurrenceFilter {

@Override
public Filter.ResultItem applyFilter(@NotNull String exceptionClassName,
@NotNull List<PsiClass> classes, int exceptionStartOffset) {
return new CreateExceptionBreakpointResult(exceptionStartOffset,
exceptionStartOffset + exceptionClassName.length(), classes
.get(0).getProject());
}

private static class CreateExceptionBreakpointResult extends Filter.ResultItem implements
InlayProvider {

private final Project project;

private final int startOffset;

CreateExceptionBreakpointResult(int highlightStartOffset, int highlightEndOffset,
Project project) {
super(highlightStartOffset, highlightEndOffset, null);
this.project = project;
this.startOffset = highlightStartOffset;
}

public EditorCustomElementRenderer createInlayRenderer(Editor editor) {
return new CodeGPTPresentation(editor, this.project, this.startOffset);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package ee.carlrobert.codegpt.filter;

import com.intellij.codeInsight.hints.presentation.InputHandler;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.EditorCustomElementRenderer;
import com.intellij.openapi.editor.Inlay;
import com.intellij.openapi.editor.impl.EditorImpl;
import com.intellij.openapi.editor.markup.TextAttributes;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import ee.carlrobert.codegpt.Icons;
import ee.carlrobert.codegpt.conversations.message.Message;
import ee.carlrobert.codegpt.toolwindow.chat.standard.StandardChatToolWindowContentManager;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import javax.swing.Icon;
import javax.swing.SwingUtilities;
import org.jetbrains.annotations.NotNull;

public class CodeGPTPresentation implements EditorCustomElementRenderer, InputHandler {

private final Editor editor;

private final Project project;

private final int startOffset;


public CodeGPTPresentation(Editor editor, Project project, int starOffset) {
this.editor = editor;
this.project = project;
this.startOffset = starOffset;
}

public void mouseClicked(@NotNull MouseEvent mouseEvent, @NotNull Point point) {
int line = this.editor.getDocument().getLineNumber(this.startOffset);
String errorInformation = this.editor.getDocument()
.getText(new TextRange(this.startOffset, this.editor.getDocument().getLineEndOffset(line)));
String prompt = "Provide recommendations for the error message: {{errorInformation}}";
var message = new Message(prompt.replace("{{errorInformation}}", errorInformation));
SwingUtilities.invokeLater(
() -> project.getService(StandardChatToolWindowContentManager.class).sendMessage(message));
}

public void mouseExited() {
((EditorImpl) this.editor).setCustomCursor(this,
Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
}

public void mouseMoved(@NotNull MouseEvent mouseEvent, @NotNull Point point) {
((EditorImpl) this.editor).setCustomCursor(this,
Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}

public int calcWidthInPixels(@NotNull Inlay inlay) {
return Icons.Default.getIconWidth();
}

public void paint(@NotNull Inlay inlay, @NotNull Graphics g, @NotNull Rectangle r,
@NotNull TextAttributes textAttributes) {
Icon consoleIcon = Icons.Default;
int curX = r.x + r.width / 2 - consoleIcon.getIconWidth() / 2;
int curY = r.y + r.height / 2 - consoleIcon.getIconHeight() / 2;
consoleIcon.paintIcon(inlay.getEditor().getComponent(), g, curX, curY);
}
}
5 changes: 4 additions & 1 deletion src/main/resources/META-INF/plugin-java.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@
<listener topic="com.intellij.openapi.compiler.CompilationStatusListener"
class="ee.carlrobert.codegpt.ProjectCompilationStatusListener" />
</projectListeners>
</idea-plugin>
<extensions defaultExtensionNs="com.intellij">
<jvm.exceptionFilter implementation="ee.carlrobert.codegpt.filter.CodeGPTJvmExceptionFilter"/>
</extensions>
</idea-plugin>