Skip to content

Commit 2674831

Browse files
committed
Highlight breakpoints in the code browser
1 parent a06cc2d commit 2674831

File tree

6 files changed

+54
-24
lines changed

6 files changed

+54
-24
lines changed

BreakpointModel.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ QVariant BreakpointModel::data(const QModelIndex &index, int role) const {
3131
return QVariant();
3232
}
3333

34-
void BreakpointModel::setBreakpoints(std::vector<Breakpoint> &&newBps) {
34+
void BreakpointModel::setBreakpoints(const std::vector<Breakpoint> &newBps) {
3535
const size_t prevSize = bps.size();
36-
bps = std::move(newBps);
36+
bps = newBps;
3737
int diff = bps.size() - prevSize;
3838
if (diff > 0)
3939
insertRows(prevSize, diff);

BreakpointModel.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class BreakpointModel : public QAbstractTableModel {
1919
QVariant data(const QModelIndex &index,
2020
int role = Qt::DisplayRole) const override;
2121

22-
void setBreakpoints(std::vector<Breakpoint> &&newBps);
22+
void setBreakpoints(const std::vector<Breakpoint> &newBps);
2323

2424
private:
2525
std::vector<Breakpoint> bps;

CodeBrowser.cpp

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "CodeBrowser.h"
22

3+
#include <QMenu>
34
#include <QPainter>
45
#include <QTextBlock>
56

@@ -12,11 +13,8 @@ CodeBrowser::CodeBrowser(QWidget *parent) : QPlainTextEdit(parent) {
1213
&CodeBrowser::updateLineNumberAreaWidth);
1314
connect(this, &CodeBrowser::updateRequest, this,
1415
&CodeBrowser::updateLineNumberArea);
15-
connect(this, &CodeBrowser::cursorPositionChanged, this,
16-
&CodeBrowser::highlightCurrentLine);
1716

1817
updateLineNumberAreaWidth(0);
19-
highlightCurrentLine();
2018
}
2119

2220
int CodeBrowser::lineNumberAreaWidth() {
@@ -32,6 +30,13 @@ int CodeBrowser::lineNumberAreaWidth() {
3230
return space;
3331
}
3432

33+
void CodeBrowser::contextMenuEvent(QContextMenuEvent *event) {
34+
QMenu *menu = createStandardContextMenu();
35+
menu->addAction(tr("Toggle Breakpoint"));
36+
menu->exec(event->globalPos());
37+
delete menu;
38+
}
39+
3540
void CodeBrowser::updateLineNumberAreaWidth(int /* newBlockCount */) {
3641
setViewportMargins(lineNumberAreaWidth(), 0, 0, 0);
3742
}
@@ -54,17 +59,28 @@ void CodeBrowser::resizeEvent(QResizeEvent *e) {
5459
QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
5560
}
5661

57-
void CodeBrowser::highlightCurrentLine() {
62+
void CodeBrowser::updateHighlightedLines(const std::vector<Breakpoint> &bps,
63+
size_t currentLine) {
5864
QList<QTextEdit::ExtraSelection> extraSelections;
59-
60-
if (!isReadOnly()) {
65+
// Mark each breakpoint as red.
66+
for (const auto &bp : bps) {
6167
QTextEdit::ExtraSelection selection;
62-
68+
QTextCursor cursor(document()->findBlockByLineNumber(bp.lineNumber - 1));
69+
QColor lineColor = QColor(Qt::red).lighter(160);
70+
selection.format.setBackground(lineColor);
71+
selection.format.setProperty(QTextFormat::FullWidthSelection, true);
72+
selection.cursor = cursor;
73+
selection.cursor.clearSelection();
74+
extraSelections.append(selection);
75+
}
76+
// Mark current position as yellow.
77+
if (currentLine > 0) {
78+
QTextEdit::ExtraSelection selection;
79+
QTextCursor cursor(document()->findBlockByLineNumber(currentLine));
6380
QColor lineColor = QColor(Qt::yellow).lighter(160);
64-
6581
selection.format.setBackground(lineColor);
6682
selection.format.setProperty(QTextFormat::FullWidthSelection, true);
67-
selection.cursor = textCursor();
83+
selection.cursor = cursor;
6884
selection.cursor.clearSelection();
6985
extraSelections.append(selection);
7086
}

CodeBrowser.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
22

3+
#include "BreakpointModel.h"
4+
35
#include <QPlainTextEdit>
46

57
QT_BEGIN_NAMESPACE
@@ -21,13 +23,15 @@ class CodeBrowser : public QPlainTextEdit {
2123

2224
void lineNumberAreaPaintEvent(QPaintEvent *event);
2325
int lineNumberAreaWidth();
26+
void contextMenuEvent(QContextMenuEvent *event) override;
27+
void updateHighlightedLines(const std::vector<Breakpoint> &bps,
28+
size_t currentLine);
2429

2530
protected:
2631
void resizeEvent(QResizeEvent *event) override;
2732

2833
private slots:
2934
void updateLineNumberAreaWidth(int newBlockCount);
30-
void highlightCurrentLine();
3135
void updateLineNumberArea(const QRect &rect, int dy);
3236

3337
private:

MainWindow.cpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,24 +54,31 @@ void MainWindow::onOpenFile() {
5454
const QString openFile = QFileDialog::getOpenFileName();
5555
if (openFile.isEmpty())
5656
return;
57-
populateCodeBrowser(openFile.toStdString());
57+
// FIXME: Breakpoints markings will disappear if you open a file manually.
58+
populateCodeBrowser(openFile.toStdString(), 0, {});
5859
}
5960

6061
void MainWindow::updateView() {
61-
updateCodeBrowser();
62-
updateBreakpointModel();
62+
// Not sure what this could should look but this ain't it.
63+
//
64+
// Since multiple components care about breakpoints, maybe we should be using
65+
// that signal/slot mechanism?
66+
const auto bps = updateBreakpointModel();
67+
updateCodeBrowser(bps);
6368
updateFrameModel();
6469
}
6570

66-
void MainWindow::updateCodeBrowser() {
71+
void MainWindow::updateCodeBrowser(const std::vector<Breakpoint> &bps) {
6772
const auto codeLoc = debugger.getLocation();
6873
// Do something smarter than this.
6974
const std::string filePath =
7075
codeLoc.getDirectory() + '/' + codeLoc.getFileName();
71-
populateCodeBrowser(filePath);
76+
populateCodeBrowser(filePath, codeLoc.getLine(), bps);
7277
}
7378

74-
void MainWindow::populateCodeBrowser(const std::string &filePath) {
79+
void MainWindow::populateCodeBrowser(const std::string &filePath,
80+
size_t lineNumber,
81+
const std::vector<Breakpoint> &bps) {
7582
if (currentFile != filePath) {
7683
// Read the contents off the disk and populate the code view.
7784
std::ifstream file(filePath);
@@ -84,6 +91,7 @@ void MainWindow::populateCodeBrowser(const std::string &filePath) {
8491
currentFile = filePath;
8592
}
8693
// Mark the breakpoints visually as well as the current position.
94+
ui->codeBrowser->updateHighlightedLines(bps, lineNumber);
8795
}
8896

8997
void MainWindow::updateFrameModel() {
@@ -101,7 +109,7 @@ void MainWindow::updateFrameModel() {
101109
frameModel.setFrameVariables(std::move(modelFrame));
102110
}
103111

104-
void MainWindow::updateBreakpointModel() {
112+
std::vector<Breakpoint> MainWindow::updateBreakpointModel() {
105113
// Debugger should just return this instead of doing transformation here.
106114
auto &debuggerBps = debugger.getBreakpoints();
107115
std::vector<Breakpoint> modelBps;
@@ -117,7 +125,8 @@ void MainWindow::updateBreakpointModel() {
117125
return modelBp;
118126
});
119127
logMsg("Added " + std::to_string(modelBps.size()) + " breakpoints");
120-
bpModel.setBreakpoints(std::move(modelBps));
128+
bpModel.setBreakpoints(modelBps);
129+
return modelBps;
121130
}
122131

123132
void MainWindow::logMsg(const std::string &msg) {

MainWindow.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ public slots:
2828

2929
private:
3030
void updateView();
31-
void updateCodeBrowser();
31+
void updateCodeBrowser(const std::vector<Breakpoint> &bps);
3232
void updateFrameModel();
33-
void updateBreakpointModel();
34-
void populateCodeBrowser(const std::string &fileName);
33+
std::vector<Breakpoint> updateBreakpointModel();
34+
void populateCodeBrowser(const std::string &fileName, size_t lineNumber,
35+
const std::vector<Breakpoint> &bps);
3536
void logMsg(const std::string &msg);
3637

3738
Ui::MainWindow *ui;

0 commit comments

Comments
 (0)