File indexing completed on 2024-05-12 04:02:20
0001 /* 0002 SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org> 0003 0004 SPDX-License-Identifier: MIT 0005 */ 0006 0007 #ifndef KSYNTAXHIGHLIGHTING_QSYNTAXHIGHLIGHTER_H 0008 #define KSYNTAXHIGHLIGHTING_QSYNTAXHIGHLIGHTER_H 0009 0010 #include "ksyntaxhighlighting_export.h" 0011 0012 #include "abstracthighlighter.h" 0013 0014 #include <QSyntaxHighlighter> 0015 0016 namespace KSyntaxHighlighting 0017 { 0018 class SyntaxHighlighterPrivate; 0019 0020 /** A QSyntaxHighlighter implementation for use with QTextDocument. 0021 * This supports partial re-highlighting during editing and 0022 * tracks syntax-based code folding regions. 0023 * 0024 * @since 5.28 0025 */ 0026 class KSYNTAXHIGHLIGHTING_EXPORT SyntaxHighlighter : public QSyntaxHighlighter, public AbstractHighlighter 0027 { 0028 Q_OBJECT 0029 public: 0030 explicit SyntaxHighlighter(QObject *parent = nullptr); 0031 explicit SyntaxHighlighter(QTextDocument *document); 0032 ~SyntaxHighlighter() override; 0033 0034 void setDefinition(const Definition &def) override; 0035 void setTheme(const Theme &theme) override; 0036 0037 /** Returns whether there is a folding region beginning at @p startBlock. 0038 * This only considers syntax-based folding regions, 0039 * not indention-based ones as e.g. found in Python. 0040 * 0041 * @see findFoldingRegionEnd 0042 */ 0043 bool startsFoldingRegion(const QTextBlock &startBlock) const; 0044 0045 /** Finds the end of the folding region starting at @p startBlock. 0046 * If multiple folding regions begin at @p startBlock, the end of 0047 * the last/innermost one is returned. 0048 * This returns an invalid block if no folding region end is found, 0049 * which typically indicates an unterminated region and thus folding 0050 * until the document end. 0051 * This method performs a sequential search starting at @p startBlock 0052 * for the matching folding region end, which is a potentially expensive 0053 * operation. 0054 * 0055 * @see startsFoldingRegion 0056 */ 0057 QTextBlock findFoldingRegionEnd(const QTextBlock &startBlock) const; 0058 0059 protected: 0060 void highlightBlock(const QString &text) override; 0061 void applyFormat(int offset, int length, const Format &format) override; 0062 void applyFolding(int offset, int length, FoldingRegion region) override; 0063 0064 private: 0065 Q_DECLARE_PRIVATE_D(AbstractHighlighter::d_ptr, SyntaxHighlighter) 0066 }; 0067 } 0068 0069 #endif // KSYNTAXHIGHLIGHTING_QSYNTAXHIGHLIGHTER_H