add QCodeEditor

This commit is contained in:
Carl Philipp Klemm 2025-10-13 12:40:46 +02:00
parent bccee9bd36
commit 2f3069a388
316 changed files with 98016 additions and 0 deletions

View file

@ -0,0 +1,3 @@
#pragma once
#include <internal/QCXXHighlighter.hpp>

View file

@ -0,0 +1,3 @@
#pragma once
#include <internal/QCodeEditor.hpp>

View file

@ -0,0 +1,3 @@
#pragma once
#include <internal/QFramedTextAttribute.hpp>

View file

@ -0,0 +1,3 @@
#pragma once
#include <internal/QGLSLCompleter.hpp>

View file

@ -0,0 +1,3 @@
#pragma once
#include <internal/QGLSLHighlighter.hpp>

View file

@ -0,0 +1,3 @@
#pragma once
#include <internal/QHighlightBlockRule.hpp>

View file

@ -0,0 +1,3 @@
#pragma once
#include <internal/QHighlightRule.hpp>

View file

@ -0,0 +1,3 @@
#pragma once
#include <internal/QJSONHighlighter.hpp>

View file

@ -0,0 +1,3 @@
#pragma once
#include <internal/QLanguage.hpp>

View file

@ -0,0 +1,3 @@
#pragma once
#include <internal/QLineNumberArea.hpp>

View file

@ -0,0 +1,3 @@
#pragma once
#include <internal/QLuaCompleter.hpp>

View file

@ -0,0 +1,3 @@
#pragma once
#include <internal/QLuaHighlighter.hpp>

View file

@ -0,0 +1,3 @@
#pragma once
#include <internal/QPythonCompleter.hpp>

View file

@ -0,0 +1,3 @@
#pragma once
#include <internal/QPythonHighlighter.hpp>

View file

@ -0,0 +1,3 @@
#pragma once
#include <internal/QStyleSyntaxHighlighter.hpp>

View file

@ -0,0 +1,3 @@
#pragma once
#include <internal/QSyntaxStyle.hpp>

View file

@ -0,0 +1,3 @@
#pragma once
#include <internal/QXMLHighlighter.hpp>

View file

@ -0,0 +1,42 @@
#pragma once
// QCodeEditor
#include <QStyleSyntaxHighlighter> // Required for inheritance
#include <QHighlightRule>
// Qt
#include <QRegularExpression>
#include <QVector>
class QSyntaxStyle;
/**
* @brief Class, that describes C++ code
* highlighter.
*/
class QCXXHighlighter : public QStyleSyntaxHighlighter
{
Q_OBJECT
public:
/**
* @brief Constructor.
* @param document Pointer to document.
*/
explicit QCXXHighlighter(QTextDocument* document=nullptr);
protected:
void highlightBlock(const QString& text) override;
private:
QVector<QHighlightRule> m_highlightRules;
QRegularExpression m_includePattern;
QRegularExpression m_functionPattern;
QRegularExpression m_defTypePattern;
QRegularExpression m_commentStartPattern;
QRegularExpression m_commentEndPattern;
};

View file

@ -0,0 +1,273 @@
#pragma once
// Qt
#include <QTextEdit> // Required for inheritance
class QCompleter;
class QLineNumberArea;
class QSyntaxStyle;
class QStyleSyntaxHighlighter;
class QFramedTextAttribute;
/**
* @brief Class, that describes code editor.
*/
class QCodeEditor : public QTextEdit
{
Q_OBJECT
public:
/**
* @brief Constructor.
* @param widget Pointer to parent widget.
*/
explicit QCodeEditor(QWidget* widget=nullptr);
// Disable copying
QCodeEditor(const QCodeEditor&) = delete;
QCodeEditor& operator=(const QCodeEditor&) = delete;
/**
* @brief Method for getting first visible block
* index.
* @return Index.
*/
int getFirstVisibleBlock();
/**
* @brief Method for setting highlighter.
* @param highlighter Pointer to syntax highlighter.
*/
void setHighlighter(QStyleSyntaxHighlighter* highlighter);
/**
* @brief Method for setting syntax sty.e.
* @param style Pointer to syntax style.
*/
void setSyntaxStyle(QSyntaxStyle* style);
/**
* @brief Method setting auto parentheses enabled.
*/
void setAutoParentheses(bool enabled);
/**
* @brief Method for getting is auto parentheses enabled.
* Default value: true
*/
bool autoParentheses() const;
/**
* @brief Method for setting tab replacing
* enabled.
*/
void setTabReplace(bool enabled);
/**
* @brief Method for getting is tab replacing enabled.
* Default value: true
*/
bool tabReplace() const;
/**
* @brief Method for setting amount of spaces, that will
* replace tab.
* @param val Number of spaces.
*/
void setTabReplaceSize(int val);
/**
* @brief Method for getting number of spaces, that will
* replace tab if `tabReplace` is true.
* Default: 4
*/
int tabReplaceSize() const;
/**
* @brief Method for setting auto indentation enabled.
*/
void setAutoIndentation(bool enabled);
/**
* @brief Method for getting is auto indentation enabled.
* Default: true
*/
bool autoIndentation() const;
/**
* @brief Method for setting completer.
* @param completer Pointer to completer object.
*/
void setCompleter(QCompleter* completer);
/**
* @brief Method for getting completer.
* @return Pointer to completer.
*/
QCompleter* completer() const;
public Q_SLOTS:
/**
* @brief Slot, that performs insertion of
* completion info into code.
* @param s Data.
*/
void insertCompletion(QString s);
/**
* @brief Slot, that performs update of
* internal editor viewport based on line
* number area width.
*/
void updateLineNumberAreaWidth(int);
/**
* @brief Slot, that performs update of some
* part of line number area.
* @param rect Area that has to be updated.
*/
void updateLineNumberArea(const QRect& rect);
/**
* @brief Slot, that will proceed extra selection
* for current cursor position.
*/
void updateExtraSelection();
/**
* @brief Slot, that will update editor style.
*/
void updateStyle();
/**
* @brief Slot, that will be called on selection
* change.
*/
void onSelectionChanged();
protected:
/**
* @brief Method, that's called on any text insertion of
* mimedata into editor. If it's text - it inserts text
* as plain text.
*/
void insertFromMimeData(const QMimeData* source) override;
/**
* @brief Method, that's called on editor painting. This
* method if overloaded for line number area redraw.
*/
void paintEvent(QPaintEvent* e) override;
/**
* @brief Method, that's called on any widget resize.
* This method if overloaded for line number area
* resizing.
*/
void resizeEvent(QResizeEvent* e) override;
/**
* @brief Method, that's called on any key press, posted
* into code editor widget. This method is overloaded for:
*
* 1. Completion
* 2. Tab to spaces
* 3. Low indentation
* 4. Auto parenthesis
*/
void keyPressEvent(QKeyEvent* e) override;
/**
* @brief Method, that's called on focus into widget.
* It's required for setting this widget to set
* completer.
*/
void focusInEvent(QFocusEvent *e) override;
private:
/**
* @brief Method for initializing document
* layout handlers.
*/
void initDocumentLayoutHandlers();
/**
* @brief Method for initializing default
* monospace font.
*/
void initFont();
/**
* @brief Method for performing connection
* of objects.
*/
void performConnections();
/**
* @brief Method, that performs selection
* frame selection.
*/
void handleSelectionQuery(QTextCursor cursor);
/**
* @brief Method for updating geometry of line number area.
*/
void updateLineGeometry();
/**
* @brief Method, that performs completer processing.
* Returns true if event has to be dropped.
* @param e Pointer to key event.
* @return Shall event be dropped.
*/
bool proceedCompleterBegin(QKeyEvent *e);
void proceedCompleterEnd(QKeyEvent* e);
/**
* @brief Method for getting character under
* cursor.
* @param offset Offset to cursor.
*/
QChar charUnderCursor(int offset = 0) const;
/**
* @brief Method for getting word under
* cursor.
* @return Word under cursor.
*/
QString wordUnderCursor() const;
/**
* @brief Method, that adds highlighting of
* currently selected line to extra selection list.
*/
void highlightCurrentLine(QList<QTextEdit::ExtraSelection>& extraSelection);
/**
* @brief Method, that adds highlighting of
* parenthesis if available.
*/
void highlightParenthesis(QList<QTextEdit::ExtraSelection>& extraSelection);
/**
* @brief Method for getting number of indentation
* spaces in current line. Tabs will be treated
* as `tabWidth / spaceWidth`
*/
int getIndentationSpaces();
QStyleSyntaxHighlighter* m_highlighter;
QSyntaxStyle* m_syntaxStyle;
QLineNumberArea* m_lineNumberArea;
QCompleter* m_completer;
QFramedTextAttribute* m_framedAttribute;
bool m_autoIndentation;
bool m_autoParentheses;
bool m_replaceTab;
QString m_tabReplace;
};

