forked from 0verEngineer/InlineProblems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInlineProblemLabel.java
More file actions
229 lines (197 loc) · 8.45 KB
/
InlineProblemLabel.java
File metadata and controls
229 lines (197 loc) · 8.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package org.overengineer.inlineproblems;
import com.intellij.codeInsight.hints.presentation.InputHandler;
import com.intellij.codeInsight.intention.impl.ShowIntentionActionsHandler;
import com.intellij.ide.ui.AntialiasingType;
import com.intellij.ide.ui.UISettings;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.IdeActions;
import com.intellij.openapi.actionSystem.ex.ActionUtil;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.EditorCustomElementRenderer;
import com.intellij.openapi.editor.Inlay;
import com.intellij.openapi.editor.ScrollType;
import com.intellij.openapi.editor.impl.FontInfo;
import com.intellij.openapi.editor.markup.GutterIconRenderer;
import com.intellij.openapi.editor.markup.TextAttributes;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiFile;
import com.intellij.psi.util.PsiUtilBase;
import com.intellij.ui.paint.EffectPainter;
import lombok.Getter;
import lombok.Setter;
import org.jdesktop.swingx.action.ActionManager;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.overengineer.inlineproblems.entities.InlineProblem;
import org.overengineer.inlineproblems.settings.SettingsState;
import org.overengineer.inlineproblems.utils.FontUtil;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.font.FontRenderContext;
@Getter
public class InlineProblemLabel implements EditorCustomElementRenderer, InputHandler {
private final String text;
private final Color textColor;
private final Color backgroundColor;
private final Color hoverColor;
private final boolean isDrawBox;
private final boolean isRoundedCorners;
private final boolean isFillBackground;
private boolean hovered;
private final boolean clickableContext;
private Inlay<?> inlay;
private final int actualStartOffset;
@Setter
private boolean isBlockElement;
private final int inlayFontSizeDelta;
private final boolean isUseEditorFont;
private static final int WIDTH_OFFSET = 7;
private static final int DRAW_BOX_HEIGHT_OFFSET = -2; // Makes the box lines visible even if line below / above is highlighted
private static final int DRAW_BOX_WIDTH_OFFSET = -2; // To have space between 2 boxes
private static final int DRAW_BOX_PLACEMENT_OFFSET_Y = 1;
private static final int DRAW_STRING_LINE_PLACEMENT_OFFSET_Y = -1;
private static final int DRAW_STRING_LINE_PLACEMENT_OFFSET_X = 3;
public InlineProblemLabel(
final InlineProblem problem,
final Color textColor,
final Color backgroundColor,
final Color hoverColor,
final SettingsState settings
) {
this.textColor = textColor;
this.backgroundColor = backgroundColor;
this.hoverColor = hoverColor;
this.isDrawBox = settings.isDrawBoxesAroundErrorLabels();
this.isRoundedCorners = settings.isRoundedCornerBoxes();
this.text = problem.getText();
this.isBlockElement = false;
this.isFillBackground = settings.isFillProblemLabels();
this.isUseEditorFont = settings.isUseEditorFont();
this.inlayFontSizeDelta = settings.getInlayFontSizeDelta();
this.hovered = false;
this.actualStartOffset = problem.getActualStartffset();
this.clickableContext = settings.isClickableContext();
}
@Override
public int calcWidthInPixels(@NotNull Inlay inlay) {
return calcWidthInPixels(inlay.getEditor());
}
public int calcWidthInPixels(@NotNull Editor editor) {
var editorContext = FontInfo.getFontRenderContext(editor.getComponent());
var context = new FontRenderContext(editorContext.getTransform(),
AntialiasingType.getKeyForCurrentScope(false),
UISettings.getEditorFractionalMetricsHint());
var fontMetrics = FontInfo.getFontMetrics(FontUtil.getActiveFont(editor), context);
return fontMetrics.stringWidth(text) + WIDTH_OFFSET;
}
@Override
public void paint(@NotNull Inlay inlay, @NotNull Graphics graphics, @NotNull Rectangle targetRegion, @NotNull TextAttributes textAttributes) {
Editor editor = inlay.getEditor();
this.inlay = inlay;
// These offsets are applied here and not in the calc functions itself because we use it to shrink the drawn stuff a little bit
int width = calcWidthInPixels(inlay) + DRAW_BOX_WIDTH_OFFSET;
int height = calcHeightInPixels(inlay) + DRAW_BOX_HEIGHT_OFFSET;
int targetRegionY = targetRegion.y + DRAW_BOX_PLACEMENT_OFFSET_Y;
int editorFontSize = editor.getColorsScheme().getEditorFontSize();
// Apply delta on the boxes
if (inlayFontSizeDelta != 0 && editorFontSize > inlayFontSizeDelta) {
height -= inlayFontSizeDelta;
targetRegionY += (int) (inlayFontSizeDelta / 1.5);
}
if (isDrawBox) {
graphics.setColor(backgroundColor);
if (isRoundedCorners) {
graphics.drawRoundRect(
targetRegion.x,
targetRegionY,
width,
height,
5,
5
);
if (isFillBackground) {
graphics.fillRoundRect(
targetRegion.x,
targetRegionY,
width,
height,
5,
5
);
}
} else {
graphics.drawRect(
targetRegion.x,
targetRegionY,
width,
height
);
if (isFillBackground) {
graphics.fillRect(
targetRegion.x,
targetRegionY,
width,
height
);
}
}
}
graphics.setColor(hovered ? hoverColor : textColor);
graphics.setFont(FontUtil.getActiveFont(editor));
graphics.drawString(
text,
targetRegion.x + DRAW_STRING_LINE_PLACEMENT_OFFSET_X,
targetRegion.y + DRAW_STRING_LINE_PLACEMENT_OFFSET_Y + editor.getAscent()
);
if (hovered)
EffectPainter.LINE_UNDERSCORE.paint(
(Graphics2D) graphics,
targetRegion.x - DRAW_BOX_WIDTH_OFFSET,
targetRegion.y + editor.getAscent() ,
width + (DRAW_BOX_WIDTH_OFFSET * 2),
editor.getAscent(),
FontUtil.getActiveFont(editor));
}
private void setHovered(boolean hovered) {
if (!this.clickableContext || this.hovered == hovered) {
return;
}
this.hovered = hovered;
if (inlay != null)
inlay.repaint();
}
@Override
public @Nullable GutterIconRenderer calcGutterIconRenderer(@NotNull Inlay inlay) {
return EditorCustomElementRenderer.super.calcGutterIconRenderer(inlay);
}
@Override
public void mouseClicked(@NotNull MouseEvent mouseEvent, @NotNull Point point) {
if (!clickableContext) {
return;
}
if (mouseEvent.getButton() == MouseEvent.BUTTON1) {
mouseEvent.consume();
var editor = inlay.getEditor();
editor.getCaretModel().moveToOffset(actualStartOffset);
editor.getScrollingModel().scrollToCaret(ScrollType.CENTER);
var action = ActionManager.getInstance().getAction(IdeActions.ACTION_SHOW_INTENTION_ACTIONS);
if (action == null) {
Project project = editor.getProject();
if (project == null) return;
PsiFile psiFileInEditor = PsiUtilBase.getPsiFileInEditor(editor, project);
if (psiFileInEditor == null) return;
new ShowIntentionActionsHandler().invoke(project, editor, psiFileInEditor, false);
} else {
ActionUtil.invokeAction((AnAction) action, editor.getComponent(), "EditorInlay", null, null);
}
}
}
@Override
public void mouseMoved(@NotNull MouseEvent mouseEvent, @NotNull Point point) {
setHovered(true);
}
@Override
public void mouseExited() {
setHovered(false);
}
}