Java Code Examples for org.codehaus.groovy.ast.stmt.Statement#getLineNumber()

The following examples show how to use org.codehaus.groovy.ast.stmt.Statement#getLineNumber() . 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: AutoNewLineTransformer.java    From groovy with Apache License 2.0 6 votes vote down vote up
@Override
public void visitClosureExpression(final ClosureExpression expression) {
    super.visitClosureExpression(expression);
    if (inBuilderMethod) {
        Statement oldCode = expression.getCode();
        BlockStatement block = oldCode instanceof BlockStatement?
                ((BlockStatement)oldCode):
                new BlockStatement(Collections.singletonList(oldCode), new VariableScope());
        List<Statement> statements = block.getStatements();
        if (!statements.isEmpty()) {
            Statement first = statements.get(0);
            Statement last = statements.get(statements.size()-1);
            if (expression.getLineNumber()<first.getLineNumber()) {
                // there's a new line between { -> ... and the first statement
                statements.add(0,createNewLine(expression));
            }
            if (expression.getLastLineNumber()>last.getLastLineNumber()) {
                // there's a new line between { -> ... and the first statement
                statements.add(createNewLine(expression));
            }
        }
        expression.setCode(block);
    }
}
 
Example 2
Source File: PathFinderVisitor.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private void fixNode(ASTNode node) {
    // FIXME http://jira.codehaus.org/browse/GROOVY-3263
    if (node instanceof MethodCallExpression && !((MethodCallExpression) node).isImplicitThis()) {
        MethodCallExpression call = (MethodCallExpression) node;
        if (call.getObjectExpression() == VariableExpression.THIS_EXPRESSION
                || call.getObjectExpression() == VariableExpression.SUPER_EXPRESSION) {
            // this is not bulletproof but fix most of the problems
            VariableExpression var = new VariableExpression(
                    call.getObjectExpression() == VariableExpression.THIS_EXPRESSION ? "this" : "super", // NOI18N
                    call.getObjectExpression().getType()); // NOI18N
            var.setLineNumber(call.getLineNumber());
            var.setColumnNumber(call.getColumnNumber());
            var.setLastLineNumber(call.getMethod().getLineNumber());
            var.setLastColumnNumber(call.getMethod().getColumnNumber());
            call.setObjectExpression(var);
        }
    // FIXME http://jira.codehaus.org/browse/GROOVY-3472
    } else if (node instanceof MethodNode || node instanceof ClosureExpression) {
        Statement code = null;
        if (node instanceof MethodNode) {
            code = ((MethodNode) node).getCode();
        } else {
            code = ((ClosureExpression) node).getCode();
        }

        if (code != null && code instanceof BlockStatement
                && ((code.getLineNumber() < 0 && code.getColumnNumber() < 0)
                || (code.getLastLineNumber() < 0 && code.getLastColumnNumber() < 0))) {
            BlockStatement block = (BlockStatement) code;
            List statements = block.getStatements();
            if (statements != null && !statements.isEmpty()) {
                if (code.getLineNumber() < 0 && code.getColumnNumber() < 0) {
                    Statement first = (Statement) statements.get(0);
                    code.setLineNumber(first.getLineNumber());
                    code.setColumnNumber(first.getColumnNumber());
                }
                if (code.getLastLineNumber() < 0 && code.getLastColumnNumber() < 0) {
                    // maybe not accurate
                    code.setLastLineNumber(node.getLastLineNumber());
                    int lastColumn = node.getLastColumnNumber();
                    if (lastColumn > 0) {
                        lastColumn--;
                    }
                    code.setLastColumnNumber(lastColumn);
                }
            }
        }
    }
}