View file

@ -0,0 +1,88 @@
#pragma once
// Qt
#include <QObject> // Required for inheritance
#include <QTextObjectInterface> // Required for inheritance
class QSyntaxStyle;
/**
* @brief Class, that describes
* attribute for making text frame.
*/
class QFramedTextAttribute : public QObject,
public QTextObjectInterface
{
Q_OBJECT
Q_INTERFACES(QTextObjectInterface)
public:
enum Property
{
FramedString = 1
};
/**
* @brief Static method for getting framed text
* attribute type.
*/
static int type();
/**
* @brief Constructor.
* @param parent Pointer to parent QObject.
*/
explicit QFramedTextAttribute(QObject* parent=nullptr);
// Disable copying
QFramedTextAttribute(const QFramedTextAttribute&) = delete;
QFramedTextAttribute& operator=(const QFramedTextAttribute&) = delete;
/**
* @brief Method for getting custom element (frame)
* size. Though frame symbol has no size, this
* method returns {0, 0}
*/
QSizeF intrinsicSize(QTextDocument *doc,
int posInDocument,
const QTextFormat &format) override;
/**
* @brief Method for drawing frame.
*/
void drawObject(QPainter *painter,
const QRectF &rect,
QTextDocument *doc,
int posInDocument,
const QTextFormat &format) override;
/**
* @brief Method for creating frame in cursor
* selection.
* @param cursor Cursor.
*/
void frame(QTextCursor cursor);
/**
* @brief Method for clearing all frames
* with desired cursor.
*/
void clear(QTextCursor cursor);
/**
* @brief Method for setting syntax style
* for rendering.
*/
void setSyntaxStyle(QSyntaxStyle* style);
/**
* @brief Method for getting syntax style.
*/
QSyntaxStyle* syntaxStyle() const;
private:
QSyntaxStyle* m_style;
};

View file

@ -0,0 +1,23 @@
#pragma once
// Qt
#include <QCompleter> // Required for inheritance
/**
* @brief Class, that describes completer with
* glsl specific types and functions.
*/
class QGLSLCompleter : public QCompleter
{
Q_OBJECT
public:
/**
* @brief Constructor.
* @param parent Pointer to parent QObject.
*/
explicit QGLSLCompleter(QObject* parent=nullptr);
};

View file

@ -0,0 +1,42 @@
#pragma once
// QCodeEditor
#include <QStyleSyntaxHighlighter> // Required for inheritance
#include <QHighlightRule>
// Qt
#include <QRegularExpression>
#include <QVector>
class QSyntaxStyle;
/**
* @brief Class, that describes Glsl code
* highlighter.
*/
class QGLSLHighlighter : public QStyleSyntaxHighlighter
{
Q_OBJECT
public:
/**
* @brief Constructor.
* @param document Pointer to document.
*/
explicit QGLSLHighlighter(QTextDocument* document=nullptr);
protected:
void highlightBlock(const QString& text) override;
private:
QVector<QHighlightRule> m_highlightRules;
QRegularExpression m_includePattern;
QRegularExpression m_functionPattern;
QRegularExpression m_defTypePattern;
QRegularExpression m_commentStartPattern;
QRegularExpression m_commentEndPattern;
};

View file

@ -0,0 +1,24 @@
#pragma once
// Qt
#include <QRegularExpression>
#include <QString>
struct QHighlightBlockRule
{
QHighlightBlockRule() :
startPattern(),
endPattern(),
formatName()
{}
QHighlightBlockRule(QRegularExpression start, QRegularExpression end, QString format) :
startPattern(std::move(start)),
endPattern(std::move(end)),
formatName(std::move(format))
{}
QRegularExpression startPattern;
QRegularExpression endPattern;
QString formatName;
};

View file

@ -0,0 +1,21 @@
#pragma once
// Qt
#include <QRegularExpression>
#include <QString>
struct QHighlightRule
{
QHighlightRule() :
pattern(),
formatName()
{}
QHighlightRule(QRegularExpression p, QString f) :
pattern(std::move(p)),
formatName(std::move(f))
{}
QRegularExpression pattern;
QString formatName;
};

View file

