2018-08-16 22:30:59 +00:00
|
|
|
/*
|
2024-01-13 20:03:36 +00:00
|
|
|
* Copyright (c) 2010-2024 Tim Düsterhus.
|
2018-08-16 22:30:59 +00:00
|
|
|
*
|
|
|
|
* Use of this software is governed by the Business Source License
|
|
|
|
* included in the LICENSE file.
|
|
|
|
*
|
2024-01-13 20:03:36 +00:00
|
|
|
* Change Date: 2028-01-13
|
2018-08-16 22:30:59 +00:00
|
|
|
*
|
|
|
|
* On the date above, in accordance with the Business Source
|
|
|
|
* License, use of this software will be governed by version 2
|
|
|
|
* or later of the General Public License.
|
|
|
|
*/
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
define(['./CommandHandler', './Parser'], function (CommandHandler, Parser) {
|
|
|
|
'use strict'
|
2018-08-16 22:30:59 +00:00
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
const DEPENDENCIES = ['CommandHandler']
|
2018-08-16 22:30:59 +00:00
|
|
|
class Autocompleter {
|
|
|
|
constructor(commandHandler) {
|
2020-11-01 16:41:19 +00:00
|
|
|
if (!(commandHandler instanceof CommandHandler))
|
|
|
|
throw new TypeError(
|
|
|
|
'You must pass a CommandHandler to the Autocompleter'
|
|
|
|
)
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
|
|
this.commandHandler = commandHandler
|
|
|
|
}
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
*autocomplete(text) {
|
2018-08-16 22:30:59 +00:00
|
|
|
if (text === '/') {
|
2020-11-01 16:41:19 +00:00
|
|
|
yield* this.autocompleteCommandTrigger(text, '')
|
2018-08-16 22:30:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
const [trigger, parameterString] = this.commandHandler.splitCommand(text)
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
|
|
let command
|
|
|
|
if (trigger === null) {
|
2020-11-01 16:41:19 +00:00
|
|
|
command = this.commandHandler.getCommandByIdentifier(
|
|
|
|
'be.bastelstu.chat',
|
|
|
|
'plain'
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
const triggerDone = Parser.Slash.thenRight(
|
|
|
|
Parser.AlnumTrigger.or(Parser.SymbolicTrigger).thenLeft(
|
|
|
|
Parser.Whitespace
|
|
|
|
)
|
|
|
|
).parse(Parser.Streams.ofString(text))
|
2018-08-16 22:30:59 +00:00
|
|
|
if (!triggerDone.isAccepted()) {
|
2020-11-01 16:41:19 +00:00
|
|
|
yield* this.autocompleteCommandTrigger(text, trigger)
|
2018-08-16 22:30:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
command = this.commandHandler.getCommandByTrigger(trigger)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (command === null) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const values = command.autocomplete(parameterString)
|
|
|
|
|
|
|
|
if (trigger !== null) {
|
|
|
|
for (const item of values) {
|
|
|
|
yield `/${trigger} ${item}`
|
|
|
|
}
|
2020-11-01 16:41:19 +00:00
|
|
|
} else {
|
|
|
|
yield* values
|
2018-08-16 22:30:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
*autocompleteCommandTrigger(text, prefix) {
|
2018-08-16 22:30:59 +00:00
|
|
|
const triggers = Array.from(this.commandHandler.getTriggers())
|
|
|
|
|
|
|
|
triggers.sort()
|
|
|
|
|
|
|
|
for (const trigger of triggers) {
|
|
|
|
if (trigger === '') continue
|
|
|
|
if (!trigger.startsWith(prefix)) continue
|
2020-11-01 16:41:19 +00:00
|
|
|
if (!this.commandHandler.getCommandByTrigger(trigger).isAvailable)
|
|
|
|
continue
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
|
|
yield `/${trigger} `
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Autocompleter.DEPENDENCIES = DEPENDENCIES
|
|
|
|
|
|
|
|
return Autocompleter
|
2020-11-01 16:41:19 +00:00
|
|
|
})
|