Skip to content

Commit 8ce719c

Browse files
Julian BermanJulian Berman
Julian Berman
authored and
Julian Berman
committed
Complete magic methods within a class body (configurable).
1 parent c648c8c commit 8ce719c

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

bpython/config.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@
66
from bpython.keys import cli_key_dispatch as key_dispatch
77

88

9+
MAGIC_METHODS = ", ".join("__%s__" % s for s in [
10+
"init", "repr", "str", "lt", "le", "eq", "ne", "gt", "ge", "cmp", "hash",
11+
"nonzero", "unicode", "getattr", "setattr", "get", "set","call", "len",
12+
"getitem", "setitem", "iter", "reversed", "contains", "add", "sub", "mul",
13+
"floordiv", "mod", "divmod", "pow", "lshift", "rshift", "and", "xor", "or",
14+
"div", "truediv", "neg", "pos", "abs", "invert", "complex", "int", "float",
15+
"oct", "hex", "index", "coerce", "enter", "exit"]
16+
)
17+
918
class Struct(object):
1019
"""Simple class for instantiating objects we can add arbitrary attributes
1120
to and use for various arbitrary things."""
@@ -45,6 +54,8 @@ def loadini(struct, configfile):
4554
'arg_spec': True,
4655
'auto_display_list': True,
4756
'color_scheme': 'default',
57+
'complete_magic_methods' : True,
58+
'magic_methods' : MAGIC_METHODS,
4859
'dedent_after': 1,
4960
'flush_output': True,
5061
'highlight_show_source': True,
@@ -124,6 +135,11 @@ def loadini(struct, configfile):
124135
struct.cli_suggestion_width = config.getfloat('cli',
125136
'suggestion_width')
126137

138+
struct.complete_magic_methods = config.getboolean('general',
139+
'complete_magic_methods')
140+
methods = config.get('general', 'magic_methods')
141+
struct.magic_methods = [meth.strip() for meth in methods.split(",")]
142+
127143
struct.gtk_font = config.get('gtk', 'font')
128144

129145
color_scheme_name = config.get('general', 'color_scheme')

bpython/repl.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,11 @@ def complete(self, tab=False):
617617
e = True
618618
else:
619619
matches = self.completer.matches
620+
if (self.config.complete_magic_methods and self.buffer and
621+
self.buffer[0].startswith("class ") and
622+
self.current_line().lstrip().startswith("def ")):
623+
matches.extend(name for name in self.config.magic_methods
624+
if name.startswith(cw))
620625

621626
if not e and self.argspec:
622627
matches.extend(name + '=' for name in self.argspec[1][0]

0 commit comments

Comments
 (0)