@ -0,0 +1,33 @@
#pragma once
// QCodeEditor
#include <QStyleSyntaxHighlighter> // Required for inheritance
#include <QHighlightRule>
// Qt
#include <QVector>
/**
* @brief Class, that describes JSON code
* highlighter.
*/
class QJSONHighlighter : public QStyleSyntaxHighlighter
{
Q_OBJECT
public:
/**
* @brief Constructor.
* @param document Pointer to document.
*/
explicit QJSONHighlighter(QTextDocument* document=nullptr);
protected:
void highlightBlock(const QString& text) override;
private:
QVector<QHighlightRule> m_highlightRules;
QRegularExpression m_keyRegex;
};

View file

@ -0,0 +1,61 @@
#pragma once
// Qt
#include <QObject> // Required for inheritance
#include <QString>
#include <QMap>
class QIODevice;
/**
* Class, that describes object for parsing
* language file.
*/
class QLanguage : public QObject
{
Q_OBJECT
public:
/**
* @brief Constructor.
* @param parent Pointer to parent QObject.
*/
explicit QLanguage(QIODevice* device=nullptr, QObject* parent=nullptr);
/**
* @brief Method for parsing.
* @param device Pointer to device.
* @return Success.
*/
bool load(QIODevice* device);
/**
* @brief Method for getting available keys.
*/
QStringList keys();
/**
* @brief Method for getting names
* from key.
* @param name
* @return
*/
QStringList names(const QString& key);
/**
* @brief Method for getting is object loaded.
*/
bool isLoaded() const;
private:
bool m_loaded;
QMap<
QString,
QStringList
> m_list;
};

View file

@ -0,0 +1,56 @@
#pragma once
// Qt
#include <QWidget> // Required for inheritance
class QCodeEditor;
class QSyntaxStyle;
/**
* @brief Class, that describes line number area widget.
*/
class QLineNumberArea : public QWidget
{
Q_OBJECT
public:
/**
* @brief Constructor.
* @param parent Pointer to parent QTextEdit widget.
*/
explicit QLineNumberArea(QCodeEditor* parent=nullptr);
// Disable copying
QLineNumberArea(const QLineNumberArea&) = delete;
QLineNumberArea& operator=(const QLineNumberArea&) = delete;
/**
* @brief Overridden method for getting line number area
* size.
*/
QSize sizeHint() const override;
/**
* @brief Method for setting syntax style object.
* @param style Pointer to syntax style.
*/
void setSyntaxStyle(QSyntaxStyle* style);
/**
* @brief Method for getting syntax style.
* @return Pointer to syntax style.
*/
QSyntaxStyle* syntaxStyle() const;
protected:
void paintEvent(QPaintEvent* event) override;
private:
QSyntaxStyle* m_syntaxStyle;
QCodeEditor* m_codeEditParent;
};

View file

@ -0,0 +1,23 @@
#pragma once
// Qt
#include <QCompleter> // Required for inheritance
/**
* @brief Class, that describes completer with
* glsl specific types and functions.
*/
class QLuaCompleter : public QCompleter
{
Q_OBJECT
public:
/**
* @brief Constructor.
* @param parent Pointer to parent QObject.
*/
explicit QLuaCompleter(QObject* parent=nullptr);
};

View file

@ -0,0 +1,40 @@
#pragma once
// QCodeEditor
#include <QStyleSyntaxHighlighter> // Required for inheritance
#include <QHighlightRule>
#include <QHighlightBlockRule>
// Qt
#include <QRegularExpression>
#include <QVector>
#include <QMap>
class QSyntaxStyle;
/**
* @brief Class, that describes C++ code
* highlighter.
*/
class QLuaHighlighter : public QStyleSyntaxHighlighter
{
Q_OBJECT
public:
/**
* @brief Constructor.
* @param document Pointer to document.
*/
explicit QLuaHighlighter(QTextDocument* document=nullptr);
protected:
void highlightBlock(const QString& text) override;
private:
QVector<QHighlightRule> m_highlightRules;
QVector<QHighlightBlockRule> m_highlightBlockRules;
QRegularExpression m_requirePattern;
QRegularExpression m_functionPattern;
QRegularExpression m_defTypePattern;
};

View file

@ -0,0 +1,23 @@
#pragma once
// Qt
#include <QCompleter> // Required for inheritance
/**
* @brief Class, that describes completer with
* glsl specific types and functions.
*/
class QPythonCompleter : public QCompleter
{
Q_OBJECT
public:
/**
* @brief Constructor.
* @param parent Pointer to parent QObject.
*/
explicit QPythonCompleter(QObject* parent=nullptr);
};

