Skip to content

Commit

Permalink
Fix tabcompleting names in commands
Browse files Browse the repository at this point in the history
  • Loading branch information
mia-pi-git committed Aug 3, 2024
1 parent 6a1aab2 commit 3410986
Showing 1 changed file with 38 additions and 33 deletions.
71 changes: 38 additions & 33 deletions play.pokemonshowdown.com/js/client-chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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);
Expand Down

0 comments on commit 3410986

Please sign in to comment.