Skip to content
This repository was archived by the owner on Aug 28, 2020. It is now read-only.

Commit d196d55

Browse files
committed
Merge pull request #112 from robertbasic/feature/breakpoint-jump-to-source
Feature/breakpoint jump to source. Resolves #111
2 parents acd253d + d9cfaca commit d196d55

File tree

3 files changed

+51
-24
lines changed

3 files changed

+51
-24
lines changed

pugdebug/gui/breakpoints.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@
99

1010
__author__ = "robertbasic"
1111

12+
from PyQt5.QtCore import pyqtSignal
1213
from PyQt5.QtWidgets import QTreeWidget, QTreeWidgetItem
1314

1415

1516
class PugdebugBreakpointViewer(QTreeWidget):
1617

18+
item_double_clicked_signal = pyqtSignal(str, int)
19+
1720
def __init__(self):
1821
super(PugdebugBreakpointViewer, self).__init__()
1922

@@ -22,6 +25,8 @@ def __init__(self):
2225

2326
self.setColumnWidth(0, 350)
2427

28+
self.itemDoubleClicked.connect(self.handle_item_double_clicked)
29+
2530
def set_breakpoints(self, breakpoints):
2631
self.clear()
2732

@@ -31,3 +36,9 @@ def set_breakpoints(self, breakpoints):
3136
item = QTreeWidgetItem(args)
3237

3338
self.addTopLevelItem(item)
39+
40+
def handle_item_double_clicked(self, item, column):
41+
file = item.text(0)
42+
line = int(item.text(1))
43+
44+
self.item_double_clicked_signal.emit(file, line)

pugdebug/gui/document.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,6 @@ def paint_line_numbers(self, line_numbers, event):
9191
height = font_metrics.height()
9292
width = line_numbers.width()
9393

94-
cursor = self.document_contents.textCursor()
95-
current_line_number = cursor.blockNumber() + 1
96-
9794
while block.isValid():
9895
# blocks are numbered from zero
9996
line_number += 1
@@ -116,7 +113,7 @@ def paint_line_numbers(self, line_numbers, event):
116113
if self.document_contents.block_has_breakpoint(block):
117114
brush = painter.brush()
118115
brush.setStyle(Qt.SolidPattern)
119-
if line_number == current_line_number:
116+
if self.document_contents.block_is_current(block):
120117
brush.setColor(Qt.red)
121118
else:
122119
brush.setColor(Qt.darkGreen)
@@ -136,8 +133,8 @@ def paint_line_numbers(self, line_numbers, event):
136133
def get_path(self):
137134
return self.document_contents.document_model.path
138135

139-
def move_to_line(self, line):
140-
self.document_contents.move_to_line(line)
136+
def move_to_line(self, line, is_current=True):
137+
self.document_contents.move_to_line(line, is_current)
141138
self.rehighlight_breakpoint_lines()
142139

143140
def remove_line_highlights(self):
@@ -219,7 +216,7 @@ def mouseDoubleClickEvent(self, event):
219216
def contextMenuEvent(self, event):
220217
pass
221218

