Skip to content

Commit 433cdb9

Browse files
tobias-melchermickaelistria
authored andcommitted
fix CodeMiningLineHeaderAnnotation#getMultilineHeight
it should never return 0. Fix race condition when code minings are not yet available. #2786
1 parent c9f129d commit 433cdb9

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

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

+3
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ private int getMultilineHeight(GC gc) {
121121
}
122122
ignoreFirstLine= true;
123123
}
124+
if (sumLineHeight == 0) {
125+
return super.getHeight();
126+
}
124127
if (gc != null) {
125128
return sumLineHeight;
126129
} else {

tests/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/JFaceTextTestSuite.java

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.junit.runners.Suite;
1818
import org.junit.runners.Suite.SuiteClasses;
1919

20+
import org.eclipse.jface.text.tests.codemining.CodeMiningLineHeaderAnnotationTest;
2021
import org.eclipse.jface.text.tests.codemining.CodeMiningProjectionViewerTest;
2122
import org.eclipse.jface.text.tests.codemining.CodeMiningTest;
2223
import org.eclipse.jface.text.tests.contentassist.AsyncContentAssistTest;
@@ -72,6 +73,7 @@
7273
LineContentBoundsDrawingTest.class,
7374
AnnotationOnTabTest.class,
7475
CodeMiningTest.class,
76+
CodeMiningLineHeaderAnnotationTest.class,
7577
CodeMiningProjectionViewerTest.class,
7678

7779
TabsToSpacesConverterTest.class,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 SAP SE
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*******************************************************************************/
11+
package org.eclipse.jface.text.tests.codemining;
12+
13+
import static org.junit.Assert.assertNotEquals;
14+
15+
import java.util.Arrays;
16+
17+
import org.junit.After;
18+
import org.junit.Assert;
19+
import org.junit.Before;
20+
import org.junit.Test;
21+
22+
import org.eclipse.swt.SWT;
23+
import org.eclipse.swt.custom.StyledText;
24+
import org.eclipse.swt.layout.FillLayout;
25+
import org.eclipse.swt.widgets.Display;
26+
import org.eclipse.swt.widgets.Shell;
27+
28+
import org.eclipse.jface.internal.text.codemining.CodeMiningLineHeaderAnnotation;
29+
30+
import org.eclipse.jface.text.Document;
31+
import org.eclipse.jface.text.Position;
32+
import org.eclipse.jface.text.codemining.LineHeaderCodeMining;
33+
import org.eclipse.jface.text.source.AnnotationModel;
34+
import org.eclipse.jface.text.source.AnnotationPainter;
35+
import org.eclipse.jface.text.source.SourceViewer;
36+
import org.eclipse.jface.text.source.inlined.AbstractInlinedAnnotation;
37+
import org.eclipse.jface.text.source.inlined.InlinedAnnotationSupport;
38+
import org.eclipse.jface.text.tests.util.DisplayHelper;
39+
40+
public class CodeMiningLineHeaderAnnotationTest {
41+
42+
private SourceViewer fViewer;
43+
44+
private Shell fShell;
45+
46+
private Document document;
47+
48+
@Before
49+
public void setUp() {
50+
fShell= new Shell(Display.getDefault());
51+
fShell.setSize(500, 200);
52+
fShell.setLayout(new FillLayout());
53+
fViewer= new SourceViewer(fShell, null, SWT.NONE);
54+
final StyledText textWidget= fViewer.getTextWidget();
55+
document= new Document("a");
56+
textWidget.setText(document.get());
57+
fViewer.setDocument(document, new AnnotationModel());
58+
final Display display= textWidget.getDisplay();
59+
fShell.open();
60+
Assert.assertTrue(new DisplayHelper() {
61+
@Override
62+
protected boolean condition() {
63+
return fViewer.getTextWidget().isVisible();
64+
}
65+
}.waitForCondition(display, 3000));
66+
DisplayHelper.sleep(textWidget.getDisplay(), 1000);
67+
}
68+
69+
@After
70+
public void tearDown() {
71+
fViewer= null;
72+
}
73+
74+
@Test
75+
public void testGetHeightDoesNotReturnZero() throws Exception {
76+
var cut= new CodeMiningLineHeaderAnnotation(new Position(0, 0), fViewer);
77+
var s= new InlinedAnnotationSupport();
78+
s.install(fViewer, new AnnotationPainter(fViewer, null));
79+
var m= AbstractInlinedAnnotation.class.getDeclaredMethod("setSupport", InlinedAnnotationSupport.class);
80+
m.setAccessible(true);
81+
m.invoke(cut, s);
82+
cut.update(Arrays.asList(new LineHeaderCodeMining(0, document, null) {
83+
@Override
84+
public String getLabel() {
85+
return "mining";
86+
}
87+
}), null);
88+
// https: //github.com/eclipse-platform/eclipse.platform.ui/issues/2786
89+
assertNotEquals(0, cut.getHeight()); // getHeight should not return 0, otherwise editor content starts jumping around
90+
}
91+
}

0 commit comments

Comments
 (0)