Class DefaultFoldManager

  • All Implemented Interfaces:
    FoldManager

    public class DefaultFoldManager
    extends java.lang.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
    • Constructor Detail

      • DefaultFoldManager

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

      • 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:
        FoldManager.getDeepestFoldContaining(int)
      • 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:
        FoldManager.getFoldCount()
      • 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:
        FoldManager.isFoldStartLine(int)
      • getFoldForLineImpl

        private Fold getFoldForLineImpl​(Fold parent,
                                        java.util.List<Fold> folds,
                                        int line)
      • 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:
        FoldManager.getHiddenLineCountAbove(int, boolean)
      • 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.
      • 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:
        FoldParserManager
      • 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:
        FoldManager.getFoldForLine(int)
      • 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,
                                   java.util.List<Fold> oldFolds)
      • keepFoldStates

        private void keepFoldStates​(java.util.List<Fold> newFolds,
                                    java.util.List<Fold> oldFolds)
      • 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:
        FoldManager.isCodeFoldingEnabled()
      • setFolds

        public void setFolds​(java.util.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.