2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
|
2010-07-09 14:47:18 +02:00
|
|
|
|
2016-03-18 07:55:01 +01:00
|
|
|
#pragma once
|
2010-07-09 14:47:18 +02:00
|
|
|
|
|
|
|
|
#include "texteditor_global.h"
|
2017-05-05 20:34:29 +02:00
|
|
|
|
2013-08-13 12:57:31 +02:00
|
|
|
#include <texteditor/texteditorconstants.h>
|
2017-05-05 20:34:29 +02:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QObject>
|
|
|
|
|
#include <QTextLayout>
|
2010-07-09 14:47:18 +02:00
|
|
|
|
2017-05-05 20:34:29 +02:00
|
|
|
#include <functional>
|
2018-11-25 18:52:41 +01:00
|
|
|
#include <climits>
|
2017-05-05 20:34:29 +02:00
|
|
|
|
2010-07-09 14:47:18 +02:00
|
|
|
QT_BEGIN_NAMESPACE
|
|
|
|
|
class QTextDocument;
|
|
|
|
|
class QSyntaxHighlighterPrivate;
|
|
|
|
|
class QTextCharFormat;
|
|
|
|
|
class QFont;
|
|
|
|
|
class QColor;
|
|
|
|
|
class QTextBlockUserData;
|
|
|
|
|
class QTextEdit;
|
|
|
|
|
QT_END_NAMESPACE
|
|
|
|
|
|
|
|
|
|
namespace TextEditor {
|
|
|
|
|
|
2013-08-13 12:57:31 +02:00
|
|
|
class FontSettings;
|
2010-07-09 14:47:18 +02:00
|
|
|
class SyntaxHighlighterPrivate;
|
|
|
|
|
|
|
|
|
|
class TEXTEDITOR_EXPORT SyntaxHighlighter : public QObject
|
|
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
Q_DECLARE_PRIVATE(SyntaxHighlighter)
|
|
|
|
|
public:
|
2018-05-07 17:31:08 +02:00
|
|
|
SyntaxHighlighter(QObject *parent = nullptr);
|
2010-07-09 14:47:18 +02:00
|
|
|
SyntaxHighlighter(QTextDocument *parent);
|
|
|
|
|
SyntaxHighlighter(QTextEdit *parent);
|
2018-05-07 15:02:41 +02:00
|
|
|
~SyntaxHighlighter() override;
|
2010-07-09 14:47:18 +02:00
|
|
|
|
|
|
|
|
void setDocument(QTextDocument *doc);
|
|
|
|
|
QTextDocument *document() const;
|
|
|
|
|
|
2019-06-12 14:12:11 +02:00
|
|
|
void setExtraFormats(const QTextBlock &block, QVector<QTextLayout::FormatRange> &&formats);
|
2019-06-12 13:15:48 +02:00
|
|
|
void clearExtraFormats(const QTextBlock &block);
|
2019-06-13 09:56:39 +02:00
|
|
|
void clearAllExtraFormats();
|
2010-07-13 14:37:31 +02:00
|
|
|
|
2012-11-15 16:57:11 +01:00
|
|
|
static QList<QColor> generateColors(int n, const QColor &background);
|
|
|
|
|
|
2013-08-13 12:57:31 +02:00
|
|
|
// Don't call in constructors of derived classes
|
|
|
|
|
virtual void setFontSettings(const TextEditor::FontSettings &fontSettings);
|
2018-09-18 12:46:48 +02:00
|
|
|
TextEditor::FontSettings fontSettings() const;
|
2017-03-19 19:57:04 +02:00
|
|
|
|
2017-07-18 17:37:08 +02:00
|
|
|
void setNoAutomaticHighlighting(bool noAutomatic);
|
|
|
|
|
|
2017-03-19 19:57:04 +02:00
|
|
|
public slots:
|
2010-07-09 14:47:18 +02:00
|
|
|
void rehighlight();
|
|
|
|
|
void rehighlightBlock(const QTextBlock &block);
|
|
|
|
|
|
|
|
|
|
protected:
|
2017-05-05 20:34:29 +02:00
|
|
|
void setDefaultTextFormatCategories();
|
|
|
|
|
void setTextFormatCategories(int count, std::function<TextStyle(int)> formatMapping);
|
2013-08-13 12:57:31 +02:00
|
|
|
QTextCharFormat formatForCategory(int categoryIndex) const;
|
2017-07-06 09:24:07 +02:00
|
|
|
|
|
|
|
|
// implement in subclasses
|
|
|
|
|
// default implementation highlights whitespace
|
|
|
|
|
virtual void highlightBlock(const QString &text);
|
2010-07-09 14:47:18 +02:00
|
|
|
|
|
|
|
|
void setFormat(int start, int count, const QTextCharFormat &format);
|
|
|
|
|
void setFormat(int start, int count, const QColor &color);
|
|
|
|
|
void setFormat(int start, int count, const QFont &font);
|
|
|
|
|
QTextCharFormat format(int pos) const;
|
|
|
|
|
|
2017-05-16 12:59:36 +02:00
|
|
|
void formatSpaces(const QString &text, int start = 0, int count = INT_MAX);
|
2017-05-07 17:04:37 +02:00
|
|
|
void setFormatWithSpaces(const QString &text, int start, int count,
|
|
|
|
|
const QTextCharFormat &format);
|
2010-09-30 09:12:59 +02:00
|
|
|
|
2010-07-09 14:47:18 +02:00
|
|
|
int previousBlockState() const;
|
|
|
|
|
int currentBlockState() const;
|
|
|
|
|
void setCurrentBlockState(int newState);
|
|
|
|
|
|
|
|
|
|
void setCurrentBlockUserData(QTextBlockUserData *data);
|
|
|
|
|
QTextBlockUserData *currentBlockUserData() const;
|
|
|
|
|
|
|
|
|
|
QTextBlock currentBlock() const;
|
|
|
|
|
|
|
|
|
|
private:
|
2017-05-05 20:34:29 +02:00
|
|
|
void setTextFormatCategories(const QVector<std::pair<int, TextStyle>> &categories);
|
2017-03-19 19:57:04 +02:00
|
|
|
void reformatBlocks(int from, int charsRemoved, int charsAdded);
|
|
|
|
|
void delayedRehighlight();
|
2010-07-09 14:47:18 +02:00
|
|
|
|
|
|
|
|
QScopedPointer<SyntaxHighlighterPrivate> d_ptr;
|
|
|
|
|
};
|
|
|
|
|
|
2011-02-04 09:52:39 +01:00
|
|
|
} // namespace TextEditor
|