Skip to content

Commit 41e8853

Browse files
committed
Implement backward completion on backward tab.
See issue #70.
1 parent f955ac1 commit 41e8853

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

bpython/cli.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,9 @@ def p_key(self, key):
830830
elif key == '\t':
831831
return self.tab()
832832

833+
elif key == 'KEY_BTAB':
834+
return self.tab(back=True)
835+
833836
elif len(key) == 1 and not unicodedata.category(key) == 'Cc':
834837
self.addstr(key)
835838
self.print_line(self.s)
@@ -1135,13 +1138,15 @@ def size(self):
11351138
self.h = h - 1
11361139
self.x = 0
11371140

1138-
def tab(self):
1141+
def tab(self, back=False):
11391142
"""Process the tab key being hit. If there's only whitespace
11401143
in the line or the line is blank then process a normal tab,
11411144
otherwise attempt to autocomplete to the best match of possible
1142-
choices in the match list."""
1145+
choices in the match list.
1146+
If `back` is True, walk backwards through the list of suggestions
1147+
and don't indent if there are only whitespace in the line."""
11431148

1144-
if self.atbol():
1149+
if self.atbol() and not back:
11451150
x_pos = len(self.s) - self.cpos
11461151
num_spaces = x_pos % OPTS.tab_length
11471152
if not num_spaces:
@@ -1177,7 +1182,10 @@ def tab(self):
11771182
if not expanded and self.matches:
11781183
if self.matches_iter:
11791184
self.s = self.s[:-len(self.matches_iter.current())] + cw
1180-
current_match = self.matches_iter.next()
1185+
if back:
1186+
current_match = self.matches_iter.previous()
1187+
else:
1188+
current_match = self.matches_iter.next()
11811189
if current_match:
11821190
try:
11831191
self.show_list(self.matches, self.argspec, current_match)

bpython/repl.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,10 @@ def next(self):
213213
self.index = (self.index + 1) % len(self.matches)
214214
return self.matches[self.index]
215215

216+
def previous(self):
217+
self.index = (self.index - 1) % len(self.matches)
218+
return self.matches[self.index]
219+
216220
def update(self, current_word='', matches=[]):
217221
if current_word != self.current_word:
218222
self.current_word = current_word

0 commit comments

Comments
 (0)