Skip to content

Commit fcf7f0e

Browse files
committed
JavaElementHyperlinkDetector: add code snippet to error msg
Should help on errors where they showed up in the logfile but the context is unclear like: eclipse-jdt/eclipse.jdt.core#3262 eclipse-jdt/eclipse.jdt.core#2934 #1764 eclipse-jdt/eclipse.jdt.core#3263
1 parent f8220bf commit fcf7f0e

File tree

4 files changed

+57
-34
lines changed

4 files changed

+57
-34
lines changed

org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/JavaElementHyperlinkDetector.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858

5959
import org.eclipse.jdt.internal.ui.search.SearchMessages;
6060
import org.eclipse.jdt.internal.ui.text.JavaWordFinder;
61+
import org.eclipse.jdt.internal.ui.util.SelectionUtil;
6162

6263

6364
/**
@@ -75,7 +76,6 @@ public class JavaElementHyperlinkDetector extends AbstractHyperlinkDetector {
7576
private static IRegion fLastWordRegion;
7677

7778
private static IJavaElement[] fLastElements;
78-
7979
/*
8080
* @see org.eclipse.jface.text.hyperlink.IHyperlinkDetector#detectHyperlinks(org.eclipse.jface.text.ITextViewer, org.eclipse.jface.text.IRegion, boolean)
8181
*/
@@ -99,14 +99,13 @@ private IHyperlink[] detectHyperlinksCached(IRegion region) {
9999
if (input == null)
100100
return null;
101101

102+
IDocumentProvider documentProvider= textEditor.getDocumentProvider();
103+
IEditorInput editorInput= textEditor.getEditorInput();
104+
IDocument document= documentProvider.getDocument(editorInput);
105+
IRegion wordRegion= JavaWordFinder.findWord(document, offset);
106+
if (wordRegion == null || wordRegion.getLength() == 0)
107+
return null;
102108
try {
103-
IDocumentProvider documentProvider= textEditor.getDocumentProvider();
104-
IEditorInput editorInput= textEditor.getEditorInput();
105-
IDocument document= documentProvider.getDocument(editorInput);
106-
IRegion wordRegion= JavaWordFinder.findWord(document, offset);
107-
if (wordRegion == null || wordRegion.getLength() == 0)
108-
return null;
109-
110109
if (isInheritDoc(document, wordRegion) && getClass() != JavaElementHyperlinkDetector.class)
111110
return null;
112111

@@ -143,7 +142,9 @@ private IHyperlink[] detectHyperlinksCached(IRegion region) {
143142
return null;
144143

145144
return CollectionsUtil.toArray(links, IHyperlink.class);
146-
145+
} catch (RuntimeException e) {
146+
SelectionUtil.logException("computing hyperlink", e, textEditor.getTitle(), document, offset); //$NON-NLS-1$
147+
return null;
147148
} catch (JavaModelException e) {
148149
return null;
149150
}

org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/java/CompletionProposalComputerDescriptor.java

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import java.util.Collections;
1818
import java.util.HashSet;
1919
import java.util.List;
20-
import java.util.Objects;
2120
import java.util.Set;
2221

2322
import org.osgi.framework.Bundle;
@@ -27,19 +26,19 @@
2726
import org.eclipse.core.runtime.IConfigurationElement;
2827
import org.eclipse.core.runtime.IContributor;
2928
import org.eclipse.core.runtime.IExtension;
30-
import org.eclipse.core.runtime.ILog;
3129
import org.eclipse.core.runtime.IProgressMonitor;
3230
import org.eclipse.core.runtime.IStatus;
3331
import org.eclipse.core.runtime.InvalidRegistryObjectException;
3432
import org.eclipse.core.runtime.PerformanceStats;
3533
import org.eclipse.core.runtime.Platform;
3634
import org.eclipse.core.runtime.Status;
3735

38-
import org.eclipse.jface.text.BadLocationException;
3936
import org.eclipse.jface.text.IDocument;
4037
import org.eclipse.jface.text.contentassist.ICompletionProposal;
4138
import org.eclipse.jface.text.contentassist.IContextInformation;
4239

40+
import org.eclipse.jdt.core.ICompilationUnit;
41+
4342
import org.eclipse.jdt.internal.corext.util.Messages;
4443

4544
import org.eclipse.jdt.ui.text.IJavaPartitions;
@@ -49,6 +48,7 @@
4948

5049
import org.eclipse.jdt.internal.ui.JavaPlugin;
5150
import org.eclipse.jdt.internal.ui.javaeditor.JavaSourceViewer;
51+
import org.eclipse.jdt.internal.ui.util.SelectionUtil;
5252

5353
/**
5454
* The description of an extension to the
@@ -328,7 +328,6 @@ public IJavaCompletionProposalComputer createComputer() throws CoreException, In
328328
return (IJavaCompletionProposalComputer) fElement.createExecutableExtension(CLASS);
329329
}
330330

331-
private String lastErrorMsg;
332331
/**
333332
* Safely computes completion proposals through the described extension. If the extension
334333
* is disabled, throws an exception or otherwise does not adhere to the contract described in
@@ -360,8 +359,8 @@ public List<ICompletionProposal> computeCompletionProposals(ContentAssistInvocat
360359
}
361360
} finally {
362361
// If computers are using non-ui thread, don't report delays.
363-
fIsReportingDelay= !(context.getViewer() instanceof JavaSourceViewer)
364-
|| !((JavaSourceViewer) context.getViewer()).isAsyncCompletionActive();
362+
fIsReportingDelay= !(context.getViewer() instanceof JavaSourceViewer viewer)
363+
|| !viewer.isAsyncCompletionActive();
365364
}
366365
status= createAPIViolationStatus(COMPUTE_COMPLETION_PROPOSALS);
367366
} catch (InvalidRegistryObjectException x) {
@@ -370,25 +369,8 @@ public List<ICompletionProposal> computeCompletionProposals(ContentAssistInvocat
370369
status= createExceptionStatus(x);
371370
} catch (RuntimeException x) {
372371
// log error and keep going with other providers
373-
String where= ""; //$NON-NLS-1$
374-
if (context instanceof JavaContentAssistInvocationContext ctx) {
375-
if (ctx.getCompilationUnit() != null) {
376-
where+= ctx.getCompilationUnit().getElementName();
377-
}
378-
}
379-
where+= " at offset " + context.getInvocationOffset(); //$NON-NLS-1$
380-
try {
381-
IDocument doc= context.getDocument();
382-
int lineOfOffset= doc.getLineOfOffset(context.getInvocationOffset());
383-
where+= " line " + (lineOfOffset + 1); //$NON-NLS-1$
384-
int CONTEXT_LINES= 10;
385-
where+= " :\n" + doc.get(doc.getLineOffset(Math.max(0, lineOfOffset - CONTEXT_LINES)), context.getInvocationOffset()) + '|'; //$NON-NLS-1$
386-
} catch (BadLocationException e1) {
387-
}
388-
if (!Objects.equals(lastErrorMsg, where)) { // avoid repetitive logging
389-
lastErrorMsg= where;
390-
ILog.get().error("RuntimeException computing completion proposal for " + where, x); //$NON-NLS-1$
391-
}
372+
String title= (context instanceof JavaContentAssistInvocationContext ctx) && (ctx.getCompilationUnit() instanceof ICompilationUnit cu) ? cu.getElementName() : null;
373+
SelectionUtil.logException("computing completion proposal", x, title, context.getDocument(), context.getInvocationOffset()); //$NON-NLS-1$
392374
status= createExceptionStatus(x);
393375
return Collections.emptyList();
394376
} finally {

org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/java/hover/AbstractJavaEditorTextHover.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.eclipse.jdt.internal.ui.javaeditor.IClassFileEditorInput;
4141
import org.eclipse.jdt.internal.ui.javaeditor.WorkingCopyManager;
4242
import org.eclipse.jdt.internal.ui.text.JavaWordFinder;
43+
import org.eclipse.jdt.internal.ui.util.SelectionUtil;
4344

4445

4546
/**
@@ -119,6 +120,9 @@ protected IJavaElement[] getJavaElementsAt(ITextViewer textViewer, IRegion hover
119120
if (resolve != null) {
120121
try {
121122
return resolve.codeSelect(hoverRegion.getOffset(), hoverRegion.getLength());
123+
} catch (RuntimeException e) {
124+
SelectionUtil.logException("computing hover information", e, getEditor().getTitle(), document, hoverRegion.getOffset()); //$NON-NLS-1$
125+
return null;
122126
} catch (JavaModelException x) {
123127
return null;
124128
}

org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/util/SelectionUtil.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,19 @@
1717
import java.util.Arrays;
1818
import java.util.Iterator;
1919
import java.util.List;
20+
import java.util.Objects;
21+
22+
import org.eclipse.core.runtime.ILog;
2023

2124
import org.eclipse.core.resources.IResource;
2225

2326
import org.eclipse.jface.viewers.ISelection;
2427
import org.eclipse.jface.viewers.IStructuredSelection;
2528
import org.eclipse.jface.viewers.StructuredSelection;
2629

30+
import org.eclipse.jface.text.BadLocationException;
31+
import org.eclipse.jface.text.IDocument;
32+
2733
import org.eclipse.ui.IWorkbenchPage;
2834
import org.eclipse.ui.IWorkbenchPart;
2935
import org.eclipse.ui.IWorkbenchPartReference;
@@ -121,4 +127,34 @@ public static void selectAndReveal(IResource[] resources, IWorkbenchWindow windo
121127
private SelectionUtil() {
122128
}
123129

130+
private static String lastErrorMsg;
131+
132+
public static void logException(String action, RuntimeException e, String title, IDocument document, int offset) {
133+
// log error and keep going
134+
String errorMsg= e.getClass().getSimpleName() + " " + action; //$NON-NLS-1$
135+
if (title != null) {
136+
errorMsg+= " in " + title; //$NON-NLS-1$
137+
}
138+
errorMsg+= " at offset " + offset; //$NON-NLS-1$
139+
try {
140+
int lineOfOffset= document.getLineOfOffset(offset);
141+
String source= "Source line " + (lineOfOffset + 1); //$NON-NLS-1$
142+
int CONTEXT_LINES= 10;
143+
int startLineOffset= document.getLineOffset(Math.max(0, lineOfOffset - CONTEXT_LINES));
144+
source+= " :"+System.lineSeparator(); //$NON-NLS-1$
145+
source+= "-----" + System.lineSeparator(); //$NON-NLS-1$
146+
source+= document.get(startLineOffset, offset - startLineOffset); // source until offset
147+
source+= '|'; // cursor
148+
source+= document.get(offset, document.getLineOffset(lineOfOffset) + document.getLineLength(lineOfOffset) - offset); // rest of line
149+
source+= "-----"; //$NON-NLS-1$
150+
e.addSuppressed(new Throwable(source));
151+
} catch (BadLocationException ble) {
152+
e.addSuppressed(ble);
153+
}
154+
if (!Objects.equals(lastErrorMsg, errorMsg)) { // avoid repetitive logging
155+
lastErrorMsg= errorMsg;
156+
ILog.get().error(errorMsg, e);
157+
}
158+
}
159+
124160
}

0 commit comments

Comments
 (0)