From 599c8b31357e6ee79423847b07b005f7e414d37f Mon Sep 17 00:00:00 2001 From: Weston Vial Date: Tue, 8 Sep 2015 16:32:49 -0400 Subject: [PATCH 1/7] Add function to sort single underscore attributes before double underscore ones" --- bpython/autocomplete.py | 25 +++++++++++++++++++++++-- bpython/test/test_autocomplete.py | 6 ++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/bpython/autocomplete.py b/bpython/autocomplete.py index 754b3a394..25ee014fc 100644 --- a/bpython/autocomplete.py +++ b/bpython/autocomplete.py @@ -256,6 +256,7 @@ def matches(self, cursor_offset, line, **kwargs): if not r.word.split('.')[-1].startswith('_'): matches = set(match for match in matches if not match.split('.')[-1].startswith('_')) + # MAYBE HERE 2 ?? return matches def locate(self, current_offset, line): @@ -311,6 +312,8 @@ def attr_lookup(self, obj, expr, attr): for word in words: if self.method_match(word, n, attr) and word != "__builtins__": matches.append("%s.%s" % (expr, word)) +# print 'matches', matches + # ORGANIZE THE THINGS HERE MAYBE 1?? return matches if py3: @@ -543,13 +546,31 @@ def get_completer(completers, cursor_offset, line, **kwargs): complete_magic_methods is a bool of whether we ought to complete double underscore methods like __len__ in method signatures """ - for completer in completers: matches = completer.matches(cursor_offset, line, **kwargs) if matches is not None: - return sorted(matches), (completer if matches else None) + if len(matches) > 0: + matches = sort_by_underscore(matches) + return matches, (completer if matches else None) return [], None +def sort_by_underscore(matches): + """Sort single underscore attributes before double underscore ones. + """ + matches = sorted(matches) + if len(matches) == 0 or len(matches) == 1: + return matches + one_underscore = None + for i in xrange(1, len(matches)+1): + i = -i + dot_i = matches[i].rfind('.') + 1 + if matches[i][dot_i] == '_' and matches[i][dot_i+1] != '_': + one_underscore = i + else: + break + if one_underscore_i != None: + return matches[one_underscore:] + matches[:one_underscore] + return matches def get_default_completer(mode=SIMPLE): return ( diff --git a/bpython/test/test_autocomplete.py b/bpython/test/test_autocomplete.py index 8ec494a09..0a433eb30 100644 --- a/bpython/test/test_autocomplete.py +++ b/bpython/test/test_autocomplete.py @@ -90,6 +90,11 @@ def test_first_completer_returns_None(self): b = completer(['a']) self.assertEqual(autocomplete.get_completer([a, b], 0, ''), (['a'], b)) + def test_completer_shows_single_underscore_before_double(self): + # TODO: do this! + a = completer([]) + + class TestCumulativeCompleter(unittest.TestCase): @@ -263,6 +268,7 @@ def __getattr__(self, attr): self.com.matches(3, 'a._', locals_=locals_)) + class TestMagicMethodCompletion(unittest.TestCase): def test_magic_methods_complete_after_double_underscores(self): From 18442e3987f35f13eec0ae837931762352e96a56 Mon Sep 17 00:00:00 2001 From: Weston Vial Date: Thu, 10 Sep 2015 11:39:20 -0400 Subject: [PATCH 2/7] Sort single underscore attributes before double attributes in autocomplete --- bpython/test/test_autocomplete.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/bpython/test/test_autocomplete.py b/bpython/test/test_autocomplete.py index 0a433eb30..1e95a326c 100644 --- a/bpython/test/test_autocomplete.py +++ b/bpython/test/test_autocomplete.py @@ -88,12 +88,9 @@ def test_only_completer_returns_None(self): def test_first_completer_returns_None(self): a = completer(None) b = completer(['a']) + print '\n\n\n', b.matches, '\n\n\n' self.assertEqual(autocomplete.get_completer([a, b], 0, ''), (['a'], b)) - def test_completer_shows_single_underscore_before_double(self): - # TODO: do this! - a = completer([]) - class TestCumulativeCompleter(unittest.TestCase): @@ -268,7 +265,6 @@ def __getattr__(self, attr): self.com.matches(3, 'a._', locals_=locals_)) - class TestMagicMethodCompletion(unittest.TestCase): def test_magic_methods_complete_after_double_underscores(self): @@ -371,3 +367,6 @@ def func(apple, apricot, banana, carrot): set(['banana='])) self.assertSetEqual(com.matches(3, "car", argspec=argspec), set(['carrot='])) + +if __name__ == '__main__': + unittest.main() From d1b9c2bd306c06b488e4720cb33800fefeb161b0 Mon Sep 17 00:00:00 2001 From: Weston Vial Date: Thu, 10 Sep 2015 11:47:23 -0400 Subject: [PATCH 3/7] Remove comments --- bpython/autocomplete.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/bpython/autocomplete.py b/bpython/autocomplete.py index 25ee014fc..919c16aa6 100644 --- a/bpython/autocomplete.py +++ b/bpython/autocomplete.py @@ -256,7 +256,6 @@ def matches(self, cursor_offset, line, **kwargs): if not r.word.split('.')[-1].startswith('_'): matches = set(match for match in matches if not match.split('.')[-1].startswith('_')) - # MAYBE HERE 2 ?? return matches def locate(self, current_offset, line): @@ -312,8 +311,6 @@ def attr_lookup(self, obj, expr, attr): for word in words: if self.method_match(word, n, attr) and word != "__builtins__": matches.append("%s.%s" % (expr, word)) -# print 'matches', matches - # ORGANIZE THE THINGS HERE MAYBE 1?? return matches if py3: From f9c273c2e839f61b79cfabc3bfa22ab6a84210c0 Mon Sep 17 00:00:00 2001 From: Weston Vial Date: Thu, 10 Sep 2015 11:51:47 -0400 Subject: [PATCH 4/7] Remove accidental lines of code --- bpython/test/test_autocomplete.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/bpython/test/test_autocomplete.py b/bpython/test/test_autocomplete.py index 1e95a326c..8ec494a09 100644 --- a/bpython/test/test_autocomplete.py +++ b/bpython/test/test_autocomplete.py @@ -88,11 +88,9 @@ def test_only_completer_returns_None(self): def test_first_completer_returns_None(self): a = completer(None) b = completer(['a']) - print '\n\n\n', b.matches, '\n\n\n' self.assertEqual(autocomplete.get_completer([a, b], 0, ''), (['a'], b)) - class TestCumulativeCompleter(unittest.TestCase): def completer(self, matches, ): @@ -367,6 +365,3 @@ def func(apple, apricot, banana, carrot): set(['banana='])) self.assertSetEqual(com.matches(3, "car", argspec=argspec), set(['carrot='])) - -if __name__ == '__main__': - unittest.main() From e9f17967a39f9f2e58b175075516f158c530983c Mon Sep 17 00:00:00 2001 From: Weston Vial Date: Thu, 10 Sep 2015 12:10:09 -0400 Subject: [PATCH 5/7] Change doc string and fix variable name --- bpython/autocomplete.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/bpython/autocomplete.py b/bpython/autocomplete.py index 919c16aa6..cf859b2a6 100644 --- a/bpython/autocomplete.py +++ b/bpython/autocomplete.py @@ -552,7 +552,8 @@ def get_completer(completers, cursor_offset, line, **kwargs): return [], None def sort_by_underscore(matches): - """Sort single underscore attributes before double underscore ones. + """Returns a sorted list with single underscore attributes before double + underscore ones. """ matches = sorted(matches) if len(matches) == 0 or len(matches) == 1: @@ -565,7 +566,7 @@ def sort_by_underscore(matches): one_underscore = i else: break - if one_underscore_i != None: + if one_underscore != None: return matches[one_underscore:] + matches[:one_underscore] return matches From dd0f284cbe21337f696aa8ce319b8777c29b4f8f Mon Sep 17 00:00:00 2001 From: Weston Vial Date: Thu, 10 Sep 2015 12:25:00 -0400 Subject: [PATCH 6/7] Minor changes: moving variables around, etc --- bpython/autocomplete.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bpython/autocomplete.py b/bpython/autocomplete.py index cf859b2a6..5be716a1d 100644 --- a/bpython/autocomplete.py +++ b/bpython/autocomplete.py @@ -559,10 +559,10 @@ def sort_by_underscore(matches): if len(matches) == 0 or len(matches) == 1: return matches one_underscore = None + dot = matches[i].rfind('.') + 1 for i in xrange(1, len(matches)+1): i = -i - dot_i = matches[i].rfind('.') + 1 - if matches[i][dot_i] == '_' and matches[i][dot_i+1] != '_': + if matches[i][dot] == '_' and matches[i][dot+1] != '_': one_underscore = i else: break From 333527fab10342d9065581836ca2cc46a9bf1394 Mon Sep 17 00:00:00 2001 From: Weston Vial Date: Thu, 10 Sep 2015 12:50:51 -0400 Subject: [PATCH 7/7] Change function so it uses list comprehension instead of a for loop --- bpython/autocomplete.py | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/bpython/autocomplete.py b/bpython/autocomplete.py index 5be716a1d..307a530e7 100644 --- a/bpython/autocomplete.py +++ b/bpython/autocomplete.py @@ -558,17 +558,10 @@ def sort_by_underscore(matches): matches = sorted(matches) if len(matches) == 0 or len(matches) == 1: return matches - one_underscore = None - dot = matches[i].rfind('.') + 1 - for i in xrange(1, len(matches)+1): - i = -i - if matches[i][dot] == '_' and matches[i][dot+1] != '_': - one_underscore = i - else: - break - if one_underscore != None: - return matches[one_underscore:] + matches[:one_underscore] - return matches + dot = matches[0].rfind('.') + 1 + single = [m for m in matches if not m[dot:].startswith('__')] + double = [m for m in matches if m[dot:].startswith('__')] + return single + double def get_default_completer(mode=SIMPLE): return (