use of org.eclipse.jdt.internal.core.manipulation.search.IOccurrencesFinder.OccurrenceLocation in project eclipse.jdt.ls by eclipse.
the class DocumentHighlightHandler method computeOccurrences.
private List<DocumentHighlight> computeOccurrences(ITypeRoot unit, int line, int column, IProgressMonitor monitor) {
if (unit != null) {
try {
int offset = JsonRpcHelpers.toOffset(unit.getBuffer(), line, column);
OccurrencesFinder finder = new OccurrencesFinder();
CompilationUnit ast = CoreASTProvider.getInstance().getAST(unit, CoreASTProvider.WAIT_YES, monitor);
if (ast != null) {
String error = finder.initialize(ast, offset, 0);
if (error == null) {
List<DocumentHighlight> result = new ArrayList<>();
OccurrenceLocation[] occurrences = finder.getOccurrences();
if (occurrences != null) {
for (OccurrenceLocation loc : occurrences) {
if (monitor.isCanceled()) {
return Collections.emptyList();
}
result.add(convertToHighlight(unit, loc));
}
}
return result;
}
}
} catch (JavaModelException e) {
JavaLanguageServerPlugin.logException("Problem with compute occurrences for" + unit.getElementName(), e);
}
}
return Collections.emptyList();
}
use of org.eclipse.jdt.internal.core.manipulation.search.IOccurrencesFinder.OccurrenceLocation in project eclipse.jdt.ls by eclipse.
the class PrepareRenameHandler method prepareRename.
public Either<Range, PrepareRenameResult> prepareRename(TextDocumentPositionParams params, IProgressMonitor monitor) {
final ICompilationUnit unit = JDTUtils.resolveCompilationUnit(params.getTextDocument().getUri());
if (unit != null) {
try {
OccurrencesFinder finder = new OccurrencesFinder();
CompilationUnit ast = CoreASTProvider.getInstance().getAST(unit, CoreASTProvider.WAIT_YES, monitor);
if (ast != null) {
int offset = JsonRpcHelpers.toOffset(unit.getBuffer(), params.getPosition().getLine(), params.getPosition().getCharacter());
String error = finder.initialize(ast, offset, 0);
if (error == null) {
OccurrenceLocation[] occurrences = finder.getOccurrences();
if (occurrences != null) {
for (OccurrenceLocation loc : occurrences) {
if (monitor.isCanceled()) {
return Either.forLeft(new Range());
}
if (loc.getOffset() <= offset && loc.getOffset() + loc.getLength() >= offset) {
InnovationContext context = new InnovationContext(unit, loc.getOffset(), loc.getLength());
context.setASTRoot(ast);
ASTNode node = context.getCoveredNode();
// Rename package is not fully supported yet.
if (!isBinaryOrPackage(node)) {
return Either.forLeft(JDTUtils.toRange(unit, loc.getOffset(), loc.getLength()));
}
}
}
}
}
}
} catch (CoreException e) {
JavaLanguageServerPlugin.logException("Problem computing occurrences for" + unit.getElementName() + " in prepareRename", e);
}
}
throw new ResponseErrorException(new ResponseError(ResponseErrorCode.InvalidRequest, "Renaming this element is not supported.", null));
}
use of org.eclipse.jdt.internal.core.manipulation.search.IOccurrencesFinder.OccurrenceLocation in project eclipse.jdt.ls by eclipse.
the class JDTUtils method searchDecompiledSources.
public static List<Location> searchDecompiledSources(IJavaElement element, IClassFile classFile, boolean ignoreMethodBody, boolean declaration, IProgressMonitor monitor) throws JavaModelException {
PreferenceManager preferencesManager = JavaLanguageServerPlugin.getPreferencesManager();
if (preferencesManager == null || !preferencesManager.isClientSupportsClassFileContent() || !preferencesManager.getPreferences().isIncludeDecompiledSources()) {
return Collections.emptyList();
}
ContentProviderManager contentProvider = JavaLanguageServerPlugin.getContentProviderManager();
String contents;
try {
contents = contentProvider.getSource(classFile, new NullProgressMonitor());
} catch (Exception e) {
JavaLanguageServerPlugin.logException(e.getMessage(), e);
return Collections.emptyList();
}
if (monitor != null && monitor.isCanceled()) {
return Collections.emptyList();
}
List<Location> locations = new ArrayList<>();
if (contents != null && !contents.isBlank()) {
ICompilationUnit workingCopy = workingCopy = getWorkingCopy(classFile, contents, monitor);
try {
final ASTParser parser = ASTParser.newParser(IASTSharedValues.SHARED_AST_LEVEL);
parser.setResolveBindings(true);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setStatementsRecovery(false);
parser.setBindingsRecovery(false);
parser.setSource(workingCopy);
parser.setIgnoreMethodBodies(ignoreMethodBody);
CompilationUnit unit = (CompilationUnit) parser.createAST(monitor);
final ASTNode[] nodes = new ASTNode[1];
if (monitor != null && monitor.isCanceled()) {
return Collections.emptyList();
}
unit.accept(new ClassFileVisitor(element, nodes, monitor));
ASTNode node = nodes[0];
if (monitor != null && monitor.isCanceled()) {
return Collections.emptyList();
}
Location location;
if (node != null) {
String uriString = JDTUtils.toUri(classFile);
IDocument document = new Document(contents);
if (declaration) {
int offset = node.getStartPosition();
int length = node.getLength();
Range range;
if (offset >= 0 && length > 0 && offset + length <= contents.length()) {
int[] start = JsonRpcHelpers.toLine(document, offset);
int[] end = JsonRpcHelpers.toLine(document, offset + length);
range = new Range(new Position(start[0], start[1]), new Position(end[0], end[1]));
} else {
range = new Range();
}
location = new Location(uriString, range);
locations.add(location);
return locations;
}
OccurrencesFinder finder = new OccurrencesFinder();
if (node instanceof MethodDeclaration) {
SimpleName name = ((MethodDeclaration) node).getName();
finder.initialize(unit, name);
} else if (node instanceof Name) {
finder.initialize(unit, node);
} else if (node instanceof MethodInvocation) {
SimpleName name = ((MethodInvocation) node).getName();
} else {
return locations;
}
OccurrenceLocation[] occurrences = finder.getOccurrences();
for (OccurrenceLocation occurrence : occurrences) {
int offset = occurrence.getOffset();
int length = occurrence.getLength();
Range range;
if (offset >= 0 && length > 0 && offset + length <= contents.length()) {
int[] start = JsonRpcHelpers.toLine(document, offset);
int[] end = JsonRpcHelpers.toLine(document, offset + length);
range = new Range(new Position(start[0], start[1]), new Position(end[0], end[1]));
} else {
range = new Range();
}
location = new Location(uriString, range);
locations.add(location);
}
} else {
location = JDTUtils.toLocation(classFile, 0, 0);
locations.add(location);
}
} finally {
if (workingCopy != null) {
workingCopy.discardWorkingCopy();
}
}
}
return locations;
}
Aggregations