Skip to content

Commit

Permalink
boxes: Enable autocomplete for PM recipients.
Browse files Browse the repository at this point in the history
Currently, the recipients box has no autocomplete for users names.
This commit adds a method to autocomplete names appended with their
corresponding emails and enables it. The typeahead autocompletes the
names with their emails, but only names are shown in the suggestions.

Tests added.
  • Loading branch information
prah23 committed Jan 17, 2021
1 parent 67ec395 commit 83f5d58
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
46 changes: 46 additions & 0 deletions tests/ui_tools/test_boxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,52 @@ def test_generic_autocomplete_emojis(self, write_box, text,
typeahead_string = write_box.generic_autocomplete(text, state)
assert typeahead_string == required_typeahead

@pytest.mark.parametrize(['text', 'matching_users',
'matching_users_info'], [
('', [
'Human Myself',
'Human 1',
'Human 2'
], [
'Human Myself <[email protected]>',
'Human 1 <[email protected]>',
'Human 2 <[email protected]>'
]),
('My', ['Human Myself'],
['Human Myself <[email protected]>']),
], ids=[
'no_search_text',
'single_word_search_text',
])
def test__to_box_autocomplete(self, mocker, write_box, text,
users_fixture, matching_users,
matching_users_info, state=1):
_process_typeaheads = mocker.patch(BOXES
+ '.WriteBox._process_typeaheads')

write_box._to_box_autocomplete(text, state)

_process_typeaheads.assert_called_once_with(matching_users_info, state,
matching_users)

@pytest.mark.parametrize('text, expected_text', [
('Hu', 'Human Myself <[email protected]>'),
('Human M', 'Human Myself <[email protected]>')
])
def test__to_box_autocomplete_with_spaces(self, write_box, text,
expected_text, widget_size):
write_box.private_box_view(email='[email protected]',
recipient_user_ids=[1])
write_box.to_write_box.set_edit_text(text)
write_box.to_write_box.set_edit_pos(len(text))
write_box.focus_position = write_box.FOCUS_CONTAINER_HEADER
size = widget_size(write_box)

write_box.keypress(size, primary_key_for_command('AUTOCOMPLETE'))

assert (write_box.to_write_box.edit_text
== expected_text)

@pytest.mark.parametrize(['text', 'state', 'to_pin', 'matching_streams'], [
('', 1, [], ['Secret stream', 'Some general stream',
'Stream 1', 'Stream 2']),
Expand Down
22 changes: 22 additions & 0 deletions zulipterminal/ui_tools/boxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ def private_box_view(self, button: Any=None, email: str='',

self.to_write_box = ReadlineEdit("To: ",
edit_text=recipient_info)
self.to_write_box.enable_autocomplete(
func=self._to_box_autocomplete,
key=primary_key_for_command('AUTOCOMPLETE'),
key_reverse=primary_key_for_command('AUTOCOMPLETE_REVERSE')
)
self.to_write_box.set_completer_delims("")

self.msg_write_box = ReadlineEdit(multiline=True)
self.msg_write_box.enable_autocomplete(
Expand Down Expand Up @@ -178,6 +184,22 @@ def _set_stream_write_box_style(self, widget: ReadlineEdit,
(self.header_write_box[self.FOCUS_HEADER_PREFIX_STREAM]
.set_text((color, stream_marker)))

def _to_box_autocomplete(self, text: str, state: Optional[int]
) -> Optional[str]:
users_list = self.view.users

matching_users = [user
for user in users_list
if match_user(user, text)]

user_names_and_emails = ["{} <{}>".format(user['full_name'],
user['email']) for user in matching_users]

user_names = [user['full_name'] for user in matching_users]

return self._process_typeaheads(user_names_and_emails, state,
user_names)

def _topic_box_autocomplete(self, text: str, state: Optional[int]
) -> Optional[str]:
topic_names = self.model.topics_in_stream(self.stream_id)
Expand Down

0 comments on commit 83f5d58

Please sign in to comment.