Search in sources :

Example 1 with OccurrenceLocation

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();
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) DocumentHighlight(org.eclipse.lsp4j.DocumentHighlight) JavaModelException(org.eclipse.jdt.core.JavaModelException) ArrayList(java.util.ArrayList) OccurrenceLocation(org.eclipse.jdt.internal.core.manipulation.search.IOccurrencesFinder.OccurrenceLocation) IOccurrencesFinder(org.eclipse.jdt.internal.core.manipulation.search.IOccurrencesFinder) OccurrencesFinder(org.eclipse.jdt.internal.core.manipulation.search.OccurrencesFinder)

Example 2 with OccurrenceLocation

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));
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ResponseError(org.eclipse.lsp4j.jsonrpc.messages.ResponseError) OccurrenceLocation(org.eclipse.jdt.internal.core.manipulation.search.IOccurrencesFinder.OccurrenceLocation) InnovationContext(org.eclipse.jdt.ls.core.internal.corrections.InnovationContext) Range(org.eclipse.lsp4j.Range) CoreException(org.eclipse.core.runtime.CoreException) ResponseErrorException(org.eclipse.lsp4j.jsonrpc.ResponseErrorException) ASTNode(org.eclipse.jdt.core.dom.ASTNode) OccurrencesFinder(org.eclipse.jdt.internal.core.manipulation.search.OccurrencesFinder)

Example 3 with OccurrenceLocation

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;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ArrayList(java.util.ArrayList) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name) ASTNode(org.eclipse.jdt.core.dom.ASTNode) OccurrencesFinder(org.eclipse.jdt.internal.core.manipulation.search.OccurrencesFinder) ASTParser(org.eclipse.jdt.core.dom.ASTParser) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ContentProviderManager(org.eclipse.jdt.ls.core.internal.managers.ContentProviderManager) Position(org.eclipse.lsp4j.Position) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) OccurrenceLocation(org.eclipse.jdt.internal.core.manipulation.search.IOccurrencesFinder.OccurrenceLocation) SourceRange(org.eclipse.jdt.core.SourceRange) ISourceRange(org.eclipse.jdt.core.ISourceRange) Range(org.eclipse.lsp4j.Range) PreferenceManager(org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager) CoreException(org.eclipse.core.runtime.CoreException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) JavaModelException(org.eclipse.jdt.core.JavaModelException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) IDocument(org.eclipse.jface.text.IDocument) OccurrenceLocation(org.eclipse.jdt.internal.core.manipulation.search.IOccurrencesFinder.OccurrenceLocation) Location(org.eclipse.lsp4j.Location)

Aggregations

CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)3 OccurrenceLocation (org.eclipse.jdt.internal.core.manipulation.search.IOccurrencesFinder.OccurrenceLocation)3 OccurrencesFinder (org.eclipse.jdt.internal.core.manipulation.search.OccurrencesFinder)3 ArrayList (java.util.ArrayList)2 CoreException (org.eclipse.core.runtime.CoreException)2 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)2 JavaModelException (org.eclipse.jdt.core.JavaModelException)2 ASTNode (org.eclipse.jdt.core.dom.ASTNode)2 Range (org.eclipse.lsp4j.Range)2 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)1 ISourceRange (org.eclipse.jdt.core.ISourceRange)1 SourceRange (org.eclipse.jdt.core.SourceRange)1 ASTParser (org.eclipse.jdt.core.dom.ASTParser)1 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)1 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)1 Name (org.eclipse.jdt.core.dom.Name)1 SimpleName (org.eclipse.jdt.core.dom.SimpleName)1