Skip to content

Commit

Permalink
boxes: Support autocomplete for stream + topic in message compose box.
Browse files Browse the repository at this point in the history
This commit adds a preliminary support for autocomplete:
  `#**stream name>Topic name`

The current approach uses regex for identifying the last occurence of
required format. The dedicated autocomplete function is:
  `autocomplete_stream_and_topic`

A follow-up to this might be to handle '#**stream name**>' cases too.

Ticks a checkbox in #448.
  • Loading branch information
Ezio-Sarthak committed Apr 21, 2021
1 parent 52fa932 commit 1164b75
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions zulipterminal/ui_tools/boxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@
from zulipterminal.model import Model


# Example text: #**stream name>Topic name
REGEX_AUTOCOMPLETE_STREAM_TOPIC = r"#\*\*([^*>]+)>([^*]*)$"


class WriteBox(urwid.Pile):
def __init__(self, view: Any) -> None:
super().__init__(self.main_view(True))
Expand Down Expand Up @@ -368,6 +372,15 @@ def generic_autocomplete(self, text: str, state: Optional[int]
prefix: text.rfind(prefix)
for prefix in autocomplete_map
}

# Matches autocomplete for stream + topic at the end of a text.
m = re.search(REGEX_AUTOCOMPLETE_STREAM_TOPIC, text)
if m:
autocomplete_map.update({
m.group(0): self.autocomplete_stream_and_topic
})
prefix_indices[m.group(0)] = m.start()

found_prefix_indices = {
prefix: index
for prefix, index in prefix_indices.items()
Expand Down Expand Up @@ -484,6 +497,27 @@ def autocomplete_streams(self, text: str, prefix_string: str
self.view.pinned_streams)
return matched_data

def autocomplete_stream_and_topic(self, text: str, prefix_string: str
) -> Tuple[List[str], List[str]]:
m = re.search(REGEX_AUTOCOMPLETE_STREAM_TOPIC, text)

stream = m.group(1) if m else ''

if self.model.is_valid_stream(stream):
stream_id = self.model.stream_id_from_name(stream)
topic_names = self.model.topics_in_stream(stream_id)
else:
topic_names = []

stream_prefix = f"#**{stream}>"
topic_suggestions = match_topics(topic_names,
text[len(stream_prefix):])

topic_typeaheads = format_string(topic_suggestions,
stream_prefix + '{}**')

return topic_typeaheads, topic_suggestions

def autocomplete_emojis(self, text: str, prefix_string: str
) -> Tuple[List[str], List[str]]:
emoji_list = list(self.model.active_emoji_data.keys())
Expand Down

0 comments on commit 1164b75

Please sign in to comment.