From fcf7f0e944fe8cf23b0a395e4bfcf8d7270a6a6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Kubitz?= Date: Wed, 6 Nov 2024 15:04:37 +0100 Subject: [PATCH] JavaElementHyperlinkDetector: add code snippet to error msg Should help on errors where they showed up in the logfile but the context is unclear like: https://github.com/eclipse-jdt/eclipse.jdt.core/issues/3262 https://github.com/eclipse-jdt/eclipse.jdt.core/issues/2934 https://github.com/eclipse-jdt/eclipse.jdt.ui/issues/1764 https://github.com/eclipse-jdt/eclipse.jdt.core/issues/3263 --- .../JavaElementHyperlinkDetector.java | 19 +++++----- .../CompletionProposalComputerDescriptor.java | 32 ++++------------- .../hover/AbstractJavaEditorTextHover.java | 4 +++ .../jdt/internal/ui/util/SelectionUtil.java | 36 +++++++++++++++++++ 4 files changed, 57 insertions(+), 34 deletions(-) diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/JavaElementHyperlinkDetector.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/JavaElementHyperlinkDetector.java index 60f2620d9f8..57fa5ef0d9b 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/JavaElementHyperlinkDetector.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/JavaElementHyperlinkDetector.java @@ -58,6 +58,7 @@ import org.eclipse.jdt.internal.ui.search.SearchMessages; import org.eclipse.jdt.internal.ui.text.JavaWordFinder; +import org.eclipse.jdt.internal.ui.util.SelectionUtil; /** @@ -75,7 +76,6 @@ public class JavaElementHyperlinkDetector extends AbstractHyperlinkDetector { private static IRegion fLastWordRegion; private static IJavaElement[] fLastElements; - /* * @see org.eclipse.jface.text.hyperlink.IHyperlinkDetector#detectHyperlinks(org.eclipse.jface.text.ITextViewer, org.eclipse.jface.text.IRegion, boolean) */ @@ -99,14 +99,13 @@ private IHyperlink[] detectHyperlinksCached(IRegion region) { if (input == null) return null; + IDocumentProvider documentProvider= textEditor.getDocumentProvider(); + IEditorInput editorInput= textEditor.getEditorInput(); + IDocument document= documentProvider.getDocument(editorInput); + IRegion wordRegion= JavaWordFinder.findWord(document, offset); + if (wordRegion == null || wordRegion.getLength() == 0) + return null; try { - IDocumentProvider documentProvider= textEditor.getDocumentProvider(); - IEditorInput editorInput= textEditor.getEditorInput(); - IDocument document= documentProvider.getDocument(editorInput); - IRegion wordRegion= JavaWordFinder.findWord(document, offset); - if (wordRegion == null || wordRegion.getLength() == 0) - return null; - if (isInheritDoc(document, wordRegion) && getClass() != JavaElementHyperlinkDetector.class) return null; @@ -143,7 +142,9 @@ private IHyperlink[] detectHyperlinksCached(IRegion region) { return null; return CollectionsUtil.toArray(links, IHyperlink.class); - + } catch (RuntimeException e) { + SelectionUtil.logException("computing hyperlink", e, textEditor.getTitle(), document, offset); //$NON-NLS-1$ + return null; } catch (JavaModelException e) { return null; } diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/java/CompletionProposalComputerDescriptor.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/java/CompletionProposalComputerDescriptor.java index 4c152b2ca60..d954c5cbcbf 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/java/CompletionProposalComputerDescriptor.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/java/CompletionProposalComputerDescriptor.java @@ -17,7 +17,6 @@ import java.util.Collections; import java.util.HashSet; import java.util.List; -import java.util.Objects; import java.util.Set; import org.osgi.framework.Bundle; @@ -27,7 +26,6 @@ import org.eclipse.core.runtime.IConfigurationElement; import org.eclipse.core.runtime.IContributor; import org.eclipse.core.runtime.IExtension; -import org.eclipse.core.runtime.ILog; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.InvalidRegistryObjectException; @@ -35,11 +33,12 @@ import org.eclipse.core.runtime.Platform; import org.eclipse.core.runtime.Status; -import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.contentassist.ICompletionProposal; import org.eclipse.jface.text.contentassist.IContextInformation; +import org.eclipse.jdt.core.ICompilationUnit; + import org.eclipse.jdt.internal.corext.util.Messages; import org.eclipse.jdt.ui.text.IJavaPartitions; @@ -49,6 +48,7 @@ import org.eclipse.jdt.internal.ui.JavaPlugin; import org.eclipse.jdt.internal.ui.javaeditor.JavaSourceViewer; +import org.eclipse.jdt.internal.ui.util.SelectionUtil; /** * The description of an extension to the @@ -328,7 +328,6 @@ public IJavaCompletionProposalComputer createComputer() throws CoreException, In return (IJavaCompletionProposalComputer) fElement.createExecutableExtension(CLASS); } - private String lastErrorMsg; /** * Safely computes completion proposals through the described extension. If the extension * is disabled, throws an exception or otherwise does not adhere to the contract described in @@ -360,8 +359,8 @@ public List computeCompletionProposals(ContentAssistInvocat } } finally { // If computers are using non-ui thread, don't report delays. - fIsReportingDelay= !(context.getViewer() instanceof JavaSourceViewer) - || !((JavaSourceViewer) context.getViewer()).isAsyncCompletionActive(); + fIsReportingDelay= !(context.getViewer() instanceof JavaSourceViewer viewer) + || !viewer.isAsyncCompletionActive(); } status= createAPIViolationStatus(COMPUTE_COMPLETION_PROPOSALS); } catch (InvalidRegistryObjectException x) { @@ -370,25 +369,8 @@ public List computeCompletionProposals(ContentAssistInvocat status= createExceptionStatus(x); } catch (RuntimeException x) { // log error and keep going with other providers - String where= ""; //$NON-NLS-1$ - if (context instanceof JavaContentAssistInvocationContext ctx) { - if (ctx.getCompilationUnit() != null) { - where+= ctx.getCompilationUnit().getElementName(); - } - } - where+= " at offset " + context.getInvocationOffset(); //$NON-NLS-1$ - try { - IDocument doc= context.getDocument(); - int lineOfOffset= doc.getLineOfOffset(context.getInvocationOffset()); - where+= " line " + (lineOfOffset + 1); //$NON-NLS-1$ - int CONTEXT_LINES= 10; - where+= " :\n" + doc.get(doc.getLineOffset(Math.max(0, lineOfOffset - CONTEXT_LINES)), context.getInvocationOffset()) + '|'; //$NON-NLS-1$ - } catch (BadLocationException e1) { - } - if (!Objects.equals(lastErrorMsg, where)) { // avoid repetitive logging - lastErrorMsg= where; - ILog.get().error("RuntimeException computing completion proposal for " + where, x); //$NON-NLS-1$ - } + String title= (context instanceof JavaContentAssistInvocationContext ctx) && (ctx.getCompilationUnit() instanceof ICompilationUnit cu) ? cu.getElementName() : null; + SelectionUtil.logException("computing completion proposal", x, title, context.getDocument(), context.getInvocationOffset()); //$NON-NLS-1$ status= createExceptionStatus(x); return Collections.emptyList(); } finally { diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/java/hover/AbstractJavaEditorTextHover.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/java/hover/AbstractJavaEditorTextHover.java index b259b229dd2..f0911c4e0e7 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/java/hover/AbstractJavaEditorTextHover.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/java/hover/AbstractJavaEditorTextHover.java @@ -40,6 +40,7 @@ import org.eclipse.jdt.internal.ui.javaeditor.IClassFileEditorInput; import org.eclipse.jdt.internal.ui.javaeditor.WorkingCopyManager; import org.eclipse.jdt.internal.ui.text.JavaWordFinder; +import org.eclipse.jdt.internal.ui.util.SelectionUtil; /** @@ -119,6 +120,9 @@ protected IJavaElement[] getJavaElementsAt(ITextViewer textViewer, IRegion hover if (resolve != null) { try { return resolve.codeSelect(hoverRegion.getOffset(), hoverRegion.getLength()); + } catch (RuntimeException e) { + SelectionUtil.logException("computing hover information", e, getEditor().getTitle(), document, hoverRegion.getOffset()); //$NON-NLS-1$ + return null; } catch (JavaModelException x) { return null; } diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/util/SelectionUtil.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/util/SelectionUtil.java index c3aaa9a1e25..0c82d9ed6a9 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/util/SelectionUtil.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/util/SelectionUtil.java @@ -17,6 +17,9 @@ import java.util.Arrays; import java.util.Iterator; import java.util.List; +import java.util.Objects; + +import org.eclipse.core.runtime.ILog; import org.eclipse.core.resources.IResource; @@ -24,6 +27,9 @@ import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.viewers.StructuredSelection; +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.IDocument; + import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.IWorkbenchPart; import org.eclipse.ui.IWorkbenchPartReference; @@ -121,4 +127,34 @@ public static void selectAndReveal(IResource[] resources, IWorkbenchWindow windo private SelectionUtil() { } + private static String lastErrorMsg; + + public static void logException(String action, RuntimeException e, String title, IDocument document, int offset) { + // log error and keep going + String errorMsg= e.getClass().getSimpleName() + " " + action; //$NON-NLS-1$ + if (title != null) { + errorMsg+= " in " + title; //$NON-NLS-1$ + } + errorMsg+= " at offset " + offset; //$NON-NLS-1$ + try { + int lineOfOffset= document.getLineOfOffset(offset); + String source= "Source line " + (lineOfOffset + 1); //$NON-NLS-1$ + int CONTEXT_LINES= 10; + int startLineOffset= document.getLineOffset(Math.max(0, lineOfOffset - CONTEXT_LINES)); + source+= " :"+System.lineSeparator(); //$NON-NLS-1$ + source+= "-----" + System.lineSeparator(); //$NON-NLS-1$ + source+= document.get(startLineOffset, offset - startLineOffset); // source until offset + source+= '|'; // cursor + source+= document.get(offset, document.getLineOffset(lineOfOffset) + document.getLineLength(lineOfOffset) - offset); // rest of line + source+= "-----"; //$NON-NLS-1$ + e.addSuppressed(new Throwable(source)); + } catch (BadLocationException ble) { + e.addSuppressed(ble); + } + if (!Objects.equals(lastErrorMsg, errorMsg)) { // avoid repetitive logging + lastErrorMsg= errorMsg; + ILog.get().error(errorMsg, e); + } + } + }