org.fife.rsta.ac.common.CodeBlock Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of languagesupport Show documentation
Show all versions of languagesupport Show documentation
A library adding code completion and other advanced features for Java, JavaScript, Perl, and other languages to RSyntaxTextArea.
/* * 01/11/2010 * * Copyright (C) 2011 Robert Futrell * robert_futrell at users.sourceforge.net * http://fifesoft.com/rsyntaxtextarea * * This library is distributed under a modified BSD license. See the included * RSTALanguageSupport.License.txt file for details. */ package org.fife.rsta.ac.common; import java.util.ArrayList; import java.util.List; /** * A block of code. This can be used to implement very simple * parsing for languages that have some concept of code blocks, such as C, * Perl, Java, etc. Currently, using
if this code block and none of its children * contain the offset. */ public CodeBlock getDeepestCodeBlockContaining(int offs) { if (!contains(offs)) { return null; } for (int i=0; iCodeBlock
s provides a * means of remembering where variables are defined, as well as their scopes. * * @author Robert Futrell * @version 1.0 * @see VariableDeclaration */ public class CodeBlock { private int start; private int end; private CodeBlock parent; private List children; private List varDecs; /** * Constructor. * * @param start The starting offset of the code block. */ public CodeBlock(int start) { this.start = start; end = Integer.MAX_VALUE; } /** * Creates and returns a child (nested) code block. * * @param start The starting offset of the nested code block. * @return The code block. */ public CodeBlock addChildCodeBlock(int start) { CodeBlock child = new CodeBlock(start); child.parent = this; if (children==null) { children = new ArrayList(); } children.add(child); return child; } /** * Adds a variable declaration. * * @param varDec The variable declaration. */ public void addVariable(VariableDeclaration varDec) { if (varDecs==null) { varDecs = new ArrayList(); } varDecs.add(varDec); } /** * Returns whether this code block contains a given offset. * * @param offset The offset. * @return Whether this code block contains that offset. */ public boolean contains(int offset) { return offset>=start && offsetnull null if there isn't one. */ public CodeBlock getParent() { return parent; } /** * Returns the start offset of this code block. * * @return The start offset. * @see #getEndOffset() */ public int getStartOffset() { return start; } /** * Returns a variable declaration. * * @param index The index of the declaration. * @return The declaration. * @see #getVariableDeclarationCount() */ public VariableDeclaration getVariableDeclaration(int index) { return (VariableDeclaration)varDecs.get(index); } /** * Returns the number of variable declarations in this code block. * * @return The number of variable declarations. * @see #getVariableDeclaration(int) */ public int getVariableDeclarationCount() { return varDecs==null ? 0 : varDecs.size(); } /** * Returns all local variables declared before a given offset, both in * this code block and in all parent blocks. * * @param offs The offset. * @return The {@link VariableDeclaration}s, or an empty list of none were * declared before the offset. */ public List getVariableDeclarationsBefore(int offs) { List vars = new ArrayList(); int varCount = getVariableDeclarationCount(); for (int i=0; i