222-
def move_to_line(self, line):
219+
def move_to_line(self, line, is_current=True):
223220
"""Move cursor to line
224221
225222
Move the cursor of the QPlainTextEdit that holds the document
@@ -244,11 +241,19 @@ def move_to_line(self, line):
244241
1
245242
)
246243

244+
# Unmark block as current
245+
block = cursor.block()
246+
self.block_set_is_current(block, False)
247+
247248
if cursor_moved is False:
248249
break
249250

250251
block_number = cursor.blockNumber()
251252

253+
# Mark block on which the cursor is as the current one
254+
block = cursor.block()
255+
self.block_set_is_current(block, is_current)
256+
252257
self.setTextCursor(cursor)
253258

254259
def highlight(self):
@@ -288,6 +293,15 @@ def block_remove_breakpoint(self, block):
288293
user_data.breakpoint = False
289294
block.setUserData(user_data)
290295

296+
def block_is_current(self, block):
297+
user_data = self.__get_block_user_data(block)
298+
return user_data.is_current
299+
300+
def block_set_is_current(self, block, is_current):
301+
user_data = self.__get_block_user_data(block)
302+
user_data.is_current = is_current
303+
block.setUserData(user_data)
304+
291305
def __get_block_user_data(self, block):
292306
user_data = block.userData()
293307
if user_data is None:
@@ -317,6 +331,7 @@ def paintEvent(self, event):
317331
class PugdebugBlockData(QTextBlockUserData):
318332

319333
breakpoint = False
334+
is_current = False
320335

321336
def __init__(self):
322337
super(PugdebugBlockData, self).__init__()

pugdebug/pugdebug.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ def connect_signals(self):
8787
self.connect_debugger_signals()
8888
self.connect_expression_viewer_signals()
8989
self.connect_stacktrace_viewer_signals()
90+
self.connect_breakpoint_viewer_signals()
9091

9192
def connect_file_browser_signals(self):
9293
"""Connect file browser signals
@@ -201,7 +202,12 @@ def connect_expression_viewer_signals(self):
201202

202203
def connect_stacktrace_viewer_signals(self):
203204
self.stacktrace_viewer.item_double_clicked_signal.connect(
204-
self.handle_stacktrace_double_clicked
205+
self.jump_to_line_in_file
206+
)
207+
208+
def connect_breakpoint_viewer_signals(self):
209+
self.breakpoint_viewer.item_double_clicked_signal.connect(
210+
self.jump_to_line_in_file
205211
)
206212

207213
def file_browser_item_activated(self, index):
@@ -213,7 +219,7 @@ def file_browser_item_activated(self, index):
213219
if path is not None:
214220
self.open_document(path, False)
215221

216-
def open_document(self, path, map_paths=True, line=None):
222+
def open_document(self, path, map_paths=True):
217223
"""Open a document
218224
219225
If a document is not already open, open it and add it as a new
@@ -260,19 +266,11 @@ def open_document(self, path, map_paths=True, line=None):
260266
document_model.filename,
261267
path
262268
)
263-
264-
# If a line is given, move to that line
265-
if line:
266-
document_widget.move_to_line(line)
267269
else:
268270
# Just focus the tab that has the opened document
269271
index = self.document_viewer.find_tab_index_by_path(path)
270272
self.document_viewer.setCurrentIndex(index)
271273

272-
# If a line is given, move to that line
273-
if line:
274-
self.document_viewer.get_document(index).move_to_line(line)
275-
276274
def handle_document_double_click(self, path, line_number):
277275
"""Handle when a document gets double clicked
278276
@@ -334,6 +332,16 @@ def focus_current_line(self):
334332
document_widget = self.document_viewer.get_current_document()
335333
document_widget.move_to_line(current_line)
336334

335+
def jump_to_line_in_file(self, file, line):
336+
"""Jump to a line in a file.
337+
338+
Show the document, and scroll to the given line.
339+
"""
340+
self.open_document(file)
341+
342+
document_widget = self.document_viewer.get_current_document()
343+
document_widget.move_to_line(line, False)
344+
337345
def handle_settings_changed(self, changed_settings):
338346
"""Handle when settings have changed.
339347
@@ -709,13 +717,6 @@ def handle_expression_added_or_changed(self, index, expression):
709717
if self.debugger.is_connected():
710718
self.debugger.evaluate_expression(index, expression)
711719

712-
def handle_stacktrace_double_clicked(self, file, line):
713-
"""Handle when an item in the stack trace viewer is double clicked.
714-
715-
Show the document, and scroll to the given line.
716-
"""
717-
self.open_document(file, True, line)
718-
719720
def handle_error(self, error):
720721
em = QErrorMessage(self.main_window)
721722
em.showMessage(error)

0 commit comments

Comments
 (0)