Skip to content

Commit 9f3733a

Browse files
introduce getMouseHover, getMouseOut and getMouseMove in ICodeMining (#2770)
This change allows code mining implementors to react on mouse move events and call setCursor on the text widget for example. Co-authored-by: Tobias Melcher <[email protected]>
1 parent 0552cdc commit 9f3733a

File tree

10 files changed

+209
-18
lines changed

10 files changed

+209
-18
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<component id="org.eclipse.jface.text" version="2">
3+
<resource path="src/org/eclipse/jface/text/codemining/ICodeMining.java" type="org.eclipse.jface.text.codemining.ICodeMining">
4+
<filter id="404000815">
5+
<message_arguments>
6+
<message_argument value="org.eclipse.jface.text.codemining.ICodeMining"/>
7+
<message_argument value="getMouseHover()"/>
8+
</message_arguments>
9+
</filter>
10+
<filter id="404000815">
11+
<message_arguments>
12+
<message_argument value="org.eclipse.jface.text.codemining.ICodeMining"/>
13+
<message_argument value="getMouseMove()"/>
14+
</message_arguments>
15+
</filter>
16+
<filter id="404000815">
17+
<message_arguments>
18+
<message_argument value="org.eclipse.jface.text.codemining.ICodeMining"/>
19+
<message_argument value="getMouseOut()"/>
20+
</message_arguments>
21+
</filter>
22+
</resource>
23+
</component>

bundles/org.eclipse.jface.text/src/org/eclipse/jface/internal/text/codemining/CodeMiningLineContentAnnotation.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,7 @@ public class CodeMiningLineContentAnnotation extends LineContentAnnotation imple
7070
* @param viewer the viewer
7171
*/
7272
public CodeMiningLineContentAnnotation(Position position, ISourceViewer viewer) {
73-
super(position, viewer);
74-
fResolvedMinings= null;
75-
fMinings= new ArrayList<>();
76-
fBounds= new ArrayList<>();
77-
afterPosition= false;
73+
this(position, viewer, false);
7874
}
7975

8076
/**
@@ -84,7 +80,23 @@ public CodeMiningLineContentAnnotation(Position position, ISourceViewer viewer)
8480
* @param viewer the viewer
8581
*/
8682
public CodeMiningLineContentAnnotation(Position position, ISourceViewer viewer, boolean afterPosition) {
87-
super(position, viewer);
83+
this(position, viewer, afterPosition, null, null, null);
84+
}
85+
86+
/**
87+
* Code mining annotation constructor.
88+
*
89+
* @param position the position
90+
* @param viewer the viewer
91+
* @param onMouseHover the consumer to be called on mouse hover. If set, the implementor needs
92+
* to take care of setting the cursor if wanted.
93+
* @param onMouseOut the consumer to be called on mouse out. If set, the implementor needs to
94+
* take care of resetting the cursor.
95+
* @param onMouseMove the consumer to be called on mouse move
96+
*/
97+
public CodeMiningLineContentAnnotation(Position position, ISourceViewer viewer, boolean afterPosition, Consumer<MouseEvent> onMouseHover, Consumer<MouseEvent> onMouseOut,
98+
Consumer<MouseEvent> onMouseMove) {
99+
super(position, viewer, onMouseHover, onMouseOut, onMouseMove);
88100
fResolvedMinings= null;
89101
fMinings= new ArrayList<>();
90102
fBounds= new ArrayList<>();

bundles/org.eclipse.jface.text/src/org/eclipse/jface/internal/text/codemining/CodeMiningLineHeaderAnnotation.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,22 @@ public class CodeMiningLineHeaderAnnotation extends LineHeaderAnnotation impleme
6969
* @param viewer the viewer
7070
*/
7171
public CodeMiningLineHeaderAnnotation(Position position, ISourceViewer viewer) {
72-
super(position, viewer);
72+
this(position, viewer, null, null, null);
73+
}
74+
75+
/**
76+
* Code mining annotation constructor.
77+
*
78+
* @param position the position
79+
* @param viewer the viewer
80+
* @param onMouseHover the consumer to be called on mouse hover. If set, the implementor needs
81+
* to take care of setting the cursor if wanted.
82+
* @param onMouseOut the consumer to be called on mouse out. If set, the implementor needs to
83+
* take care of resetting the cursor.
84+
* @param onMouseMove the consumer to be called on mouse move
85+
*/
86+
public CodeMiningLineHeaderAnnotation(Position position, ISourceViewer viewer, Consumer<MouseEvent> onMouseHover, Consumer<MouseEvent> onMouseOut, Consumer<MouseEvent> onMouseMove) {
87+
super(position, viewer, onMouseHover, onMouseOut, onMouseMove);
7388
fResolvedMinings= null;
7489
fMinings= new ArrayList<>();
7590
fBounds= new ArrayList<>();

bundles/org.eclipse.jface.text/src/org/eclipse/jface/internal/text/codemining/CodeMiningManager.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@
2323
import java.util.Set;
2424
import java.util.concurrent.CancellationException;
2525
import java.util.concurrent.CompletableFuture;
26+
import java.util.function.Consumer;
2627
import java.util.function.Function;
2728
import java.util.stream.Collectors;
2829

2930
import org.osgi.framework.Bundle;
3031

32+
import org.eclipse.swt.events.MouseEvent;
3133
import org.eclipse.swt.graphics.Rectangle;
3234

3335
import org.eclipse.core.runtime.Assert;
@@ -262,7 +264,17 @@ private void renderCodeMinings(Map<Position, List<ICodeMining>> groups, ISourceV
262264
if (first instanceof LineContentCodeMining m) {
263265
afterPosition= m.isAfterPosition();
264266
}
265-
ann= inLineHeader ? new CodeMiningLineHeaderAnnotation(pos, viewer) : new CodeMiningLineContentAnnotation(pos, viewer, afterPosition);
267+
Consumer<MouseEvent> mouseHover= null;
268+
Consumer<MouseEvent> mouseOut= null;
269+
Consumer<MouseEvent> mouseMove= null;
270+
if (first != null) {
271+
mouseHover= first.getMouseHover();
272+
mouseOut= first.getMouseOut();
273+
mouseMove= first.getMouseMove();
274+
}
275+
ann= inLineHeader
276+
? new CodeMiningLineHeaderAnnotation(pos, viewer, mouseHover, mouseOut, mouseMove)
277+
: new CodeMiningLineContentAnnotation(pos, viewer, afterPosition, mouseHover, mouseOut, mouseMove);
266278
} else if (ann instanceof ICodeMiningAnnotation && ((ICodeMiningAnnotation) ann).isInVisibleLines()) {
267279
// annotation is in visible lines
268280
annotationsToRedraw.add((ICodeMiningAnnotation) ann);

bundles/org.eclipse.jface.text/src/org/eclipse/jface/text/codemining/ICodeMining.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.util.concurrent.CompletableFuture;
1717
import java.util.function.Consumer;
1818

19+
import org.eclipse.swt.SWT;
1920
import org.eclipse.swt.custom.StyledText;
2021
import org.eclipse.swt.events.MouseEvent;
2122
import org.eclipse.swt.graphics.Color;
@@ -115,6 +116,43 @@ public interface ICodeMining {
115116
*/
116117
Consumer<MouseEvent> getAction();
117118

119+
/**
120+
* Returns a consumer which is called when mouse is hovered over the code mining. If set, the
121+
* implementor needs to take care of setting the cursor if wanted.
122+
*
123+
* @since 3.27
124+
*/
125+
default Consumer<MouseEvent> getMouseHover() {
126+
return e -> {
127+
if (e.widget instanceof StyledText st) {
128+
st.setCursor(st.getDisplay().getSystemCursor(SWT.CURSOR_HAND));
129+
}
130+
};
131+
}
132+
133+
/**
134+
* Returns a consumer which is called when mouse is moved out of code mining. If set, the
135+
* implementor needs to take care of resetting the cursor.
136+
*
137+
* @since 3.27
138+
*/
139+
default Consumer<MouseEvent> getMouseOut() {
140+
return e -> {
141+
if (e.widget instanceof StyledText st) {
142+
st.setCursor(null);
143+
}
144+
};
145+
}
146+
147+
/**
148+
* Returns a consumer which is called when mouse is moved inside the code mining.
149+
*
150+
* @since 3.27
151+
*/
152+
default Consumer<MouseEvent> getMouseMove() {
153+
return null;
154+
}
155+
118156
/**
119157
* Dispose the mining. Typically shuts down or cancels all related asynchronous operations.
120158
*/

bundles/org.eclipse.jface.text/src/org/eclipse/jface/text/source/inlined/AbstractInlinedAnnotation.java

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,40 @@ public abstract class AbstractInlinedAnnotation extends Annotation {
6262

6363
int fY;
6464

65+
private final Consumer<MouseEvent> onMouseHover;
66+
67+
private final Consumer<MouseEvent> onMouseOut;
68+
69+
private final Consumer<MouseEvent> onMouseMove;
70+
6571
/**
6672
* Inlined annotation constructor.
6773
*
6874
* @param position the position where the annotation must be drawn.
69-
* @param viewer the {@link ISourceViewer} where the annotation must be drawn.
75+
* @param viewer the {@link ISourceViewer} where the annotation must be drawn.
7076
*/
7177
protected AbstractInlinedAnnotation(Position position, ISourceViewer viewer) {
78+
this(position, viewer, null, null, null);
79+
}
80+
81+
/**
82+
* Inlined annotation constructor.
83+
*
84+
* @param position the position where the annotation must be drawn.
85+
* @param viewer the {@link ISourceViewer} where the annotation must be drawn.
86+
* @param onMouseHover the consumer to be called on mouse hover. If set, the implementor needs
87+
* to take care of setting the cursor if wanted.
88+
* @param onMouseOut the consumer to be called on mouse out. If set, the implementor needs to
89+
* take care of resetting the cursor.
90+
* @param onMouseMove the consumer to be called on mouse move
91+
* @since 3.27
92+
*/
93+
protected AbstractInlinedAnnotation(Position position, ISourceViewer viewer, Consumer<MouseEvent> onMouseHover, Consumer<MouseEvent> onMouseOut, Consumer<MouseEvent> onMouseMove) {
7294
super(TYPE, false, ""); //$NON-NLS-1$
7395
this.position= position;
96+
this.onMouseHover= onMouseHover;
97+
this.onMouseOut= onMouseOut;
98+
this.onMouseMove= onMouseMove;
7499
}
75100

76101
/**
@@ -165,8 +190,24 @@ public void draw(GC gc, StyledText textWidget, int widgetOffset, int length, Col
165190
* @param e the mouse event
166191
*/
167192
public void onMouseHover(MouseEvent e) {
168-
StyledText styledText= (StyledText) e.widget;
169-
styledText.setCursor(styledText.getDisplay().getSystemCursor(SWT.CURSOR_HAND));
193+
if (onMouseHover != null) {
194+
onMouseHover.accept(e);
195+
} else {
196+
StyledText styledText= (StyledText) e.widget;
197+
styledText.setCursor(styledText.getDisplay().getSystemCursor(SWT.CURSOR_HAND));
198+
}
199+
}
200+
201+
/**
202+
* Called when mouse moved in the inlined annotation.
203+
*
204+
* @param e the mouse event
205+
* @since 3.27
206+
*/
207+
public void onMouseMove(MouseEvent e) {
208+
if (onMouseMove != null) {
209+
onMouseMove.accept(e);
210+
}
170211
}
171212

172213
/**
@@ -175,8 +216,12 @@ public void onMouseHover(MouseEvent e) {
175216
* @param e the mouse event
176217
*/
177218
public void onMouseOut(MouseEvent e) {
178-
StyledText styledText= (StyledText) e.widget;
179-
styledText.setCursor(null);
219+
if (onMouseOut != null) {
220+
onMouseOut.accept(e);
221+
} else {
222+
StyledText styledText= (StyledText) e.widget;
223+
styledText.setCursor(null);
224+
}
180225
}
181226

182227
/**

bundles/org.eclipse.jface.text/src/org/eclipse/jface/text/source/inlined/InlinedAnnotationDrawingStrategy.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,17 @@ private static void draw(LineHeaderAnnotation annotation, GC gc, StyledText text
198198
annotation.draw(gc, textWidget, offset, length, color, x, y);
199199
} else if (textWidget.getLineVerticalIndent(line) > 0) {
200200
// Here vertical indent is done, the redraw of the full line width is done to avoid annotation clipping
201-
Rectangle bounds= textWidget.getTextBounds(offset, offset);
202201
Rectangle client= textWidget.getClientArea();
203-
textWidget.redraw(0, bounds.y, client.width, bounds.height, false);
202+
int y, height;
203+
if (offset < charCount) {
204+
Rectangle bounds= textWidget.getTextBounds(offset, offset);
205+
y= bounds.y;
206+
height= bounds.height;
207+
} else {
208+
y= 0;
209+
height= client.height;
210+
}
211+
textWidget.redraw(0, y, client.width, height, false);
204212
} else {
205213
if (offset >= charCount) {
206214
if (charCount > 0) {

bundles/org.eclipse.jface.text/src/org/eclipse/jface/text/source/inlined/InlinedAnnotationSupport.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ public void mouseMove(MouseEvent e) {
290290
update(e);
291291
if (oldAnnotation != null) {
292292
if (oldAnnotation.equals(fAnnotation)) {
293-
// Same annotations which was hovered, do nothing.
293+
fAnnotation.onMouseMove(e);
294294
return;
295295
} else {
296296
oldAnnotation.onMouseOut(e);

bundles/org.eclipse.jface.text/src/org/eclipse/jface/text/source/inlined/LineContentAnnotation.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@
1313
*/
1414
package org.eclipse.jface.text.source.inlined;
1515

16+
import java.util.function.Consumer;
17+
1618
import org.eclipse.swt.custom.StyleRange;
1719
import org.eclipse.swt.custom.StyledText;
20+
import org.eclipse.swt.events.MouseEvent;
1821
import org.eclipse.swt.graphics.Color;
1922
import org.eclipse.swt.graphics.FontMetrics;
2023
import org.eclipse.swt.graphics.GC;
@@ -48,7 +51,23 @@ public class LineContentAnnotation extends AbstractInlinedAnnotation {
4851
* @param viewer the {@link ISourceViewer} where the annotation must be drawn.
4952
*/
5053
public LineContentAnnotation(Position position, ISourceViewer viewer) {
51-
super(position, viewer);
54+
this(position, viewer, null, null, null);
55+
}
56+
57+
/**
58+
* Line content annotation constructor.
59+
*
60+
* @param position the position where the annotation must be drawn.
61+
* @param viewer the {@link ISourceViewer} where the annotation must be drawn.
62+
* @param onMouseHover the consumer to be called on mouse hover. If set, the implementor needs
63+
* to take care of setting the cursor if wanted.
64+
* @param onMouseOut the consumer to be called on mouse out. If set, the implementor needs to
65+
* take care of resetting the cursor.
66+
* @param onMouseMove the consumer to be called on mouse move
67+
* @since 3.27
68+
*/
69+
public LineContentAnnotation(Position position, ISourceViewer viewer, Consumer<MouseEvent> onMouseHover, Consumer<MouseEvent> onMouseOut, Consumer<MouseEvent> onMouseMove) {
70+
super(position, viewer, onMouseHover, onMouseOut, onMouseMove);
5271
}
5372

5473
/**

bundles/org.eclipse.jface.text/src/org/eclipse/jface/text/source/inlined/LineHeaderAnnotation.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
*/
1414
package org.eclipse.jface.text.source.inlined;
1515

16+
import java.util.function.Consumer;
17+
1618
import org.eclipse.swt.custom.StyledText;
19+
import org.eclipse.swt.events.MouseEvent;
1720

1821
import org.eclipse.jface.text.Position;
1922
import org.eclipse.jface.text.source.ISourceViewer;
@@ -34,7 +37,23 @@ public class LineHeaderAnnotation extends AbstractInlinedAnnotation {
3437
* @param viewer the {@link ISourceViewer} where the annotation must be drawn.
3538
*/
3639
public LineHeaderAnnotation(Position position, ISourceViewer viewer) {
37-
super(position, viewer);
40+
this(position, viewer, null, null, null);
41+
}
42+
43+
/**
44+
* Line header annotation constructor.
45+
*
46+
* @param position the position where the annotation must be drawn.
47+
* @param viewer the {@link ISourceViewer} where the annotation must be drawn.
48+
* @param onMouseHover the consumer to be called on mouse hover. If set, the implementor needs
49+
* to take care of setting the cursor if wanted.
50+
* @param onMouseOut the consumer to be called on mouse out. If set, the implementor needs to
51+
* take care of resetting the cursor.
52+
* @param onMouseMove the consumer to be called on mouse move
53+
* @since 3.27
54+
*/
55+
public LineHeaderAnnotation(Position position, ISourceViewer viewer, Consumer<MouseEvent> onMouseHover, Consumer<MouseEvent> onMouseOut, Consumer<MouseEvent> onMouseMove) {
56+
super(position, viewer, onMouseHover, onMouseOut, onMouseMove);
3857
oldLine= -1;
3958
}
4059

0 commit comments

Comments
 (0)