Skip to content

Commit 599c8b3

Browse files
committed
Add function to sort single underscore attributes before double underscore ones"
1 parent c08b42d commit 599c8b3

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

bpython/autocomplete.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ def matches(self, cursor_offset, line, **kwargs):
256256
if not r.word.split('.')[-1].startswith('_'):
257257
matches = set(match for match in matches
258258
if not match.split('.')[-1].startswith('_'))
259+
# MAYBE HERE 2 ??
259260
return matches
260261

261262
def locate(self, current_offset, line):
@@ -311,6 +312,8 @@ def attr_lookup(self, obj, expr, attr):
311312
for word in words:
312313
if self.method_match(word, n, attr) and word != "__builtins__":
313314
matches.append("%s.%s" % (expr, word))
315+
# print 'matches', matches
316+
# ORGANIZE THE THINGS HERE MAYBE 1??
314317
return matches
315318

316319
if py3:
@@ -543,13 +546,31 @@ def get_completer(completers, cursor_offset, line, **kwargs):
543546
complete_magic_methods is a bool of whether we ought to complete
544547
double underscore methods like __len__ in method signatures
545548
"""
546-
547549
for completer in completers:
548550
matches = completer.matches(cursor_offset, line, **kwargs)
549551
if matches is not None:
550-
return sorted(matches), (completer if matches else None)
552+
if len(matches) > 0:
553+
matches = sort_by_underscore(matches)
554+
return matches, (completer if matches else None)
551555
return [], None
552556

557+
def sort_by_underscore(matches):
558+
"""Sort single underscore attributes before double underscore ones.
559+
"""
560+
matches = sorted(matches)
561+
if len(matches) == 0 or len(matches) == 1:
562+
return matches
563+
one_underscore = None
564+
for i in xrange(1, len(matches)+1):
565+
i = -i
566+
dot_i = matches[i].rfind('.') + 1
567+
if matches[i][dot_i] == '_' and matches[i][dot_i+1] != '_':
568+
one_underscore = i
569+
else:
570+
break
571+
if one_underscore_i != None:
572+
return matches[one_underscore:] + matches[:one_underscore]
573+
return matches
553574

554575
def get_default_completer(mode=SIMPLE):
555576
return (

bpython/test/test_autocomplete.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ def test_first_completer_returns_None(self):
9090
b = completer(['a'])
9191
self.assertEqual(autocomplete.get_completer([a, b], 0, ''), (['a'], b))
9292

93+
def test_completer_shows_single_underscore_before_double(self):
94+
# TODO: do this!
95+
a = completer([])
96+
97+
9398

9499
class TestCumulativeCompleter(unittest.TestCase):
95100

@@ -263,6 +268,7 @@ def __getattr__(self, attr):
263268
self.com.matches(3, 'a._', locals_=locals_))
264269

265270

271+
266272
class TestMagicMethodCompletion(unittest.TestCase):
267273

268274
def test_magic_methods_complete_after_double_underscores(self):

0 commit comments

Comments
 (0)