Search in sources :

Example 6 with GenericVisitor

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

the class AdvancedQuickAssistProcessor method getFullyCoveredNodes.

static ArrayList<ASTNode> getFullyCoveredNodes(IInvocationContext context, ASTNode coveringNode) {
    final ArrayList<ASTNode> coveredNodes = new ArrayList<ASTNode>();
    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 : ArrayList(java.util.ArrayList) GenericVisitor(org.eclipse.jdt.internal.corext.dom.GenericVisitor)

Example 7 with GenericVisitor

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

the class AdvancedQuickAssistProcessor method getFullyCoveredNodes.

static ArrayList<ASTNode> getFullyCoveredNodes(IInvocationContext context, ASTNode coveringNode) {
    final ArrayList<ASTNode> coveredNodes = new ArrayList<ASTNode>();
    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)

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