Skip to content

Commit 800e39f

Browse files
committed
replace input stem instead of appending
1 parent 6c417e4 commit 800e39f

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

Lib/_pyrepl/completing_reader.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ def prefix(wordlist: list[str], j: int = 0) -> str:
4141
for word in wordlist:
4242
d[word[i]] = 1
4343
if len(d) > 1:
44-
return wordlist[0][j:i]
44+
return wordlist[0][:i]
4545
i += 1
4646
d = {}
4747
except IndexError:
48-
return wordlist[0][j:i]
48+
return wordlist[0][:i]
4949
return ""
5050

5151

@@ -181,10 +181,16 @@ def do(self) -> None:
181181
if completions_unchangable and len(completions[0]) == len(stem):
182182
r.msg = "[ sole completion ]"
183183
r.dirty = True
184-
r.insert(completions[0][len(stem):])
184+
stem_len = len(stem)
185+
del r.buffer[r.pos - stem_len:r.pos]
186+
r.pos -= stem_len
187+
r.insert(completions[0])
185188
else:
186189
p = prefix(completions, len(stem))
187190
if p:
191+
stem_len = len(stem)
192+
del r.buffer[r.pos - stem_len:r.pos]
193+
r.pos -= stem_len
188194
r.insert(p)
189195
if last_is_completer:
190196
r.cmpltn_menu_visible = True

Lib/test/test_pyrepl/test_pyrepl.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -936,6 +936,25 @@ def test_func(self):
936936
self.assertEqual(output, "dummy.test_func.__")
937937
self.assertEqual(mock_stderr.getvalue(), "")
938938

939+
def test_completion_replaces_input(self):
940+
matches = []
941+
def case_insensitive_completer(text, state):
942+
nonlocal matches
943+
candidates = ["PYTHON", "PYREPL"]
944+
if state == 0:
945+
matches = [c for c in candidates if c.lower().startswith(text.lower())]
946+
if state < len(matches):
947+
return matches[state]
948+
return None
949+
950+
events = code_to_events("s='pyt\t'\ns='py\t\tr\t'\n")
951+
reader = self.prepare_reader(events, {})
952+
reader.config.readline_completer = case_insensitive_completer
953+
output = multiline_input(reader)
954+
self.assertEqual(output, "s='PYTHON'")
955+
956+
output = multiline_input(reader)
957+
self.assertEqual(output, "s='PYREPL'")
939958

940959
class TestPyReplModuleCompleter(TestCase):
941960
def setUp(self):

0 commit comments

Comments
 (0)