Skip to content

Commit

Permalink
Prevent some IIOB exception when offset < 0
Browse files Browse the repository at this point in the history
May fix redhat-developer/vscode-java#55

Signed-off-by: Fred Bricon <[email protected]>
  • Loading branch information
fbricon committed Nov 1, 2016
1 parent a00a40d commit bad8def
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.eclipse.jdt.core.ISourceRange;
import org.eclipse.jdt.core.ITypeParameter;
import org.eclipse.jdt.core.ITypeRoot;
import org.jboss.tools.vscode.java.internal.handlers.JsonRpcHelpers;
import org.jboss.tools.vscode.java.internal.javadoc.JavadocContentAccess;

public class HoverInfoProvider {
Expand All @@ -35,7 +34,7 @@ public HoverInfoProvider(ITypeRoot aUnit) {

public String computeHover(int line, int column) {
try {
IJavaElement[] elements = unit.codeSelect(JsonRpcHelpers.toOffset(unit.getBuffer(),line,column),0);
IJavaElement[] elements = JDTUtils.findElementsAtSelection(unit, line, column);
if(elements == null || elements.length == 0) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,4 +246,23 @@ public static String getFileURI(IResource resource) {
return uri.replaceFirst("file:/([^/])", "file:///$1");
}

public static IJavaElement findElementAtSelection(ITypeRoot unit, int line, int column) throws JavaModelException {
IJavaElement[] elements = findElementsAtSelection(unit, line, column);
if (elements != null && elements.length == 1) {
return elements[0];
}
return null;
}

public static IJavaElement[] findElementsAtSelection(ITypeRoot unit, int line, int column) throws JavaModelException {
if (unit == null) {
return null;
}
int offset = JsonRpcHelpers.toOffset(unit.getBuffer(), line, column);
if (offset > -1) {
return unit.codeSelect(offset, 0);
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private CodeLens resolve(CodeLens lens) {
return lens;
}
Map<String, Object> position = (Map<String, Object>) data.get(1);
IJavaElement element = findElementAtSelection(unit, ((Double)position.get("line")).intValue(), ((Double)position.get("character")).intValue());
IJavaElement element = JDTUtils.findElementAtSelection(unit, ((Double)position.get("line")).intValue(), ((Double)position.get("character")).intValue());
List<Location> locations = findReferences(element);
int nReferences = locations.size();
lens.setCommand(new Command().withTitle(nReferences == 1 ? "1 reference" : nReferences + " references")
Expand All @@ -97,15 +97,6 @@ private CodeLens resolve(CodeLens lens) {
return lens;
}

private IJavaElement findElementAtSelection(ICompilationUnit unit, int line, int column) throws JavaModelException {
IJavaElement[] elements = unit.codeSelect(JsonRpcHelpers.toOffset(unit.getBuffer(), line, column), 0);

if (elements == null || elements.length != 1)
return null;
return elements[0];

}

private List<Location> findReferences(IJavaElement element) throws JavaModelException, CoreException {
if (element == null) {
return Collections.emptyList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ private List<CompletionItem> computeContentAssist(ICompilationUnit unit, int lin

collector.setAllowsRequiredProposals(CompletionProposal.TYPE_REF, CompletionProposal.TYPE_REF, true);

unit.codeComplete(JsonRpcHelpers.toOffset(unit.getBuffer(), line, column), collector, new NullProgressMonitor());
int offset = JsonRpcHelpers.toOffset(unit.getBuffer(), line, column);
if (offset >-1) {
unit.codeComplete(offset, collector, new NullProgressMonitor());
}
} catch (JavaModelException e) {
JavaLanguageServerPlugin.logException("Problem with codeComplete for " + unit.getElementName(), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,11 @@ public boolean canHandle(String request) {

private Location computeDefinitonNavigation(ITypeRoot unit, int line, int column) {
try {
IJavaElement[] elements = unit.codeSelect(JsonRpcHelpers.toOffset(unit.getBuffer(), line, column), 0);

if (elements == null || elements.length != 1)
IJavaElement element = JDTUtils.findElementAtSelection(unit, line, column);
if (element == null) {
return null;
IJavaElement element = elements[0];
ICompilationUnit compilationUnit = (ICompilationUnit) element
.getAncestor(IJavaElement.COMPILATION_UNIT);
}
ICompilationUnit compilationUnit = (ICompilationUnit) element.getAncestor(IJavaElement.COMPILATION_UNIT);
IClassFile cf = (IClassFile) element.getAncestor(IJavaElement.CLASS_FILE);
if (compilationUnit != null || (cf != null && cf.getSourceRange() != null) ) {
return JDTUtils.toLocation(element);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.ITypeRoot;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.search.IJavaSearchConstants;
Expand Down Expand Up @@ -49,17 +48,6 @@ public boolean canHandle(String request) {
return LSPMethods.DOCUMENT_REFERENCES.getMethod().equals(request);
}


private IJavaElement findElementAtSelection(ITypeRoot unit, int line, int column) throws JavaModelException {
if(unit == null ) return null;
IJavaElement[] elements = unit.codeSelect(JsonRpcHelpers.toOffset(unit.getBuffer(), line, column), 0);

if (elements == null || elements.length != 1)
return null;
return elements[0];

}

private IJavaSearchScope createSearchScope() throws JavaModelException {
IJavaProject[] projects = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()).getJavaProjects();
return SearchEngine.createJavaSearchScope(projects, IJavaSearchScope.SOURCES | IJavaSearchScope.APPLICATION_LIBRARIES);
Expand All @@ -70,7 +58,7 @@ public List<org.jboss.tools.langs.Location> handle(org.jboss.tools.langs.Referen
SearchEngine engine = new SearchEngine();

try {
IJavaElement elementToSearch = findElementAtSelection(JDTUtils.resolveTypeRoot(param.getTextDocument().getUri()),
IJavaElement elementToSearch = JDTUtils.findElementAtSelection(JDTUtils.resolveTypeRoot(param.getTextDocument().getUri()),
param.getPosition().getLine().intValue(),
param.getPosition().getCharacter().intValue());

Expand Down

0 comments on commit bad8def

Please sign in to comment.