Merge pull request #4058 from Vane11ope/vane11ope/fix

List was not cycled right for tab auto-completion
This commit is contained in:
Maximilian Hils 2020-07-05 04:04:44 +02:00 committed by GitHub
commit 65318603ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 8 deletions

View File

@ -28,15 +28,17 @@ class ListCompleter(Completer):
if o.startswith(start): if o.startswith(start):
self.options.append(o) self.options.append(o)
self.options.sort() self.options.sort()
self.offset = 0 self.pos = -1
def cycle(self, forward: bool = True) -> str: def cycle(self, forward: bool = True) -> str:
if not self.options: if not self.options:
return self.start return self.start
ret = self.options[self.offset] if self.pos == -1:
delta = 1 if forward else -1 self.pos = 0 if forward else len(self.options) - 1
self.offset = (self.offset + delta) % len(self.options) else:
return ret delta = 1 if forward else -1
self.pos = (self.pos + delta) % len(self.options)
return self.options[self.pos]
class CompletionState(typing.NamedTuple): class CompletionState(typing.NamedTuple):

View File

@ -31,23 +31,35 @@ class TestListCompleter:
[ [
"", "",
["a", "b", "c"], ["a", "b", "c"],
["a", "b", "c", "a"] ["a", "b", "c", "a"],
["c", "b", "a", "c"],
["a", "c", "a", "c"]
], ],
[ [
"xxx", "xxx",
["a", "b", "c"], ["a", "b", "c"],
["xxx", "xxx", "xxx"],
["xxx", "xxx", "xxx"],
["xxx", "xxx", "xxx"] ["xxx", "xxx", "xxx"]
], ],
[ [
"b", "b",
["a", "b", "ba", "bb", "c"], ["a", "b", "ba", "bb", "c"],
["b", "ba", "bb", "b"] ["b", "ba", "bb", "b"],
["bb", "ba", "b", "bb"],
["b", "bb", "b", "bb"]
], ],
] ]
for start, opts, cycle in tests: for start, opts, cycle, cycle_reverse, cycle_mix in tests:
c = commander.ListCompleter(start, opts) c = commander.ListCompleter(start, opts)
for expected in cycle: for expected in cycle:
assert c.cycle() == expected assert c.cycle() == expected
for expected in cycle_reverse:
assert c.cycle(False) == expected
forward = True
for expected in cycle_mix:
assert c.cycle(forward) == expected
forward = not forward
class TestCommandEdit: class TestCommandEdit: