| 1 | /* |
| 2 | SPDX-FileCopyrightText: 2016 Volker Krause <[email protected]> |
| 3 | |
| 4 | SPDX-License-Identifier: MIT |
| 5 | */ |
| 6 | |
| 7 | #ifndef KSYNTAXHIGHLIGHTING_QSYNTAXHIGHLIGHTER_H |
| 8 | #define KSYNTAXHIGHLIGHTING_QSYNTAXHIGHLIGHTER_H |
| 9 | |
| 10 | #include "ksyntaxhighlighting_export.h" |
| 11 | |
| 12 | #include "abstracthighlighter.h" |
| 13 | |
| 14 | #include <QSyntaxHighlighter> |
| 15 | |
| 16 | namespace KSyntaxHighlighting |
| 17 | { |
| 18 | class SyntaxHighlighterPrivate; |
| 19 | |
| 20 | /*! |
| 21 | * \class KSyntaxHighlighting::SyntaxHighlighter |
| 22 | * \inheaderfile KSyntaxHighlighting/SyntaxHighlighter |
| 23 | * \inmodule KSyntaxHighlighting |
| 24 | * \brief A QSyntaxHighlighter implementation for use with QTextDocument. |
| 25 | * This supports partial re-highlighting during editing and |
| 26 | * tracks syntax-based code folding regions. |
| 27 | * |
| 28 | * \since 5.28 |
| 29 | */ |
| 30 | class KSYNTAXHIGHLIGHTING_EXPORT SyntaxHighlighter : public QSyntaxHighlighter, public AbstractHighlighter |
| 31 | { |
| 32 | Q_OBJECT |
| 33 | public: |
| 34 | /*! |
| 35 | */ |
| 36 | explicit SyntaxHighlighter(QObject *parent = nullptr); |
| 37 | /*! |
| 38 | */ |
| 39 | explicit SyntaxHighlighter(QTextDocument *document); |
| 40 | ~SyntaxHighlighter() override; |
| 41 | |
| 42 | void setDefinition(const Definition &def) override; |
| 43 | void setTheme(const Theme &theme) override; |
| 44 | |
| 45 | /*! |
| 46 | * Returns whether there is a folding region beginning at \a startBlock. |
| 47 | * This only considers syntax-based folding regions, |
| 48 | * not indention-based ones as e.g. found in Python. |
| 49 | * |
| 50 | * \sa findFoldingRegionEnd |
| 51 | */ |
| 52 | bool startsFoldingRegion(const QTextBlock &startBlock) const; |
| 53 | |
| 54 | /*! Finds the end of the folding region starting at \a startBlock. |
| 55 | * If multiple folding regions begin at \a startBlock, the end of |
| 56 | * the last/innermost one is returned. |
| 57 | * This returns an invalid block if no folding region end is found, |
| 58 | * which typically indicates an unterminated region and thus folding |
| 59 | * until the document end. |
| 60 | * This method performs a sequential search starting at \a startBlock |
| 61 | * for the matching folding region end, which is a potentially expensive |
| 62 | * operation. |
| 63 | * |
| 64 | * \sa startsFoldingRegion |
| 65 | */ |
| 66 | QTextBlock findFoldingRegionEnd(const QTextBlock &startBlock) const; |
| 67 | |
| 68 | protected: |
| 69 | void highlightBlock(const QString &text) override; |
| 70 | void applyFormat(int offset, int length, const Format &format) override; |
| 71 | void applyFolding(int offset, int length, FoldingRegion region) override; |
| 72 | |
| 73 | private: |
| 74 | Q_DECLARE_PRIVATE_D(AbstractHighlighter::d_ptr, SyntaxHighlighter) |
| 75 | }; |
| 76 | } |
| 77 | |
| 78 | #endif // KSYNTAXHIGHLIGHTING_QSYNTAXHIGHLIGHTER_H |
| 79 | |