Skip to content

Commit 4df0397

Browse files
committed
Fix #3405: Ensure that code mining type and annotation type fit together
1 parent cc290be commit 4df0397

File tree

1 file changed

+36
-13
lines changed

1 file changed

+36
-13
lines changed

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

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,20 @@ private static Map<Position, List<ICodeMining>> groupByLines(List<? extends ICod
230230
Collectors.mapping(Function.identity(), Collectors.toList())));
231231
}
232232

233+
private enum CodeMiningMode {
234+
InLine(LineContentCodeMining.class, CodeMiningLineContentAnnotation.class), HeaderLine(LineHeaderCodeMining.class,
235+
CodeMiningLineHeaderAnnotation.class), FooterLine(DocumentFooterCodeMining.class, CodeMiningDocumentFooterAnnotation.class);
236+
237+
public Class<? extends ICodeMining> codeMiningType;
238+
public Class<? extends AbstractInlinedAnnotation> annotationType;
239+
240+
CodeMiningMode(Class<? extends ICodeMining> codeMiningType,
241+
Class<? extends AbstractInlinedAnnotation> annotationType) {
242+
this.codeMiningType= codeMiningType;
243+
this.annotationType= annotationType;
244+
}
245+
}
246+
233247
/**
234248
* Render the codemining grouped by line position.
235249
*
@@ -257,11 +271,22 @@ private void renderCodeMinings(Map<Position, List<ICodeMining>> groups, ISourceV
257271
Position pos= new Position(g.getKey().offset, g.getKey().length);
258272
List<ICodeMining> minings= g.getValue();
259273
ICodeMining first= minings.get(0);
260-
boolean inLineHeader= !minings.isEmpty() ? (first instanceof LineHeaderCodeMining) : true;
274+
275+
CodeMiningMode mode= CodeMiningMode.InLine;
276+
if (!minings.isEmpty()) {
277+
if (CodeMiningMode.InLine.codeMiningType.isInstance(first)) {
278+
mode= CodeMiningMode.InLine;
279+
} else if (CodeMiningMode.HeaderLine.codeMiningType.isInstance(first)) {
280+
mode= CodeMiningMode.HeaderLine;
281+
} else if (CodeMiningMode.FooterLine.codeMiningType.isInstance(first)) {
282+
mode= CodeMiningMode.FooterLine;
283+
}
284+
}
285+
261286
// Try to find existing annotation
262287
AbstractInlinedAnnotation ann= fInlinedAnnotationSupport.findExistingAnnotation(pos);
263-
if (ann == null) {
264-
// The annotation doesn't exists, create it.
288+
if (ann == null || !mode.annotationType.isInstance(ann)) {
289+
// The annotation doesn't exists or has wrong type => create a new one.
265290
boolean afterPosition= false;
266291
if (first instanceof LineContentCodeMining m) {
267292
afterPosition= m.isAfterPosition();
@@ -274,16 +299,14 @@ private void renderCodeMinings(Map<Position, List<ICodeMining>> groups, ISourceV
274299
mouseOut= first.getMouseOut();
275300
mouseMove= first.getMouseMove();
276301
}
277-
if (inLineHeader) {
278-
ann= new CodeMiningLineHeaderAnnotation(pos, viewer, mouseHover, mouseOut, mouseMove);
279-
} else {
280-
boolean inFooter= !minings.isEmpty() ? (first instanceof DocumentFooterCodeMining) : false;
281-
if (inFooter) {
282-
ann= new CodeMiningDocumentFooterAnnotation(pos, viewer, mouseHover, mouseOut, mouseMove);
283-
} else {
284-
ann= new CodeMiningLineContentAnnotation(pos, viewer, afterPosition, mouseHover, mouseOut, mouseMove);
285-
}
286-
}
302+
303+
ann = switch (mode) {
304+
case InLine -> new CodeMiningLineContentAnnotation(pos, viewer, afterPosition, mouseHover, mouseOut, mouseMove);
305+
case HeaderLine -> new CodeMiningLineHeaderAnnotation(pos, viewer, mouseHover, mouseOut, mouseMove);
306+
case FooterLine -> new CodeMiningDocumentFooterAnnotation(pos, viewer, mouseHover, mouseOut, mouseMove);
307+
308+
default -> throw new IllegalStateException("Found unexpected code mining display mode: " + mode); //$NON-NLS-1$
309+
};
287310
} else if (ann instanceof ICodeMiningAnnotation && ((ICodeMiningAnnotation) ann).isInVisibleLines()) {
288311
// annotation is in visible lines
289312
annotationsToRedraw.add((ICodeMiningAnnotation) ann);

0 commit comments

Comments
 (0)