Skip to content

Commit fd546ec

Browse files
committed
Sorting matches earlier
1 parent cba3dd1 commit fd546ec

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

cmd2.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2065,6 +2065,13 @@ def complete(self, text, state):
20652065

20662066
if len(self.completion_matches) > 0:
20672067

2068+
# Eliminate duplicates
2069+
matches_set = set(self.completion_matches)
2070+
self.completion_matches = list(matches_set)
2071+
2072+
display_matches_set = set(self.display_matches)
2073+
self.display_matches = list(display_matches_set)
2074+
20682075
# Get the token being completed as it appears on the command line
20692076
raw_completion_token = raw_tokens[-1]
20702077

@@ -2103,15 +2110,6 @@ def complete(self, text, state):
21032110
strs_to_match = alias_names + visible_commands
21042111
self.completion_matches = self.basic_complete(text, line, begidx, endidx, strs_to_match)
21052112

2106-
# Eliminate duplicates and sort
2107-
matches_set = set(self.completion_matches)
2108-
self.completion_matches = list(matches_set)
2109-
self.completion_matches.sort()
2110-
2111-
display_matches_set = set(self.display_matches)
2112-
self.display_matches = list(display_matches_set)
2113-
self.display_matches.sort()
2114-
21152113
# Handle single result
21162114
if len(self.completion_matches) == 1:
21172115
str_to_append = ''
@@ -2126,6 +2124,11 @@ def complete(self, text, state):
21262124

21272125
self.completion_matches[0] += str_to_append
21282126

2127+
# Otherwise sort matches
2128+
elif len(self.completion_matches) > 0:
2129+
self.completion_matches.sort()
2130+
self.display_matches.sort()
2131+
21292132
try:
21302133
return self.completion_matches[state]
21312134
except IndexError:

0 commit comments

Comments
 (0)