Class DefaultFoldManager

java.lang.Object
org.fife.ui.rsyntaxtextarea.folding.DefaultFoldManager
All Implemented Interfaces:
FoldManager

public class DefaultFoldManager extends Object implements FoldManager
The default implementation of a fold manager. Besides keeping track of folds, this class behaves as follows:
  • If text containing a newline is inserted in a collapsed fold, that fold, and any ancestor folds, are expanded. This ensures that modified text is always visible to the user.
  • If the text area's RSyntaxTextArea.SYNTAX_STYLE_PROPERTY changes, the current fold parser is uninstalled, and one appropriate for the new language, if any, is installed.
The folding strategy to use is retrieved from FoldParserManager.
Version:
1.0
  • Field Details

  • Constructor Details

    • DefaultFoldManager

      public DefaultFoldManager(RSyntaxTextArea textArea)
      Constructor.
      Parameters:
      textArea - The text area whose folds we are managing.
  • Method Details

    • addPropertyChangeListener

      public void addPropertyChangeListener(PropertyChangeListener l)
      Description copied from interface: FoldManager
      Adds a property change listener to this fold manager.
      Specified by:
      addPropertyChangeListener in interface FoldManager
      Parameters:
      l - The new listener.
      See Also:
    • clear

      public void clear()
      Description copied from interface: FoldManager
      Removes all folds.
      Specified by:
      clear in interface FoldManager
    • ensureOffsetNotInClosedFold

      public boolean ensureOffsetNotInClosedFold(int offs)
      Description copied from interface: FoldManager
      Ensures that the specified offset is not hidden in a collapsed fold. Any folds containing this offset that are collapsed will be expanded.
      Specified by:
      ensureOffsetNotInClosedFold in interface FoldManager
      Parameters:
      offs - The offset.
      Returns:
      Whether any folds had to be opened.
      See Also:
    • getDeepestFoldContaining

      public Fold getDeepestFoldContaining(int offs)
      Description copied from interface: FoldManager
      Returns the "deepest" nested fold containing the specified offset.
      Specified by:
      getDeepestFoldContaining in interface FoldManager
      Parameters:
      offs - The offset.
      Returns:
      The deepest fold containing the offset, or null if no fold contains the offset.
    • getDeepestOpenFoldContaining

      public Fold getDeepestOpenFoldContaining(int offs)
      Description copied from interface: FoldManager
      Returns the "deepest" open fold containing the specified offset.
      Specified by:
      getDeepestOpenFoldContaining in interface FoldManager
      Parameters:
      offs - The offset.
      Returns:
      The fold, or null if no open fold contains the offset.
    • getFold

      public Fold getFold(int index)
      Description copied from interface: FoldManager
      Returns a specific top-level fold, which may have child folds.
      Specified by:
      getFold in interface FoldManager
      Parameters:
      index - The index of the fold.
      Returns:
      The fold.
      See Also:
    • getFoldCount

      public int getFoldCount()
      Description copied from interface: FoldManager
      Returns the number of top-level folds.
      Specified by:
      getFoldCount in interface FoldManager
      Returns:
      The number of top-level folds.
      See Also:
    • getFoldForLine

      public Fold getFoldForLine(int line)
      Description copied from interface: FoldManager
      Returns the fold region that starts at the specified line.
      Specified by:
      getFoldForLine in interface FoldManager
      Parameters:
      line - The line number.
      Returns:
      The fold, or null if the line is not the start of a fold region.
      See Also:
    • getFoldForLineImpl

      private Fold getFoldForLineImpl(Fold parent, List<Fold> folds, int line)
    • getHiddenLineCount

      public int getHiddenLineCount()
      Description copied from interface: FoldManager
      Returns the total number of hidden (folded) lines.
      Specified by:
      getHiddenLineCount in interface FoldManager
      Returns:
      The total number of hidden (folded) lines.
      See Also:
    • getHiddenLineCountAbove

      public int getHiddenLineCountAbove(int line)
      Description copied from interface: FoldManager
      Returns the number of lines "hidden" by collapsed folds above the specified line.
      Specified by:
      getHiddenLineCountAbove in interface FoldManager
      Parameters:
      line - The line. This is the line number for a logical line. For the line number of a physical line (i.e. visible, not folded), use getHiddenLineCountAbove(int, true).
      Returns:
      The number of lines hidden in folds above line.
      See Also:
    • getHiddenLineCountAbove

      public int getHiddenLineCountAbove(int line, boolean physical)
      Description copied from interface: FoldManager
      Returns the number of lines "hidden" by collapsed folds above the specified line.
      Specified by:
      getHiddenLineCountAbove in interface FoldManager
      Parameters:
      line - The line.
      physical - Whether line is the number of a physical line (i.e. visible, not code-folded), or a logical one (i.e. any line from the model). If line was determined by a raw line calculation (i.e. (visibleTopY / lineHeight)), this value should be true. It should be false when it was calculated from an offset in the document (for example).
      Returns:
      The number of lines hidden in folds above line.
    • getHiddenLineCountAboveImpl

      private int getHiddenLineCountAboveImpl(Fold fold, int line, boolean physical)
      Returns the number of lines "hidden" by collapsed folds above the specified line.
      Parameters:
      fold - The current fold in the recursive algorithm. It and its children are examined.
      line - The line.
      physical - Whether line is the number of a physical line (i.e. visible, not code-folded), or a logical one (i.e. any line from the model). If line was determined by a raw line calculation (i.e. (visibleTopY / lineHeight)), this value should be true. It should be false when it was calculated from an offset in the document (for example).
      Returns:
      The number of lines hidden in folds that are descendants of fold, or fold itself, above line.
    • getLastVisibleLine

      public int getLastVisibleLine()
      Description copied from interface: FoldManager
      Returns the last visible line in the text area, taking into account folds.
      Specified by:
      getLastVisibleLine in interface FoldManager
      Returns:
      The last visible line.
    • getVisibleLineAbove

      public int getVisibleLineAbove(int line)
      Description copied from interface: FoldManager
      Returns the visible line above a specified line.
      Specified by:
      getVisibleLineAbove in interface FoldManager
      Parameters:
      line - The line to check.
      Returns:
      The visible line above the specified line.
      See Also:
    • getVisibleLineBelow

      public int getVisibleLineBelow(int line)
      Description copied from interface: FoldManager
      Returns the visible line below a specified line.
      Specified by:
      getVisibleLineBelow in interface FoldManager
      Parameters:
      line - The line to check.
      Returns:
      The visible line below the specified line.
      See Also:
    • isCodeFoldingEnabled

      public boolean isCodeFoldingEnabled()
      Description copied from interface: FoldManager
      Returns whether code folding is enabled. Note that only certain languages support code folding; those that do not will ignore this property.
      Specified by:
      isCodeFoldingEnabled in interface FoldManager
      Returns:
      Whether code folding is enabled.
      See Also:
    • isCodeFoldingSupportedAndEnabled

      public boolean isCodeFoldingSupportedAndEnabled()
      Description copied from interface: FoldManager
      Returns true if and only if code folding is enabled for this text area, AND folding is supported for the language it is editing. Whether folding is supported for a language depends on whether a fold parser is registered for that language with the FoldParserManager.
      Specified by:
      isCodeFoldingSupportedAndEnabled in interface FoldManager
      Returns:
      Whether folding is supported and enabled for this text area.
      See Also:
    • isFoldStartLine

      public boolean isFoldStartLine(int line)
      Description copied from interface: FoldManager
      Returns whether the specified line contains the start of a fold region.
      Specified by:
      isFoldStartLine in interface FoldManager
      Parameters:
      line - The line.
      Returns:
      Whether the line contains the start of a fold region.
      See Also:
    • isLineHidden

      public boolean isLineHidden(int line)
      Description copied from interface: FoldManager
      Returns whether a line is hidden in a collapsed fold.
      Specified by:
      isLineHidden in interface FoldManager
      Parameters:
      line - The line to check.
      Returns:
      Whether the line is hidden in a collapsed fold.
    • isLineHiddenImpl

      private boolean isLineHiddenImpl(Fold parent, int line)
    • keepFoldState

      private void keepFoldState(Fold newFold, List<Fold> oldFolds)
    • keepFoldStates

      private void keepFoldStates(List<Fold> newFolds, List<Fold> oldFolds)
    • removePropertyChangeListener

      public void removePropertyChangeListener(PropertyChangeListener l)
      Description copied from interface: FoldManager
      Removes a property change listener from this fold manager.
      Specified by:
      removePropertyChangeListener in interface FoldManager
      Parameters:
      l - The listener to remove.
      See Also:
    • reparse

      public void reparse()
      Description copied from interface: FoldManager
      Forces an immediate reparsing for folds, if folding is enabled. This usually does not need to be called by the programmer, since fold parsing is done automatically by RSTA.
      Specified by:
      reparse in interface FoldManager
    • setCodeFoldingEnabled

      public void setCodeFoldingEnabled(boolean enabled)
      Description copied from interface: FoldManager
      Sets whether code folding is enabled. Note that only certain languages will support code folding out of the box. Those languages which do not support folding will ignore this property.
      Specified by:
      setCodeFoldingEnabled in interface FoldManager
      Parameters:
      enabled - Whether code folding should be enabled.
      See Also:
    • setFolds

      public void setFolds(List<Fold> folds)
      Description copied from interface: FoldManager
      Sets the folds for this fold manager.
      Specified by:
      setFolds in interface FoldManager
      Parameters:
      folds - The new folds. This should not be null.
    • updateFoldParser

      private void updateFoldParser()
      Updates the fold parser to be the one appropriate for the language currently being highlighted.