2022-01-28 15:56:11 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2023-01-28 10:16:34 +01:00
|
|
|
* Copyright (C) 2007-2023 Cppcheck team.
|
2022-01-28 15:56:11 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2018-02-17 22:24:41 +01:00
|
|
|
#ifndef CODEEDITOR_H
|
|
|
|
#define CODEEDITOR_H
|
|
|
|
|
2023-04-08 16:08:47 +02:00
|
|
|
#include <QObject>
|
2018-02-17 22:24:41 +01:00
|
|
|
#include <QPlainTextEdit>
|
|
|
|
#include <QRegularExpression>
|
2023-04-08 16:08:47 +02:00
|
|
|
#include <QSize>
|
|
|
|
#include <QString>
|
|
|
|
#include <QStringList>
|
2022-02-02 16:17:28 +01:00
|
|
|
#include <QSyntaxHighlighter>
|
2023-04-08 16:08:47 +02:00
|
|
|
#include <QTextCharFormat>
|
|
|
|
#include <QVector>
|
|
|
|
#include <QWidget>
|
2018-02-17 22:24:41 +01:00
|
|
|
|
2020-04-13 13:44:48 +02:00
|
|
|
class CodeEditorStyle;
|
2018-02-17 22:24:41 +01:00
|
|
|
class QPaintEvent;
|
2022-04-13 12:24:00 +02:00
|
|
|
class QRect;
|
2018-02-17 22:24:41 +01:00
|
|
|
class QResizeEvent;
|
2022-04-13 12:24:00 +02:00
|
|
|
class QTextDocument;
|
2018-02-17 22:24:41 +01:00
|
|
|
|
|
|
|
class Highlighter : public QSyntaxHighlighter {
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
2019-06-08 07:24:38 +02:00
|
|
|
explicit Highlighter(QTextDocument *parent,
|
|
|
|
CodeEditorStyle *widgetStyle);
|
2018-02-17 22:24:41 +01:00
|
|
|
|
2018-02-18 12:06:54 +01:00
|
|
|
void setSymbols(const QStringList &symbols);
|
|
|
|
|
2019-06-25 15:29:15 +02:00
|
|
|
void setStyle(const CodeEditorStyle &newStyle);
|
2019-06-23 19:04:53 +02:00
|
|
|
|
2018-02-17 22:24:41 +01:00
|
|
|
protected:
|
|
|
|
void highlightBlock(const QString &text) override;
|
|
|
|
|
|
|
|
private:
|
2019-06-23 19:04:53 +02:00
|
|
|
enum RuleRole {
|
|
|
|
Keyword = 1,
|
|
|
|
Class = 2,
|
|
|
|
Comment = 3,
|
|
|
|
Quote = 4,
|
|
|
|
Symbol = 5
|
|
|
|
};
|
2018-02-17 22:24:41 +01:00
|
|
|
struct HighlightingRule {
|
|
|
|
QRegularExpression pattern;
|
|
|
|
QTextCharFormat format;
|
2019-06-23 19:04:53 +02:00
|
|
|
RuleRole ruleRole;
|
2018-02-17 22:24:41 +01:00
|
|
|
};
|
2019-06-23 19:04:53 +02:00
|
|
|
|
2019-06-25 15:29:15 +02:00
|
|
|
void applyFormat(HighlightingRule &rule);
|
2019-06-23 19:04:53 +02:00
|
|
|
|
2018-06-18 10:10:11 +02:00
|
|
|
QVector<HighlightingRule> mHighlightingRules;
|
|
|
|
QVector<HighlightingRule> mHighlightingRulesWithSymbols;
|
|
|
|
|
|
|
|
QRegularExpression mCommentStartExpression;
|
|
|
|
QRegularExpression mCommentEndExpression;
|
|
|
|
|
|
|
|
QTextCharFormat mKeywordFormat;
|
|
|
|
QTextCharFormat mClassFormat;
|
|
|
|
QTextCharFormat mSingleLineCommentFormat;
|
|
|
|
QTextCharFormat mMultiLineCommentFormat;
|
|
|
|
QTextCharFormat mQuotationFormat;
|
|
|
|
QTextCharFormat mSymbolFormat;
|
2019-06-08 07:23:48 +02:00
|
|
|
|
|
|
|
CodeEditorStyle *mWidgetStyle;
|
2018-02-17 22:24:41 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class CodeEditor : public QPlainTextEdit {
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
2019-06-23 19:04:53 +02:00
|
|
|
explicit CodeEditor(QWidget *parent);
|
2018-05-01 09:35:53 +02:00
|
|
|
CodeEditor(const CodeEditor &) = delete;
|
|
|
|
CodeEditor &operator=(const CodeEditor &) = delete;
|
2022-03-13 20:07:58 +01:00
|
|
|
~CodeEditor() override;
|
2018-02-17 22:24:41 +01:00
|
|
|
|
2023-11-20 07:23:45 +01:00
|
|
|
void lineNumberAreaPaintEvent(const QPaintEvent *event);
|
2018-02-17 22:24:41 +01:00
|
|
|
int lineNumberAreaWidth();
|
2019-06-23 19:04:53 +02:00
|
|
|
void setStyle(const CodeEditorStyle& newStyle);
|
2018-02-17 22:24:41 +01:00
|
|
|
|
2018-09-01 06:39:22 +02:00
|
|
|
/**
|
|
|
|
* Set source code to show, goto error line and highlight that line.
|
|
|
|
* \param code The source code.
|
|
|
|
* \param errorLine line number
|
|
|
|
* \param symbols the related symbols, these are marked
|
|
|
|
*/
|
2018-02-18 12:06:54 +01:00
|
|
|
void setError(const QString &code, int errorLine, const QStringList &symbols);
|
2018-02-17 22:24:41 +01:00
|
|
|
|
2020-07-18 18:14:55 +02:00
|
|
|
/**
|
|
|
|
* Goto another error in existing source file
|
|
|
|
* \param errorLine line number
|
|
|
|
* \param symbols the related symbols, these are marked
|
|
|
|
*/
|
|
|
|
void setError(int errorLine, const QStringList &symbols);
|
|
|
|
|
2020-07-19 11:10:53 +02:00
|
|
|
void setFileName(const QString &fileName) {
|
2020-07-18 18:14:55 +02:00
|
|
|
mFileName = fileName;
|
|
|
|
}
|
|
|
|
|
2020-07-19 11:10:53 +02:00
|
|
|
QString getFileName() const {
|
2020-07-18 18:14:55 +02:00
|
|
|
return mFileName;
|
|
|
|
}
|
|
|
|
|
2020-07-19 11:10:53 +02:00
|
|
|
void clear() {
|
2020-07-18 18:14:55 +02:00
|
|
|
mFileName.clear();
|
|
|
|
setPlainText(QString());
|
|
|
|
}
|
|
|
|
|
2018-02-17 22:24:41 +01:00
|
|
|
protected:
|
|
|
|
void resizeEvent(QResizeEvent *event) override;
|
|
|
|
|
|
|
|
private slots:
|
|
|
|
void updateLineNumberAreaWidth(int newBlockCount);
|
2018-02-21 22:49:50 +01:00
|
|
|
void highlightErrorLine();
|
2022-09-27 20:03:25 +02:00
|
|
|
void updateLineNumberArea(const QRect & /*rect*/, int /*dy*/);
|
2018-02-17 22:24:41 +01:00
|
|
|
|
2019-06-23 19:04:53 +02:00
|
|
|
private:
|
|
|
|
QString generateStyleString();
|
|
|
|
|
2018-02-17 22:24:41 +01:00
|
|
|
private:
|
2018-06-18 10:10:11 +02:00
|
|
|
QWidget *mLineNumberArea;
|
|
|
|
Highlighter *mHighlighter;
|
2019-06-08 07:23:48 +02:00
|
|
|
CodeEditorStyle *mWidgetStyle;
|
2018-02-17 22:24:41 +01:00
|
|
|
int mErrorPosition;
|
2020-07-18 18:14:55 +02:00
|
|
|
QString mFileName;
|
2018-02-17 22:24:41 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class LineNumberArea : public QWidget {
|
|
|
|
public:
|
|
|
|
explicit LineNumberArea(CodeEditor *editor) : QWidget(editor) {
|
2018-06-18 10:10:11 +02:00
|
|
|
mCodeEditor = editor;
|
2018-02-17 22:24:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QSize sizeHint() const override {
|
2018-06-18 10:10:11 +02:00
|
|
|
return QSize(mCodeEditor->lineNumberAreaWidth(), 0);
|
2018-02-17 22:24:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void paintEvent(QPaintEvent *event) override {
|
2018-06-18 10:10:11 +02:00
|
|
|
mCodeEditor->lineNumberAreaPaintEvent(event);
|
2018-02-17 22:24:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-06-18 10:10:11 +02:00
|
|
|
CodeEditor *mCodeEditor;
|
2018-02-17 22:24:41 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // CODEEDITOR_H
|