View file

@ -0,0 +1,41 @@
#pragma once
// QCodeEditor
#include <QStyleSyntaxHighlighter> // Required for inheritance
#include <QHighlightRule>
#include <QHighlightBlockRule>
// Qt
#include <QRegularExpression>
#include <QVector>
class QSyntaxStyle;
/**
* @brief Class, that describes Glsl code
* highlighter.
*/
class QPythonHighlighter : public QStyleSyntaxHighlighter
{
Q_OBJECT
public:
/**
* @brief Constructor.
* @param document Pointer to document.
*/
explicit QPythonHighlighter(QTextDocument* document=nullptr);
protected:
void highlightBlock(const QString& text) override;
private:
QVector<QHighlightRule> m_highlightRules;
QVector<QHighlightBlockRule> m_highlightBlockRules;
QRegularExpression m_includePattern;
QRegularExpression m_functionPattern;
QRegularExpression m_defTypePattern;
};

View file

@ -0,0 +1,41 @@
#pragma once
// Qt
#include <QSyntaxHighlighter> // Required for inheritance
class QSyntaxStyle;
/**
* @brief Class, that descrubes highlighter with
* syntax style.
*/
class QStyleSyntaxHighlighter : public QSyntaxHighlighter
{
public:
/**
* @brief Constructor.
* @param document Pointer to text document.
*/
explicit QStyleSyntaxHighlighter(QTextDocument* document=nullptr);
// Disable copying
QStyleSyntaxHighlighter(const QStyleSyntaxHighlighter&) = delete;
QStyleSyntaxHighlighter& operator=(const QStyleSyntaxHighlighter&) = delete;
/**
* @brief Method for setting syntax style.
* @param style Pointer to syntax style.
*/
void setSyntaxStyle(QSyntaxStyle* style);
/**
* @brief Method for getting syntax style.
* @return Pointer to syntax style. May be nullptr.
*/
QSyntaxStyle* syntaxStyle() const;
private:
QSyntaxStyle* m_syntaxStyle;
};

View file

@ -0,0 +1,70 @@
#pragma once
// Qt
#include <QObject> // Required for inheritance
#include <QMap>
#include <QString>
#include <QTextCharFormat>
/**
* @brief Class, that describes Qt style
* parser for QCodeEditor.
*/
class QSyntaxStyle : public QObject
{
Q_OBJECT
public:
/**
* @brief Constructor.
* @param parent Pointer to parent QObject
*/
explicit QSyntaxStyle(QObject* parent=nullptr);
/**
* @brief Method for loading and parsing
* style.
* @param fl Style.
* @return Success.
*/
bool load(QString fl);
/**
* @brief Method for getting style name.
* @return Name.
*/
QString name() const;
/**
* @brief Method for checking is syntax style loaded.
* @return Is loaded.
*/
bool isLoaded() const;
/**
* @brief Method for getting format for property
* name.
* @param name Property name.
* @return Text char format.
*/
QTextCharFormat getFormat(QString name) const;
/**
* @brief Static method for getting default style.
* @return Pointer to default style.
*/
static QSyntaxStyle* defaultStyle();
private:
QString m_name;
QMap<
QString,
QTextCharFormat
> m_data;
bool m_loaded;
};

View file

@ -0,0 +1,43 @@
#pragma once
// QCodeEditor
#include <QStyleSyntaxHighlighter> // Required for inheritance
// Qt
#include <QVector>
#include <QRegularExpression>
/**
* @brief Class, that describes XML code
* highlighter.
*/
class QXMLHighlighter : public QStyleSyntaxHighlighter
{
Q_OBJECT
public:
/**
* @brief Constructor.
* @param document Pointer to document.
*/
explicit QXMLHighlighter(QTextDocument* document=nullptr);
protected:
void highlightBlock(const QString& text) override;
private:
void highlightByRegex(const QTextCharFormat& format,
const QRegularExpression& regex,
const QString& text);
QVector<QRegularExpression> m_xmlKeywordRegexes;
QRegularExpression m_xmlElementRegex;
QRegularExpression m_xmlAttributeRegex;
QRegularExpression m_xmlValueRegex;
QRegularExpression m_xmlCommentBeginRegex;
QRegularExpression m_xmlCommentEndRegex;
};