Skip to content

Commit 5802a46

Browse files
tobias-melcheriloveeclipse
authored andcommitted
Ensure code minings are rendered when editor is initially opened
Add a null check for the `support` field in the `AbstractInlinedAnnotation` class to prevent `NullPointerException` when the `isInVisibleLines` method is called. This issue occurs because `support` may not be initialized when the editor is first opened, causing code minings to not render initially.
1 parent 2cfd95c commit 5802a46

7 files changed

Lines changed: 49 additions & 17 deletions

File tree

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ static int getMultilineHeight(GC gc, List<ICodeMining> minings, StyledText style
125125
if (gc != null) {
126126
return sumLineHeight;
127127
} else {
128-
int lineHeight= styledText.getLineHeight();
129-
int result= numLinesOfAllMinings * (lineHeight + styledText.getLineSpacing());
128+
int lineHeight= styledText != null ? styledText.getLineHeight() : 0;
129+
int result= numLinesOfAllMinings * (lineHeight + (styledText != null ? styledText.getLineSpacing() : 0));
130130
return result;
131131
}
132132
}
@@ -145,7 +145,7 @@ private static int calculateLineHeight(List<ICodeMining> minings, GC gc, StyledT
145145
}
146146
}
147147
Point ext= gc.textExtent(line);
148-
sumLineHeight+= ext.y + styledText.getLineSpacing();
148+
sumLineHeight+= ext.y + (styledText != null ? styledText.getLineSpacing() : 0);
149149
}
150150
return sumLineHeight;
151151
}

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,16 @@ private void updateCodeMinings() {
145145
IProgressMonitor monitor= fMonitor;
146146
// Collect the code minings for the viewer
147147
getCodeMinings(fViewer, fCodeMiningProviders, monitor).thenAccept(symbols -> {
148-
// check if request was canceled.
149-
monitor.isCanceled();
150-
// then group code minings by lines position
151-
Map<Position, List<ICodeMining>> groups= groupByLines(symbols, fCodeMiningProviders);
152-
// resolve and render code minings
153-
renderCodeMinings(groups, fViewer, monitor);
148+
try {
149+
// check if request was canceled.
150+
monitor.isCanceled();
151+
// then group code minings by lines position
152+
Map<Position, List<ICodeMining>> groups= groupByLines(symbols, fCodeMiningProviders);
153+
// resolve and render code minings
154+
renderCodeMinings(groups, fViewer, monitor);
155+
} catch (Throwable e) {
156+
logCodeMiningProviderException(e);
157+
}
154158
});
155159
}
156160

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public interface ICodeMining {
7878
* {@link CompletableFuture#completedFuture(Object)} if no such resolution is necessary (in
7979
* which case {#isResolved()} is expected to return <code>true</code>).
8080
*
81-
* @param viewer the viewer.
81+
* @param viewer the viewer, can be null
8282
* @param monitor the monitor.
8383
* @return the future to resolve the content of mining, or
8484
* {@link CompletableFuture#completedFuture(Object)} if no such resolution is necessary

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

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ protected AbstractInlinedAnnotation(Position position, ISourceViewer viewer) {
9393
protected AbstractInlinedAnnotation(Position position, ISourceViewer viewer, Consumer<MouseEvent> onMouseHover, Consumer<MouseEvent> onMouseOut, Consumer<MouseEvent> onMouseMove) {
9494
super(TYPE, false, ""); //$NON-NLS-1$
9595
this.position= position;
96+
this.fViewer= viewer;
9697
this.onMouseHover= onMouseHover;
9798
this.onMouseOut= onMouseOut;
9899
this.onMouseMove= onMouseMove;
@@ -122,18 +123,25 @@ final Position computeWidgetPosition(ITextViewer viewer) {
122123
}
123124

124125
/**
125-
* Returns the {@link StyledText} widget where the annotation must be drawn.
126+
* Returns the {@link StyledText} widget where the annotation must be drawn or null if not
127+
* available.
126128
*
127-
* @return the {@link StyledText} widget where the annotation must be drawn.
129+
* @return the {@link StyledText} widget where the annotation must be drawn or null if not
130+
* available.
128131
*/
129132
public StyledText getTextWidget() {
133+
if (fViewer == null) {
134+
return null;
135+
}
130136
return fViewer.getTextWidget();
131137
}
132138

133139
/**
134-
* Returns the {@link ISourceViewer} where the annotation must be drawn.
140+
* Returns the {@link ISourceViewer} where the annotation must be drawn or null if not
141+
* available.
135142
*
136-
* @return the {@link ISourceViewer} where the annotation must be drawn.
143+
* @return the {@link ISourceViewer} where the annotation must be drawn or null if not
144+
* available.
137145
*/
138146
public ISourceViewer getViewer() {
139147
return fViewer;
@@ -155,9 +163,9 @@ public void redraw() {
155163
Position pos= getPosition();
156164
int offset= pos.getOffset();
157165
ISourceViewer viewer= getViewer();
158-
if (viewer instanceof ITextViewerExtension5) {
166+
if (viewer instanceof ITextViewerExtension5 extViewer) {
159167
// adjust offset according folded content
160-
offset= ((ITextViewerExtension5) viewer).modelOffset2WidgetOffset(offset);
168+
offset= extViewer.modelOffset2WidgetOffset(offset);
161169
}
162170
InlinedAnnotationDrawingStrategy.draw(this, null, text, offset, pos.getLength(), null);
163171
} catch (RuntimeException e) {
@@ -248,6 +256,9 @@ void setSupport(InlinedAnnotationSupport support) {
248256
* otherwise.
249257
*/
250258
protected boolean isInVisibleLines() {
259+
if (support == null) {
260+
return true; // support not yet available, we have to be optimistic
261+
}
251262
return support.isInVisibleLines(getPosition().getOffset());
252263
}
253264

@@ -272,6 +283,9 @@ boolean isFirstVisibleOffset(int widgetOffset, ITextViewer viewer) {
272283
* otherwise.
273284
*/
274285
protected boolean isInVisibleLines(int offset) {
286+
if (support == null) {
287+
return true; // support not yet available, we have to be optimistic
288+
}
275289
return support.isInVisibleLines(offset);
276290
}
277291

@@ -297,6 +311,9 @@ void setLocation(int x, int y) {
297311
*/
298312
boolean contains(int x, int y) {
299313
StyledText styledText= getTextWidget();
314+
if (styledText == null) {
315+
return false;
316+
}
300317
GC gc= null;
301318
try {
302319
gc= new GC(styledText);

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,9 @@ void setRedrawnCharacterWidth(int redrawnCharacterWidth) {
119119

120120
@Override
121121
boolean contains(int x, int y) {
122-
return (x >= this.fX && x <= this.fX + width && y >= this.fY && y <= this.fY + getTextWidget().getLineHeight());
122+
StyledText textWidget= getTextWidget();
123+
int lineHeight= textWidget != null ? textWidget.getLineHeight() : 0;
124+
return (x >= this.fX && x <= this.fX + width && y >= this.fY && y <= this.fY + lineHeight);
123125
}
124126

125127
/**
@@ -137,6 +139,9 @@ boolean contains(int x, int y) {
137139
* not model position.
138140
*/
139141
StyleRange updateStyle(StyleRange style, FontMetrics fontMetrics, ITextViewer viewer, boolean afterPosition) {
142+
if (viewer == null) {
143+
return null;
144+
}
140145
Position widgetPosition= computeWidgetPosition(viewer);
141146
if (widgetPosition == null) {
142147
return null;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ protected LineFooterAnnotation(Position position, ISourceViewer viewer, Consumer
3737
*/
3838
public int getHeight() {
3939
StyledText styledText= super.getTextWidget();
40+
if (styledText == null) {
41+
return 0;
42+
}
4043
return styledText.getLineHeight();
4144
}
4245

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ public LineHeaderAnnotation(Position position, ISourceViewer viewer, Consumer<Mo
6464
*/
6565
public int getHeight() {
6666
StyledText styledText= super.getTextWidget();
67+
if (styledText == null) {
68+
return 0;
69+
}
6770
return styledText.getLineHeight();
6871
}
6972

0 commit comments

Comments
 (0)