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

The following examples show how to use org.codehaus.groovy.ast.ASTNode#getLastColumnNumber() . 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: FindUsagesPainter.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * For the given {@link Line} and {@link ASTNode} returns colored text. In the
 * returned value there is the same text, but for example keywords are colored,
 * usage itself is bold, etc.
 *
 * @param node concrete usage node
 * @param line line where we have found the usage
 * @return colored text
 */
public static String colorASTNode(final ASTNode node, final Line line) {
    final int columnStart = node.getColumnNumber();
    final int columnEnd = node.getLastColumnNumber();

    if (node instanceof ClassNode) {
        return colorLine(line, ((ClassNode) node).getNameWithoutPackage());
    } else if (node instanceof ConstructorNode) {
        return colorLine(line, ((ConstructorNode) node).getDeclaringClass().getNameWithoutPackage());
    } else if (node instanceof ConstructorCallExpression) {
        return colorLine(line, ((ConstructorCallExpression) node).getType().getNameWithoutPackage());
    } else if (node instanceof MethodNode) {
        return colorLine(line, ((MethodNode) node).getName());
    } else if (node instanceof FieldNode) {
        return colorLine(line, ((FieldNode) node).getName());
    } else if (node instanceof FakeASTNode) {
        // I know this isn't the nicest way, but I can't pass ImportNode directly and
        // don't see easier way how to find out if the FakeASTNode is based on ImportNode
        ASTNode originalNode = ((FakeASTNode) node).getOriginalNode();
        if (originalNode instanceof ImportNode) {
            return colorLine(line, ((ImportNode) originalNode).getAlias());
        }
    }

    final String beforePart = line.createPart(0, columnStart - 1).getText();
    final String usagePart = line.createPart(columnStart - 1, columnEnd - columnStart).getText();
    final String afterPart = line.createPart(columnEnd - 1, line.getText().length()).getText();

    return buildHTML(beforePart, usagePart, afterPart);
}
 
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: 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);
                }
            }
        }
    }
}
 
Example 8
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 9
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());
}