1 | /* |
2 | SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org> |
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 | /** A QSyntaxHighlighter implementation for use with QTextDocument. |
21 | * This supports partial re-highlighting during editing and |
22 | * tracks syntax-based code folding regions. |
23 | * |
24 | * @since 5.28 |
25 | */ |
26 | class KSYNTAXHIGHLIGHTING_EXPORT SyntaxHighlighter : public QSyntaxHighlighter, public AbstractHighlighter |
27 | { |
28 | Q_OBJECT |
29 | public: |
30 | explicit SyntaxHighlighter(QObject *parent = nullptr); |
31 | explicit SyntaxHighlighter(QTextDocument *document); |
32 | ~SyntaxHighlighter() override; |
33 | |
34 | void setDefinition(const Definition &def) override; |
35 | void setTheme(const Theme &theme) override; |
36 | |
37 | /** Returns whether there is a folding region beginning at @p startBlock. |
38 | * This only considers syntax-based folding regions, |
39 | * not indention-based ones as e.g. found in Python. |
40 | * |
41 | * @see findFoldingRegionEnd |
42 | */ |
43 | bool startsFoldingRegion(const QTextBlock &startBlock) const; |
44 | |
45 | /** Finds the end of the folding region starting at @p startBlock. |
46 | * If multiple folding regions begin at @p startBlock, the end of |
47 | * the last/innermost one is returned. |
48 | * This returns an invalid block if no folding region end is found, |
49 | * which typically indicates an unterminated region and thus folding |
50 | * until the document end. |
51 | * This method performs a sequential search starting at @p startBlock |
52 | * for the matching folding region end, which is a potentially expensive |
53 | * operation. |
54 | * |
55 | * @see startsFoldingRegion |
56 | */ |
57 | QTextBlock findFoldingRegionEnd(const QTextBlock &startBlock) const; |
58 | |
59 | protected: |
60 | void highlightBlock(const QString &text) override; |
61 | void applyFormat(int offset, int length, const Format &format) override; |
62 | void applyFolding(int offset, int length, FoldingRegion region) override; |
63 | |
64 | private: |
65 | Q_DECLARE_PRIVATE_D(AbstractHighlighter::d_ptr, SyntaxHighlighter) |
66 | }; |
67 | } |
68 | |
69 | #endif // KSYNTAXHIGHLIGHTING_QSYNTAXHIGHLIGHTER_H |
70 | |