Skip to content

Commit 42d9ba4

Browse files
introduce getMouseHover, getMouseOut and getMouseMove in ICodeMining
This change allows code mining implementors to react on mouse move events and call setCursor on the text widget for example.
1 parent 55481d3 commit 42d9ba4

File tree

9 files changed

+171
-18
lines changed

9 files changed

+171
-18
lines changed

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: 8 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,12 @@ 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= first != null ? first.getMouseHover() : null;
268+
Consumer<MouseEvent> mouseOut= first != null ? first.getMouseOut() : null;
269+
Consumer<MouseEvent> mouseMove= first != null ? first.getMouseMove() : null;
270+
ann= inLineHeader
271+
? new CodeMiningLineHeaderAnnotation(pos, viewer, mouseHover, mouseOut, mouseMove)
272+
: new CodeMiningLineContentAnnotation(pos, viewer, afterPosition, mouseHover, mouseOut, mouseMove);
266273
} else if (ann instanceof ICodeMiningAnnotation && ((ICodeMiningAnnotation) ann).isInVisibleLines()) {
267274
// annotation is in visible lines
268275
annotationsToRedraw.add((ICodeMiningAnnotation) ann);

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

Lines changed: 32 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,37 @@ 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+
default Consumer<MouseEvent> getMouseHover() {
124+
return e -> {
125+
if (e.widget instanceof StyledText st) {
126+
st.setCursor(st.getDisplay().getSystemCursor(SWT.CURSOR_HAND));
127+
}
128+
};
129+
}
130+
131+
/**
132+
* Returns a consumer which is called when mouse is moved out of code mining. If set, the
133+
* implementor needs to take care of resetting the cursor.
134+
*/
135+
default Consumer<MouseEvent> getMouseOut() {
136+
return e -> {
137+
if (e.widget instanceof StyledText st) {
138+
st.setCursor(null);
139+
}
140+
};
141+
}
142+
143+
/**
144+
* Returns a consumer which is called when mouse is moved inside the code mining.
145+
*/
146+
default Consumer<MouseEvent> getMouseMove() {
147+
return null;
148+
}
149+
118150
/**
119151
* Dispose the mining. Typically shuts down or cancels all related asynchronous operations.
120152
*/

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

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,39 @@ 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+
*/
92+
protected AbstractInlinedAnnotation(Position position, ISourceViewer viewer, Consumer<MouseEvent> onMouseHover, Consumer<MouseEvent> onMouseOut, Consumer<MouseEvent> onMouseMove) {
7293
super(TYPE, false, ""); //$NON-NLS-1$
7394
this.position= position;
95+
this.onMouseHover= onMouseHover;
96+
this.onMouseOut= onMouseOut;
97+
this.onMouseMove= onMouseMove;
7498
}
7599

76100
/**
@@ -165,8 +189,23 @@ public void draw(GC gc, StyledText textWidget, int widgetOffset, int length, Col
165189
* @param e the mouse event
166190
*/
167191
public void onMouseHover(MouseEvent e) {
168-
StyledText styledText= (StyledText) e.widget;
169-
styledText.setCursor(styledText.getDisplay().getSystemCursor(SWT.CURSOR_HAND));
192+
if (onMouseHover != null) {
193+
onMouseHover.accept(e);
194+
} else {
195+
StyledText styledText= (StyledText) e.widget;
196+
styledText.setCursor(styledText.getDisplay().getSystemCursor(SWT.CURSOR_HAND));
197+
}
198+
}
199+
200+
/**
201+
* Called when mouse moved in the inlined annotation.
202+
*
203+
* @param e the mouse event
204+
*/
205+
public void onMouseMove(MouseEvent e) {
206+
if (onMouseMove != null) {
207+
onMouseMove.accept(e);
208+
}
170209
}
171210

172211
/**
@@ -175,8 +214,12 @@ public void onMouseHover(MouseEvent e) {
175214
* @param e the mouse event
176215
*/
177216
public void onMouseOut(MouseEvent e) {
178-
StyledText styledText= (StyledText) e.widget;
179-
styledText.setCursor(null);
217+
if (onMouseOut != null) {
218+
onMouseOut.accept(e);
219+
} else {
220+
StyledText styledText= (StyledText) e.widget;
221+
styledText.setCursor(null);
222+
}
180223
}
181224

182225
/**

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: 19 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,22 @@ 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+
*/
68+
public LineContentAnnotation(Position position, ISourceViewer viewer, Consumer<MouseEvent> onMouseHover, Consumer<MouseEvent> onMouseOut, Consumer<MouseEvent> onMouseMove) {
69+
super(position, viewer, onMouseHover, onMouseOut, onMouseMove);
5270
}
5371

5472
/**

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

Lines changed: 19 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,22 @@ 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+
*/
54+
public LineHeaderAnnotation(Position position, ISourceViewer viewer, Consumer<MouseEvent> onMouseHover, Consumer<MouseEvent> onMouseOut, Consumer<MouseEvent> onMouseMove) {
55+
super(position, viewer, onMouseHover, onMouseOut, onMouseMove);
3856
oldLine= -1;
3957
}
4058

0 commit comments

Comments
 (0)