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 <gerald@wireshark.org>
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 <QPoint>
24#include <QRegularExpression>
25#include <QString>
26#include <QSyntaxHighlighter>
27#include <QTextCharFormat>
28#include <QTextCursor>
29#include <QVector>
30#include <QtGlobal>
31
34class QContextMenuEvent;
35class QEvent;
36class QPlainTextEdit;
37class QStandardItemModel;
38class QSyntaxHighlighter;
39class QTabWidget;
40class QTextDocument;
41class QTreeView;
42
43/* ===== code_palette ===== */
44
55{
56 QColor editorBackground;
57 QColor editorText;
58 QColor selection;
59 QColor selectionText;
60
61 QColor gutterBackground;
62 QColor gutterText;
63
65 QColor pausedLine;
66};
67
71
78
79/* ===== lua_highlighter ===== */
80
89class LuaSyntaxHighlighter : public QSyntaxHighlighter
90{
91 public:
93 explicit LuaSyntaxHighlighter(QTextDocument *parent, bool isDark);
94
96 void setTheme(bool isDark);
97
98 protected:
100 void highlightBlock(const QString &text) override;
101
102 private:
103 struct Rule
104 {
105 QRegularExpression pattern;
106 QTextCharFormat format;
107 };
108
109 QVector<Rule> rules_;
110 QTextCharFormat stringFormat_;
111 QTextCharFormat commentFormat_;
112 QRegularExpression singleLineComment_;
113
115 bool highlightLongBlock(const QString &text, bool isComment, bool continuingPrevious, qint32 eqCountFromState,
116 qint32 &nextStateEqCount);
118 qint32 findLongBlockStart(const QString &text, qint32 from, bool isComment, qint32 &eqCount,
119 qint32 &tokenLength) const;
121 qint32 findLongBlockEnd(const QString &text, qint32 from, qint32 eqCount) const;
122
127 void buildRules(bool isDark);
128};
129
130/* ===== code_view ===== */
131
135class LuaDebuggerCodeView : public QPlainTextEdit
136{
137 Q_OBJECT
138
139 public:
144 LuaDebuggerCodeView(QWidget *parent = nullptr);
145
150 void lineNumberAreaPaintEvent(QPaintEvent *event);
155 qint32 lineNumberAreaWidth();
156
164 void setFilename(const QString &f) { filename = f; }
165
170 QString getFilename() const { return filename; }
171
176 void setCurrentLine(qint32 line);
183 void moveCaretToLineStart(qint32 line);
188 void setEditorFont(const QFont &font);
189
192
194 void applyTheme();
195
205 QString luaIdentifierUnderCursor(const QTextCursor &cursor) const;
206
211 QString watchExpressionForContextMenu(const QPoint &viewportPos) const;
212
213 signals:
221 void breakpointToggled(const QString &filename, qint32 line, bool toggleActive);
222
227 void breakpointMoveRequested(const QString &filename, qint32 fromLine, qint32 toLine);
228
247 void breakpointGutterMenuRequested(const QString &filename, qint32 line, const QPoint &globalPos);
248
249 protected:
251 void resizeEvent(QResizeEvent *event) override;
253 bool eventFilter(QObject *watched, QEvent *event) override;
254
255 private slots:
257 void updateLineNumberAreaWidth(int newBlockCount);
259 void rebuildLineHighlights();
261 void updateLineNumberArea(const QRect &rect, int dy);
262
263 private:
265 QWidget *lineNumberArea;
267 QSyntaxHighlighter *syntaxHighlighter;
269 qint32 pausedExecutionLine_ = -1;
270
272 friend class LineNumberArea;
273
275 QString filename;
277 void applyEditorPalette();
278};
279
283class LineNumberArea : public QWidget
284{
285 public:
290 LineNumberArea(LuaDebuggerCodeView *editor) : QWidget(editor), codeEditor(editor) {}
291
295 QSize sizeHint() const override { return QSize(codeEditor->lineNumberAreaWidth(), 0); }
296
300 bool isDraggingBreakpoint() const { return draggingBreakpoint_; }
301
305 qint32 dragTargetLine() const { return dragTargetLine_; }
306
310 qint32 dragSourceLine() const { return pressedLine_; }
311
312 protected:
316 void paintEvent(QPaintEvent *event) override { codeEditor->lineNumberAreaPaintEvent(event); }
317
321 void mousePressEvent(QMouseEvent *event) override;
322
326 void mouseMoveEvent(QMouseEvent *event) override;
327
331 void mouseReleaseEvent(QMouseEvent *event) override;
332
341 void contextMenuEvent(QContextMenuEvent *event) override;
342
343 private:
345 LuaDebuggerCodeView *codeEditor;
346
359 qint32 lineAtY(qint32 yPx) const;
360
366 bool hasBreakpointAtLine(qint32 line) const;
367
374 qint32 nearestVisibleDropLine(qint32 yPx, qint32 sourceLine) const;
375
377 QPoint pressPos_;
378
380 qint32 pressedLine_ = -1;
381
383 qint32 dragTargetLine_ = -1;
384
386 bool leftPressArmed_ = false;
387
389 bool draggingBreakpoint_ = false;
390
392 bool pressShiftToggle_ = false;
393
395 bool pressHadBreakpoint_ = false;
396};
397
398/* ===== font_policy ===== */
399
414{
415 public:
418
431 void attach(QTabWidget *codeTabs, QTreeView *variablesTree, QTreeView *watchTree, QStandardItemModel *watchModel,
432 QTreeView *stackTree, QTreeView *fileTree, QTreeView *breakpointsTree,
433 QPlainTextEdit *evalInputEdit, QPlainTextEdit *evalOutputEdit);
434
438 void applyAll();
439
444 void applyToCodeEditors(const QFont &font = QFont());
445
450 void applyToPanels();
451
456
463 QFont monospaceFont(bool zoomed) const;
464
468 QFont regularFont() const;
469
470 private:
472 QTabWidget *codeTabs_ = nullptr;
474 QTreeView *variablesTree_ = nullptr;
476 QTreeView *watchTree_ = nullptr;
478 QStandardItemModel *watchModel_ = nullptr;
480 QTreeView *stackTree_ = nullptr;
482 QTreeView *fileTree_ = nullptr;
484 QTreeView *breakpointsTree_ = nullptr;
486 QPlainTextEdit *evalInputEdit_ = nullptr;
488 QPlainTextEdit *evalOutputEdit_ = nullptr;
489};
490
491/* ===== code_tabs_controller ===== */
492
514class LuaDebuggerCodeTabsController : public QObject
515{
516 Q_OBJECT
517
518 public:
524
529 void attach(QTabWidget *tabs);
530
537 QTabWidget *tabs() const { return tabs_; }
538
544 QString lastOpenDirectory();
545
550 void setLastOpenDirectory(const QString &dir);
551
563 LuaDebuggerCodeView *loadFile(const QString &file_path);
564
569
573 qint32 unsavedOpenScriptTabCount() const;
574
578 bool hasUnsavedChanges() const;
579
585 bool ensureUnsavedChangesHandled(const QString &title);
586
589
595
599 bool saveAllModified();
600
605
608
611
615 void openInitialBreakpointFiles(const QVector<QString> &files);
616
619
621 void applyThemeToAllTabs();
622
623 public slots:
628 void onTabCloseRequested(int index);
629
634 void onCurrentTabChanged(int index);
635
637 void onSaveFile();
638
639 private:
647 void onCodeViewBreakpointToggled(const QString &file_path, qint32 line, bool toggleActive);
648
650 LuaDebuggerDialog *host_ = nullptr;
651
653 QTabWidget *tabs_ = nullptr;
654
656 QString lastOpenDirectory_;
657};
658
659#endif
A widget for displaying line numbers in the code editor.
Definition lua_debugger_code_editor.h:284
qint32 dragTargetLine() const
Current drag target line, or -1 if not dragging.
Definition lua_debugger_code_editor.h:305
void mouseMoveEvent(QMouseEvent *event) override
Track drag gestures in the breakpoint gutter.
Definition lua_debugger_code_editor.cpp:935
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:1050
void paintEvent(QPaintEvent *event) override
Delegate painting back to the code view.
Definition lua_debugger_code_editor.h:316
void mousePressEvent(QMouseEvent *event) override
Toggle breakpoints when the gutter is clicked.
Definition lua_debugger_code_editor.cpp:891
qint32 dragSourceLine() const
Source line being dragged, or -1 if not dragging.
Definition lua_debugger_code_editor.h:310
void mouseReleaseEvent(QMouseEvent *event) override
Commit click vs drag-drop action on mouse release.
Definition lua_debugger_code_editor.cpp:965
bool isDraggingBreakpoint() const
True if a breakpoint drag-and-drop is currently in progress.
Definition lua_debugger_code_editor.h:300
LineNumberArea(LuaDebuggerCodeView *editor)
Construct the helper widget bound to a specific code view.
Definition lua_debugger_code_editor.h:290
QSize sizeHint() const override
Size the gutter according to the editor's width requirements.
Definition lua_debugger_code_editor.h:295
Owns the script tab strip and the documents inside it.
Definition lua_debugger_code_editor.h:515
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:1272
QTabWidget * tabs() const
Borrowed reference to the tab strip. Exposed so other controllers (breakpoints, reload coordinator) c...
Definition lua_debugger_code_editor.h:537
bool ensureUnsavedChangesHandled(const QString &title)
If any tab is modified, prompt to save / discard / cancel.
Definition lua_debugger_code_editor.cpp:1401
bool saveCodeView(LuaDebuggerCodeView *view)
Persist one editor buffer to its file path.
Definition lua_debugger_code_editor.cpp:1442
void clearAllDocumentModified()
Mark every open document as unmodified without saving.
Definition lua_debugger_code_editor.cpp:1425
LuaDebuggerCodeView * currentCodeView() const
The code editor in the active tab, or nullptr.
Definition lua_debugger_code_editor.cpp:1368
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:1259
void clearAllCodeHighlights()
Drop the current-line stripe on every open editor.
Definition lua_debugger_code_editor.cpp:1535
void updateSaveActionState()
Enable Save when the current tab has unsaved edits.
Definition lua_debugger_code_editor.cpp:1508
void updateWindowModifiedState()
Reflect unsaved scripts in the window title (e.g. close hint).
Definition lua_debugger_code_editor.cpp:1518
qint32 unsavedOpenScriptTabCount() const
How many open code tabs currently have unsaved edits.
Definition lua_debugger_code_editor.cpp:1377
void attach(QTabWidget *tabs)
Attaches the controller to the given tab widget.
Definition lua_debugger_code_editor.cpp:1242
void updateTabTextForCodeView(LuaDebuggerCodeView *view)
Update the tab label (e.g. trailing " *") for one editor.
Definition lua_debugger_code_editor.cpp:1489
void onTabCloseRequested(int index)
Handles a request to close a specific tab.
Definition lua_debugger_code_editor.cpp:1593
bool saveAllModified()
Save every tab that has unsaved edits.
Definition lua_debugger_code_editor.cpp:1468
void onCurrentTabChanged(int index)
Handles a change in the active tab.
Definition lua_debugger_code_editor.cpp:1580
void onSaveFile()
Save the active script tab (toolbar action).
Definition lua_debugger_code_editor.cpp:1569
LuaDebuggerCodeView * loadFile(const QString &file_path)
Load file_path into a code tab, creating one if necessary.
Definition lua_debugger_code_editor.cpp:1277
bool hasUnsavedChanges() const
True if any open tab has unsaved edits.
Definition lua_debugger_code_editor.cpp:1396
void openInitialBreakpointFiles(const QVector< QString > &files)
Open each initial breakpoint file once tabs are ready.
Definition lua_debugger_code_editor.cpp:1527
void applyThemeToAllTabs()
Re-apply the active syntax-highlight theme to every open editor.
Definition lua_debugger_code_editor.cpp:1552
Editable code editor supporting gutter breakpoints and highlighting.
Definition lua_debugger_code_editor.h:136
void breakpointMoveRequested(const QString &filename, qint32 fromLine, qint32 toLine)
Request moving a breakpoint in filename from fromLine to toLine after a gutter drag-and-drop gesture.
void clearCurrentLineHighlight()
Clear the debugger paused-line highlight (caret stripe unchanged).
Definition lua_debugger_code_editor.cpp:595
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:392
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:544
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:431
void setFilename(const QString &f)
Set the file path this editor is currently hosting, for use in breakpoint management and the "edited"...
Definition lua_debugger_code_editor.h:164
void updateBreakpointMarkers()
Refresh breakpoint markers in the gutter area.
Definition lua_debugger_code_editor.cpp:621
void resizeEvent(QResizeEvent *event) override
Update margins whenever Qt reports a size change.
Definition lua_debugger_code_editor.cpp:471
void lineNumberAreaPaintEvent(QPaintEvent *event)
Paint the custom gutter that hosts line numbers and breakpoints.
Definition lua_debugger_code_editor.cpp:668
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:441
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:577
bool eventFilter(QObject *watched, QEvent *event) override
Forward Esc to LuaDebuggerDialog (keys go to viewport, not the dialog).
Definition lua_debugger_code_editor.cpp:479
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:626
QString getFilename() const
Get the file path associated with this editor.
Definition lua_debugger_code_editor.h:170
void setEditorFont(const QFont &font)
Apply a monospace font to both editor text and gutter.
Definition lua_debugger_code_editor.cpp:606
Top-level dialog hosting the Lua debugger UI components.
Definition lua_debugger_dialog.h:189
Owns the dialog's font story end-to-end.
Definition lua_debugger_code_editor.h:414
LuaDebuggerFontPolicy()=default
Default constructor for the font policy.
void applyAll()
Apply zoomed monospace to code editors and panel mono + regular header fonts to the side panels....
Definition lua_debugger_code_editor.cpp:1111
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:1095
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:1204
void applyToPanels()
Apply panel-mono bodies + regular headers to all side panels and re-sync watch QStandardItem fonts....
Definition lua_debugger_code_editor.cpp:1140
QFont monospaceFont(bool zoomed) const
Effective monospace font; respects the main-app preference and the optional zoom step....
Definition lua_debugger_code_editor.cpp:1217
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:1117
QFont regularFont() const
Effective regular UI font for tree-column headers.
Definition lua_debugger_code_editor.cpp:1229
Syntax highlighter tuned for Lua keywords, strings, numbers, comments and Lua's long-bracket strings/...
Definition lua_debugger_code_editor.h:90
void highlightBlock(const QString &text) override
Apply highlighting to a single text block.
Definition lua_debugger_code_editor.cpp:205
void setTheme(bool isDark)
Rebuild the rule set for the new theme and rehighlight.
Definition lua_debugger_code_editor.cpp:153
bool luaDebuggerThemeIsDark()
Resolve the effective theme based on the debugger preference and Wireshark's current colour scheme wh...
LuaDebuggerEditorPalette luaDebuggerEditorPaletteFor(bool isDark)
Return the editor palette for the requested theme.
Definition lua_debugger_code_editor.cpp:65
Single source of truth for the script editor's theme-aware colours.
Definition lua_debugger_code_editor.h:55
QColor pausedLine
Background for the line the debugger is paused on.
Definition lua_debugger_code_editor.h:65