Java Code Examples for org.codehaus.groovy.ast.ASTNode#getLastLineNumber()

The following examples show how to use org.codehaus.groovy.ast.ASTNode#getLastLineNumber() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PathFinderVisitor.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private boolean isInSource(ASTNode node) {
    if (node instanceof AnnotatedNode) {
        if (((AnnotatedNode) node).hasNoRealSourcePosition()) {
            return false;
        }
    }

    // FIXME probably http://jira.codehaus.org/browse/GROOVY-3263
    if (node instanceof StaticMethodCallExpression && node.getLineNumber() == -1
            && node.getLastLineNumber() == -1 && node.getColumnNumber() == -1
            && node.getLastColumnNumber() == -1) {

        StaticMethodCallExpression methodCall = (StaticMethodCallExpression) node;
        if ("initMetaClass".equals(methodCall.getMethod())) { // NOI18N
            Expression args = methodCall.getArguments();
            if (args instanceof VariableExpression) {
                VariableExpression var = (VariableExpression) args;
                if ("this".equals(var.getName())) { // NOI18N
                    return false;
                }
            }
        }
    }
    return true;
}
 
Example 2
Source File: GeneralUtils.java    From groovy with Apache License 2.0 6 votes vote down vote up
/**
 * Converts an expression into the String source. Only some specific expressions like closure expression
 * support this.
 *
 * @param readerSource a source
 * @param expression an expression. Can't be null
 * @return the source the closure was created from
 * @throws java.lang.IllegalArgumentException when expression is null
 * @throws java.lang.Exception when closure can't be read from source
 */
public static String convertASTToSource(final ReaderSource readerSource, final ASTNode expression) throws Exception {
    if (expression == null) throw new IllegalArgumentException("Null: expression");

    StringBuilder result = new StringBuilder();
    for (int x = expression.getLineNumber(), y = expression.getLastLineNumber(); x <= y; x += 1) {
        String line = readerSource.getLine(x, null);
        if (line == null) {
            throw new Exception(
                    "Error calculating source code for expression. Trying to read line " + x + " from " + readerSource.getClass()
            );
        }
        if (x == expression.getLastLineNumber()) {
            line = line.substring(0, expression.getLastColumnNumber() - 1);
        }
        if (x == expression.getLineNumber()) {
            line = line.substring(expression.getColumnNumber() - 1);
        }
        //restoring line breaks is important b/c of lack of semicolons
        result.append(line).append('\n');
    }

    String source = result.toString().trim();

    return source;
}
 
Example 3
Source File: GroovyGradleDetector.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected Location createLocation(@NonNull Context context, @NonNull Object cookie) {
    ASTNode node = (ASTNode) cookie;
    Pair<Integer, Integer> offsets = getOffsets(node, context);
    int fromLine = node.getLineNumber() - 1;
    int fromColumn = node.getColumnNumber() - 1;
    int toLine = node.getLastLineNumber() - 1;
    int toColumn = node.getLastColumnNumber() - 1;
    return Location.create(context.file,
            new DefaultPosition(fromLine, fromColumn, offsets.getFirst()),
            new DefaultPosition(toLine, toColumn, offsets.getSecond()));
}
 
Example 4
Source File: GradleDetectorTest.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected Location createLocation(@NonNull Context context, @NonNull Object cookie) {
    ASTNode node = (ASTNode) cookie;
    Pair<Integer, Integer> offsets = getOffsets(node, context);
    int fromLine = node.getLineNumber() - 1;
    int fromColumn = node.getColumnNumber() - 1;
    int toLine = node.getLastLineNumber() - 1;
    int toColumn = node.getLastColumnNumber() - 1;
    return Location.create(context.file,
            new DefaultPosition(fromLine, fromColumn, offsets.getFirst()),
            new DefaultPosition(toLine, toColumn, offsets.getSecond()));
}
 
Example 5
Source File: SourceText.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static boolean hasPlausibleSourcePosition(ASTNode node) {
    return node.getLineNumber() > 0
            && node.getColumnNumber() > 0
            && node.getLastLineNumber() >= node.getLineNumber()
            && node.getLastColumnNumber() >
            (node.getLineNumber() == node.getLastLineNumber() ? node.getColumnNumber() : 0);
}
 
Example 6
Source File: PathFinderVisitor.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private boolean isInside(ASTNode node, int line, int column, boolean addToPath) {
    if (node == null || !isInSource(node)) {
        return false;
    }

    fixNode(node);

    int beginLine = node.getLineNumber();
    int beginColumn = node.getColumnNumber();
    int endLine = node.getLastLineNumber();
    int endColumn = node.getLastColumnNumber();

    if (LOG.isLoggable(Level.FINEST)) {
        LOG.log(Level.FINEST, "isInside: " + node + " - "
                + beginLine + ", " + beginColumn + ", " + endLine + ", " + endColumn);
    }

    if (beginLine == -1 || beginColumn == -1 || endLine == -1 || endColumn == -1) {
        // this node doesn't provide its coordinates, some wrappers do that
        // let's say yes and visit its children
        return addToPath ? true : false;
    }

    boolean result = false;

    if (beginLine == endLine) {
        if (line == beginLine && column >= beginColumn && column < endColumn) {
            result = true;
        }
    } else if (line == beginLine) {
        if (column >= beginColumn) {
            result = true;
        }
    } else if (line == endLine) {
        if (column < endColumn) {
            result = true;
        }
    } else if (beginLine < line && line < endLine) {
        result = true;
    } else {
        result = false;
    }

    if (result && addToPath) {
        path.add(node);
        LOG.log(Level.FINEST, "Path: {0}", path);
    }

    // if addToPath is false, return result, we want to know real state of affairs
    // and not to continue traversing
    return addToPath ? true : result;
}
 
Example 7
Source File: RuntimeParserException.java    From groovy with Apache License 2.0 4 votes vote down vote up
public void throwParserException() throws SyntaxException {
    final ASTNode node = getNode();
    throw new SyntaxException(getMessage(), node.getLineNumber(), node.getColumnNumber(), node.getLastLineNumber(), node.getLastColumnNumber());
}
 
Example 8
Source File: SyntaxException.java    From groovy with Apache License 2.0 4 votes vote down vote up
public SyntaxException(String message, ASTNode node) {
    this(message, node.getLineNumber(), node.getColumnNumber(), node.getLastLineNumber(), node.getLastColumnNumber());
}