Skip to content

Commit

Permalink
boxes/regexes: Add stream fences handling in stream+topic autocomplete.
Browse files Browse the repository at this point in the history
This commit improves support for stream+topic autocomplete done in previous
commit to include stream fences case: `#**stream name**>Topic name`.
                                                     ^^
The current approach uses regex for identifying the last occurence of
required format containing stream fences. The dedicated autocomplete
function remains the same: `autocomplete_stream_and_topic`.

Tests added.

Ticks a checkbox in #448.
  • Loading branch information
Ezio-Sarthak committed Jul 25, 2021
1 parent 8879faf commit 191e664
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
4 changes: 4 additions & 0 deletions tests/ui_tools/test_boxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,12 @@ def test_not_calling_typing_method_without_recipients(self, mocker, write_box):
("#**Stream 1>", 1, True, "#**Stream 1>This is a topic**"),
("#**Stream 1>", -1, True, "#**Stream 1>Hello there!**"),
("#**Stream 1>", -2, True, "#**Stream 1>This is a topic**"),
# Fenced prefix
("#**Stream 1**>T", 0, True, "#**Stream 1>Topic 1**"),
# Invalid stream
("#**invalid stream>", 0, False, None),
# Invalid prefix format
("#**Stream 1*>", 0, True, None),
("#*Stream 1>", 0, True, None),
("#Stream 1>", 0, True, None),
# Complex autocomplete prefixes.
Expand All @@ -95,6 +98,7 @@ def test_not_calling_typing_method_without_recipients(self, mocker, write_box):
("@#**Stream 1>", 0, True, "@#**Stream 1>Topic 1**"),
("@_#**Stream 1>", 0, True, "@_#**Stream 1>Topic 1**"),
(":#**Stream 1>", 0, True, ":#**Stream 1>Topic 1**"),
("(#**Stream 1**>", 0, True, "(#**Stream 1>Topic 1**"),
],
)
def test_generic_autocomplete_stream_and_topic(
Expand Down
5 changes: 5 additions & 0 deletions zulipterminal/config/regexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@
REGEX_STREAM_FENCED_HALF,
REGEX_TOPIC_NAME,
)
# Example text: #**stream name**>Topic name
REGEX_AUTOCOMPLETE_STREAM_TOPIC_FENCED = r"{}>{}$".format(
REGEX_STREAM_FENCED,
REGEX_TOPIC_NAME,
)
16 changes: 15 additions & 1 deletion zulipterminal/ui_tools/boxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
keys_for_command,
primary_key_for_command,
)
from zulipterminal.config.regexes import REGEX_AUTOCOMPLETE_STREAM_TOPIC
from zulipterminal.config.regexes import (
REGEX_AUTOCOMPLETE_STREAM_TOPIC,
REGEX_AUTOCOMPLETE_STREAM_TOPIC_FENCED,
)
from zulipterminal.config.symbols import (
INVALID_MARKER,
MESSAGE_CONTENT_MARKER,
Expand Down Expand Up @@ -469,10 +472,21 @@ def generic_autocomplete(self, text: str, state: Optional[int]) -> Optional[str]

# Matches autocomplete for stream + topic at the end of a text.
match = re.search(REGEX_AUTOCOMPLETE_STREAM_TOPIC, text)
match_fenced = re.search(REGEX_AUTOCOMPLETE_STREAM_TOPIC_FENCED, text)
if match:
prefix = f"#**{match.group(1)}>"
autocomplete_map.update({prefix: self.autocomplete_stream_and_topic})
prefix_indices[prefix] = match.start()
elif match_fenced:
# Amending the prefix to remove stream fence `**`
prefix = f"#**{match_fenced.group(1)}>"
prefix_with_topic = prefix + match_fenced.group(2)

autocomplete_map.update({prefix: self.autocomplete_stream_and_topic})
prefix_indices[prefix] = match_fenced.start()

# Amending the text to have new prefix (without `**` fence)
text = text[: match_fenced.start()] + prefix_with_topic

found_prefix_indices = {
prefix: index for prefix, index in prefix_indices.items() if index > -1
Expand Down

0 comments on commit 191e664

Please sign in to comment.