Wireshark 5.1.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
17#ifndef LUA_DEBUGGER_CODE_EDITOR_H
18#define LUA_DEBUGGER_CODE_EDITOR_H
19
20#include <QColor>
21#include <QFont>
22#include <QObject>
23#include <QPlainTextEdit>
24#include <QPoint>
25#include <QRegularExpression>
26#include <QString>
27#include <QSyntaxHighlighter>
28#include <QTextCharFormat>
29#include <QTextCursor>
30#include <QVector>
31#include <QtGlobal>
32
34
37class QContextMenuEvent;
38class QEvent;
39class QPlainTextEdit;
40class QStandardItemModel;
41class QSyntaxHighlighter;
42class QTabWidget;
43class QTextDocument;
44class QTreeView;
45
46/* ===== code_palette ===== */
47
59{
60 QColor editorBackground;
61 QColor editorText;
62 QColor selection;
63 QColor selectionText;
64
65 QColor gutterBackground;
66 QColor gutterText;
67
69 QColor pausedLine;
70};
71
77
78/* ===== code_view ===== */
79
83class LuaDebuggerCodeView : public QPlainTextEdit
84{
85 Q_OBJECT
86
87 public:
92 LuaDebuggerCodeView(QWidget *parent = nullptr);
93
98 void lineNumberAreaPaintEvent(QPaintEvent *event);
103 qint32 lineNumberAreaWidth();
104
112 void setFilename(const QString &f) { filename = f; }
113
118 QString getFilename() const { return filename; }
119
124 void setCurrentLine(qint32 line);
131 void moveCaretToLineStart(qint32 line);
136 void setEditorFont(const QFont &font);
137
140
142 void applyTheme();
143
153 QString luaIdentifierUnderCursor(const QTextCursor &cursor) const;
154
159 QString watchExpressionForContextMenu(const QPoint &viewportPos) const;
160
161 signals:
169 void breakpointToggled(const QString &filename, qint32 line, bool toggleActive);
170
175 void breakpointMoveRequested(const QString &filename, qint32 fromLine, qint32 toLine);
176
195 void breakpointGutterMenuRequested(const QString &filename, qint32 line, const QPoint &globalPos);
196
197 protected:
199 void resizeEvent(QResizeEvent *event) override;
201 bool eventFilter(QObject *watched, QEvent *event) override;
202
203 private slots:
205 void updateLineNumberAreaWidth(int newBlockCount);
207 void rebuildLineHighlights();
209 void updateLineNumberArea(const QRect &rect, int dy);
210
211 private:
213 QWidget *lineNumberArea;
215 QSyntaxHighlighter *syntaxHighlighter;
217 qint32 pausedExecutionLine_ = -1;
218
220 friend class LineNumberArea;
221
223 QString filename;
225 void applyEditorPalette();
226};
227
231class LineNumberArea : public QWidget
232{
233 public:
238 LineNumberArea(LuaDebuggerCodeView *editor) : QWidget(editor), codeEditor(editor) {}
239
243 QSize sizeHint() const override { return QSize(codeEditor->lineNumberAreaWidth(), 0); }
244
248 bool isDraggingBreakpoint() const { return draggingBreakpoint_; }
249
253 qint32 dragTargetLine() const { return dragTargetLine_; }
254
258 qint32 dragSourceLine() const { return pressedLine_; }
259
260 protected:
264 void paintEvent(QPaintEvent *event) override { codeEditor->lineNumberAreaPaintEvent(event); }
265
269 void mousePressEvent(QMouseEvent *event) override;
270
274 void mouseMoveEvent(QMouseEvent *event) override;
275
279 void mouseReleaseEvent(QMouseEvent *event) override;
280
289 void contextMenuEvent(QContextMenuEvent *event) override;
290
291 private:
293 LuaDebuggerCodeView *codeEditor;
294
307 qint32 lineAtY(qint32 yPx) const;
308
314 bool hasBreakpointAtLine(qint32 line) const;
315
322 qint32 nearestVisibleDropLine(qint32 yPx, qint32 sourceLine) const;
323
325 QPoint pressPos_;
326
328 qint32 pressedLine_ = -1;
329
331 qint32 dragTargetLine_ = -1;
332
334 bool leftPressArmed_ = false;
335
337 bool draggingBreakpoint_ = false;
338
340 bool pressShiftToggle_ = false;
341
343 bool pressHadBreakpoint_ = false;
344};
345
346/* ===== font_policy ===== */
347
362{
363 public:
366
379 void attach(QTabWidget *codeTabs, QTreeView *variablesTree, QTreeView *watchTree, QStandardItemModel *watchModel,
380 QTreeView *stackTree, QTreeView *fileTree, QTreeView *breakpointsTree,
381 QPlainTextEdit *evalInputEdit, QPlainTextEdit *evalOutputEdit);
382
386 void applyAll();
387
392 void applyToCodeEditors(const QFont &font = QFont());
393
398 void applyToPanels();
399
404
411 QFont monospaceFont(bool zoomed) const;
412
416 QFont regularFont() const;
417
418 private:
420 QTabWidget *codeTabs_ = nullptr;
422 QTreeView *variablesTree_ = nullptr;
424 QTreeView *watchTree_ = nullptr;
426 QStandardItemModel *watchModel_ = nullptr;
428 QTreeView *stackTree_ = nullptr;
430 QTreeView *fileTree_ = nullptr;
432 QTreeView *breakpointsTree_ = nullptr;
434 QPlainTextEdit *evalInputEdit_ = nullptr;
436 QPlainTextEdit *evalOutputEdit_ = nullptr;
437};
438
439/* ===== code_tabs_controller ===== */
440
462class LuaDebuggerCodeTabsController : public QObject
463{
464 Q_OBJECT
465
466 public:
472
477 void attach(QTabWidget *tabs);
478
485 QTabWidget *tabs() const { return tabs_; }
486
492 QString lastOpenDirectory();
493
498 void setLastOpenDirectory(const QString &dir);
499
511 LuaDebuggerCodeView *loadFile(const QString &file_path);
512
517
521 qint32 unsavedOpenScriptTabCount() const;
522
526 bool hasUnsavedChanges() const;
527
533 bool ensureUnsavedChangesHandled(const QString &title);
534
537
543
547 bool saveAllModified();
548
553
556
559
563 void openInitialBreakpointFiles(const QVector<QString> &files);
564
567
569 void applyThemeToAllTabs();
570
571 public slots:
576 void onTabCloseRequested(int index);
577
582 void onCurrentTabChanged(int index);
583
585 void onSaveFile();
586
587 private:
595 void onCodeViewBreakpointToggled(const QString &file_path, qint32 line, bool toggleActive);
596
598 LuaDebuggerDialog *host_ = nullptr;
599
601 QTabWidget *tabs_ = nullptr;
602
604 QString lastOpenDirectory_;
605};
606
607#endif
qint32 dragTargetLine() const
Current drag target line, or -1 if not dragging.
Definition lua_debugger_code_editor.h:253
void mouseMoveEvent(QMouseEvent *event) override
Track drag gestures in the breakpoint gutter.
Definition lua_debugger_code_editor.cpp:657
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:767
void paintEvent(QPaintEvent *event) override
Delegate painting back to the code view.
Definition lua_debugger_code_editor.h:264
void mousePressEvent(QMouseEvent *event) override
Toggle breakpoints when the gutter is clicked.
Definition lua_debugger_code_editor.cpp:613
qint32 dragSourceLine() const
Source line being dragged, or -1 if not dragging.
Definition lua_debugger_code_editor.h:258
void mouseReleaseEvent(QMouseEvent *event) override
Commit click vs drag-drop action on mouse release.
Definition lua_debugger_code_editor.cpp:687
bool isDraggingBreakpoint() const
True if a breakpoint drag-and-drop is currently in progress.
Definition lua_debugger_code_editor.h:248
LineNumberArea(LuaDebuggerCodeView *editor)
Construct the helper widget bound to a specific code view.
Definition lua_debugger_code_editor.h:238
QSize sizeHint() const override
Size the gutter according to the editor's width requirements.
Definition lua_debugger_code_editor.h:243
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:991
LuaDebuggerCodeTabsController(LuaDebuggerDialog *host)
Constructs a new LuaDebuggerCodeTabsController object.
Definition lua_debugger_code_editor.cpp:959
QTabWidget * tabs() const
Borrowed reference to the tab strip. Exposed so other controllers (breakpoints, reload coordinator) c...
Definition lua_debugger_code_editor.h:485
bool ensureUnsavedChangesHandled(const QString &title)
If any tab is modified, prompt to save / discard / cancel.
Definition lua_debugger_code_editor.cpp:1120
bool saveCodeView(LuaDebuggerCodeView *view)
Persist one editor buffer to its file path.
Definition lua_debugger_code_editor.cpp:1161
void clearAllDocumentModified()
Mark every open document as unmodified without saving.
Definition lua_debugger_code_editor.cpp:1144
LuaDebuggerCodeView * currentCodeView() const
The code editor in the active tab, or nullptr.
Definition lua_debugger_code_editor.cpp:1087
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:978
void clearAllCodeHighlights()
Drop the current-line stripe on every open editor.
Definition lua_debugger_code_editor.cpp:1254
void updateSaveActionState()
Enable Save when the current tab has unsaved edits.
Definition lua_debugger_code_editor.cpp:1227
void updateWindowModifiedState()
Reflect unsaved scripts in the window title (e.g. close hint).
Definition lua_debugger_code_editor.cpp:1237
qint32 unsavedOpenScriptTabCount() const
How many open code tabs currently have unsaved edits.
Definition lua_debugger_code_editor.cpp:1096
void attach(QTabWidget *tabs)
Attaches the controller to the given tab widget.
Definition lua_debugger_code_editor.cpp:961
void updateTabTextForCodeView(LuaDebuggerCodeView *view)
Update the tab label (e.g. trailing " *") for one editor.
Definition lua_debugger_code_editor.cpp:1208
void onTabCloseRequested(int index)
Handles a request to close a specific tab.
Definition lua_debugger_code_editor.cpp:1312
bool saveAllModified()
Save every tab that has unsaved edits.
Definition lua_debugger_code_editor.cpp:1187
void onCurrentTabChanged(int index)
Handles a change in the active tab.
Definition lua_debugger_code_editor.cpp:1299
void onSaveFile()
Save the active script tab (toolbar action).
Definition lua_debugger_code_editor.cpp:1288
LuaDebuggerCodeView * loadFile(const QString &file_path)
Load file_path into a code tab, creating one if necessary.
Definition lua_debugger_code_editor.cpp:996
bool hasUnsavedChanges() const
True if any open tab has unsaved edits.
Definition lua_debugger_code_editor.cpp:1115
void openInitialBreakpointFiles(const QVector< QString > &files)
Open each initial breakpoint file once tabs are ready.
Definition lua_debugger_code_editor.cpp:1246
void applyThemeToAllTabs()
Re-apply the active syntax-highlight theme to every open editor.
Definition lua_debugger_code_editor.cpp:1271
Editable code editor supporting gutter breakpoints and highlighting.
Definition lua_debugger_code_editor.h:84
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:318
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:115
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:267
LuaDebuggerCodeView(QWidget *parent=nullptr)
Create the code view and configure the line number gutter.
Definition lua_debugger_code_editor.cpp:83
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:154
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:112
void updateBreakpointMarkers()
Refresh breakpoint markers in the gutter area.
Definition lua_debugger_code_editor.cpp:344
void resizeEvent(QResizeEvent *event) override
Update margins whenever Qt reports a size change.
Definition lua_debugger_code_editor.cpp:194
void lineNumberAreaPaintEvent(QPaintEvent *event)
Paint the custom gutter that hosts line numbers and breakpoints.
Definition lua_debugger_code_editor.cpp:390
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:164
friend class LineNumberArea
Friend class allowing access to private members.
Definition lua_debugger_code_editor.h:220
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:300
bool eventFilter(QObject *watched, QEvent *event) override
Forward Esc to LuaDebuggerDialog (keys go to viewport, not the dialog).
Definition lua_debugger_code_editor.cpp:202
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 active theme and color scheme.
Definition lua_debugger_code_editor.cpp:349
QString getFilename() const
Get the file path associated with this editor.
Definition lua_debugger_code_editor.h:118
void setEditorFont(const QFont &font)
Apply a monospace font to both editor text and gutter.
Definition lua_debugger_code_editor.cpp:329
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:828
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:812
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:921
void applyToPanels()
Apply panel-mono bodies + regular headers to all side panels and re-sync watch QStandardItem fonts....
Definition lua_debugger_code_editor.cpp:857
QFont monospaceFont(bool zoomed) const
Effective monospace font; respects the main-app preference and the optional zoom step....
Definition lua_debugger_code_editor.cpp:936
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:834
QFont regularFont() const
Effective regular UI font for tree-column headers.
Definition lua_debugger_code_editor.cpp:948
LuaDebuggerEditorPalette luaDebuggerEditorPalette()
Return the editor palette of the active theme and light/dark mode.
Definition lua_debugger_code_editor.cpp:67
Script editor colours, read from the theme's code section.
Definition lua_debugger_code_editor.h:59
QColor pausedLine
Background for the line the debugger is paused on.
Definition lua_debugger_code_editor.h:69