4d8b62364d
* Add commands to completable inputs when no prefix
108 lines
2.8 KiB
C++
108 lines
2.8 KiB
C++
#include "controllers/completion/sources/CommandSource.hpp"
|
|
|
|
#include "Application.hpp"
|
|
#include "controllers/commands/Command.hpp"
|
|
#include "controllers/commands/CommandController.hpp"
|
|
#include "controllers/completion/sources/Helpers.hpp"
|
|
#include "providers/twitch/TwitchCommon.hpp"
|
|
|
|
namespace chatterino::completion {
|
|
|
|
namespace {
|
|
|
|
void addCommand(const QString &command, std::vector<CommandItem> &out)
|
|
{
|
|
if (command.startsWith('/') || command.startsWith('.'))
|
|
{
|
|
out.push_back({
|
|
.name = command.mid(1),
|
|
.prefix = command.at(0),
|
|
});
|
|
}
|
|
else
|
|
{
|
|
out.push_back({
|
|
.name = command,
|
|
.prefix = "",
|
|
});
|
|
}
|
|
}
|
|
|
|
} // namespace
|
|
|
|
CommandSource::CommandSource(std::unique_ptr<CommandStrategy> strategy,
|
|
ActionCallback callback)
|
|
: strategy_(std::move(strategy))
|
|
, callback_(std::move(callback))
|
|
{
|
|
this->initializeItems();
|
|
}
|
|
|
|
void CommandSource::update(const QString &query)
|
|
{
|
|
this->output_.clear();
|
|
if (this->strategy_)
|
|
{
|
|
this->strategy_->apply(this->items_, this->output_, query);
|
|
}
|
|
}
|
|
|
|
void CommandSource::addToListModel(GenericListModel &model,
|
|
size_t maxCount) const
|
|
{
|
|
addVecToListModel(this->output_, model, maxCount,
|
|
[this](const CommandItem &command) {
|
|
return std::make_unique<InputCompletionItem>(
|
|
nullptr, command.name, this->callback_);
|
|
});
|
|
}
|
|
|
|
void CommandSource::addToStringList(QStringList &list, size_t maxCount,
|
|
bool /* isFirstWord */) const
|
|
{
|
|
addVecToStringList(this->output_, list, maxCount,
|
|
[](const CommandItem &command) {
|
|
return command.prefix + command.name + " ";
|
|
});
|
|
}
|
|
|
|
void CommandSource::initializeItems()
|
|
{
|
|
std::vector<CommandItem> commands;
|
|
|
|
#ifdef CHATTERINO_HAVE_PLUGINS
|
|
for (const auto &command : getApp()->commands->pluginCommands())
|
|
{
|
|
addCommand(command, commands);
|
|
}
|
|
#endif
|
|
|
|
// Custom Chatterino commands
|
|
for (const auto &command : getIApp()->getCommands()->items)
|
|
{
|
|
addCommand(command.name, commands);
|
|
}
|
|
|
|
// Default Chatterino commands
|
|
auto x = getIApp()->getCommands()->getDefaultChatterinoCommandList();
|
|
for (const auto &command : x)
|
|
{
|
|
addCommand(command, commands);
|
|
}
|
|
|
|
// Default Twitch commands
|
|
for (const auto &command : TWITCH_DEFAULT_COMMANDS)
|
|
{
|
|
addCommand(command, commands);
|
|
}
|
|
|
|
this->items_ = std::move(commands);
|
|
}
|
|
|
|
const std::vector<CommandItem> &CommandSource::output() const
|
|
{
|
|
return this->output_;
|
|
}
|
|
|
|
} // namespace chatterino::completion
|