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

The following examples show how to use org.codehaus.groovy.ast.ASTNode#getColumnNumber() . 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: FindTypeUsagesVisitor.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void addIfEquals(ASTNode node) {
    final ClassNode type = ElementUtils.getType(node);
    if (isEquals(node)) {
        if (type.getLineNumber() > 0 && type.getColumnNumber() > 0) {
            usages.add(new ASTUtils.FakeASTNode(type, ElementUtils.getTypeName(node)));
        } else if (node.getLineNumber() > 0 && node.getColumnNumber() > 0) {
            usages.add(new ASTUtils.FakeASTNode(node, ElementUtils.getTypeName(node)));
        }
    }

    final GenericsType[] genericTypes = type.getGenericsTypes();
    if (genericTypes != null && genericTypes.length > 0) {
        for (GenericsType genericType : genericTypes) {
            addIfEquals(genericType.getType());
        }
    }
}
 
Example 3
Source File: MapEntryOrKeyValue.java    From groovy with Apache License 2.0 6 votes vote down vote up
static Options parse(MethodNode mn, ASTNode source, String[] options) throws IncorrectTypeHintException {
    int pIndex = 0;
    boolean generateIndex = false;
    for (String option : options) {
        String[] keyValue = option.split("=");
        if (keyValue.length==2) {
            String key = keyValue[0];
            String value = keyValue[1];
            if ("argNum".equals(key)) {
                pIndex = Integer.parseInt(value);
            } else if ("index".equals(key)) {
                generateIndex = Boolean.parseBoolean(value);
            } else {
                throw new IncorrectTypeHintException(mn, "Unrecognized option: "+key, source.getLineNumber(), source.getColumnNumber());
            }
        } else {
            throw new IncorrectTypeHintException(mn, "Incorrect option format. Should be argNum=<num> or index=<boolean> ", source.getLineNumber(), source.getColumnNumber());
        }
    }
    return new Options(pIndex, generateIndex);
}
 
Example 4
Source File: DefinitionProvider.java    From groovy-language-server with Apache License 2.0 5 votes vote down vote up
public CompletableFuture<Either<List<? extends Location>, List<? extends LocationLink>>> provideDefinition(
		TextDocumentIdentifier textDocument, Position position) {
	if (ast == null) {
		//this shouldn't happen, but let's avoid an exception if something
		//goes terribly wrong.
		return CompletableFuture.completedFuture(Either.forLeft(Collections.emptyList()));
	}
	URI uri = URI.create(textDocument.getUri());
	ASTNode offsetNode = ast.getNodeAtLineAndColumn(uri, position.getLine(), position.getCharacter());
	if (offsetNode == null) {
		return CompletableFuture.completedFuture(Either.forLeft(Collections.emptyList()));
	}

	ASTNode definitionNode = GroovyASTUtils.getDefinition(offsetNode, true, ast);
	if (definitionNode == null || definitionNode.getLineNumber() == -1 || definitionNode.getColumnNumber() == -1) {
		return CompletableFuture.completedFuture(Either.forLeft(Collections.emptyList()));
	}

	URI definitionURI = ast.getURI(definitionNode);
	if (definitionURI == null) {
		definitionURI = uri;
	}

	Location location = new Location(definitionURI.toString(),
			GroovyLanguageServerUtils.astNodeToRange(definitionNode));
	return CompletableFuture.completedFuture(Either.forLeft(Collections.singletonList(location)));
}
 
Example 5
Source File: TypeDefinitionProvider.java    From groovy-language-server with Apache License 2.0 5 votes vote down vote up
public CompletableFuture<Either<List<? extends Location>, List<? extends LocationLink>>> provideTypeDefinition(
		TextDocumentIdentifier textDocument, Position position) {
	if (ast == null) {
		//this shouldn't happen, but let's avoid an exception if something
		//goes terribly wrong.
		return CompletableFuture.completedFuture(Either.forLeft(Collections.emptyList()));
	}
	URI uri = URI.create(textDocument.getUri());
	ASTNode offsetNode = ast.getNodeAtLineAndColumn(uri, position.getLine(), position.getCharacter());
	if (offsetNode == null) {
		return CompletableFuture.completedFuture(Either.forLeft(Collections.emptyList()));
	}

	ASTNode definitionNode = GroovyASTUtils.getTypeDefinition(offsetNode, ast);
	if (definitionNode == null || definitionNode.getLineNumber() == -1 || definitionNode.getColumnNumber() == -1) {
		return CompletableFuture.completedFuture(Either.forLeft(Collections.emptyList()));
	}

	URI definitionURI = ast.getURI(definitionNode);
	if (definitionURI == null) {
		definitionURI = uri;
	}

	Location location = new Location(definitionURI.toString(),
			GroovyLanguageServerUtils.astNodeToRange(definitionNode));
	return CompletableFuture.completedFuture(Either.forLeft(Collections.singletonList(location)));
}
 
Example 6
Source File: FindTypeUtils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static OffsetRange getRange(ASTNode node, BaseDocument doc, int cursorOffset) {
    if (node.getLineNumber() < 0 || node.getColumnNumber() < 0) {
        return OffsetRange.NONE;
    }

    OffsetRange range = ASTUtils.getNextIdentifierByName(doc, ElementUtils.getTypeName(node), getOffset(node, doc));
    if (range.containsInclusive(cursorOffset)) {
        return range;
    }
    return OffsetRange.NONE;
}
 
Example 7
Source File: GroovyDeclarationFinder.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private DeclarationLocation getMethodDeclaration(GroovyParserResult info, String name, Set<IndexedMethod> methods,
        AstPath path, ASTNode closest, GroovyIndex index, int astOffset, int lexOffset) {
    BaseDocument doc = LexUtilities.getDocument(info, false);
    if (doc == null) {
        return DeclarationLocation.NONE;
    }

    IndexedMethod candidate =
        findBestMethodMatch(name, methods, doc,
            astOffset, lexOffset, path, closest, index);

    if (candidate != null) {
        FileObject fileObject = candidate.getFileObject();
        if (fileObject == null) {
            return DeclarationLocation.NONE;
        }

        ASTNode node = ASTUtils.getForeignNode(candidate);
        // negative line/column can happen due to bugs in groovy parser
        int nodeOffset = (node != null && node.getLineNumber() > 0 && node.getColumnNumber() > 0)
                ? ASTUtils.getOffset(doc, node.getLineNumber(), node.getColumnNumber())
                : 0;

        DeclarationLocation loc = new DeclarationLocation(
            fileObject, nodeOffset, candidate);

        return loc;
    }

    return DeclarationLocation.NONE;
}
 
Example 8
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 9
Source File: AbstractFindUsages.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void run(ResultIterator resultIterator) throws Exception {
    final GroovyParserResult result = ASTUtils.getParseResult(resultIterator.getParserResult());
    final ModuleNode moduleNode = result.getRootElement().getModuleNode();
    final BaseDocument doc = GroovyProjectUtil.getDocument(result, fo);
    
    for (AbstractFindUsagesVisitor visitor : getVisitors(moduleNode, defClass)) {
        for (ASTNode node : visitor.findUsages()) {
            if (node.getLineNumber() != -1 && node.getColumnNumber() != -1) {
                usages.add(new FindUsagesElement(new ClassRefactoringElement(fo, node), doc));
            }
        }
    }
    Collections.sort(usages);
}
 
Example 10
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 11
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 12
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 13
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 14
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 15
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());
}