#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 &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 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( 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 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 &CommandSource::output() const { return this->output_; } } // namespace chatterino::completion