Example usage for org.eclipse.jdt.core.dom ASTNode getStartPosition

List of usage examples for org.eclipse.jdt.core.dom ASTNode getStartPosition

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom ASTNode getStartPosition.

Prototype

public final int getStartPosition() 

Source Link

Document

Returns the character index into the original source file indicating where the source fragment corresponding to this node begins.

Usage

From source file:ast.AstNodeLocationMapper.java

License:Apache License

/**
 * wm?[hvf?Awu??o?B/*from  w w  w . j  ava2  s  .  c  om*/
 * li[?B
 * <ul>
 * <li>
 *   {@link AstNodeLocationMapper.FindResult#enclosing} -
 *     wLbg?m?[h?A?m?[h?B
 *     lK{@code null}?B
 * </li>
 * <li>
 *   {@link AstNodeLocationMapper.FindResult#previous} -
 *     wLbg?AwLbgO?m?[h?A
 *     {@link AstNodeLocationMapper.FindResult#enclosing enclosing}
 *     ?qvf?B?Avf?B
 * ?@?@?@?@?AL{@code // CARET}uLbg????A
 *     l{@code System.out.println("prev");}?B
 * <pre><code>
 * public void hoge() {
 *   System.out.println("prev");
 *   // CARET
 *   System.out.println("next");
 * }
 * </code></pre>
 *     ?Lm?[h????Al{@code null}?B
 * </li>
 * </ul>
 * @param root ???[gm?[h
 * @param position u
 * @return
 *      ?o\{@link AstNodeLocationMapper.FindResult}IuWFNg?B
 *      {@code root}{@code position}??{@code null}
 * @throws NullPointerException ?{@code null}w??
 * @throws IllegalArgumentException
 *          {@code position}{@code 0}lw??
 */
public static FindResult findNode(ASTNode root, int position) {
    if (root == null) {
        throw new NullPointerException("root"); //$NON-NLS-1$
    }
    if (position < 0) {
        throw new IllegalArgumentException(MessageFormat.format("position = {0} (< 0)", //$NON-NLS-1$
                position));
    }
    ASTNode current = root;
    if (!isEnclosing(root.getStartPosition(), root.getLength(), position)) {
        return null;
    }

    while (true) {
        ASTNode child = findNearestPreviousChild(current, position);

        // qvfuw???A?[?
        if (child != null && isEnclosing(child.getStartPosition(), child.getLength(), position)) {
            current = child;
        }

        // wu?qvf???A?B
        else {
            return new FindResult(current, child);
        }
    }
}

From source file:ast.AstNodeLocationMapper.java

License:Apache License

private static ASTNode findNearestPreviousChild(ASTNode node, int position) {
    int childAt = -1;
    ASTNode childNode = null;//  w w w.  j av a  2  s  . c  o m
    for (Object o : node.structuralPropertiesForType()) {
        if (o instanceof ChildPropertyDescriptor) {
            ASTNode n = (ASTNode) node.getStructuralProperty((StructuralPropertyDescriptor) o);
            if (n != null) {
                int start = n.getStartPosition();
                if (start >= 0 && childAt < start && start <= position) {
                    childAt = start;
                    childNode = n;
                }
            }
        } else if (o instanceof ChildListPropertyDescriptor) {
            List<?> list = (List<?>) node.getStructuralProperty((StructuralPropertyDescriptor) o);
            for (Object e : list) {
                ASTNode n = (ASTNode) e;
                assert n != null;
                int start = n.getStartPosition();
                if (start >= 0 && childAt < start && start <= position) {
                    childAt = start;
                    childNode = n;
                }
            }
        }
    }
    return childNode;
}

From source file:at.bestsolution.fxide.jdt.text.javadoc.JavadocContentAccess2.java

License:Open Source License

private void handleContentElements(List<? extends ASTNode> nodes, boolean skipLeadingWhitespace) {
    ASTNode previousNode = null;
    for (Iterator<? extends ASTNode> iter = nodes.iterator(); iter.hasNext();) {
        ASTNode child = iter.next();//from   w ww .  j a v a  2 s .com
        if (previousNode != null) {
            int previousEnd = previousNode.getStartPosition() + previousNode.getLength();
            int childStart = child.getStartPosition();
            if (previousEnd > childStart) {
                // should never happen, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=304826
                Exception exception = new Exception("Illegal ASTNode positions: previousEnd=" + previousEnd //$NON-NLS-1$
                        + ", childStart=" + childStart //$NON-NLS-1$
                        + ", element=" + fElement.getHandleIdentifier() //$NON-NLS-1$
                        + ", Javadoc:\n" + fSource); //$NON-NLS-1$
                //TODO
                exception.printStackTrace();
            } else if (previousEnd != childStart) {
                // Need to preserve whitespace before a node that's not
                // directly following the previous node (e.g. on a new line)
                // due to https://bugs.eclipse.org/bugs/show_bug.cgi?id=206518 :
                String textWithStars = fSource.substring(previousEnd, childStart);
                String text = removeDocLineIntros(textWithStars);
                fBuf.append(text);
            }
        }
        previousNode = child;
        if (child instanceof TextElement) {
            String text = ((TextElement) child).getText();
            if (skipLeadingWhitespace) {
                text = text.replaceFirst("^\\s+", ""); //$NON-NLS-1$ //$NON-NLS-2$
            }
            // workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=233481 :
            text = text.replaceAll("(\r\n?|\n)([ \t]*\\*)", "$1"); //$NON-NLS-1$ //$NON-NLS-2$
            text = text.replace("#__", "}");
            text = text.replace("__#", "{");

            handleText(text);

        } else if (child instanceof TagElement) {
            handleInlineTagElement((TagElement) child);
        } else {
            // This is unexpected. Fail gracefully by just copying the source.
            int start = child.getStartPosition();
            String text = fSource.substring(start, start + child.getLength());
            fBuf.append(removeDocLineIntros(text));
        }
    }
}

From source file:boa.datagen.util.Java7Visitor.java

License:Apache License

protected void buildPosition(final ASTNode node) {
    pos = PositionInfo.newBuilder();// w  ww. ja  va2 s. c om
    int start = node.getStartPosition();
    int length = node.getLength();
    pos.setStartPos(start);
    pos.setLength(length);
    pos.setStartLine(root.getLineNumber(start));
    pos.setStartCol(root.getColumnNumber(start));
    pos.setEndLine(root.getLineNumber(start + length));
    pos.setEndCol(root.getColumnNumber(start + length));
}

From source file:boa.datagen.util.JavaVisitor.java

License:Apache License

private void buildPosition(final ASTNode node) {
    pos = PositionInfo.newBuilder();//from  w  w  w. ja  v  a  2  s. co  m
    int start = node.getStartPosition();
    int length = node.getLength();
    pos.setStartPos(start);
    pos.setLength(length);
    pos.setStartLine(root.getLineNumber(start));
    pos.setStartCol(root.getColumnNumber(start));
    pos.setEndLine(root.getLineNumber(start + length));
    pos.setEndCol(root.getColumnNumber(start + length));
}

From source file:br.ufal.cideei.soot.instrument.asttounit.ASTNodesAtRangeFinder.java

License:Open Source License

@Override
public void preVisit(ASTNode node) {
    int nodeStartPosition = node.getStartPosition();
    int nodeEndPosition = nodeStartPosition + node.getLength();

    if (compilationUnit.getLineNumber(nodeStartPosition) == startLine
            && compilationUnit.getColumnNumber(nodeStartPosition) + 1 >= startPos
            && compilationUnit.getLineNumber(nodeEndPosition) == endLine
            && compilationUnit.getColumnNumber(nodeEndPosition) - 1 <= endPos) {

        this.foundNodes.add(node);
    }//w  ww . j  a v  a 2 s .  c  om
}

From source file:br.ufal.cideei.soot.instrument.asttounit.ASTNodeUnitBridge.java

License:Open Source License

/**
 * Gets the lines from the AST nodes./* w  w w.  j ava  2  s.  co m*/
 * 
 * @param nodes
 *            the nodes
 * @param compilationUnit
 *            the compilation unit
 * @return the lines from ast nodes
 */
public static Collection<Integer> getLinesFromASTNodes(Collection<ASTNode> nodes,
        CompilationUnit compilationUnit) {
    Set<Integer> lineSet = new HashSet<Integer>();
    for (ASTNode node : nodes) {
        lineSet.add(compilationUnit.getLineNumber(node.getStartPosition()));
    }
    return lineSet;
}

From source file:br.ufal.cideei.soot.instrument.asttounit.MultipleLineNumbersVisitor.java

License:Open Source License

public boolean visit(ASTNode node) {
    if (lines.contains(compilationUnit.getLineNumber(node.getStartPosition()))) {
        nodes.add(node);//from www  .j  a  v  a  2  s . c  o  m
    }
    return true;
}

From source file:br.ufal.cideei.soot.instrument.asttounit.SingleLineNumberVisitor.java

License:Open Source License

public boolean visit(ASTNode node) {
    if (compilationUnit.getLineNumber(node.getStartPosition()) == line) {
        nodes.add(node);/*from  w ww.java 2s  . com*/
    }
    return true;
}

From source file:br.ufal.cideei.soot.instrument.LineNumberColorMapper.java

License:Open Source License

@Override
public void preVisit(ASTNode node) {
    int lineNumber = compilationUnit.getLineNumber(node.getStartPosition());
    Set<String> mappedFeatureSet = lineToColors.get(lineNumber);
    if (mappedFeatureSet == null) {

        long startExtract = System.nanoTime();
        Set<String> extractedFeatures = extracter.getFeaturesNames(node, file);
        long endExtract = System.nanoTime();
        long extractDelta = endExtract - startExtract;
        LineNumberColorMapper.extractTime += extractDelta;

        if (!extractedFeatures.isEmpty()) {
            lineToColors.put(lineNumber, extractedFeatures);
        }//  w ww.  j ava  2 s .c  om
    }
}