Skip to content

Show single underscore attributes before double underscore ones #528 #561

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions bpython/autocomplete.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,13 +543,25 @@ 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):
"""Returns a sorted list with single underscore attributes before double
underscore ones.
"""
matches = sorted(matches)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't really matter, but I'd prefer matches.sort() instead of reassigning to the variables - I guess this keeps the matches list being passed in safe from modifications, but if we're doing this we should change the docstring from "sort asdfasdf" to "Returns a sorted list" because sort implies mutating the list imo.

if len(matches) == 0 or len(matches) == 1:
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 (
Expand Down