Wireshark 4.7.3
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
15
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:
106 struct Rule
107 {
108 QRegularExpression pattern;
109 QTextCharFormat format;
110 };
111
112
113 QVector<Rule> rules_;
114 QTextCharFormat stringFormat_;
115 QTextCharFormat commentFormat_;
116 QRegularExpression singleLineComment_;
117
119 bool highlightLongBlock(const QString &text, bool isComment, bool continuingPrevious, qint32 eqCountFromState,
120 qint32 &nextStateEqCount);
122 qint32 findLongBlockStart(const QString &text, qint32 from, bool isComment, qint32 &eqCount,
123 qint32 &tokenLength) const;
125 qint32 findLongBlockEnd(const QString &text, qint32 from, qint32 eqCount) const;
126
131 void buildRules(bool isDark);
132};
133
134/* ===== code_view ===== */
135
139class LuaDebuggerCodeView : public QPlainTextEdit
140{
141 Q_OBJECT
142
143 public:
148 LuaDebuggerCodeView(QWidget *parent = nullptr);
149
154 void lineNumberAreaPaintEvent(QPaintEvent *event);
159 qint32 lineNumberAreaWidth();
160
168 void setFilename(const QString &f) { filename = f; }
169
174 QString getFilename() const { return filename; }
175
180 void setCurrentLine(qint32 line);
187 void moveCaretToLineStart(qint32 line);
192 void setEditorFont(const QFont &font);
193
196
198 void applyTheme();
199
209 QString luaIdentifierUnderCursor(const QTextCursor &cursor) const;
210
215 QString watchExpressionForContextMenu(const QPoint &viewportPos) const;
216
217 signals:
225 void breakpointToggled(const QString &filename, qint32 line, bool toggleActive);
226
231 void breakpointMoveRequested(const QString &filename, qint32 fromLine, qint32 toLine);
232
251 void breakpointGutterMenuRequested(const QString &filename, qint32 line, const QPoint &globalPos);
252
253 protected:
255 void resizeEvent(QResizeEvent *event) override;
257 bool eventFilter(QObject *watched, QEvent *event) override;
258
259 private slots:
261 void updateLineNumberAreaWidth(int newBlockCount);
263 void rebuildLineHighlights();
265 void updateLineNumberArea(const QRect &rect, int dy);
266
267 private:
269 QWidget *lineNumberArea;
271 QSyntaxHighlighter *syntaxHighlighter;
273 qint32 pausedExecutionLine_ = -1;
274
276 friend class LineNumberArea;
277
279 QString filename;
281 void applyEditorPalette();
282};
283
287class LineNumberArea : public QWidget
288{
289 public:
294 LineNumberArea(LuaDebuggerCodeView *editor) : QWidget(editor), codeEditor(editor) {}
295
299 QSize sizeHint() const override { return QSize(codeEditor->lineNumberAreaWidth(), 0); }
300
304 bool isDraggingBreakpoint() const { return draggingBreakpoint_; }
305
309 qint32 dragTargetLine() const { return dragTargetLine_; }
310
314 qint32 dragSourceLine() const { return pressedLine_; }
315
316 protected:
320 void paintEvent(QPaintEvent *event) override { codeEditor->lineNumberAreaPaintEvent(event); }
321
325 void mousePressEvent(QMouseEvent *event) override;
326
330 void mouseMoveEvent(QMouseEvent *event) override;
331
335 void mouseReleaseEvent(QMouseEvent *event) override;
336
345 void contextMenuEvent(QContextMenuEvent *event) override;
346
347 private:
349 LuaDebuggerCodeView *codeEditor;
350
363 qint32 lineAtY(qint32 yPx) const;
364
370 bool hasBreakpointAtLine(qint32 line) const;
371
378 qint32 nearestVisibleDropLine(qint32 yPx, qint32 sourceLine) const;
379
381 QPoint pressPos_;
382
384 qint32 pressedLine_ = -1;
385
387 qint32 dragTargetLine_ = -1;
388
390 bool leftPressArmed_ = false;
391
393 bool draggingBreakpoint_ = false;
394
396 bool pressShiftToggle_ = false;
397
399 bool pressHadBreakpoint_ = false;
400};
401
402/* ===== font_policy ===== */
403
418{
419 public:
422
435 void attach(QTabWidget *codeTabs, QTreeView *variablesTree, QTreeView *watchTree, QStandardItemModel *watchModel,
436 QTreeView *stackTree, QTreeView *fileTree, QTreeView *breakpointsTree,
437 QPlainTextEdit *evalInputEdit, QPlainTextEdit *evalOutputEdit);
438
442 void applyAll();
443
448 void applyToCodeEditors(const QFont &font = QFont());
449
454 void applyToPanels();
455
460
467 QFont monospaceFont(bool zoomed) const;
468
472 QFont regularFont() const;
473
474 private:
476 QTabWidget *codeTabs_ = nullptr;
478 QTreeView *variablesTree_ = nullptr;
480 QTreeView *watchTree_ = nullptr;
482 QStandardItemModel *watchModel_ = nullptr;
484 QTreeView *stackTree_ = nullptr;
486 QTreeView *fileTree_ = nullptr;
488 QTreeView *breakpointsTree_ = nullptr;
490 QPlainTextEdit *evalInputEdit_ = nullptr;
492 QPlainTextEdit *evalOutputEdit_ = nullptr;
493};
494
495/* ===== code_tabs_controller ===== */
496
518class LuaDebuggerCodeTabsController : public QObject
519{
520 Q_OBJECT
521
522 public:
528
533 void attach(QTabWidget *tabs);
534
541 QTabWidget *tabs() const { return tabs_; }
542
548 QString lastOpenDirectory();
549
554 void setLastOpenDirectory(const QString &dir);
555
567 LuaDebuggerCodeView *loadFile(const QString &file_path);
568
573
577 qint32 unsavedOpenScriptTabCount() const;
578
582 bool hasUnsavedChanges() const;
583
589 bool ensureUnsavedChangesHandled(const QString &title);
590
593
599
603 bool saveAllModified();
604
609
612
615
619 void openInitialBreakpointFiles(const QVector<QString> &files);
620
623
625 void applyThemeToAllTabs();
626
627 public slots:
632 void onTabCloseRequested(int index);
633
638 void onCurrentTabChanged(int index);
639
641 void onSaveFile();
642
643 private:
651 void onCodeViewBreakpointToggled(const QString &file_path, qint32 line, bool toggleActive);
652
654 LuaDebuggerDialog *host_ = nullptr;
655
657 QTabWidget *tabs_ = nullptr;
658
660 QString lastOpenDirectory_;
661};
662
663#endif
qint32 dragTargetLine() const
Current drag target line, or -1 if not dragging.
Definition lua_debugger_code_editor.h:309
void mouseMoveEvent(QMouseEvent *event) override
Track drag gestures in the breakpoint gutter.
Definition lua_debugger_code_editor.cpp:936
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:1046
void paintEvent(QPaintEvent *event) override
Delegate painting back to the code view.
Definition lua_debugger_code_editor.h:320
void mousePressEvent(QMouseEvent *event) override
Toggle breakpoints when the gutter is clicked.
Definition lua_debugger_code_editor.cpp:892
qint32 dragSourceLine() const
Source line being dragged, or -1 if not dragging.
Definition lua_debugger_code_editor.h:314
void mouseReleaseEvent(QMouseEvent *event) override
Commit click vs drag-drop action on mouse release.
Definition lua_debugger_code_editor.cpp:966
bool isDraggingBreakpoint() const
True if a breakpoint drag-and-drop is currently in progress.
Definition lua_debugger_code_editor.h:304
LineNumberArea(LuaDebuggerCodeView *editor)
Construct the helper widget bound to a specific code view.
Definition lua_debugger_code_editor.h:294
QSize sizeHint() const override
Size the gutter according to the editor's width requirements.
Definition lua_debugger_code_editor.h:299
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:1270
LuaDebuggerCodeTabsController(LuaDebuggerDialog *host)
Constructs a new LuaDebuggerCodeTabsController object.
Definition lua_debugger_code_editor.cpp:1238
QTabWidget * tabs() const
Borrowed reference to the tab strip. Exposed so other controllers (breakpoints, reload coordinator) c...
Definition lua_debugger_code_editor.h:541
bool ensureUnsavedChangesHandled(const QString &title)
If any tab is modified, prompt to save / discard / cancel.
Definition lua_debugger_code_editor.cpp:1399
bool saveCodeView(LuaDebuggerCodeView *view)
Persist one editor buffer to its file path.
Definition lua_debugger_code_editor.cpp:1440
void clearAllDocumentModified()
Mark every open document as unmodified without saving.
Definition lua_debugger_code_editor.cpp:1423
LuaDebuggerCodeView * currentCodeView() const
The code editor in the active tab, or nullptr.
Definition lua_debugger_code_editor.cpp:1366
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:1257
void clearAllCodeHighlights()
Drop the current-line stripe on every open editor.
Definition lua_debugger_code_editor.cpp:1533
void updateSaveActionState()
Enable Save when the current tab has unsaved edits.
Definition lua_debugger_code_editor.cpp:1506
void updateWindowModifiedState()
Reflect unsaved scripts in the window title (e.g. close hint).
Definition lua_debugger_code_editor.cpp:1516
qint32 unsavedOpenScriptTabCount() const
How many open code tabs currently have unsaved edits.
Definition lua_debugger_code_editor.cpp:1375
void attach(QTabWidget *tabs)
Attaches the controller to the given tab widget.
Definition lua_debugger_code_editor.cpp:1240
void updateTabTextForCodeView(LuaDebuggerCodeView *view)
Update the tab label (e.g. trailing " *") for one editor.
Definition lua_debugger_code_editor.cpp:1487
void onTabCloseRequested(int index)
Handles a request to close a specific tab.
Definition lua_debugger_code_editor.cpp:1591
bool saveAllModified()
Save every tab that has unsaved edits.
Definition lua_debugger_code_editor.cpp:1466
void onCurrentTabChanged(int index)
Handles a change in the active tab.
Definition lua_debugger_code_editor.cpp:1578
void onSaveFile()
Save the active script tab (toolbar action).
Definition lua_debugger_code_editor.cpp:1567
LuaDebuggerCodeView * loadFile(const QString &file_path)
Load file_path into a code tab, creating one if necessary.
Definition lua_debugger_code_editor.cpp:1275
bool hasUnsavedChanges() const
True if any open tab has unsaved edits.
Definition lua_debugger_code_editor.cpp:1394
void openInitialBreakpointFiles(const QVector< QString > &files)
Open each initial breakpoint file once tabs are ready.
Definition lua_debugger_code_editor.cpp:1525
void applyThemeToAllTabs()
Re-apply the active syntax-highlight theme to every open editor.
Definition lua_debugger_code_editor.cpp:1550
Editable code editor supporting gutter breakpoints and highlighting.
Definition lua_debugger_code_editor.h:140
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:596
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:393
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:545
LuaDebuggerCodeView(QWidget *parent=nullptr)
Create the code view and configure the line number gutter.
Definition lua_debugger_code_editor.cpp:361
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:432
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:168
void updateBreakpointMarkers()
Refresh breakpoint markers in the gutter area.
Definition lua_debugger_code_editor.cpp:622
void resizeEvent(QResizeEvent *event) override
Update margins whenever Qt reports a size change.
Definition lua_debugger_code_editor.cpp:472
void lineNumberAreaPaintEvent(QPaintEvent *event)
Paint the custom gutter that hosts line numbers and breakpoints.
Definition lua_debugger_code_editor.cpp:669
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:442
friend class LineNumberArea
Friend class allowing access to private members.
Definition lua_debugger_code_editor.h:276
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:578
bool eventFilter(QObject *watched, QEvent *event) override
Forward Esc to LuaDebuggerDialog (keys go to viewport, not the dialog).
Definition lua_debugger_code_editor.cpp:480
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:627
QString getFilename() const
Get the file path associated with this editor.
Definition lua_debugger_code_editor.h:174
void setEditorFont(const QFont &font)
Apply a monospace font to both editor text and gutter.
Definition lua_debugger_code_editor.cpp:607
Top-level dialog hosting the Lua debugger UI components.
Definition lua_debugger_dialog.h:189
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:1107
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:1091
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:1200
void applyToPanels()
Apply panel-mono bodies + regular headers to all side panels and re-sync watch QStandardItem fonts....
Definition lua_debugger_code_editor.cpp:1136
QFont monospaceFont(bool zoomed) const
Effective monospace font; respects the main-app preference and the optional zoom step....
Definition lua_debugger_code_editor.cpp:1215
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:1113
QFont regularFont() const
Effective regular UI font for tree-column headers.
Definition lua_debugger_code_editor.cpp:1227
LuaSyntaxHighlighter(QTextDocument *parent, bool isDark)
Build the rule set for isDark and bind to parent's document.
Definition lua_debugger_code_editor.cpp:148
void highlightBlock(const QString &text) override
Apply highlighting to a single text block.
Definition lua_debugger_code_editor.cpp:206
void setTheme(bool isDark)
Rebuild the rule set for the new theme and rehighlight.
Definition lua_debugger_code_editor.cpp:154
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:66
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