diff --git a/kallithea/lib/utils2.py b/kallithea/lib/utils2.py --- a/kallithea/lib/utils2.py +++ b/kallithea/lib/utils2.py @@ -553,17 +553,22 @@ def time_to_datetime(tm): return return datetime.datetime.fromtimestamp(tm) -MENTIONS_REGEX = r'(?:^@|\s@)([a-zA-Z0-9]{1}[a-zA-Z0-9\-\_\.]+)(?:\s{1})' - +# Must match regexp in kallithea/public/js/base.js MentionsAutoComplete() +# Eat char before @ - it must not look like we are in an email addresses. +# Matching is gready so we don't have to look beyond the end. +MENTIONS_REGEX = re.compile(r'(?:^|[^a-zA-Z0-9])@([a-zA-Z0-9][-_.a-zA-Z0-9]*[a-zA-Z0-9])') def extract_mentioned_users(s): - """ + r""" Returns unique usernames from given string s that have @mention :param s: string to get mentions + + >>> extract_mentioned_users('@1-2.a_X,@1234 not@not @ddd@not @n @ee @ff @gg, @gg;@hh @n\n@zz,') + ['1-2.a_X', '1234', 'ddd', 'ee', 'ff', 'gg', 'hh', 'zz'] """ usrs = set() - for username in re.findall(MENTIONS_REGEX, s): + for username in MENTIONS_REGEX.findall(s): usrs.add(username) return sorted(list(usrs), key=lambda k: k.lower()) diff --git a/kallithea/public/js/base.js b/kallithea/public/js/base.js --- a/kallithea/public/js/base.js +++ b/kallithea/public/js/base.js @@ -1449,10 +1449,12 @@ var MentionsAutoComplete = function (div ownerAC.get_mention = function(msg, max_pos) { var org = msg; - var re = new RegExp('(?:^@|\s@)([a-zA-Z0-9]{1}[a-zA-Z0-9\-\_\.]+)$') + // Must match utils2.py MENTIONS_REGEX. + // Only matching on string up to cursor, so it must end with $ + var re = new RegExp('(?:^|[^a-zA-Z0-9])@([a-zA-Z0-9][-_.a-zA-Z0-9]*[a-zA-Z0-9])$') var chunks = []; - // cut first chunk until curret pos + // cut first chunk until current pos var to_max = msg.substr(0, max_pos); var at_pos = Math.max(0,to_max.lastIndexOf('@')-1); var msg2 = to_max.substr(at_pos); diff --git a/kallithea/tests/other/test_libs.py b/kallithea/tests/other/test_libs.py --- a/kallithea/tests/other/test_libs.py +++ b/kallithea/tests/other/test_libs.py @@ -113,9 +113,8 @@ class TestLibs(BaseTestCase): ) s = sorted([ - 'first', 'marcink', 'lukaszb', 'one_more22', 'MARCIN', 'maRCiN', 'john', - 'marian.user', 'marco-polo', 'marco_polo' - ], key=lambda k: k.lower()) + '2one_more22', 'first', 'marcink', 'lukaszb', 'one', 'one_more22', 'MARCIN', 'maRCiN', 'john', + 'marian.user', 'marco-polo', 'marco_polo'], key=lambda k: k.lower()) self.assertEqual(s, extract_mentioned_users(sample)) @parameterized.expand([