From 77ecefff34b3e7824301783510f92e2dd899b1df Mon Sep 17 00:00:00 2001 From: jfmontanaro Date: Fri, 31 Jan 2025 10:40:43 -0500 Subject: [PATCH] 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 --- news/ptk-history-combining.rst | 23 +++++++++++++++++++++++ tests/shell/test_ptk_shell.py | 22 ++++++++++++++++++++++ xonsh/shells/ptk_shell/history.py | 5 +++-- 3 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 news/ptk-history-combining.rst diff --git a/news/ptk-history-combining.rst b/news/ptk-history-combining.rst new file mode 100644 index 000000000..cea98a196 --- /dev/null +++ b/news/ptk-history-combining.rst @@ -0,0 +1,23 @@ +**Added:** + +* + +**Changed:** + +* + +**Deprecated:** + +* + +**Removed:** + +* + +**Fixed:** + +* prompt toolkit: Fixed autosuggest sometimes not updating when up-arrow is pressed (#5787). + +**Security:** + +* diff --git a/tests/shell/test_ptk_shell.py b/tests/shell/test_ptk_shell.py index cf596f711..57acd3999 100644 --- a/tests/shell/test_ptk_shell.py +++ b/tests/shell/test_ptk_shell.py @@ -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 diff --git a/xonsh/shells/ptk_shell/history.py b/xonsh/shells/ptk_shell/history.py index 182cf890a..96edc80ec 100644 --- a/xonsh/shells/ptk_shell/history.py +++ b/xonsh/shells/ptk_shell/history.py @@ -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]