-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_editor.h
More file actions
253 lines (193 loc) · 7.06 KB
/
code_editor.h
File metadata and controls
253 lines (193 loc) · 7.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#ifndef CODE_EDITOR_H
#define CODE_EDITOR_H
#include <Qsci/qsciscintilla.h>
#include <Qsci/qscilexercpp.h>
#include <Qsci/qscilexerpython.h>
#include <Qsci/qscilexerjavascript.h>
#include <Qsci/qscilexerjava.h>
#include <Qsci/qscilexerruby.h>
#include <Qsci/qscilexerbash.h>
#include <Qsci/qscilexerjson.h>
#include <Qsci/qscilexermarkdown.h>
#include <Qsci/qsciapis.h>
#include <QMap>
#include <QTimer>
// --- Central Indicator Definitions ---
enum EditorIndicator {
// ... your other indicators ...
INDICATOR_BRACE_MATCH = 12,
// NEW: Indicators for warnings and errors
INDICATOR_WARNING = 2,
INDICATOR_ERROR = 1,
INDICATOR_BRACKET_BASE = 17,
// ...
};
enum CustomIndicator {
// We can define other indicators here in the future
INDICATOR_COLORBLOCK_BASE = 20, // As used by your colorBlockRx
// Let's reserve the highest available indicators for brackets to be safe.
// INDICATOR_BRACKET_BASE = 17, // Using 29, 30, 31
MAX_BRACKET_COLORS = 3
};
// ═══════════════════════════════════════════════════════════════════════════
// Theme Definition
// ═══════════════════════════════════════════════════════════════════════════
struct EditorTheme {
QString name;
// Base colors
QColor background;
QColor foreground;
QColor lineHighlight;
QColor selection;
QColor selectionForeground;
QColor caret;
QColor invisibles;
// Margin colors
QColor marginBackground;
QColor marginForeground;
QColor foldMarginBackground;
// Syntax colors
QColor keyword;
QColor keywordSecondary;
QColor string;
QColor stringEscape;
QColor comment;
QColor commentDoc;
QColor number;
QColor operator_;
QColor identifier;
QColor type;
QColor function;
QColor preprocessor;
QColor decorator;
QColor className;
QColor constant;
QColor error;
// Brace matching
QColor braceMatchBg;
QColor braceUnmatchBg; // no foreground since we use Rainbow bracket
QVector<QColor> bracketColors;
// Indentation guides
QColor indentGuide;
static EditorTheme oneDarkPro();
static EditorTheme dracula();
static EditorTheme monokai();
static EditorTheme githubDark();
static EditorTheme tokyoNight();
};
// ═══════════════════════════════════════════════════════════════════════════
// Custom QScintilla Editor
// ═══════════════════════════════════════════════════════════════════════════
class CodeEditor : public QsciScintilla {
Q_OBJECT
public:
explicit CodeEditor(QWidget *parent = nullptr);
~CodeEditor() override;
void setLexer(QsciLexer *lexer) override;
void registerExtraKeywords(const QString &lexerName,
int set,
const QStringList &words);
// Language support
void setLanguage(const QString &langId);
QString currentLanguage() const { return m_currentLanguage; }
// Theming
void setTheme(const EditorTheme &theme);
void setTheme(const QString &themeName);
EditorTheme currentTheme() const { return m_theme; }
// Editor settings
void setFontFamily(const QString &family);
void setFontSize(int size);
void setTabSize(int size);
void setShowWhitespace(bool show);
void setShowIndentGuides(bool show);
void setWordWrap(bool wrap);
void setMinimap(bool show);
// Code intelligence
void setAutoCompleteEnabled(bool enabled);
void setBraceMatchingEnabled(bool enabled);
void setAutoIndentEnabled(bool enabled);
// Utilities
void goToLine(int line);
void duplicateLine();
void deleteLine();
void moveLineUp();
void moveLineDown();
void toggleComment();
void setKeywordSet(int set, const char* words);
// Data for keyPressEvent
const QMap<QChar, QChar> bracketPairs = {
{'(', ')'}, {'{', '}'}, {'[', ']'}, {'"', '"'}, {'\'', '\''}
};
// rainbow bracket
// void setupIndicators_bracket(); // good at public
void setupBracketIndicators(); // good at public
QTimer *m_highlightTimer;
QVector<QColor> m_bracketColors;
QVector<QColor> bracketColors;
QTimer *m_idleProcessingTimer; // Single timer for all idle tasks
void setupColorBlockIndicators(QsciScintilla* editor);
signals:
void languageChanged(const QString &langId);
void cursorPositionChanged(int line, int column);
void toggleCommentRequested();
protected:
void keyPressEvent(QKeyEvent *event) override;
void focusInEvent(QFocusEvent *event) override;
void wheelEvent(QWheelEvent *event) override;
private slots:
void onCursorPositionChanged(int line, int index);
void onTextChanged();
void updateColorBlocks();
void onIdleTimeout(); // private slot
void highlightVisibleBrackets(); // private slot
void scheduleIdleProcessing();
private:
void applyExtraKeywords();
QMap<QString, QMap<int, QStringList>> keywordRegistry;
// Setup methods
void setupEditor();
void setupMargins();
void setupFolding();
void setupIndicators();
void setupShortcuts();
void setupAutocompletion();
// Lexer management
void createLexers();
void applyLexer(QsciLexer *lexer);
void themeLexer(QsciLexer *lexer);
// Specific lexer theming
void themeCppLexer(QsciLexerCPP *lexer);
void themePythonLexer(QsciLexerPython *lexer);
void themeJavaScriptLexer(QsciLexerJavaScript *lexer);
void themeJavaLexer(QsciLexerJava *lexer);
// Helper methods
void lockBackground();
void applyTheme();
long toScintillaColor(const QColor &color);
QString getCommentPrefix() const;
// Lexer instances (lazy-loaded)
QsciLexerCPP *m_lexerCpp = nullptr;
QsciLexerCPP *m_lexerC = nullptr;
QsciLexerPython *m_lexerPython = nullptr;
QsciLexerJavaScript *m_lexerJavaScript = nullptr;
QsciLexerJava *m_lexerJava = nullptr;
QsciLexerJSON *m_lexerJson = nullptr;
// API instances for autocomplete
QMap<QString, QsciAPIs*> m_apis;
// State
QString m_currentLanguage = "cpp";
EditorTheme m_theme;
QString m_fontFamily = "JetBrains Mono";
int m_fontSize = 12;
int m_tabSize = 4;
// Timers
// QTimer *m_colorBlockTimer = nullptr;
// Indicator IDs
static const int IndicatorError = 8;
static const int IndicatorWarning = 9;
static const int IndicatorColorBlockStart = 10;
static const int IndicatorColorBlockEnd = 19;
};
// Alias for backward compatibility
using CustomQsciEditor = CodeEditor;
#endif // CODE_EDITOR_H