Skip to content

Commit

Permalink
Fix for #1391: static import name range can be used for editor selection
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Sep 19, 2022
1 parent df3d0a8 commit a354532
Showing 1 changed file with 1 addition and 56 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2020 the original author or authors.
* Copyright 2009-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -53,12 +53,9 @@
import org.eclipse.debug.ui.actions.IRunToLineTarget;
import org.eclipse.debug.ui.actions.IToggleBreakpointsTarget;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IImportDeclaration;
import org.eclipse.jdt.core.ISourceRange;
import org.eclipse.jdt.core.ISourceReference;
import org.eclipse.jdt.core.ITypeRoot;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.groovy.core.util.ReflectionUtils;
import org.eclipse.jdt.internal.core.CompilationUnit;
import org.eclipse.jdt.internal.core.manipulation.search.IOccurrencesFinder.OccurrenceLocation;
Expand Down Expand Up @@ -689,58 +686,6 @@ protected void setTitleImage(Image titleImage) {
// prevent JavaEditor from replacing image set via plugin.xml
}

@Override
protected void setSelection(ISourceReference reference, boolean moveCursor) {
super.setSelection(reference, moveCursor);
// must override functionality because JavaEditor expects that there is a ';' at end of declaration
try {
if (reference instanceof IImportDeclaration && moveCursor) {
int offset;
int length;
ISourceRange range = reference.getSourceRange();
String content = reference.getSource();
if (content != null) {
int start = Math.max(content.indexOf("import") + 6, 7);
while (start < content.length() && content.charAt(start) == ' ') {
start += 1;
}
int end = content.trim().length();
do {
end -= 1;
} while (end >= 0 && (content.charAt(end) == ' ' || content.charAt(end) == ';'));

offset = range.getOffset() + start;
length = end - start + 1; // Note, original JDT code has 8 here

// just in case...
int docLength = ((IImportDeclaration) reference).getOpenable().getBuffer().getLength();
if (docLength < offset + length) {
offset = docLength;
}
} else {
// fallback
offset = range.getOffset() + 1;
length = range.getLength() - 2;
}

if (offset > -1 && length > 0) {
try {
getSourceViewer().getTextWidget().setRedraw(false);
getSourceViewer().revealRange(offset, length);
getSourceViewer().setSelectedRange(offset, length);
} finally {
getSourceViewer().getTextWidget().setRedraw(true);
}

markInNavigationHistory();
}

}
} catch (JavaModelException e) {
GroovyPlugin.getDefault().logError("Error selecting import statement", e);
}
}

private IFile getFile() {
IEditorInput input = getEditorInput();
if (input instanceof FileEditorInput) {
Expand Down

0 comments on commit a354532

Please sign in to comment.