Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR: Add permanent history to the debugger #10010

Merged
merged 12 commits into from
Aug 23, 2019
40 changes: 40 additions & 0 deletions spyder/plugins/ipythonconsole/widgets/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,33 @@
# (see spyder/__init__.py for details)

"""Control widgets used by ShellWidget"""
import pdb

from qtpy.QtCore import Qt, Signal
from qtpy.QtWidgets import QTextEdit
from IPython.core.history import HistoryManager

from spyder.config.base import get_conf_path
from spyder.utils.qthelpers import restore_keyevent
from spyder.widgets.calltip import CallTipWidget
from spyder.widgets.mixins import (BaseEditMixin, GetHelpMixin,
TracebackLinksMixin,
BrowseHistoryMixin)


class PdbHistory(HistoryManager):

def _get_hist_file_name(self, profile=None):
"""
Get default pdb history file name.

The profile parameter is ignored, but must exist for compatibility with
the parent class.
"""

return get_conf_path('pdb_history.sqlite')


class ControlWidget(TracebackLinksMixin, GetHelpMixin, BrowseHistoryMixin,
QTextEdit, BaseEditMixin):
"""
Expand All @@ -26,6 +42,7 @@ class ControlWidget(TracebackLinksMixin, GetHelpMixin, BrowseHistoryMixin,
visibility_changed = Signal(bool)
go_to_error = Signal(str)
focus_changed = Signal()
PDB_HIST_MAX = 400

def __init__(self, parent=None):
QTextEdit.__init__(self, parent)
Expand All @@ -34,6 +51,11 @@ def __init__(self, parent=None):
GetHelpMixin.__init__(self)
BrowseHistoryMixin.__init__(self)

self.pdb_history_file = PdbHistory()
self.history = [line[-1]
for line in self.pdb_history_file.get_tail(
self.PDB_HIST_MAX, include_latest=True)]

self.calltip_widget = CallTipWidget(self, hide_timer_on=False)
self.found_results = []

Expand Down Expand Up @@ -73,6 +95,24 @@ def focusOutEvent(self, event):
self.focus_changed.emit()
return super(ControlWidget, self).focusOutEvent(event)

def add_to_pdb_history(self, line_num, line):
"""Add command to history"""
self.histidx = None
if not line:
return
line = line.strip()

# If repeated line
if len(self.history) > 0 and self.history[-1] == line:
return

cmd = line.split(" ")[0]
args = line.split(" ")[1:]
is_pdb_cmd = "do_" + cmd in dir(pdb.Pdb)
if cmd and (not is_pdb_cmd or len(args) > 0):
self.history.append(line)
self.pdb_history_file.store_inputs(line_num, line)


class PageControlWidget(QTextEdit, BaseEditMixin):
"""
Expand Down
20 changes: 13 additions & 7 deletions spyder/plugins/ipythonconsole/widgets/debugging.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
mode and Spyder
"""

import pdb
import re

from qtpy.QtCore import Qt
Expand All @@ -35,13 +34,18 @@ def __init__(self, *args, **kwargs):
self._previous_prompt = None
self._input_queue = []
self._input_ready = False
self._last_pdb_cmd = ''
self._pdb_line_num = 0

def set_queued_input(self, client):
"""Change the kernel client input function queue calls."""
old_input = client.input

def queued_input(string):
"""If input is not ready, save it in a queue."""
if not string.strip():
# Must get the last genuine command
string = self._last_pdb_cmd
if self._input_ready:
self._input_ready = False
return old_input(string)
Expand All @@ -54,6 +58,7 @@ def _debugging_hook(self, debugging):
"""Catches debugging state."""
# If debugging starts or stops, clear the input queue.
self._input_queue = []
self._pdb_line_num = 0

# --- Public API --------------------------------------------------
def write_to_stdin(self, line):
Expand Down Expand Up @@ -120,13 +125,14 @@ def _handle_input_request(self, msg):
return

def callback(line):
line = line.strip()

# Save history to browse it later
if not (len(self._control.history) > 0
and self._control.history[-1] == line):
# do not save pdb commands
cmd = line.split(" ")[0]
if cmd and "do_" + cmd not in dir(pdb.Pdb):
self._control.history.append(line)
self._pdb_line_num += 1
self._control.add_to_pdb_history(self._pdb_line_num, line)

if line:
self._last_pdb_cmd = line

# must match ConsoleWidget.do_execute
self._executing = True
Expand Down
1 change: 1 addition & 0 deletions spyder/widgets/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -1357,6 +1357,7 @@ def browse_history(self, backward):
text, self.histidx = self.find_in_history(tocursor, self.histidx,
backward)
if text is not None:
text = text.strip()
if self.hist_wholeline:
self.clear_line()
self.insert_text(text)
Expand Down