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 {
constructor(opts) {
opts.startingWordSuffix = opts.startingWordSuffix || "";
opts.wordSuffix = opts.wordSuffix || "";
opts.allowLooping = opts.allowLooping || false;
opts.autoEnterTabComplete = opts.autoEnterTabComplete || false;
opts.onClickCompletes = opts.onClickCompletes || false;
@ -96,7 +94,9 @@ class TabComplete {
* @param {Entry} entry The tab-complete entry to complete to.
*/
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();
// keep focus on the text area
this.textArea.focus();
@ -224,7 +224,7 @@ class TabComplete {
this.textArea.value = this._replaceWith(
this.matchedList[this.currentIndex].getText(),
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 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
@ -259,14 +259,9 @@ class TabComplete {
boundaryChar = "";
}
var suffix = "";
if (includeSuffix) {
if (overrideSuffix) {
suffix = overrideSuffix;
}
else {
suffix = (this.isFirstWord ? this.opts.startingWordSuffix : this.opts.wordSuffix);
}
suffix = suffix || "";
if (!includeSuffix) {
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.
*/
getOverrideSuffix() {
getSuffix(isFirstWord) {
return null;
}
@ -67,7 +67,7 @@ class CommandEntry extends Entry {
return this.getText();
}
getOverrideSuffix() {
getSuffix(isFirstWord) {
return " "; // force a space after the command.
}
}
@ -94,6 +94,10 @@ class MemberEntry extends Entry {
getKey() {
return this.member.userId;
}
getSuffix(isFirstWord) {
return isFirstWord ? ": " : " ";
}
}
MemberEntry.fromMemberList = function(members) {

View File

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