Skip to content

Commit c46e964

Browse files
committed
Make sure the Find highlights are visible also in HTML documents with custom background (OQL results).
1 parent dceac81 commit c46e964

File tree

1 file changed

+45
-8
lines changed

1 file changed

+45
-8
lines changed

visualvm/libs.profiler/lib.profiler.ui/src/org/graalvm/visualvm/lib/ui/components/HTMLTextAreaSearchUtils.java

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
import javax.swing.text.Document;
7979
import javax.swing.text.Highlighter;
8080
import javax.swing.text.JTextComponent;
81+
import javax.swing.text.View;
8182
import org.graalvm.visualvm.lib.profiler.api.ActionsSupport;
8283
import org.graalvm.visualvm.lib.profiler.api.icons.GeneralIcons;
8384
import org.graalvm.visualvm.lib.profiler.api.icons.Icons;
@@ -230,7 +231,7 @@ void highlightResults(Highlighter hl) {
230231
clearHighlightedResults(hl);
231232

232233
for (int offset : result) {
233-
try { highlights.add(hl.addHighlight(offset, offset + search.length(), new ResultsHighlightPainter())); }
234+
try { highlights.add(hl.addHighlight(offset, offset + search.length(), new CustomHighlightPainter())); }
234235
catch (BadLocationException ex) {}
235236
}
236237
}
@@ -270,14 +271,50 @@ int getPreviousIndex(int offset) {
270271
}
271272

272273

273-
// NOTE: must not be direct subclass of DefaultHighlightPainter to not overlap selection
274-
private static final class ResultsHighlightPainter implements Highlighter.HighlightPainter {
274+
// // NOTE: must not be direct subclass of DefaultHighlightPainter to not overlap selection
275+
// private static final class ResultsHighlightPainter implements Highlighter.HighlightPainter {
276+
//
277+
// private static final Highlighter.HighlightPainter IMPL = new DefaultHighlightPainter(Color.ORANGE);
278+
//
279+
// @Override
280+
// public void paint(Graphics g, int p0, int p1, Shape bounds, JTextComponent c) {
281+
// IMPL.paint(g, p0, p1, bounds, c);
282+
// }
283+
//
284+
// }
285+
286+
287+
private static final class CustomHighlightPainter extends DefaultHighlightPainter {
275288

276-
private static final Highlighter.HighlightPainter IMPL = new DefaultHighlightPainter(Color.ORANGE);
277-
278-
@Override
279-
public void paint(Graphics g, int p0, int p1, Shape bounds, JTextComponent c) {
280-
IMPL.paint(g, p0, p1, bounds, c);
289+
CustomHighlightPainter() {
290+
super(Color.ORANGE);
291+
}
292+
293+
public Shape paintLayer(Graphics g, int offs0, int offs1,
294+
Shape bounds, JTextComponent c, View view) {
295+
296+
int selStart = c.getSelectionStart();
297+
int selEnd = c.getSelectionEnd();
298+
299+
// No selection or selection fully outside of the highlight
300+
if (selEnd - selStart == 0 || offs0 >= selEnd || offs1 <= selStart) return super.paintLayer(g, offs0, offs1, bounds, c, view);
301+
302+
// Selection fully covers the highlight
303+
if (offs0 >= selStart && offs1 <= selEnd) return bounds;
304+
305+
// Selection partially covers the highlight
306+
if (offs0 < selStart || offs1 > selEnd) {
307+
// Selection ends inside of the highlight
308+
if (offs0 >= selStart) return super.paintLayer(g, selEnd, offs1, bounds, c, view);
309+
// Selection starts inside of the highlight
310+
else if (offs1 <= selEnd) return super.paintLayer(g, offs0, selStart, bounds, c, view);
311+
312+
// Selection fully inside of the highlight
313+
super.paintLayer(g, offs0, selStart, bounds, c, view);
314+
super.paintLayer(g, selEnd, offs1, bounds, c, view);
315+
}
316+
317+
return bounds;
281318
}
282319

283320
}

0 commit comments

Comments
 (0)