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 | |
36 | /** Returns whether there is a folding region beginning at @p startBlock. |
37 | * This only considers syntax-based folding regions, |
38 | * not indention-based ones as e.g. found in Python. |
39 | * |
40 | * @see findFoldingRegionEnd |
41 | */ |
42 | bool startsFoldingRegion(const QTextBlock &startBlock) const; |
43 | |
44 | /** Finds the end of the folding region starting at @p startBlock. |
45 | * If multiple folding regions begin at @p startBlock, the end of |
46 | * the last/innermost one is returned. |
47 | * This returns an invalid block if no folding region end is found, |
48 | * which typically indicates an unterminated region and thus folding |
49 | * until the document end. |
50 | * This method performs a sequential search starting at @p startBlock |
51 | * for the matching folding region end, which is a potentially expensive |
52 | * operation. |
53 | * |
54 | * @see startsFoldingRegion |
55 | */ |
56 | QTextBlock findFoldingRegionEnd(const QTextBlock &startBlock) const; |
57 | |
58 | protected: |
59 | void highlightBlock(const QString &text) override; |
60 | void applyFormat(int offset, int length, const Format &format) override; |
61 | void applyFolding(int offset, int length, FoldingRegion region) override; |
62 | |
63 | private: |
64 | Q_DECLARE_PRIVATE_D(AbstractHighlighter::d_ptr, SyntaxHighlighter) |
65 | }; |
66 | } |
67 | |
68 | #endif // KSYNTAXHIGHLIGHTING_QSYNTAXHIGHLIGHTER_H |
69 | |