From 33b5c3646f1e8c293c27d25dc1c27f025444e744 Mon Sep 17 00:00:00 2001 From: Mia <49593536+mia-pi-git@users.noreply.github.com> Date: Sat, 3 Aug 2024 15:36:13 -0500 Subject: [PATCH] Fix tabcompleting names in commands --- play.pokemonshowdown.com/js/client-chat.js | 71 ++++++++++++---------- 1 file changed, 38 insertions(+), 33 deletions(-) diff --git a/play.pokemonshowdown.com/js/client-chat.js b/play.pokemonshowdown.com/js/client-chat.js index 905f4932be..d237f9b4e1 100644 --- a/play.pokemonshowdown.com/js/client-chat.js +++ b/play.pokemonshowdown.com/js/client-chat.js @@ -354,14 +354,15 @@ // This is a new tab completion. // There needs to be non-whitespace to the left of the cursor. - var m1 = /^([\s\S]*?)([A-Za-z0-9][^, \n]*)$/.exec(prefix); - var m2 = /^([\s\S]*?)([A-Za-z0-9][^, \n]* [^, ]*)$/.exec(prefix); + // no command prefixes either, we're testing for usernames here. + var m1 = /^([\s\S!/]*?)([A-Za-z0-9][^, \n]*)$/.exec(prefix); + var m2 = /^([\s\S!/]*?)([A-Za-z0-9][^, \n]* [^, ]*)$/.exec(prefix); if (!m1 && !m2) return true; var cmds = this.tabComplete.commands; var shouldSearchCommands = !cmds || (cmds.length ? !!cmds.length && !cmds.filter(function (x) { return x.startsWith(prefix); }).length : prefix != this.tabComplete.prefix); - var isCommandSearch = text.startsWith('/') || text.startsWith('!'); + var isCommandSearch = (text.startsWith('/') && !text.startsWith('//')) || text.startsWith('!'); if (isCommandSearch && shouldSearchCommands) { if (this.tabComplete.searchPending) return true; // wait this.tabComplete.isCommand = true; @@ -387,44 +388,48 @@ var spaceprefix = (m2 ? m2[2].replace(/[^A-Za-z0-9 ]+/g, '').toLowerCase() : ''); var candidates = []; // array of [candidate userid, prefix length] - if (!this.tabComplete.isCommand) { - // don't include command names in autocomplete - if (m2 && (m2[0] === '/' || m2[0] === '!')) spaceprefix = ''; + // don't include command names in autocomplete + if (m2 && (m2[0] === '/' || m2[0] === '!')) spaceprefix = ''; - for (var i in users) { - if (spaceprefix && users[i].name.replace(/[^A-Za-z0-9 ]+/g, '').toLowerCase().substr(0, spaceprefix.length) === spaceprefix) { - candidates.push([i, m2[1].length]); - } else if (idprefix && i.substr(0, idprefix.length) === idprefix) { - candidates.push([i, m1[1].length]); - } + for (var i in users) { + if (spaceprefix && users[i].name.replace(/[^A-Za-z0-9 ]+/g, '').toLowerCase().substr(0, spaceprefix.length) === spaceprefix) { + candidates.push([i, m2[1].length]); + } else if (idprefix && i.substr(0, idprefix.length) === idprefix) { + candidates.push([i, m1[1].length]); } + } - // Sort by most recent to speak in the chat, or, in the case of a tie, - // in alphabetical order. - var self = this; - candidates.sort(function (a, b) { - if (a[1] !== b[1]) { - // shorter prefix length comes first - return a[1] - b[1]; - } - var aidx = self.userActivity.indexOf(a[0]); - var bidx = self.userActivity.indexOf(b[0]); - if (aidx !== -1) { - if (bidx !== -1) { - return bidx - aidx; - } - return -1; // a comes first - } else if (bidx != -1) { - return 1; // b comes first + // Sort by most recent to speak in the chat, or, in the case of a tie, + // in alphabetical order. + var self = this; + candidates.sort(function (a, b) { + if (a[1] !== b[1]) { + // shorter prefix length comes first + return a[1] - b[1]; + } + var aidx = self.userActivity.indexOf(a[0]); + var bidx = self.userActivity.indexOf(b[0]); + if (aidx !== -1) { + if (bidx !== -1) { + return bidx - aidx; } - return (a[0] < b[0]) ? -1 : 1; // alphabetical order + return -1; // a comes first + } else if (bidx != -1) { + return 1; // b comes first + } + return (a[0] < b[0]) ? -1 : 1; // alphabetical order + }); + + if (this.tabComplete.isCommand) { + this.tabComplete.commands.sort(function (a, b) { + return a.length < b.length ? 1 : -1; }); - } else { for (var i = 0; i < this.tabComplete.commands.length; i++) { var cmd = this.tabComplete.commands[i]; - candidates.push([i, cmd]); + candidates.unshift([i, cmd]); } } + this.tabComplete.candidates = candidates; this.tabComplete.index = 0; if (!candidates.length) { @@ -436,7 +441,7 @@ // Substitute in the tab-completed name. var candidate = this.tabComplete.candidates[this.tabComplete.index]; var substituteUserId = candidate[0]; - var substituteUser = this.tabComplete.isCommand ? candidate[1] : users[substituteUserId]; + var substituteUser = users[substituteUserId] || candidate[1]; if (!substituteUser) return true; var name = typeof substituteUser === 'object' ? substituteUser.name : substituteUser; name = Dex.getShortName(name);