Skip to content

Commit c2e4ed3

Browse files
committed
boxes: Improve trigger sequence for generic_autocomplete.
Updated generic_autocomplete along with its autocomplete_mentions/ streams to provide typeahead even when there are some characters before their respective trigger character. Tests amended. Fixes #541.
1 parent 397b128 commit c2e4ed3

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

tests/ui_tools/test_boxes.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ def test_init(self, write_box):
2929
('@_Boo', '@_', 1),
3030
('Plain Text', '', 0),
3131
('Plain Text', '', 1),
32+
('(@Boo', '@', 0),
33+
('(@_Boo', '@_', 0),
34+
('(', '', 0),
3235
])
3336
def test_generic_autocomplete(self, mocker, write_box, text,
3437
prefix_string, state):
@@ -93,6 +96,13 @@ def test_generic_autocomplete(self, mocker, write_box, text,
9396
('@_', 3, '@_', None), # Reached last match
9497
('@_', 4, '@_', None), # Beyond end
9598
('@_', -1, '@_', '@_**Human 2**'),
99+
# When some character is preceding the prefix_string (trigger).
100+
('(@H', 0, '@', '(@**Human Myself**'),
101+
('(@H', 1, '@', '(@**Human 1**'),
102+
('(@_H', 0, '@_', '(@_**Human Myself**'),
103+
('(@_H', 1, '@_', '(@_**Human 1**'),
104+
('(@G', 0, '@', '(@*Group 1*'),
105+
('(@G', 1, '@', '(@*Group 2*'),
96106
])
97107
def test_autocomplete_mentions(self, write_box, users_fixture,
98108
text, state, prefix_string,
@@ -125,7 +135,9 @@ def test_autocomplete_mentions(self, write_box, users_fixture,
125135
('#St', 1, '#', '#**Stream 2**'),
126136
('#Stream 1', 0, '#', '#**Stream 1**'),
127137
('No match', 0, '#', None),
128-
('No match', -1, '#', None)
138+
('No match', -1, '#', None),
139+
('(#Stream', 0, '#', '(#**Stream 1**'),
140+
('(#Stream', 1, '#', '(#**Stream 2**'),
129141
])
130142
def test_autocomplete_streams(self, write_box, streams_fixture,
131143
text, state, prefix_string,

zulipterminal/ui_tools/boxes.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,11 @@ def stream_box_view(self, caption: str='', title: str='') -> None:
9494
self.contents = write_box
9595

9696
def generic_autocomplete(self, text: str, state: int) -> Optional[str]:
97-
if text.startswith('@_'):
97+
if '@_' in text:
9898
return self.autocomplete_mentions(text, state, '@_')
99-
elif text.startswith('@'):
99+
elif '@' in text:
100100
return self.autocomplete_mentions(text, state, '@')
101-
elif text.startswith('#'):
101+
elif '#' in text:
102102
return self.autocomplete_streams(text, state, '#')
103103
else:
104104
return text
@@ -107,28 +107,33 @@ def autocomplete_mentions(self, text: str, state: int,
107107
prefix_string: str) -> Optional[str]:
108108
# Handles user mentions (@ mentions and silent mentions)
109109
# and group mentions.
110+
prefix_index = max(text.find(prefix_string), 0)
110111
group_typeahead = [prefix_string+'*{}*'.format(group_name)
111112
for group_name in self.model.user_group_names
112-
if match_groups(group_name, text[1:])]
113+
if match_groups(group_name, text[prefix_index+1:])]
113114

114115
users_list = self.view.users
115116
user_typeahead = [prefix_string+'**{}**'.format(user['full_name'])
116117
for user in users_list
117-
if match_user(user, text[len(prefix_string):])]
118+
if match_user(
119+
user, text[len(prefix_string)+prefix_index:])]
118120
combined_typeahead = group_typeahead + user_typeahead
119121
try:
120-
return combined_typeahead[state]
122+
return text[:prefix_index] + combined_typeahead[state]
121123
except (IndexError, TypeError):
122124
return None
123125

124126
def autocomplete_streams(self, text: str, state: int,
125127
prefix_string: str) -> Optional[str]:
128+
prefix_index = max(text.find(prefix_string), 0)
126129
streams_list = self.view.pinned_streams + self.view.unpinned_streams
127130
stream_typeahead = [prefix_string+'**{}**'.format(stream[0])
128131
for stream in streams_list
129-
if match_stream(stream, text[len(prefix_string):])]
132+
if match_stream(
133+
stream, text[len(prefix_string)
134+
+ prefix_index:])]
130135
try:
131-
return stream_typeahead[state]
136+
return text[:prefix_index] + stream_typeahead[state]
132137
except (IndexError, TypeError):
133138
return None
134139

0 commit comments

Comments
 (0)