Skip to content

Commit 06851d8

Browse files
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 62f3974 commit 06851d8

File tree

6 files changed

+45
-14
lines changed

6 files changed

+45
-14
lines changed

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/source/inlined/AbstractInlinedAnnotation.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,18 +122,25 @@ final Position computeWidgetPosition(ITextViewer viewer) {
122122
}
123123

124124
/**
125-
* Returns the {@link StyledText} widget where the annotation must be drawn.
125+
* Returns the {@link StyledText} widget where the annotation must be drawn or null if not
126+
* available.
126127
*
127-
* @return the {@link StyledText} widget where the annotation must be drawn.
128+
* @return the {@link StyledText} widget where the annotation must be drawn or null if not
129+
* available.
128130
*/
129131
public StyledText getTextWidget() {
132+
if (fViewer == null) {
133+
return null;
134+
}
130135
return fViewer.getTextWidget();
131136
}
132137

133138
/**
134-
* Returns the {@link ISourceViewer} where the annotation must be drawn.
139+
* Returns the {@link ISourceViewer} where the annotation must be drawn or null if not
140+
* available.
135141
*
136-
* @return the {@link ISourceViewer} where the annotation must be drawn.
142+
* @return the {@link ISourceViewer} where the annotation must be drawn or null if not
143+
* available.
137144
*/
138145
public ISourceViewer getViewer() {
139146
return fViewer;
@@ -248,6 +255,9 @@ void setSupport(InlinedAnnotationSupport support) {
248255
* otherwise.
249256
*/
250257
protected boolean isInVisibleLines() {
258+
if (support == null) {
259+
return true; // support not yet available, we have to be optimistic
260+
}
251261
return support.isInVisibleLines(getPosition().getOffset());
252262
}
253263

@@ -272,6 +282,9 @@ boolean isFirstVisibleOffset(int widgetOffset, ITextViewer viewer) {
272282
* otherwise.
273283
*/
274284
protected boolean isInVisibleLines(int offset) {
285+
if (support == null) {
286+
return true; // support not yet available, we have to be optimistic
287+
}
275288
return support.isInVisibleLines(offset);
276289
}
277290

@@ -297,6 +310,9 @@ void setLocation(int x, int y) {
297310
*/
298311
boolean contains(int x, int y) {
299312
StyledText styledText= getTextWidget();
313+
if (styledText == null) {
314+
return false;
315+
}
300316
GC gc= null;
301317
try {
302318
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)