Class DefaultFoldManager
- java.lang.Object
-
- org.fife.ui.rsyntaxtextarea.folding.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.
FoldParserManager
.- Version:
- 1.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private class
DefaultFoldManager.Listener
Listens for events in the text editor.
-
Field Summary
Fields Modifier and Type Field Description private boolean
codeFoldingEnabled
private FoldParser
foldParser
private java.util.List<Fold>
folds
private DefaultFoldManager.Listener
l
private Parser
rstaParser
private java.beans.PropertyChangeSupport
support
private RSyntaxTextArea
textArea
-
Fields inherited from interface org.fife.ui.rsyntaxtextarea.folding.FoldManager
PROPERTY_FOLDS_UPDATED
-
-
Constructor Summary
Constructors Constructor Description DefaultFoldManager(RSyntaxTextArea textArea)
Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addPropertyChangeListener(java.beans.PropertyChangeListener l)
Adds a property change listener to this fold manager.void
clear()
Removes all folds.boolean
ensureOffsetNotInClosedFold(int offs)
Ensures that the specified offset is not hidden in a collapsed fold.Fold
getDeepestFoldContaining(int offs)
Returns the "deepest" nested fold containing the specified offset.Fold
getDeepestOpenFoldContaining(int offs)
Returns the "deepest" open fold containing the specified offset.Fold
getFold(int index)
Returns a specific top-level fold, which may have child folds.int
getFoldCount()
Returns the number of top-level folds.Fold
getFoldForLine(int line)
Returns the fold region that starts at the specified line.private Fold
getFoldForLineImpl(Fold parent, java.util.List<Fold> folds, int line)
int
getHiddenLineCount()
Returns the total number of hidden (folded) lines.int
getHiddenLineCountAbove(int line)
Returns the number of lines "hidden" by collapsed folds above the specified line.int
getHiddenLineCountAbove(int line, boolean physical)
Returns the number of lines "hidden" by collapsed folds above the specified line.private int
getHiddenLineCountAboveImpl(Fold fold, int line, boolean physical)
Returns the number of lines "hidden" by collapsed folds above the specified line.int
getLastVisibleLine()
Returns the last visible line in the text area, taking into account folds.int
getVisibleLineAbove(int line)
Returns the visible line above a specified line.int
getVisibleLineBelow(int line)
Returns the visible line below a specified line.boolean
isCodeFoldingEnabled()
Returns whether code folding is enabled.boolean
isCodeFoldingSupportedAndEnabled()
Returnstrue
if and only if code folding is enabled for this text area, AND folding is supported for the language it is editing.boolean
isFoldStartLine(int line)
Returns whether the specified line contains the start of a fold region.boolean
isLineHidden(int line)
Returns whether a line is hidden in a collapsed fold.private boolean
isLineHiddenImpl(Fold parent, int line)
private void
keepFoldState(Fold newFold, java.util.List<Fold> oldFolds)
private void
keepFoldStates(java.util.List<Fold> newFolds, java.util.List<Fold> oldFolds)
void
removePropertyChangeListener(java.beans.PropertyChangeListener l)
Removes a property change listener from this fold manager.void
reparse()
Forces an immediate reparsing for folds, if folding is enabled.void
setCodeFoldingEnabled(boolean enabled)
Sets whether code folding is enabled.void
setFolds(java.util.List<Fold> folds)
Sets the folds for this fold manager.private void
updateFoldParser()
Updates the fold parser to be the one appropriate for the language currently being highlighted.
-
-
-
Field Detail
-
textArea
private RSyntaxTextArea textArea
-
rstaParser
private Parser rstaParser
-
foldParser
private FoldParser foldParser
-
folds
private java.util.List<Fold> folds
-
codeFoldingEnabled
private boolean codeFoldingEnabled
-
support
private java.beans.PropertyChangeSupport support
-
l
private DefaultFoldManager.Listener l
-
-
Constructor Detail
-
DefaultFoldManager
public DefaultFoldManager(RSyntaxTextArea textArea)
Constructor.- Parameters:
textArea
- The text area whose folds we are managing.
-
-
Method Detail
-
addPropertyChangeListener
public void addPropertyChangeListener(java.beans.PropertyChangeListener l)
Description copied from interface:FoldManager
Adds a property change listener to this fold manager.- Specified by:
addPropertyChangeListener
in interfaceFoldManager
- Parameters:
l
- The new listener.- See Also:
FoldManager.removePropertyChangeListener(PropertyChangeListener)
-
clear
public void clear()
Description copied from interface:FoldManager
Removes all folds.- Specified by:
clear
in interfaceFoldManager
-
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 interfaceFoldManager
- 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 interfaceFoldManager
- 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 interfaceFoldManager
- 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 interfaceFoldManager
- Parameters:
index
- The index of the fold.- Returns:
- The fold.
- See Also:
FoldManager.getFoldCount()
-
getFoldCount
public int getFoldCount()
Description copied from interface:FoldManager
Returns the number of top-level folds.- Specified by:
getFoldCount
in interfaceFoldManager
- Returns:
- The number of top-level folds.
- See Also:
FoldManager.getFold(int)
-
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 interfaceFoldManager
- 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)
-
getHiddenLineCount
public int getHiddenLineCount()
Description copied from interface:FoldManager
Returns the total number of hidden (folded) lines.- Specified by:
getHiddenLineCount
in interfaceFoldManager
- Returns:
- The total number of hidden (folded) lines.
- See Also:
FoldManager.getHiddenLineCountAbove(int)
-
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 interfaceFoldManager
- 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), usegetHiddenLineCountAbove(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 interfaceFoldManager
- Parameters:
line
- The line.physical
- Whetherline
is the number of a physical line (i.e. visible, not code-folded), or a logical one (i.e. any line from the model). Ifline
was determined by a raw line calculation (i.e.(visibleTopY / lineHeight)
), this value should betrue
. It should befalse
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
- Whetherline
is the number of a physical line (i.e. visible, not code-folded), or a logical one (i.e. any line from the model). Ifline
was determined by a raw line calculation (i.e.(visibleTopY / lineHeight)
), this value should betrue
. It should befalse
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
, orfold
itself, aboveline
.
-
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 interfaceFoldManager
- 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 interfaceFoldManager
- Parameters:
line
- The line to check.- Returns:
- The visible line above the specified line.
- See Also:
FoldManager.getVisibleLineBelow(int)
-
getVisibleLineBelow
public int getVisibleLineBelow(int line)
Description copied from interface:FoldManager
Returns the visible line below a specified line.- Specified by:
getVisibleLineBelow
in interfaceFoldManager
- Parameters:
line
- The line to check.- Returns:
- The visible line below the specified line.
- See Also:
FoldManager.getVisibleLineAbove(int)
-
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 interfaceFoldManager
- Returns:
- Whether code folding is enabled.
- See Also:
FoldManager.setCodeFoldingEnabled(boolean)
-
isCodeFoldingSupportedAndEnabled
public boolean isCodeFoldingSupportedAndEnabled()
Description copied from interface:FoldManager
Returnstrue
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 theFoldParserManager
.- Specified by:
isCodeFoldingSupportedAndEnabled
in interfaceFoldManager
- 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 interfaceFoldManager
- 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 interfaceFoldManager
- Parameters:
line
- The line to check.- Returns:
- Whether the line is hidden in a collapsed fold.
-
isLineHiddenImpl
private boolean isLineHiddenImpl(Fold parent, int line)
-
keepFoldStates
private void keepFoldStates(java.util.List<Fold> newFolds, java.util.List<Fold> oldFolds)
-
removePropertyChangeListener
public void removePropertyChangeListener(java.beans.PropertyChangeListener l)
Description copied from interface:FoldManager
Removes a property change listener from this fold manager.- Specified by:
removePropertyChangeListener
in interfaceFoldManager
- Parameters:
l
- The listener to remove.- See Also:
FoldManager.addPropertyChangeListener(PropertyChangeListener)
-
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 interfaceFoldManager
-
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 interfaceFoldManager
- 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 interfaceFoldManager
- Parameters:
folds
- The new folds. This should not benull
.
-
updateFoldParser
private void updateFoldParser()
Updates the fold parser to be the one appropriate for the language currently being highlighted.
-
-