Skip to content

Commit faab6a8

Browse files
committed
update tab method to support new complete types
1 parent c100ece commit faab6a8

File tree

5 files changed

+243
-112
lines changed

5 files changed

+243
-112
lines changed

bpython/autocomplete.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,15 @@
2727
import re
2828
from bpython import inspection
2929

30-
30+
# Needed for special handling of __abstractmethods__
31+
# abc only exists since 2.6, so check both that it exists and that it's
32+
# the one we're expecting
33+
try:
34+
import abc
35+
abc.ABCMeta
36+
has_abc = True
37+
except (ImportError, AttributeError):
38+
has_abc = False
3139

3240
class Autocomplete(rlcompleter.Completer):
3341
"""
@@ -102,7 +110,7 @@ def global_matches(self, text):
102110
for word in keyword.kwlist:
103111
if self.method_match(word, n, text):
104112
hash[word] = 1
105-
for nspace in [__builtin__.__dict__, __main__.__dict__]:
113+
for nspace in [__builtin__.__dict__, self.namespace]:
106114
for word, val in nspace.items():
107115
if self.method_match(word, len(text), text) and word != "__builtins__":
108116
hash[self._callable_postfix(val, word)] = 1
@@ -111,8 +119,11 @@ def global_matches(self, text):
111119
return matches
112120

113121
def method_match(self, word, size, text):
114-
if self.autocomplete_mode == "1":
122+
if self.autocomplete_mode == 1:
115123
return word[:size] == text
124+
elif self.autocomplete_mode == 2:
125+
s = r'.*%s.*' % text
126+
return re.search(s, word)
116127
else:
117128
s = r'.*%s.*' % '.*'.join(list(text))
118129
return re.search(s, word)

bpython/cli.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,7 +1089,7 @@ def repl(self):
10891089
"""Initialise the repl and jump into the loop. This method also has to
10901090
keep a stack of lines entered for the horrible "undo" feature. It also
10911091
tracks everything that would normally go to stdout in the normal Python
1092-
interpreter so it can quickly write it to stdout on exit after
1092+
interpreter so it can quickly write it to st on exit after
10931093
curses.endwin(), as well as a history of lines entered for using
10941094
up/down to go back and forth (which has to be separate to the
10951095
evaluation history, which will be truncated when undoing."""
@@ -1404,6 +1404,8 @@ def tab(self, back=False):
14041404
and don't indent if there are only whitespace in the line.
14051405
"""
14061406

1407+
mode = self.config.autocomplete_mode
1408+
14071409
# 1. check if we should add a tab character
14081410
if self.atbol() and not back:
14091411
x_pos = len(self.s) - self.cpos
@@ -1427,21 +1429,29 @@ def tab(self, back=False):
14271429
else:
14281430
cw = self.matches_iter.current_word
14291431

1430-
# check to see if we can expand the current word
1431-
b = os.path.commonprefix(self.matches)
1432-
if b and self.config.autocomplete_mode == 1:
1433-
expanded_string = b[len(cw):]
1432+
# 3. check to see if we can expand the current word
1433+
cseq = None
1434+
if mode == 2:
1435+
if all([len(match.split(cw)) == 2 for match in self.matches]):
1436+
seq = [cw + match.split(cw)[1] for match in self.matches]
1437+
cseq = os.path.commonprefix(seq)
1438+
else:
1439+
seq = self.matches
1440+
cseq = os.path.commonprefix(seq)
1441+
1442+
if cseq and mode != 3:
1443+
expanded_string = cseq[len(cw):]
14341444
self.s += expanded_string
14351445
expanded = bool(expanded_string)
14361446
self.print_line(self.s)
14371447
if len(self.matches) == 1 and self.config.auto_display_list:
14381448
self.scr.touchwin()
14391449
if expanded:
1440-
self.matches_iter.update(b, self.matches)
1450+
self.matches_iter.update(cseq, self.matches)
14411451
else:
14421452
expanded = False
14431453

1444-
# swap current word for a match list item
1454+
# 4. swap current word for a match list item
14451455
if not expanded and self.matches:
14461456
# reset s if this is the nth result
14471457
if self.matches_iter:
@@ -1464,7 +1474,7 @@ def tab(self, back=False):
14641474
if self.config.autocomplete_mode == 1:
14651475
self.s += current_match[len(cw):]
14661476
else:
1467-
self.s = current_match
1477+
self.s = self.s[:-len(cw)] + current_match
14681478

14691479
self.print_line(self.s, True)
14701480
return True

bpython/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def loadini(struct, configfile):
148148
'complete_magic_methods')
149149
methods = config.get('general', 'magic_methods')
150150
struct.magic_methods = [meth.strip() for meth in methods.split(",")]
151-
struct.autocomplete_mode = config.get('general', 'autocomplete_mode')
151+
struct.autocomplete_mode = config.getint('general', 'autocomplete_mode')
152152

153153
struct.gtk_font = config.get('gtk', 'font')
154154

bpython/repl.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,6 @@
4949
from bpython.formatter import Parenthesis
5050
from bpython.autocomplete import Autocomplete
5151

52-
# Needed for special handling of __abstractmethods__
53-
# abc only exists since 2.6, so check both that it exists and that it's
54-
# the one we're expecting
55-
try:
56-
import abc
57-
abc.ABCMeta
58-
has_abc = True
59-
except (ImportError, AttributeError):
60-
has_abc = False
61-
6252
py3 = sys.version_info[0] == 3
6353

6454

0 commit comments

Comments
 (0)