Search in sources :

Example 1 with GenericVisitor

use of org.eclipse.jdt.internal.corext.dom.GenericVisitor in project che by eclipse.

the class ConvertForLoopOperation method convertBody.

private void convertBody(Statement body, final IBinding indexBinding, final IBinding arrayBinding, final String parameterName, final ASTRewrite rewrite, final TextEditGroup editGroup, final LinkedProposalPositionGroup pg) {
    final AST ast = body.getAST();
    body.accept(new GenericVisitor() {

        @Override
        public boolean visit(ArrayAccess node) {
            IBinding binding = getBinding(node.getArray());
            if (arrayBinding.equals(binding)) {
                IBinding index = getBinding(node.getIndex());
                if (indexBinding.equals(index)) {
                    replaceAccess(node);
                }
            }
            return super.visit(node);
        }

        private void replaceAccess(ASTNode node) {
            if (fElementDeclaration != null && node.getLocationInParent() == VariableDeclarationFragment.INITIALIZER_PROPERTY) {
                VariableDeclarationFragment fragment = (VariableDeclarationFragment) node.getParent();
                IBinding targetBinding = fragment.getName().resolveBinding();
                if (targetBinding != null) {
                    VariableDeclarationStatement statement = (VariableDeclarationStatement) fragment.getParent();
                    if (statement.fragments().size() == 1) {
                        rewrite.remove(statement, editGroup);
                    } else {
                        ListRewrite listRewrite = rewrite.getListRewrite(statement, VariableDeclarationStatement.FRAGMENTS_PROPERTY);
                        listRewrite.remove(fragment, editGroup);
                    }
                } else {
                    SimpleName name = ast.newSimpleName(parameterName);
                    rewrite.replace(node, name, editGroup);
                    pg.addPosition(rewrite.track(name), true);
                }
            } else {
                SimpleName name = ast.newSimpleName(parameterName);
                rewrite.replace(node, name, editGroup);
                pg.addPosition(rewrite.track(name), true);
            }
        }
    });
}
Also used : AST(org.eclipse.jdt.core.dom.AST) ArrayAccess(org.eclipse.jdt.core.dom.ArrayAccess) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) IBinding(org.eclipse.jdt.core.dom.IBinding) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ASTNode(org.eclipse.jdt.core.dom.ASTNode) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) GenericVisitor(org.eclipse.jdt.internal.corext.dom.GenericVisitor)

Example 2 with GenericVisitor

use of org.eclipse.jdt.internal.corext.dom.GenericVisitor in project eclipse.jdt.ls by eclipse.

the class QuickAssistProcessor method getFullyCoveredNodes.

static ArrayList<ASTNode> getFullyCoveredNodes(IInvocationContext context, ASTNode coveringNode) {
    final ArrayList<ASTNode> coveredNodes = new ArrayList<>();
    final int selectionBegin = context.getSelectionOffset();
    final int selectionEnd = selectionBegin + context.getSelectionLength();
    coveringNode.accept(new GenericVisitor() {

        @Override
        protected boolean visitNode(ASTNode node) {
            int nodeStart = node.getStartPosition();
            int nodeEnd = nodeStart + node.getLength();
            // if node does not intersects with selection, don't visit children
            if (nodeEnd < selectionBegin || selectionEnd < nodeStart) {
                return false;
            }
            // if node is fully covered, we don't need to visit children
            if (isCovered(node)) {
                ASTNode parent = node.getParent();
                if (parent == null || !isCovered(parent)) {
                    coveredNodes.add(node);
                    return false;
                }
            }
            // if node only partly intersects with selection, we try to find fully covered children
            return true;
        }

        private boolean isCovered(ASTNode node) {
            int begin = node.getStartPosition();
            int end = begin + node.getLength();
            return begin >= selectionBegin && end <= selectionEnd;
        }
    });
    return coveredNodes;
}
Also used : ASTNode(org.eclipse.jdt.core.dom.ASTNode) ArrayList(java.util.ArrayList) GenericVisitor(org.eclipse.jdt.internal.corext.dom.GenericVisitor)

Example 3 with GenericVisitor

use of org.eclipse.jdt.internal.corext.dom.GenericVisitor in project che by eclipse.

the class ConvertForLoopQuickFixTest method getForStatement.

private static ForStatement getForStatement(ICompilationUnit cu) {
    CompilationUnit ast = SharedASTProvider.getAST(cu, SharedASTProvider.WAIT_YES, new NullProgressMonitor());
    final ForStatement[] statement = new ForStatement[1];
    ast.accept(new GenericVisitor() {

        protected boolean visitNode(ASTNode node) {
            if (node instanceof ForStatement) {
                statement[0] = (ForStatement) node;
                return false;
            } else {
                return super.visitNode(node);
            }
        }
    });
    return statement[0];
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ForStatement(org.eclipse.jdt.core.dom.ForStatement) GenericVisitor(org.eclipse.jdt.internal.corext.dom.GenericVisitor)

Example 4 with GenericVisitor

use of org.eclipse.jdt.internal.corext.dom.GenericVisitor in project che by eclipse.

the class ASTNodeDeleteUtil method getRemovedNodes.

private static Set<ASTNode> getRemovedNodes(final List<ASTNode> removed, final CompilationUnitRewrite rewrite) {
    final Set<ASTNode> result = new HashSet<ASTNode>();
    rewrite.getRoot().accept(new GenericVisitor(true) {

        @Override
        protected boolean visitNode(ASTNode node) {
            if (removed.contains(node))
                result.add(node);
            return true;
        }
    });
    return result;
}
Also used : ASTNode(org.eclipse.jdt.core.dom.ASTNode) GenericVisitor(org.eclipse.jdt.internal.corext.dom.GenericVisitor) HashSet(java.util.HashSet)

Example 5 with GenericVisitor

use of org.eclipse.jdt.internal.corext.dom.GenericVisitor in project che by eclipse.

the class ConvertLoopOperation method getUsedVariableNames.

protected String[] getUsedVariableNames() {
    final List<String> results = new ArrayList<String>();
    ForStatement forStatement = getForStatement();
    CompilationUnit root = (CompilationUnit) forStatement.getRoot();
    Collection<String> variableNames = new ScopeAnalyzer(root).getUsedVariableNames(forStatement.getStartPosition(), forStatement.getLength());
    results.addAll(variableNames);
    forStatement.accept(new GenericVisitor() {

        @Override
        public boolean visit(SingleVariableDeclaration node) {
            results.add(node.getName().getIdentifier());
            return super.visit(node);
        }

        @Override
        public boolean visit(VariableDeclarationFragment fragment) {
            results.add(fragment.getName().getIdentifier());
            return super.visit(fragment);
        }
    });
    results.addAll(Arrays.asList(fUsedNames));
    return results.toArray(new String[results.size()]);
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ArrayList(java.util.ArrayList) ScopeAnalyzer(org.eclipse.jdt.internal.corext.dom.ScopeAnalyzer) ForStatement(org.eclipse.jdt.core.dom.ForStatement) GenericVisitor(org.eclipse.jdt.internal.corext.dom.GenericVisitor)

Aggregations

GenericVisitor (org.eclipse.jdt.internal.corext.dom.GenericVisitor)7 ASTNode (org.eclipse.jdt.core.dom.ASTNode)5 ArrayList (java.util.ArrayList)4 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)2 ForStatement (org.eclipse.jdt.core.dom.ForStatement)2 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)2 HashSet (java.util.HashSet)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1 AST (org.eclipse.jdt.core.dom.AST)1 ArrayAccess (org.eclipse.jdt.core.dom.ArrayAccess)1 IBinding (org.eclipse.jdt.core.dom.IBinding)1 SimpleName (org.eclipse.jdt.core.dom.SimpleName)1 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)1 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)1 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)1 ScopeAnalyzer (org.eclipse.jdt.internal.corext.dom.ScopeAnalyzer)1