Skip to content

Commit

Permalink
Use input_buffer to do history completions
Browse files Browse the repository at this point in the history
  • Loading branch information
dalthviz committed Nov 28, 2024
1 parent c30e461 commit 6aaccbe
Showing 1 changed file with 53 additions and 29 deletions.
82 changes: 53 additions & 29 deletions qtconsole/history_console_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@
class HistoryListWidget(CompletionWidget):
""" A widget for GUI list history.
"""
complete_current = QtCore.Signal(str)

def _complete_current(self):
""" Perform the completion with the currently selected item.
"""
text = self.currentItem().data(QtCore.Qt.UserRole)
self.parent()._store_edits()
self.parent().input_buffer = text
self.complete_current.emit(text)
self.hide()


class HistoryConsoleWidget(ConsoleWidget):
Expand Down Expand Up @@ -47,6 +57,7 @@ def __init__(self, *args, **kw):
)
self.addAction(self.droplist_history)
self._history_list_widget = HistoryListWidget(self, self.gui_completion_height)
self._history_list_widget.complete_current.connect(self.change_input_buffer)

#---------------------------------------------------------------------------
# 'ConsoleWidget' public interface
Expand Down Expand Up @@ -162,7 +173,7 @@ def _show_history_droplist(self):
# Perform the search.
prompt_cursor = self._get_prompt_cursor()
if self._get_cursor().blockNumber() == prompt_cursor.blockNumber():

# Set a search prefix based on the cursor position.
pos = self._get_input_buffer_cursor_pos()
input_buffer = self.input_buffer
Expand All @@ -171,7 +182,7 @@ def _show_history_droplist(self):
n = min(pos, len(self._history_prefix))

# prefix changed, restart search from the beginning
if (self._history_prefix[:n] != input_buffer[:n]):
if self._history_prefix[:n] != input_buffer[:n]:
self._history_index = len(self._history)

# the only time we shouldn't set the history prefix
Expand All @@ -183,42 +194,44 @@ def _show_history_droplist(self):
c = self._get_cursor()
current_pos = c.position()
c.movePosition(QtGui.QTextCursor.EndOfBlock)
at_eol = (c.position() == current_pos)

if self._history_index == len(self._history) or \
not (self._history_prefix == '' and at_eol) or \
not (self._get_edited_history(self._history_index)[:pos] == input_buffer[:pos]):
at_eol = c.position() == current_pos

if (
self._history_index == len(self._history)
or not (self._history_prefix == "" and at_eol)
or not (
self._get_edited_history(self._history_index)[:pos]
== input_buffer[:pos]
)
):
self._history_prefix = input_buffer[:pos]
items = self._history
items.reverse()
if (self._history_prefix):
items = [item[len(self._history_prefix):] for item in items if item.startswith(self._history_prefix)]
if self._history_prefix:
items = [
item
for item in items
if item.startswith(self._history_prefix)
]

cursor = self._get_cursor()
pos = len(self._history_prefix)
cursor_pos = self._get_input_buffer_cursor_pos()
cursor.movePosition(QtGui.QTextCursor.Left,
n=(cursor_pos - pos))
cursor.movePosition(QtGui.QTextCursor.Left, n=(cursor_pos - pos))
# This line actually applies the move to control's cursor
self._control.setTextCursor(cursor)

offset = cursor_pos - pos
# Move the local cursor object to the start of the match and
# complete.
cursor.movePosition(QtGui.QTextCursor.Left, n=offset)
self._history_list_widget.cancel_completion()
if len(items) == 1:
cursor.setPosition(self._control.textCursor().position(),
QtGui.QTextCursor.KeepAnchor)
self._history_list_widget.show_items(
cursor, items
)
elif len(items) > 1:
current_pos = self._control.textCursor().position()
prefix = os.path.commonprefix(items)
if prefix:
cursor.setPosition(current_pos, QtGui.QTextCursor.KeepAnchor)
current_pos = cursor.position()
self._history_list_widget.show_items(cursor, items,
prefix_length=len(prefix))

self._history_list_widget.show_items(
cursor, items, prefix_length=len(prefix)
)

#---------------------------------------------------------------------------
# 'HistoryConsoleWidget' public interface
Expand Down Expand Up @@ -251,9 +264,7 @@ def history_previous(self, substring='', as_prefix=True):
break

if replace:
self._store_edits()
self._history_index = index
self.input_buffer = history
self.change_input_buffer(history, index=index)

return replace

Expand Down Expand Up @@ -284,9 +295,7 @@ def history_next(self, substring='', as_prefix=True):
break

if replace:
self._store_edits()
self._history_index = index
self.input_buffer = history
self.change_input_buffer(history, index=index)

return replace

Expand All @@ -300,6 +309,21 @@ def history_tail(self, n=10):
"""
return self._history[-n:]

@QtCore.Slot(str)
def change_input_buffer(self, buffer, index=0):
"""Change input_buffer value while storing edits and updating history index.
Parameters
----------
buffer : str
New value for the inpur buffer.
index : int, optional
History index to set. The default is 0.
"""
self._store_edits()
self._history_index = index
self.input_buffer = buffer

#---------------------------------------------------------------------------
# 'HistoryConsoleWidget' protected interface
#---------------------------------------------------------------------------
Expand Down

0 comments on commit 6aaccbe

Please sign in to comment.