SILICON
Loading...
Searching...
No Matches
codeEditor.hpp
Go to the documentation of this file.
1/*
2 Copyright (c) 2026. Giulio Cocconi
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#pragma once
19
20#include <ui/lifetime.hpp>
21
22#include <optional>
23#include <vector>
24
25#include <QPlainTextEdit>
26#include <QRegularExpression>
27
29
30class QCompleter;
31class QContextMenuEvent;
32class QEvent;
33class QFont;
34class QKeyEvent;
35class QPaintEvent;
36class QResizeEvent;
37class QWheelEvent;
38class QStringListModel;
39class QWidget;
40
41namespace SILICON::ui {
42
45
47class CodeEditor : public QPlainTextEdit {
48public:
49 explicit CodeEditor(QWidget* parent = nullptr);
50
52 void clearFileType();
54 void zoomBy(int steps);
55
56 [[nodiscard]] const std::optional<SILICON::project::DocumentType>& fileType() const;
57 [[nodiscard]] QString completionPrefix() const;
58
59protected:
60#ifndef QT_NO_CONTEXTMENU
61 void contextMenuEvent(QContextMenuEvent* event) override;
62#endif
63 void resizeEvent(QResizeEvent* event) override;
64 void changeEvent(QEvent* event) override;
65 void keyPressEvent(QKeyEvent* event) override;
66 void wheelEvent(QWheelEvent* event) override;
67
68private:
69 friend class CodeLineNumberArea;
70
71 [[nodiscard]] int lineNumberAreaWidth() const;
72 void paintLineNumberArea(QPaintEvent* event);
73 [[nodiscard]] QFont lineNumberFont() const;
76 void refreshTheme();
79 [[nodiscard]] int currentIndentWidth() const;
81 void showCompletion(bool explicitRequest);
82 void insertCompletion(const QString& completion);
83 [[nodiscard]] bool insertAutoPair(QChar typed);
84 [[nodiscard]] bool removeEmptyPair();
85 [[nodiscard]] bool expandPairedDelimiters();
86 void indentCurrentLine();
87 void dedentSelection();
88 [[nodiscard]] bool currentLineMatchesIndentationTrigger() const;
89 [[nodiscard]] bool isWordDelimiter(QChar character) const;
90
93 QCompleter* completer;
94 QStringListModel* completionModel;
95 std::vector<QRegularExpression> indentationTriggers;
96 std::optional<SILICON::project::DocumentType> fileTypeValue;
97 qreal zoomFontPointSize = 0.0;
99};
100
101} // namespace SILICON::ui
void changeEvent(QEvent *event) override
CodeEditor(QWidget *parent=nullptr)
bool isWordDelimiter(QChar character) const
CodeLineNumberArea * lineNumberArea
void contextMenuEvent(QContextMenuEvent *event) override
QString completionPrefix() const
bool currentLineMatchesIndentationTrigger() const
CallbackScope callbacks
void keyPressEvent(QKeyEvent *event) override
void setFileType(SILICON::project::DocumentType type)
QFont lineNumberFont() const
int currentIndentWidth() const
std::optional< SILICON::project::DocumentType > fileTypeValue
void resizeEvent(QResizeEvent *event) override
void insertCompletion(const QString &completion)
const std::optional< SILICON::project::DocumentType > & fileType() const
void wheelEvent(QWheelEvent *event) override
void zoomBy(int steps)
Adjusts text and line-number size by the given number of points.
bool insertAutoPair(QChar typed)
CodeSyntaxHighlighter * syntaxHighlighter
void paintLineNumberArea(QPaintEvent *event)
QStringListModel * completionModel
void showCompletion(bool explicitRequest)
int lineNumberAreaWidth() const
std::vector< QRegularExpression > indentationTriggers
friend class CodeLineNumberArea