Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

composebox_typeahead: Don't treat punctuation after a link as a topic.

Shubham Padiae26e967e714a
diff --git a/web/src/composebox_typeahead.ts b/web/src/composebox_typeahead.ts
index f031affe895..73914043b92 100644
--- a/web/src/composebox_typeahead.ts
+++ b/web/src/composebox_typeahead.ts
@@ -52,6 +52,11 @@ import * as util from "./util.ts";
/* Maximum channel name length + link syntax (#**>**) + some topic characters */
const MAX_LOOKBACK_FOR_TYPEAHEAD_COMPLETION = 60 + 6 + 20;
+function starts_with_terminating_punctuation(link_tokens: RegExpExecArray | null): boolean {
+ const query = link_tokens?.at(-1) ?? "";
+ return /^\p{P}/u.test(query);
+}
+
// **********************************
// AN IMPORTANT NOTE ABOUT TYPEAHEADS
// **********************************
@@ -1142,6 +1147,8 @@ export function get_candidates(
// completion. The query must start immediately after the link —
// leading whitespace is rejected so an older link on the line
// can't match across message text and suppress other typeaheads.
+ // A query that starts with punctuation is not a topic, see
+ // starts_with_terminating_punctuation.
// It excludes `>` to stay non-overlapping with topic_jump above.
const complete_stream_regex = /#\*\*([^*>]+)\*\*((?!\s)[^*>\n]*)$/;
// Matches the '[#channel](url)' fallback link for special characters.
@@ -1156,12 +1163,18 @@ export function get_candidates(
// after picking a channel), not when the cursor lands after an
// existing link.
const typeahead_already_shown = compose_ui.compose_textarea_typeahead?.shown ?? false;
- const complete_channel_link_tokens = typeahead_already_shown
+ let complete_channel_link_tokens = typeahead_already_shown
? complete_stream_regex.exec(split[0])
: null;
- const complete_fallback_link_tokens = typeahead_already_shown
+ let complete_fallback_link_tokens = typeahead_already_shown
? complete_fallback_stream_regex.exec(split[0])
: null;
+ if (starts_with_terminating_punctuation(complete_channel_link_tokens)) {
+ complete_channel_link_tokens = null;
+ }
+ if (starts_with_terminating_punctuation(complete_fallback_link_tokens)) {
+ complete_fallback_link_tokens = null;
+ }
const tokens =
partial_stream_topic_tokens ?? // #**channel>topic
topic_shortcut_tokens ?? // #>topic
diff --git a/web/tests/composebox_typeahead.test.cjs b/web/tests/composebox_typeahead.test.cjs
index 04c8ad54e24..d7e4e060eb4 100644
--- a/web/tests/composebox_typeahead.test.cjs
+++ b/web/tests/composebox_typeahead.test.cjs
@@ -2986,6 +2986,47 @@ test("begins_typeahead", ({override, override_rewire}) => {
"#**Sweden**totally new topic",
typed_topics("Sweden", ["totally new topic"], is_new_topic),
);
+ // Punctuation right after the complete link ends the sentence rather
+ // than starting a topic.
+ for (const punctuation of [
+ ",",
+ ".",
+ ":",
+ ";",
+ "?",
+ "!",
+ "-",
+ "(",
+ ")",
+ "[",
+ "]",
+ '"',
+ "'",
+ "@",
+ // Chinese and Japanese full stop.
+ "。",
+ // Chinese comma.
+ ",",
+ "¿",
+ "—",
+ ]) {
+ assert_typeahead_equals(`#**Sweden**${punctuation}`, []);
+ }
+ // Symbols are not punctuation, so they still start a topic.
+ assert_typeahead_equals("#**Sweden**~", typed_topics("Sweden", ["~"], is_new_topic));
+ assert_typeahead_equals("#**Sweden**. Now see", []);
+ assert.deepEqual(
+ get_values("#**Sweden**. Now see #den", "").map((item) => item.name),
+ ["Denmark", "Sweden"],
+ );
+ // The same applies to the markdown fallback link form.
+ assert.deepEqual(
+ get_values("[#A* Algorithm](#narrow/channel/6-A.2A-Algorithm)", "").map(
+ (item) => item.topic,
+ ),
+ ["A* Algorithm"],
+ );
+ assert_typeahead_equals("[#A* Algorithm](#narrow/channel/6-A.2A-Algorithm).", []);
// A space immediately after the complete link means the user has
// moved on to the message body: the complete-link pattern doesn't
// match, and with no other token present there are no suggestions.