Class CurlyFoldParser

  • All Implemented Interfaces:
    FoldParser
    Direct Known Subclasses:
    LispFoldParser

    public class CurlyFoldParser
    extends Object
    implements FoldParser
    A basic fold parser that can be used for languages such as C, that use curly braces to denote code blocks. This parser searches for curly brace pairs and creates code folds out of them. It can also optionally find C-style multi-line comments ("/* ... */") and make them foldable as well.

    This parser knows nothing about language semantics; it uses RSyntaxTextArea's syntax highlighting tokens to identify curly braces. By default, it looks for single-char tokens of type TokenTypes.SEPARATOR, with lexemes '{' or '}'. If your TokenMaker uses a different token type for curly braces, you should override the isLeftCurly(Token) and isRightCurly(Token) methods with your own definitions. In theory, you could extend this fold parser to parse languages that use completely different tokens than curly braces to denote foldable regions by overriding those two methods.

    Note also that this class may impose somewhat of a performance penalty on large source files, since it re-parses the entire document each time folds are reevaluated.

    • Field Detail

      • C_MLC_END

        protected static final char[] C_MLC_END
        Ending of a multi-line comment in C, C++, Java, etc.
    • Constructor Detail

      • CurlyFoldParser

        public CurlyFoldParser()
        Creates a fold parser that identifies foldable regions via curly braces as well as C-style multi-line comments.
      • CurlyFoldParser

        public CurlyFoldParser​(boolean cStyleMultiLineComments,
                               boolean java)
        Constructor.
        Parameters:
        cStyleMultiLineComments - Whether to scan for C-style multi-line comments and make them foldable.
        java - Whether this parser is folding Java. This adds extra parsing rules, such as grouping all import statements into a fold section.
    • Method Detail

      • getFoldableMultiLineComments

        public boolean getFoldableMultiLineComments()
        Returns whether multi-line comments are foldable with this parser.
        Returns:
        Whether multi-line comments are foldable.
        See Also:
        setFoldableMultiLineComments(boolean)
      • getFolds

        public List<Fold> getFolds​(RSyntaxTextArea textArea)
        Returns a list of all folds in the text area.
        Specified by:
        getFolds in interface FoldParser
        Parameters:
        textArea - The text area whose contents should be analyzed.
        Returns:
        The list of folds. If this method returns null, it is treated as if no folds were found.
      • isLeftCurly

        public boolean isLeftCurly​(Token t)
        Returns whether the token is a left curly brace. This method exists so subclasses can provide their own curly brace definition.
        Parameters:
        t - The token.
        Returns:
        Whether it is a left curly brace.
        See Also:
        isRightCurly(Token)
      • isRightCurly

        public boolean isRightCurly​(Token t)
        Returns whether the token is a right curly brace. This method exists so subclasses can provide their own curly brace definition.
        Parameters:
        t - The token.
        Returns:
        Whether it is a right curly brace.
        See Also:
        isLeftCurly(Token)
      • setFoldableMultiLineComments

        public void setFoldableMultiLineComments​(boolean foldable)
        Sets whether multi-line comments are foldable with this parser.
        Parameters:
        foldable - Whether multi-line comments are foldable.
        See Also:
        getFoldableMultiLineComments()