Java Code Examples for org.codehaus.groovy.ast.ASTNode#getLineNumber()
The following examples show how to use
org.codehaus.groovy.ast.ASTNode#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: MapEntryOrKeyValue.java From groovy with Apache License 2.0 | 6 votes |
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 2
Source File: PathFinderVisitor.java From netbeans with Apache License 2.0 | 6 votes |
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 3
Source File: MethodCallExpression.java From groovy with Apache License 2.0 | 6 votes |
@Override public void setSourcePosition(ASTNode node) { super.setSourcePosition(node); // GROOVY-8002: propagate position to (possibly new) method expression if (node instanceof MethodCall) { if (node instanceof MethodCallExpression) { method.setSourcePosition(((MethodCallExpression) node).getMethod()); } else if (node.getLineNumber() > 0) { method.setLineNumber(node.getLineNumber()); method.setColumnNumber(node.getColumnNumber()); method.setLastLineNumber(node.getLineNumber()); method.setLastColumnNumber(node.getColumnNumber() + getMethodAsString().length()); } if (arguments != null) { arguments.setSourcePosition(((MethodCall) node).getArguments()); } } else if (node instanceof PropertyExpression) { method.setSourcePosition(((PropertyExpression) node).getProperty()); } }
Example 4
Source File: GeneralUtils.java From groovy with Apache License 2.0 | 6 votes |
/** * 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 5
Source File: FindTypeUsagesVisitor.java From netbeans with Apache License 2.0 | 6 votes |
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 6
Source File: GradleDetectorTest.java From javaide with GNU General Public License v3.0 | 5 votes |
@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 7
Source File: SourceText.java From groovy with Apache License 2.0 | 5 votes |
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 8
Source File: AsmClassGenerator.java From groovy with Apache License 2.0 | 5 votes |
public void onLineNumber(final ASTNode statement, final String message) { if (statement == null || statement instanceof BlockStatement) return; currentASTNode = statement; int line = statement.getLineNumber(); if (line < 0 || (!ASM_DEBUG && line == controller.getLineNumber())) return; controller.setLineNumber(line); MethodVisitor mv = controller.getMethodVisitor(); if (mv != null) { Label l = new Label(); mv.visitLabel(l); mv.visitLineNumber(line, l); } }
Example 9
Source File: DefinitionProvider.java From groovy-language-server with Apache License 2.0 | 5 votes |
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 10
Source File: GroovyGradleDetector.java From javaide with GNU General Public License v3.0 | 5 votes |
@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: AbstractFindUsages.java From netbeans with Apache License 2.0 | 5 votes |
@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 12
Source File: GroovyDeclarationFinder.java From netbeans with Apache License 2.0 | 5 votes |
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 13
Source File: FindTypeUtils.java From netbeans with Apache License 2.0 | 5 votes |
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 14
Source File: TypeDefinitionProvider.java From groovy-language-server with Apache License 2.0 | 5 votes |
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 15
Source File: RuntimeParserException.java From groovy with Apache License 2.0 | 4 votes |
public void throwParserException() throws SyntaxException { final ASTNode node = getNode(); throw new SyntaxException(getMessage(), node.getLineNumber(), node.getColumnNumber(), node.getLastLineNumber(), node.getLastColumnNumber()); }
Example 16
Source File: SyntaxException.java From groovy with Apache License 2.0 | 4 votes |
public SyntaxException(String message, ASTNode node) { this(message, node.getLineNumber(), node.getColumnNumber(), node.getLastLineNumber(), node.getLastColumnNumber()); }
Example 17
Source File: ClassCompletionVerifier.java From groovy with Apache License 2.0 | 4 votes |
private void checkNoStaticMethodWithSameSignatureAsNonStatic(final ClassNode node) { ClassNode parent = node.getSuperClass(); Map<String, MethodNode> result; // start with methods from the parent if any if (parent != null) { result = parent.getDeclaredMethodsMap(); } else { result = new HashMap<String, MethodNode>(); } // add in unimplemented abstract methods from the interfaces ClassNodeUtils.addDeclaredMethodsFromInterfaces(node, result); for (MethodNode methodNode : node.getMethods()) { MethodNode mn = result.get(methodNode.getTypeDescriptor()); if (mn != null && (mn.isStatic() ^ methodNode.isStatic()) && !methodNode.isStaticConstructor()) { if (!mn.isAbstract()) continue; ClassNode declaringClass = mn.getDeclaringClass(); ClassNode cn = declaringClass.getOuterClass(); if (cn == null && declaringClass.isResolved()) { // in case of a precompiled class, the outerclass is unknown Class typeClass = declaringClass.getTypeClass(); typeClass = typeClass.getEnclosingClass(); if (typeClass != null) { cn = ClassHelper.make(typeClass); } } if (!Traits.isTrait(cn)) { ASTNode errorNode = methodNode; String name = mn.getName(); if (errorNode.getLineNumber() == -1) { // try to get a better error message location based on the property for (PropertyNode propertyNode : node.getProperties()) { if (name.startsWith("set") || name.startsWith("get") || name.startsWith("is")) { String propName = Verifier.capitalize(propertyNode.getField().getName()); String shortName = name.substring(name.startsWith("is") ? 2 : 3); if (propName.equals(shortName)) { errorNode = propertyNode; break; } } } } addError("The " + getDescription(methodNode) + " is already defined in " + getDescription(node) + ". You cannot have both a static and an instance method with the same signature", errorNode); } } result.put(methodNode.getTypeDescriptor(), methodNode); } }
Example 18
Source File: PathFinderVisitor.java From netbeans with Apache License 2.0 | 4 votes |
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; }