Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
typehead_helper: Show a maximum of one alias per language.
Sumanth V Rao386ddf15178e
diff --git a/frontend_tests/node_tests/typeahead_helper.js b/frontend_tests/node_tests/typeahead_helper.jsindex 485744de746..48e0126262d 100644--- a/frontend_tests/node_tests/typeahead_helper.js+++ b/frontend_tests/node_tests/typeahead_helper.js@@ -285,13 +285,19 @@ test("sort_languages", () => {// Sort according to priority only.test_langs = th.sort_languages(test_langs, "jav");- assert.deepEqual(test_langs, ["javascript", "java", "js", "j"]);+ assert.deepEqual(test_langs, ["javascript", "java", "j"]);// Push exact matches to top, regardless of prioritytest_langs = th.sort_languages(test_langs, "java");- assert.deepEqual(test_langs, ["java", "javascript", "js", "j"]);+ assert.deepEqual(test_langs, ["java", "javascript", "j"]);test_langs = th.sort_languages(test_langs, "j");- assert.deepEqual(test_langs, ["j", "javascript", "js", "java"]);+ assert.deepEqual(test_langs, ["j", "javascript", "java"]);++ // (Only one alias should be shown per language+ // (i.e searching for "js" shouldn't show "javascript")+ test_langs = ["js", "javascript", "java"];+ test_langs = th.sort_languages(test_langs, "js");+ assert.deepEqual(test_langs, ["js", "java"]);});function get_typeahead_result(query, current_stream, current_topic) {diff --git a/static/js/typeahead_helper.js b/static/js/typeahead_helper.jsindex 17293db3807..0fd2a62c9a4 100644--- a/static/js/typeahead_helper.js+++ b/static/js/typeahead_helper.js@@ -251,6 +251,24 @@ export function compare_by_popularity(lang_a, lang_b) {return util.strcmp(lang_a, lang_b);}+function retain_unique_language_aliases(matches) {+ // We make the typeahead a little more nicer but only showing one alias per language.+ // For example if the user searches for prefix "j", then the typeahead list should contain+ // "javascript" only, and not "js" and "javascript".+ const seen_aliases = new Set();+ const unique_aliases = [];+ for (const lang of matches) {+ // The matched list is already sorted based on popularity and has exact matches+ // at the top, so we don't need to worry about sorting again.+ const canonical_name = _.get(pygments_data.langs, [lang, "pretty_name"], lang);+ if (!seen_aliases.has(canonical_name)) {+ seen_aliases.add(canonical_name);+ unique_aliases.push(lang);+ }+ }+ return unique_aliases;+}+export function sort_languages(matches, query) {const results = typeahead.triage(query, matches);@@ -266,7 +284,7 @@ export function sort_languages(matches, query) {// Languages that have the query somewhere in their nameresults.rest = results.rest.sort(compare_by_popularity);- return results.matches.concat(results.rest);+ return retain_unique_language_aliases(results.matches.concat(results.rest));}export function sort_recipients(