Fix PTK shell sometimes not updating auto-suggest when up-arrow is pressed (#5787)

* fix combining consecutive history entries when loading ptk shell

* add test case for combining history items

* add news item for PTK history combining

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update ptk-history-combining.rst

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Andy Kipp <anki-code@users.noreply.github.com>
This commit is contained in:
jfmontanaro 2025-01-31 10:40:43 -05:00 committed by GitHub
parent 2cdcd6c976
commit 77ecefff34
Failed to generate hash of commit
3 changed files with 48 additions and 2 deletions

View file

@ -0,0 +1,23 @@
**Added:**
* <news item>
**Changed:**
* <news item>
**Deprecated:**
* <news item>
**Removed:**
* <news item>
**Fixed:**
* prompt toolkit: Fixed autosuggest sometimes not updating when up-arrow is pressed (#5787).
**Security:**
* <news item>

View file

@ -8,6 +8,7 @@ import pytest
from xonsh.platform import minimum_required_ptk_version
from xonsh.shell import Shell
from xonsh.shells.ptk_shell import tokenize_ansi
from xonsh.shells.ptk_shell.history import PromptToolkitHistory
# verify error if ptk not installed or below min
@ -145,3 +146,24 @@ def test_ptk_default_append_history(cmd, exp_append_history, ptk_shell, monkeypa
assert len(append_history_calls) == 1
else:
assert len(append_history_calls) == 0
def test_ptk_combine_history(monkeypatch):
"""Test that consecutive identical history items are combined into a single item
when loading xonsh history items into prompt-toolkit history."""
def all_items(*args, **kwargs):
lines = [
"one two three",
"four five six",
"four five six",
"one two three",
]
for line in lines:
yield {"inp": line}
monkeypatch.setattr("xonsh.built_ins.XSH.history.all_items", all_items)
shell_hist = PromptToolkitHistory()
hist_strs = list(shell_hist.load_history_strings())
assert len(hist_strs) == 3

View file

@ -25,11 +25,12 @@ class PromptToolkitHistory(prompt_toolkit.history.History):
hist = XSH.history
if hist is None:
return
prev_line = None
for cmd in hist.all_items(newest_first=True):
line = cmd["inp"].rstrip()
strs = self.get_strings()
if len(strs) == 0 or line != strs[-1]:
if line != prev_line:
yield line
prev_line = line
def __getitem__(self, index):
return self.get_strings()[index]