Skip to content

Commit 7b45033

Browse files
committed
boxes: Improve trigger sequence for generic_autocomplete.
This updates generic_autocomplete to trigger autocomplete functions even when there are some characters before the prefix_string. Tests amended. Fixes #541.
1 parent a07c87d commit 7b45033

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

tests/ui_tools/test_boxes.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ def test_generic_autocomplete_no_prefix(self, mocker, write_box, text,
8181
('@_', 3, None), # Reached last match
8282
('@_', 4, None), # Beyond end
8383
('@_', -1, '@_**Human 2**'),
84+
# When some character is preceding the prefix_string (trigger).
85+
('(@H', 0, '(@**Human Myself**'),
86+
('(@H', 1, '(@**Human 1**'),
87+
('>@_H', 0, '>@_**Human Myself**'),
88+
('>@_H', 1, '>@_**Human 1**'),
89+
('-@G', 0, '-@*Group 1*'),
90+
('-@G', 1, '-@*Group 2*'),
8491
])
8592
def test_generic_autocomplete_mentions(self, write_box, users_fixture,
8693
text, state, required_typeahead,
@@ -110,6 +117,8 @@ def test_generic_autocomplete_mentions(self, write_box, users_fixture,
110117
('#St', 0, '#**Stream 1**'),
111118
('#St', 1, '#**Stream 2**'),
112119
('#Stream 1', 0, '#**Stream 1**'),
120+
('[#Stream', 0, '[#**Stream 1**'),
121+
('(#Stream', 1, '(#**Stream 2**'),
113122
])
114123
def test_generic_autocomplete_streams(self, write_box, streams_fixture,
115124
text, state, required_typeahead):
@@ -139,6 +148,8 @@ def test_generic_autocomplete_streams(self, write_box, streams_fixture,
139148
(':', -1, ':smirk:'),
140149
(':nomatch', 0, None),
141150
(':nomatch', -1, None),
151+
('(:smi', 0, '(:smile:'),
152+
('&:smi', 1, '&:smiley:'),
142153
])
143154
def test_generic_autocomplete_emojis(self, write_box, emojis_fixture, text,
144155
mocker, state, required_typeahead):

zulipterminal/ui_tools/boxes.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,20 +101,22 @@ def generic_autocomplete(self, text: str, state: int) -> Optional[str]:
101101
'mentions': self.autocomplete_mentions,
102102
'streams': self.autocomplete_streams,
103103
}
104-
if text.startswith('@_'):
104+
if '@_' in text:
105105
form, prefix_string = 'mentions', '@_'
106-
elif text.startswith('@'):
106+
elif '@' in text:
107107
form, prefix_string = 'mentions', '@'
108-
elif text.startswith('#'):
108+
elif '#' in text:
109109
form, prefix_string = 'streams', '#'
110-
elif text.startswith(':'):
110+
elif ':' in text:
111111
form, prefix_string = 'emojis', ':'
112112
else:
113113
return text
114-
typeahead = autocomplete_forms[form](text, prefix_string)
114+
prefix_index = max(text.find(prefix_string), 0)
115+
typeahead = autocomplete_forms[form](text[prefix_index:],
116+
prefix_string)
115117

116118
try:
117-
return typeahead[state]
119+
return text[:prefix_index] + typeahead[state]
118120
except (IndexError, TypeError):
119121
return None
120122

0 commit comments

Comments
 (0)