File indexing completed on 2024-05-12 04:02:18

0001 /*
0002     SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: MIT
0005 */
0006 
0007 #ifndef KSYNTAXHIGHLIGHTING_FOLDINGREGION_H
0008 #define KSYNTAXHIGHLIGHTING_FOLDINGREGION_H
0009 
0010 #include "ksyntaxhighlighting_export.h"
0011 
0012 #include <QTypeInfo>
0013 
0014 namespace KSyntaxHighlighting
0015 {
0016 /** Represents a begin or end of a folding region.
0017  *  @since 5.28 */
0018 class KSYNTAXHIGHLIGHTING_EXPORT FoldingRegion
0019 {
0020 public:
0021     /**
0022      * Defines whether a FoldingRegion starts or ends a folding region.
0023      */
0024     enum Type {
0025         //! Used internally as indicator for an invalid FoldingRegion.
0026         None,
0027         //! Indicates the start of a FoldingRegion.
0028         Begin,
0029         //! Indicates the end of a FoldingRegion.
0030         End
0031     };
0032 
0033     /**
0034      * Constructs an invalid folding region, meaning that isValid() returns @e false.
0035      * To obtain valid instances, see AbstractHighlighter::applyFolding().
0036      */
0037     FoldingRegion();
0038 
0039     /** Compares two FoldingRegion instances for equality. */
0040     bool operator==(const FoldingRegion &other) const;
0041 
0042     /**
0043      * Returns @c true if this is a valid folding region.
0044      * A valid FoldingRegion is defined by a type() other than Type::None.
0045      *
0046      * @note The FoldingRegion%s passed in AbstractHighlighter::applyFolding()
0047      *       are always valid.
0048      */
0049     bool isValid() const;
0050 
0051     /**
0052      * Returns a unique identifier for this folding region.
0053      *
0054      * As example, the C/C++ highlighter starts and ends a folding region for
0055      * scopes, e.g.:
0056      * \code
0057      * void foo() {     // '{' starts a folding region
0058      *     if (bar()) { // '{' starts a (nested) folding region
0059      *     }            // '}' ends the (nested) folding region
0060      * }                // '}' ends the outer folding region
0061      * \endcode
0062      * In this example, all braces '{' and '}' have the same id(), meaning that
0063      * if you want to find the matching closing region for the first opening
0064      * brace, you need to do kind of a reference counting to find the correct
0065      * closing brace.
0066      */
0067     int id() const;
0068 
0069     /**
0070      * Returns whether this is the begin or end of a region.
0071      *
0072      * @note The FoldingRegion%s passed in AbstractHighlighter::applyFolding()
0073      *       are always valid, i.e. either Type::Begin or Type::End.
0074      */
0075     Type type() const;
0076 
0077     /**
0078      * Returns the matching start or end region.
0079      *
0080      * @note Will return invalid region for an invalid region.
0081      *
0082      * @since 6.0
0083      */
0084     FoldingRegion sibling() const;
0085 
0086 private:
0087     friend class Rule;
0088     KSYNTAXHIGHLIGHTING_NO_EXPORT FoldingRegion(Type type, int id);
0089 
0090     // 0 is invalid, positive begin, negative end
0091     int m_idWithType = 0;
0092 };
0093 
0094 }
0095 
0096 QT_BEGIN_NAMESPACE
0097 Q_DECLARE_TYPEINFO(KSyntaxHighlighting::FoldingRegion, Q_PRIMITIVE_TYPE);
0098 QT_END_NAMESPACE
0099 
0100 #endif