diff --git a/zulipterminal/ui_tools/boxes.py b/zulipterminal/ui_tools/boxes.py index fbdbe88e68..a753613908 100644 --- a/zulipterminal/ui_tools/boxes.py +++ b/zulipterminal/ui_tools/boxes.py @@ -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)) @@ -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() @@ -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())