java.lang.Object
org.fife.ui.rsyntaxtextarea.folding.Fold
All Implemented Interfaces:
Comparable<Fold>

public class Fold extends Object implements Comparable<Fold>
Information about a foldable region.

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 Details

    • type

      private int type
    • textArea

      private RSyntaxTextArea textArea
    • startOffs

      private Position startOffs
    • endOffs

      private Position endOffs
    • parent

      private Fold parent
    • children

      private List<Fold> 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

  • Method Details

    • createChild

      public Fold createChild(int type, int startOffs) throws BadLocationException
      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 - If startOffs is invalid.
      See Also:
    • compareTo

      public int compareTo(Fold otherFold)
      Two folds are considered equal if they start at the same offset.
      Specified by:
      compareTo in interface Comparable<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) and getEndLine(), 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 returns true 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

      public boolean equals(Object otherFold)
      Two folds are considered equal if they have the same starting offset.
      Overrides:
      equals in class Object
      Parameters:
      otherFold - Another fold to compare this one to.
      Returns:
      Whether the two folds are equal.
      See Also:
    • getChild

      public Fold getChild(int index)
      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

      List<Fold> 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 returns getLineCount(), 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

      Fold getDeepestFoldContaining(int offs)
      Returns the "deepest" fold containing the specified offset. It is assumed that it's already been verified that offs 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

      Fold getDeepestOpenFoldContaining(int offs)
      Returns the "deepest" open fold containing the specified offset. It is assumed that it's already been verified that offs 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 in FoldType, 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

      public Fold 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

      public Fold 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()
      Overrides:
      hashCode in class Object
    • 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, a FoldParser 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 by FoldParser 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 this Fold is collapsed. Calling this method will update both the text area and all Gutter components.
      Parameters:
      collapsed - Whether this fold should be collapsed.
      See Also:
    • setEndOffset

      public void setEndOffset(int endOffs) throws BadLocationException
      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 existing Fold upon finding its end. If this method isn't called, then this Fold 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 - If endOffs 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

      public String toString()
      Overridden for debugging purposes.
      Overrides:
      toString in class Object
      Returns:
      A string representation of this Fold.