Class Fold
- All Implemented Interfaces:
Comparable<Fold>
A Fold
has zero or more children, and Folds
thus
form a hierarchical structure, with "parent" folds containing the info about
any "child" folds they contain.
Fold regions are denoted by a starting and ending offset, but the actual
folding is done on a per-line basis, so Fold
instances provide
methods for retrieving both starting and ending offsets and lines. The
starting and ending offsets/lines are "sticky" and correctly track their
positions even as the document is modified.
- Version:
- 1.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate int
private int
private int
private boolean
private Position
private int
private int
private Fold
private Position
private RSyntaxTextArea
private int
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionint
Two folds are considered equal if they start at the same offset.boolean
containsLine
(int line) Returns whether the specified line would be hidden in this fold.boolean
containsOffset
(int offs) Returns whether the specified offset is "inside" the fold.boolean
containsOrStartsOnLine
(int line) Returns whether the given line is in the range[getStartLine(), getEndLine()]
, inclusive.createChild
(int type, int startOffs) Creates a fold that is a child of this one.boolean
Two folds are considered equal if they have the same starting offset.getChild
(int index) Returns a specific child fold.int
Returns the number of child folds.Returns the array of child folds.int
Returns the number of collapsed lines under this fold.(package private) Fold
getDeepestFoldContaining
(int offs) Returns the "deepest" fold containing the specified offset.(package private) Fold
getDeepestOpenFoldContaining
(int offs) Returns the "deepest" open fold containing the specified offset.int
Returns the end line of this fold.int
Returns the end offset of this fold.int
Returns the type of fold this is.boolean
Returns whether this fold has any child folds.Returns the last child fold.int
Returns the number of lines that are hidden when this fold is collapsed.Returns the parent fold of this one.int
Returns the starting line of this fold region.int
Returns the starting offset of this fold region.int
hashCode()
boolean
Returns whether this fold is collapsed.boolean
Returns whether this fold is entirely on a single line.boolean
Removes this fold from its parent.private void
void
setCollapsed
(boolean collapsed) Sets whether thisFold
is collapsed.void
setEndOffset
(int endOffs) Sets the ending offset of this fold, such as the closing curly brace of a code block in C or Java.void
Toggles the collapsed state of this fold.toString()
Overridden for debugging purposes.private void
updateChildCollapsedLineCount
(int count)
-
Field Details
-
type
private int type -
textArea
-
startOffs
-
endOffs
-
parent
-
children
-
collapsed
private boolean collapsed -
childCollapsedLineCount
private int childCollapsedLineCount -
lastStartOffs
private int lastStartOffs -
cachedStartLine
private int cachedStartLine -
lastEndOffs
private int lastEndOffs -
cachedEndLine
private int cachedEndLine
-
-
Constructor Details
-
Fold
- Throws:
BadLocationException
-
-
Method Details
-
createChild
Creates a fold that is a child of this one.- Parameters:
type
- The type of fold.startOffs
- The starting offset of the fold.- Returns:
- The child fold.
- Throws:
BadLocationException
- IfstartOffs
is invalid.- See Also:
-
compareTo
Two folds are considered equal if they start at the same offset.- Specified by:
compareTo
in interfaceComparable<Fold>
- Parameters:
otherFold
- Another fold to compare this one to.- Returns:
- How this fold compares to the other.
-
containsLine
public boolean containsLine(int line) Returns whether the specified line would be hidden in this fold. Since RSTA displays the "first" line in a fold, this means that the line must be between(getStartLine()+1)
andgetEndLine()
, inclusive.- Parameters:
line
- The line to check.- Returns:
- Whether the line would be hidden if this fold is collapsed.
- See Also:
-
containsOrStartsOnLine
public boolean containsOrStartsOnLine(int line) Returns whether the given line is in the range[getStartLine(), getEndLine()]
, inclusive.- Parameters:
line
- The line to check.- Returns:
- Whether this fold contains, or starts on, the line.
- See Also:
-
containsOffset
public boolean containsOffset(int offs) Returns whether the specified offset is "inside" the fold. This method returnstrue
if the offset is greater than the fold start offset, and no further than the last offset of the last folded line.- Parameters:
offs
- The offset to check.- Returns:
- Whether the offset is "inside" the fold.
- See Also:
-
equals
Two folds are considered equal if they have the same starting offset. -
getChild
Returns a specific child fold.- Parameters:
index
- The index of the child fold.- Returns:
- The child fold.
- See Also:
-
getChildCount
public int getChildCount()Returns the number of child folds.- Returns:
- The number of child folds.
- See Also:
-
getChildren
Returns the array of child folds. This is a shallow copy.- Returns:
- The array of child folds, or
null
if there are none.
-
getCollapsedLineCount
public int getCollapsedLineCount()Returns the number of collapsed lines under this fold. If this fold is collapsed, this method returnsgetLineCount()
, otherwise it returns the sum of all collapsed lines of all child folds of this one.The value returned is cached, so this method returns quickly and shouldn't affect performance.
- Returns:
- The number of collapsed lines under this fold.
-
getDeepestFoldContaining
Returns the "deepest" fold containing the specified offset. It is assumed that it's already been verified thatoffs
is indeed contained in this fold.- Parameters:
offs
- The offset.- Returns:
- The fold, or
null
if no child fold also contains the offset. - See Also:
-
getDeepestOpenFoldContaining
Returns the "deepest" open fold containing the specified offset. It is assumed that it's already been verified thatoffs
is indeed contained in this fold.- Parameters:
offs
- The offset.- Returns:
- The fold, or
null
if no open fold contains the offset. - See Also:
-
getEndLine
public int getEndLine()Returns the end line of this fold. For example, in languages such as C and Java, this might be the line containing the closing curly brace of a code block.The value returned by this method will automatically update as the text area's contents are modified, to track the ending line of the code block.
- Returns:
- The end line of this code block.
- See Also:
-
getEndOffset
public int getEndOffset()Returns the end offset of this fold. For example, in languages such as C and Java, this might be the offset of the closing curly brace of a code block.The value returned by this method will automatically update as the text area's contents are modified, to track the ending offset of the code block.
- Returns:
- The end offset of this code block, or
Integer.MAX_VALUE
if this fold region isn't closed properly. The latter causes this fold to collapse all lines through the end of the file. - See Also:
-
getFoldType
public int getFoldType()Returns the type of fold this is. This will be one of the values inFoldType
, or a user-defined value.- Returns:
- The type of fold this is.
-
getHasChildFolds
public boolean getHasChildFolds()Returns whether this fold has any child folds.- Returns:
- Whether this fold has any children.
- See Also:
-
getLastChild
Returns the last child fold.- Returns:
- The last child fold, or
null
if this fold does not have any children. - See Also:
-
getLineCount
public int getLineCount()Returns the number of lines that are hidden when this fold is collapsed.- Returns:
- The number of lines hidden.
- See Also:
-
getParent
Returns the parent fold of this one.- Returns:
- The parent fold, or
null
if this is a top-level fold.
-
getStartLine
public int getStartLine()Returns the starting line of this fold region. This is the only line in the fold region that is not hidden when a fold is collapsed.The value returned by this method will automatically update as the text area's contents are modified, to track the starting line of the code block.
- Returns:
- The starting line of the code block.
- See Also:
-
getStartOffset
public int getStartOffset()Returns the starting offset of this fold region. For example, for languages such as C and Java, this would be the offset of the opening curly brace of a code block.The value returned by this method will automatically update as the text area's contents are modified, to track the starting offset of the code block.
- Returns:
- The start offset of this fold.
- See Also:
-
hashCode
public int hashCode() -
isCollapsed
public boolean isCollapsed()Returns whether this fold is collapsed.- Returns:
- Whether this fold is collapsed.
- See Also:
-
isOnSingleLine
public boolean isOnSingleLine()Returns whether this fold is entirely on a single line. In general, aFoldParser
should not remember fold regions all on a single line, since there's really nothing to fold.- Returns:
- Whether this fold is on a single line.
- See Also:
-
removeFromParent
public boolean removeFromParent()Removes this fold from its parent. This should only be called byFoldParser
implementations if they determine that a fold is all on a single line (and thus shouldn't be remembered) after creating it.- Returns:
- Whether this fold had a parent to be removed from.
- See Also:
-
removeMostRecentChild
private void removeMostRecentChild() -
setCollapsed
public void setCollapsed(boolean collapsed) Sets whether thisFold
is collapsed. Calling this method will update both the text area and allGutter
components.- Parameters:
collapsed
- Whether this fold should be collapsed.- See Also:
-
setEndOffset
Sets the ending offset of this fold, such as the closing curly brace of a code block in C or Java.FoldParser
implementations should call this on an existingFold
upon finding its end. If this method isn't called, then thisFold
is considered to have no end, i.e., it will collapse everything to the end of the file.- Parameters:
endOffs
- The end offset of this fold.- Throws:
BadLocationException
- IfendOffs
is not a valid location in the text area.
-
toggleCollapsedState
public void toggleCollapsedState()Toggles the collapsed state of this fold.- See Also:
-
updateChildCollapsedLineCount
private void updateChildCollapsedLineCount(int count) -
toString
Overridden for debugging purposes.
-