|
| 1 | +package ee.carlrobert.codegpt.filter; |
| 2 | + |
| 3 | +import com.intellij.codeInsight.hints.presentation.InputHandler; |
| 4 | +import com.intellij.openapi.editor.Editor; |
| 5 | +import com.intellij.openapi.editor.EditorCustomElementRenderer; |
| 6 | +import com.intellij.openapi.editor.Inlay; |
| 7 | +import com.intellij.openapi.editor.impl.EditorImpl; |
| 8 | +import com.intellij.openapi.editor.markup.TextAttributes; |
| 9 | +import com.intellij.openapi.project.Project; |
| 10 | +import com.intellij.openapi.util.TextRange; |
| 11 | +import ee.carlrobert.codegpt.Icons; |
| 12 | +import java.awt.Cursor; |
| 13 | +import java.awt.Graphics; |
| 14 | +import java.awt.Point; |
| 15 | +import java.awt.Rectangle; |
| 16 | +import java.awt.event.MouseEvent; |
| 17 | +import javax.swing.*; |
| 18 | + |
| 19 | +import ee.carlrobert.codegpt.conversations.message.Message; |
| 20 | +import ee.carlrobert.codegpt.toolwindow.chat.standard.StandardChatToolWindowContentManager; |
| 21 | +import org.jetbrains.annotations.NotNull; |
| 22 | + |
| 23 | +public class CodeGPTPresentation implements EditorCustomElementRenderer, InputHandler { |
| 24 | + |
| 25 | + private final Editor editor; |
| 26 | + |
| 27 | + private final Project project; |
| 28 | + |
| 29 | + private final int startOffset; |
| 30 | + |
| 31 | + |
| 32 | + public CodeGPTPresentation(Editor editor, Project project, int starOffset) { |
| 33 | + this.editor = editor; |
| 34 | + this.project = project; |
| 35 | + this.startOffset = starOffset; |
| 36 | + } |
| 37 | + |
| 38 | + public void mouseClicked(@NotNull MouseEvent mouseEvent, @NotNull Point point) { |
| 39 | + int line = this.editor.getDocument().getLineNumber(this.startOffset); |
| 40 | + String errorInformation = this.editor.getDocument() |
| 41 | + .getText(new TextRange(this.startOffset, this.editor.getDocument().getLineEndOffset(line))); |
| 42 | + String prompt = "Provide recommendations for the error message: {{errorInformation}}"; |
| 43 | + var message = new Message(prompt.replace("{{errorInformation}}", errorInformation)); |
| 44 | + SwingUtilities.invokeLater( |
| 45 | + () -> project.getService(StandardChatToolWindowContentManager.class).sendMessage(message)); |
| 46 | + } |
| 47 | + |
| 48 | + public void mouseExited() { |
| 49 | + ((EditorImpl) this.editor).setCustomCursor(this, |
| 50 | + Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR)); |
| 51 | + } |
| 52 | + |
| 53 | + public void mouseMoved(@NotNull MouseEvent mouseEvent, @NotNull Point point) { |
| 54 | + ((EditorImpl) this.editor).setCustomCursor(this, |
| 55 | + Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); |
| 56 | + } |
| 57 | + |
| 58 | + public int calcWidthInPixels(@NotNull Inlay inlay) { |
| 59 | + return Icons.DefaultIcon.getIconWidth(); |
| 60 | + } |
| 61 | + |
| 62 | + public void paint(@NotNull Inlay inlay, @NotNull Graphics g, @NotNull Rectangle r, |
| 63 | + @NotNull TextAttributes textAttributes) { |
| 64 | + Icon consoleIcon = Icons.DefaultIcon; |
| 65 | + int curX = r.x + r.width / 2 - consoleIcon.getIconWidth() / 2; |
| 66 | + int curY = r.y + r.height / 2 - consoleIcon.getIconHeight() / 2; |
| 67 | + consoleIcon.paintIcon(inlay.getEditor().getComponent(), g, curX, curY); |
| 68 | + } |
| 69 | +} |
0 commit comments