1
0
mirror of https://github.com/wbbaddons/Tims-Chat.git synced 2024-10-31 14:10:08 +00:00
Tims-Chat/files_wcf/js/Bastelstu.be/Chat/Autocompleter.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

90 lines
2.1 KiB
JavaScript
Raw Normal View History

2018-08-16 22:30:59 +00:00
/*
2021-02-04 22:04:35 +00:00
* Copyright (c) 2010-2021 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.
*
2022-09-17 14:23:02 +00:00
* Change Date: 2026-09-17
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.
*/
define(['./CommandHandler', './Parser'], function (CommandHandler, Parser) {
'use strict'
const DEPENDENCIES = ['CommandHandler']
class Autocompleter {
constructor(commandHandler) {
if (!(commandHandler instanceof CommandHandler))
throw new TypeError(
'You must pass a CommandHandler to the Autocompleter'
)
this.commandHandler = commandHandler
}
*autocomplete(text) {
if (text === '/') {
yield* this.autocompleteCommandTrigger(text, '')
return
}
const [trigger, parameterString] = this.commandHandler.splitCommand(text)
let command
if (trigger === null) {
command = this.commandHandler.getCommandByIdentifier(
'be.bastelstu.chat',
'plain'
)
} else {
const triggerDone = Parser.Slash.thenRight(
Parser.AlnumTrigger.or(Parser.SymbolicTrigger).thenLeft(
Parser.Whitespace
2020-11-01 16:41:19 +00:00
)
2018-08-16 22:30:59 +00:00
).parse(Parser.Streams.ofString(text))
if (!triggerDone.isAccepted()) {
yield* this.autocompleteCommandTrigger(text, trigger)
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}`
}
} else {
yield* values
}
}
*autocompleteCommandTrigger(text, prefix) {
const triggers = Array.from(this.commandHandler.getTriggers())
triggers.sort()
for (const trigger of triggers) {
if (trigger === '') continue
if (!trigger.startsWith(prefix)) continue
if (!this.commandHandler.getCommandByTrigger(trigger).isAvailable)
continue
yield `/${trigger} `
}
}
}
Autocompleter.DEPENDENCIES = DEPENDENCIES
return Autocompleter
})