Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions Lib/_pyrepl/completing_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ def prefix(wordlist: list[str], j: int = 0) -> str:
for word in wordlist:
d[word[i]] = 1
if len(d) > 1:
return wordlist[0][j:i]
return wordlist[0][:i]
i += 1
d = {}
except IndexError:
return wordlist[0][j:i]
return wordlist[0][:i]
return ""


Expand Down Expand Up @@ -181,10 +181,16 @@ def do(self) -> None:
if completions_unchangable and len(completions[0]) == len(stem):
r.msg = "[ sole completion ]"
r.dirty = True
r.insert(completions[0][len(stem):])
stem_len = len(stem)
del r.buffer[r.pos - stem_len:r.pos]
r.pos -= stem_len
r.insert(completions[0])
else:
p = prefix(completions, len(stem))
if p:
stem_len = len(stem)
del r.buffer[r.pos - stem_len:r.pos]
r.pos -= stem_len
r.insert(p)
if last_is_completer:
r.cmpltn_menu_visible = True
Expand Down
19 changes: 19 additions & 0 deletions Lib/test/test_pyrepl/test_pyrepl.py
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,25 @@ def test_func(self):
self.assertEqual(output, "dummy.test_func.__")
self.assertEqual(mock_stderr.getvalue(), "")

def test_completion_replaces_input(self):
matches = []
def case_insensitive_completer(text, state):
nonlocal matches
candidates = ["PYTHON", "PYREPL"]
if state == 0:
matches = [c for c in candidates if c.lower().startswith(text.lower())]
if state < len(matches):
return matches[state]
return None

events = code_to_events("s='pyt\t'\ns='py\t\tr\t'\n")
reader = self.prepare_reader(events, {})
reader.config.readline_completer = case_insensitive_completer
output = multiline_input(reader)
self.assertEqual(output, "s='PYTHON'")

output = multiline_input(reader)
self.assertEqual(output, "s='PYREPL'")

class TestPyReplModuleCompleter(TestCase):
def setUp(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix :mod:`!_pyrepl` completion to replace input stem instead of appending.
Loading