Skip to content

Commit

Permalink
JavaElementHyperlinkDetector: add code snippet to error msg
Browse files Browse the repository at this point in the history
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
  • Loading branch information
EcljpseB0T authored and jukzi committed Nov 8, 2024
1 parent f8220bf commit fcf7f0e
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;


/**
Expand All @@ -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)
*/
Expand All @@ -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;

Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,19 +26,19 @@
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;
import org.eclipse.core.runtime.PerformanceStats;
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;
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -360,8 +359,8 @@ public List<ICompletionProposal> 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) {
Expand All @@ -370,25 +369,8 @@ public List<ICompletionProposal> 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;


/**
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,19 @@
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;

import org.eclipse.jface.viewers.ISelection;
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;
Expand Down Expand Up @@ -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);
}
}

}

0 comments on commit fcf7f0e

Please sign in to comment.