Make individual Entrys responsible for determining suffixes

This makes it cleaner as CommandEntry always wants a space, but MemberEntry
wants a space or ": " depending on if it is the first word or not.
pull/21833/head
Kegan Dougal 2016-01-14 11:39:24 +00:00
parent f4be4880b8
commit 864d10f412
3 changed files with 15 additions and 18 deletions

View File

@ -32,8 +32,6 @@ const MATCH_REGEX = /(^|\s)(\S+)$/;
class TabComplete { class TabComplete {
constructor(opts) { constructor(opts) {
opts.startingWordSuffix = opts.startingWordSuffix || "";
opts.wordSuffix = opts.wordSuffix || "";
opts.allowLooping = opts.allowLooping || false; opts.allowLooping = opts.allowLooping || false;
opts.autoEnterTabComplete = opts.autoEnterTabComplete || false; opts.autoEnterTabComplete = opts.autoEnterTabComplete || false;
opts.onClickCompletes = opts.onClickCompletes || false; opts.onClickCompletes = opts.onClickCompletes || false;
@ -96,7 +94,9 @@ class TabComplete {
* @param {Entry} entry The tab-complete entry to complete to. * @param {Entry} entry The tab-complete entry to complete to.
*/ */
completeTo(entry) { completeTo(entry) {
this.textArea.value = this._replaceWith(entry.getText(), true, entry.getOverrideSuffix()); this.textArea.value = this._replaceWith(
entry.getText(), true, entry.getSuffix(this.isFirstWord)
);
this.stopTabCompleting(); this.stopTabCompleting();
// keep focus on the text area // keep focus on the text area
this.textArea.focus(); this.textArea.focus();
@ -224,7 +224,7 @@ class TabComplete {
this.textArea.value = this._replaceWith( this.textArea.value = this._replaceWith(
this.matchedList[this.currentIndex].getText(), this.matchedList[this.currentIndex].getText(),
this.currentIndex !== 0, // don't suffix the original text! this.currentIndex !== 0, // don't suffix the original text!
this.matchedList[this.currentIndex].getOverrideSuffix() this.matchedList[this.currentIndex].getSuffix(this.isFirstWord)
); );
} }
@ -244,7 +244,7 @@ class TabComplete {
} }
} }
_replaceWith(newVal, includeSuffix, overrideSuffix) { _replaceWith(newVal, includeSuffix, suffix) {
// The regex to replace the input matches a character of whitespace AND // The regex to replace the input matches a character of whitespace AND
// the partial word. If we just use string.replace() with the regex it will // the partial word. If we just use string.replace() with the regex it will
// replace the partial word AND the character of whitespace. We want to // replace the partial word AND the character of whitespace. We want to
@ -259,14 +259,9 @@ class TabComplete {
boundaryChar = ""; boundaryChar = "";
} }
var suffix = ""; suffix = suffix || "";
if (includeSuffix) { if (!includeSuffix) {
if (overrideSuffix) { suffix = "";
suffix = overrideSuffix;
}
else {
suffix = (this.isFirstWord ? this.opts.startingWordSuffix : this.opts.wordSuffix);
}
} }
var replacementText = boundaryChar + newVal + suffix; var replacementText = boundaryChar + newVal + suffix;

View File

@ -43,10 +43,10 @@ class Entry {
} }
/** /**
* @return {?string} The suffix to override whatever the default is, or null to * @return {?string} The suffix to append to the tab-complete, or null to
* not do this. * not do this.
*/ */
getOverrideSuffix() { getSuffix(isFirstWord) {
return null; return null;
} }
@ -67,7 +67,7 @@ class CommandEntry extends Entry {
return this.getText(); return this.getText();
} }
getOverrideSuffix() { getSuffix(isFirstWord) {
return " "; // force a space after the command. return " "; // force a space after the command.
} }
} }
@ -94,6 +94,10 @@ class MemberEntry extends Entry {
getKey() { getKey() {
return this.member.userId; return this.member.userId;
} }
getSuffix(isFirstWord) {
return isFirstWord ? ": " : " ";
}
} }
MemberEntry.fromMemberList = function(members) { MemberEntry.fromMemberList = function(members) {

View File

@ -96,8 +96,6 @@ module.exports = React.createClass({
// xchat-style tab complete, add a colon if tab // xchat-style tab complete, add a colon if tab
// completing at the start of the text // completing at the start of the text
this.tabComplete = new TabComplete({ this.tabComplete = new TabComplete({
startingWordSuffix: ": ",
wordSuffix: " ",
allowLooping: false, allowLooping: false,
autoEnterTabComplete: true, autoEnterTabComplete: true,
onClickCompletes: true, onClickCompletes: true,