Skip to content

Commit 1e1891a

Browse files
committed
cache arguments and decrease frequency of _complete_from_full_parse
1 parent 940c6e8 commit 1e1891a

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

awsshell/autocomplete.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def __init__(self, index_data, match_fuzzy=True):
2424
# This will get populated as a command is completed.
2525
self.cmd_path = [self._current_name]
2626
self.match_fuzzy = match_fuzzy
27+
self._cache_all_args = []
2728

2829
@property
2930
def global_arg_metadata(self):
@@ -42,6 +43,7 @@ def reset(self):
4243
self._last_position = 0
4344
self.last_option = ''
4445
self.cmd_path = [self._current_name]
46+
self._cache_all_args = []
4547

4648
def autocomplete(self, line):
4749
"""Given a line, return a list of suggestions."""
@@ -57,7 +59,9 @@ def autocomplete(self, line):
5759
return self._handle_backspace()
5860
elif not line:
5961
return []
60-
elif current_length != self._last_position + 1:
62+
elif self._last_position == 0 and current_length > 2:
63+
return self._complete_from_full_parse()
64+
elif current_length != self._last_position + 1 and '--' in line:
6165
return self._complete_from_full_parse()
6266

6367
# This position is important. We only update the _last_position
@@ -75,6 +79,7 @@ def autocomplete(self, line):
7579
# this as self.last_arg
7680
self.last_option = last_word
7781
if line[-1] == ' ':
82+
self._cache_all_args = []
7883
# At this point the user has autocompleted a command
7984
# or an argument and has hit space. If they've
8085
# just completed a command, we need to change the
@@ -101,14 +106,12 @@ def autocomplete(self, line):
101106
# in either of the above two cases.
102107
return self._current['commands'][:]
103108
elif last_word.startswith('-'):
104-
# TODO: cache this for the duration of the current context.
105-
# We don't need to recompute this until the args are
106-
# different.
107-
all_args = self._get_all_args()
109+
if not self._cache_all_args:
110+
self._cache_all_args = self._get_all_args()
108111
if self.match_fuzzy:
109-
return fuzzy_search(last_word, all_args)
112+
return fuzzy_search(last_word, self._cache_all_args)
110113
else:
111-
return substring_search(last_word, all_args)
114+
return substring_search(last_word, self._cache_all_args)
112115
if self.match_fuzzy:
113116
return fuzzy_search(last_word, self._current['commands'])
114117
else:

0 commit comments

Comments
 (0)