Wireshark 4.7.0
The Wireshark network protocol analyzer
Loading...
Searching...
No Matches
lua_debugger_code_editor.h
Go to the documentation of this file.
1/* lua_debugger_code_editor.h
2 *
3 * Wireshark - Network traffic analyzer
4 * By Gerald Combs <[email protected]>
5 * Copyright 1998 Gerald Combs
6 *
7 * SPDX-License-Identifier: GPL-2.0-or-later
8 */
9
16#ifndef LUA_DEBUGGER_CODE_EDITOR_H
17#define LUA_DEBUGGER_CODE_EDITOR_H
18
19#include <QColor>
20#include <QFont>
21#include <QObject>
22#include <QPlainTextEdit>
23#include <QRegularExpression>
24#include <QString>
25#include <QSyntaxHighlighter>
26#include <QTextCharFormat>
27#include <QTextCursor>
28#include <QVector>
29#include <QtGlobal>
30
33class QContextMenuEvent;
34class QEvent;
35class QPlainTextEdit;
36class QStandardItemModel;
37class QSyntaxHighlighter;
38class QTabWidget;
39class QTextDocument;
40class QTreeView;
41
42/* ===== code_palette ===== */
43
54{
55 QColor editorBackground;
56 QColor editorText;
57 QColor selection;
58 QColor selectionText;
59
60 QColor gutterBackground;
61 QColor gutterText;
62
64 QColor pausedLine;
65};
66
70
73
74/* ===== lua_highlighter ===== */
75
84class LuaSyntaxHighlighter : public QSyntaxHighlighter
85{
86 public:
88 explicit LuaSyntaxHighlighter(QTextDocument *parent, bool isDark);
89
91 void setTheme(bool isDark);
92
93 protected:
95 void highlightBlock(const QString &text) override;
96
97 private:
98 struct Rule
99 {
100 QRegularExpression pattern;
101 QTextCharFormat format;
102 };
103
104 QVector<Rule> rules_;
105 QTextCharFormat stringFormat_;
106 QTextCharFormat commentFormat_;
107 QRegularExpression singleLineComment_;
108
110 bool highlightLongBlock(const QString &text, bool isComment, bool continuingPrevious, qint32 eqCountFromState,
111 qint32 &nextStateEqCount);
113 qint32 findLongBlockStart(const QString &text, qint32 from, bool isComment, qint32 &eqCount,
114 qint32 &tokenLength) const;
116 qint32 findLongBlockEnd(const QString &text, qint32 from, qint32 eqCount) const;
117
118 void buildRules(bool isDark);
119};
120
121/* ===== code_view ===== */
122
126class LuaDebuggerCodeView : public QPlainTextEdit
127{
128 Q_OBJECT
129
130 public:
135 LuaDebuggerCodeView(QWidget *parent = nullptr);
136
141 void lineNumberAreaPaintEvent(QPaintEvent *event);
146 qint32 lineNumberAreaWidth();
147
148 void setFilename(const QString &f) { filename = f; }
149 QString getFilename() const { return filename; }
154 void setCurrentLine(qint32 line);
161 void moveCaretToLineStart(qint32 line);
163 void setEditorFont(const QFont &font);
167 void applyTheme();
177 QString luaIdentifierUnderCursor(const QTextCursor &cursor) const;
182 QString watchExpressionForContextMenu(const QPoint &viewportPos) const;
183
184 signals:
192 void breakpointToggled(const QString &filename, qint32 line, bool toggleActive);
193
212 void breakpointGutterMenuRequested(const QString &filename, qint32 line, const QPoint &globalPos);
213
214 protected:
216 void resizeEvent(QResizeEvent *event) override;
218 bool eventFilter(QObject *watched, QEvent *event) override;
219
220 private slots:
222 void updateLineNumberAreaWidth(int newBlockCount);
224 void rebuildLineHighlights();
226 void updateLineNumberArea(const QRect &rect, int dy);
227
228 private:
229 QWidget *lineNumberArea;
230 QSyntaxHighlighter *syntaxHighlighter;
232 qint32 pausedExecutionLine_ = -1;
233
234 friend class LineNumberArea;
235 QString filename;
237 void applyEditorPalette();
238};
239
240class LineNumberArea : public QWidget
241{
242 public:
247 LineNumberArea(LuaDebuggerCodeView *editor) : QWidget(editor), codeEditor(editor) {}
248
250 QSize sizeHint() const override { return QSize(codeEditor->lineNumberAreaWidth(), 0); }
251
252 protected:
254 void paintEvent(QPaintEvent *event) override { codeEditor->lineNumberAreaPaintEvent(event); }
255
257 void mousePressEvent(QMouseEvent *event) override;
258
266 void contextMenuEvent(QContextMenuEvent *event) override;
267
268 private:
269 LuaDebuggerCodeView *codeEditor;
270
281 qint32 lineAtY(qint32 yPx) const;
282};
283
284/* ===== font_policy ===== */
285
300{
301 public:
302 LuaDebuggerFontPolicy() = default;
303
305 void attach(QTabWidget *codeTabs, QTreeView *variablesTree, QTreeView *watchTree, QStandardItemModel *watchModel,
306 QTreeView *stackTree, QTreeView *fileTree, QTreeView *breakpointsTree,
307 QPlainTextEdit *evalInputEdit, QPlainTextEdit *evalOutputEdit);
308
311 void applyAll();
312
315 void applyToCodeEditors(const QFont &font = QFont());
316
320 void applyToPanels();
321
325
329 QFont monospaceFont(bool zoomed) const;
330
332 QFont regularFont() const;
333
334 private:
335 QTabWidget *codeTabs_ = nullptr;
336 QTreeView *variablesTree_ = nullptr;
337 QTreeView *watchTree_ = nullptr;
338 QStandardItemModel *watchModel_ = nullptr;
339 QTreeView *stackTree_ = nullptr;
340 QTreeView *fileTree_ = nullptr;
341 QTreeView *breakpointsTree_ = nullptr;
342 QPlainTextEdit *evalInputEdit_ = nullptr;
343 QPlainTextEdit *evalOutputEdit_ = nullptr;
344};
345
346/* ===== code_tabs_controller ===== */
347
369class LuaDebuggerCodeTabsController : public QObject
370{
371 Q_OBJECT
372
373 public:
375
376 void attach(QTabWidget *tabs);
377
382 QTabWidget *tabs() const { return tabs_; }
383
387 QString lastOpenDirectory();
388
391 void setLastOpenDirectory(const QString &dir);
392
403 LuaDebuggerCodeView *loadFile(const QString &file_path);
404
407
409 qint32 unsavedOpenScriptTabCount() const;
410
412 bool hasUnsavedChanges() const;
413
418 bool ensureUnsavedChangesHandled(const QString &title);
419
422
425
428 bool saveAllModified();
429
432
435
438
440 void openInitialBreakpointFiles(const QVector<QString> &files);
441
444
446 void applyThemeToAllTabs();
447
448 public slots:
449 void onTabCloseRequested(int index);
450 void onCurrentTabChanged(int index);
451
453 void onSaveFile();
454
455 private:
459 void onCodeViewBreakpointToggled(const QString &file_path, qint32 line, bool toggleActive);
460
461 LuaDebuggerDialog *host_ = nullptr;
462 QTabWidget *tabs_ = nullptr;
463 QString lastOpenDirectory_;
464};
465
466#endif
Definition lua_debugger_code_editor.h:241
void contextMenuEvent(QContextMenuEvent *event) override
Right-click / Ctrl-click / two-finger trackpad tap on the breakpoint gutter: always pop the Edit / Di...
Definition lua_debugger_code_editor.cpp:882
void paintEvent(QPaintEvent *event) override
Delegate painting back to the code view.
Definition lua_debugger_code_editor.h:254
void mousePressEvent(QMouseEvent *event) override
Toggle breakpoints when the gutter is clicked.
Definition lua_debugger_code_editor.cpp:813
LineNumberArea(LuaDebuggerCodeView *editor)
Construct the helper widget bound to a specific code view.
Definition lua_debugger_code_editor.h:247
QSize sizeHint() const override
Size the gutter according to the editor's width requirements.
Definition lua_debugger_code_editor.h:250
Owns the script tab strip and the documents inside it.
Definition lua_debugger_code_editor.h:370
void setLastOpenDirectory(const QString &dir)
Remember the directory the user last opened a script from. Persists for the lifetime of the controlle...
Definition lua_debugger_code_editor.cpp:1104
QTabWidget * tabs() const
Borrowed reference to the tab strip. Exposed so other controllers (breakpoints, reload coordinator) c...
Definition lua_debugger_code_editor.h:382
bool ensureUnsavedChangesHandled(const QString &title)
If any tab is modified, prompt to save / discard / cancel.
Definition lua_debugger_code_editor.cpp:1230
bool saveCodeView(LuaDebuggerCodeView *view)
Persist one editor buffer to its file path.
Definition lua_debugger_code_editor.cpp:1271
void clearAllDocumentModified()
Mark every open document as unmodified without saving.
Definition lua_debugger_code_editor.cpp:1254
LuaDebuggerCodeView * currentCodeView() const
The code editor in the active tab, or nullptr.
Definition lua_debugger_code_editor.cpp:1197
QString lastOpenDirectory()
Directory to seed the next "Open Lua Script" dialog with. Lazily resolves to Documents (or $HOME) on ...
Definition lua_debugger_code_editor.cpp:1091
void clearAllCodeHighlights()
Drop the current-line stripe on every open editor.
Definition lua_debugger_code_editor.cpp:1364
void updateSaveActionState()
Enable Save when the current tab has unsaved edits.
Definition lua_debugger_code_editor.cpp:1337
void updateWindowModifiedState()
Reflect unsaved scripts in the window title (e.g. close hint).
Definition lua_debugger_code_editor.cpp:1347
qint32 unsavedOpenScriptTabCount() const
How many open code tabs currently have unsaved edits.
Definition lua_debugger_code_editor.cpp:1206
void updateTabTextForCodeView(LuaDebuggerCodeView *view)
Update the tab label (e.g. trailing " *") for one editor.
Definition lua_debugger_code_editor.cpp:1318
bool saveAllModified()
Save every tab that has unsaved edits.
Definition lua_debugger_code_editor.cpp:1297
void onSaveFile()
Save the active script tab (toolbar action).
Definition lua_debugger_code_editor.cpp:1398
LuaDebuggerCodeView * loadFile(const QString &file_path)
Load file_path into a code tab, creating one if necessary.
Definition lua_debugger_code_editor.cpp:1109
bool hasUnsavedChanges() const
True if any open tab has unsaved edits.
Definition lua_debugger_code_editor.cpp:1225
void openInitialBreakpointFiles(const QVector< QString > &files)
Open each initial breakpoint file once tabs are ready.
Definition lua_debugger_code_editor.cpp:1356
void applyThemeToAllTabs()
Re-apply the active syntax-highlight theme to every open editor.
Definition lua_debugger_code_editor.cpp:1381
Editable code editor supporting gutter breakpoints and highlighting.
Definition lua_debugger_code_editor.h:127
void clearCurrentLineHighlight()
Clear the debugger paused-line highlight (caret stripe unchanged).
Definition lua_debugger_code_editor.cpp:601
QString luaIdentifierUnderCursor(const QTextCursor &cursor) const
Return the Lua identifier under the given cursor position, or an empty string if the position is not ...
Definition lua_debugger_code_editor.cpp:398
void setCurrentLine(qint32 line)
Set the debugger "execution paused" line (amber bar) and move the caret to that line....
Definition lua_debugger_code_editor.cpp:550
QString watchExpressionForContextMenu(const QPoint &viewportPos) const
Watch text for the editor context menu: trimmed selection if any, otherwise the Lua identifier at vie...
Definition lua_debugger_code_editor.cpp:437
void updateBreakpointMarkers()
Refresh breakpoint markers in the gutter area.
Definition lua_debugger_code_editor.cpp:627
void resizeEvent(QResizeEvent *event) override
Update margins whenever Qt reports a size change.
Definition lua_debugger_code_editor.cpp:477
void lineNumberAreaPaintEvent(QPaintEvent *event)
Paint the custom gutter that hosts line numbers and breakpoints.
Definition lua_debugger_code_editor.cpp:674
void breakpointToggled(const QString &filename, qint32 line, bool toggleActive)
Emitted when a breakpoint icon is clicked within the gutter (right margin).
qint32 lineNumberAreaWidth()
Compute the width required for the gutter, including icons.
Definition lua_debugger_code_editor.cpp:447
void moveCaretToLineStart(qint32 line)
Move the caret to the start of a line without changing the paused line (e.g. go-to-line).
Definition lua_debugger_code_editor.cpp:583
bool eventFilter(QObject *watched, QEvent *event) override
Forward Esc to LuaDebuggerDialog (keys go to viewport, not the dialog).
Definition lua_debugger_code_editor.cpp:485
void breakpointGutterMenuRequested(const QString &filename, qint32 line, const QPoint &globalPos)
Request an Edit / Disable (Enable) / Remove popup for the breakpoint at filename:line,...
void applyTheme()
Re-apply theme colors from the current preference.
Definition lua_debugger_code_editor.cpp:632
void setEditorFont(const QFont &font)
Apply a monospace font to both editor text and gutter.
Definition lua_debugger_code_editor.cpp:612
Top-level dialog hosting the Lua debugger UI components.
Definition lua_debugger_dialog.h:165
Owns the dialog's font story end-to-end.
Definition lua_debugger_code_editor.h:300
void applyAll()
Apply zoomed monospace to code editors and panel mono + regular header fonts to the side panels....
Definition lua_debugger_code_editor.cpp:943
void attach(QTabWidget *codeTabs, QTreeView *variablesTree, QTreeView *watchTree, QStandardItemModel *watchModel, QTreeView *stackTree, QTreeView *fileTree, QTreeView *breakpointsTree, QPlainTextEdit *evalInputEdit, QPlainTextEdit *evalOutputEdit)
Seed the policy with the dialog's panel widgets. Idempotent.
Definition lua_debugger_code_editor.cpp:927
void reapplyToWatchItemModel()
Re-walk the watch model and seed each item's font to panel-mono, preserving its current bold flag (us...
Definition lua_debugger_code_editor.cpp:1036
void applyToPanels()
Apply panel-mono bodies + regular headers to all side panels and re-sync watch QStandardItem fonts....
Definition lua_debugger_code_editor.cpp:972
QFont monospaceFont(bool zoomed) const
Effective monospace font; respects the main-app preference and the optional zoom step....
Definition lua_debugger_code_editor.cpp:1049
void applyToCodeEditors(const QFont &font=QFont())
Apply the (optionally explicit) monospace to all open code-view tabs. When font is empty,...
Definition lua_debugger_code_editor.cpp:949
QFont regularFont() const
Effective regular UI font for tree-column headers.
Definition lua_debugger_code_editor.cpp:1061
Syntax highlighter tuned for Lua keywords, strings, numbers, comments and Lua's long-bracket strings/...
Definition lua_debugger_code_editor.h:85
void highlightBlock(const QString &text) override
Apply highlighting to a single text block.
Definition lua_debugger_code_editor.cpp:212
void setTheme(bool isDark)
Rebuild the rule set for the new theme and rehighlight.
Definition lua_debugger_code_editor.cpp:160
bool luaDebuggerThemeIsDark()
Resolve the effective theme based on the debugger preference and Wireshark's current colour scheme wh...
Definition lua_debugger_code_editor.cpp:62
LuaDebuggerEditorPalette luaDebuggerEditorPaletteFor(bool isDark)
Return the editor palette for the requested theme.
Definition lua_debugger_code_editor.cpp:72
Single source of truth for the script editor's theme-aware colours.
Definition lua_debugger_code_editor.h:54
QColor pausedLine
Background for the line the debugger is paused on.
Definition lua_debugger_code_editor.h:64