Lua: Add "printToOutputPane" plugin option

Allows a plugin to automatically forward the "print" command to
the output pane instead of only to qDebug()

Change-Id: I10fb8063bc1713eaaf77368ea7f760270df190b3
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Marcus Tillmanns
2024-05-06 16:50:04 +02:00
parent 84e706b6da
commit 58f596e36a
5 changed files with 27 additions and 4 deletions

View File

@@ -5,7 +5,11 @@
#include "luapluginspec.h" #include "luapluginspec.h"
#include <coreplugin/messagemanager.h>
#include <utils/algorithm.h> #include <utils/algorithm.h>
#include <utils/stringutils.h>
#include <utils/theme/theme.h>
#include <QJsonArray> #include <QJsonArray>
#include <QJsonObject> #include <QJsonObject>
@@ -95,10 +99,6 @@ expected_str<LuaPluginSpec *> LuaEngine::loadPlugin(const Utils::FilePath &path)
sol::state lua; sol::state lua;
lua["print"] = [prefix = path.fileName()](sol::variadic_args va) {
qDebug().noquote() << "[" << prefix << "]" << variadicToStringList(va).join("\t");
};
auto result = lua.safe_script( auto result = lua.safe_script(
std::string_view(contents->data(), contents->size()), std::string_view(contents->data(), contents->size()),
sol::script_pass_on_error, sol::script_pass_on_error,
@@ -135,6 +135,19 @@ expected_str<void> LuaEngine::prepareSetup(
sol::lib::table, sol::lib::table,
sol::lib::utf8); sol::lib::utf8);
const bool printToOutputPane = pluginSpec.printToOutputPane();
const QString prefix = pluginSpec.filePath().fileName();
lua["print"] = [prefix, printToOutputPane](sol::variadic_args va) {
const QString msg = variadicToStringList(va).join("\t");
qDebug().noquote() << "[" << prefix << "]" << msg;
if (printToOutputPane) {
static const QString p
= ansiColoredText("[" + prefix + "]", creatorTheme()->color(Theme::Token_Text_Muted));
Core::MessageManager::writeSilently(QString("%1 %2").arg(p, msg));
}
};
const QString searchPath = (pluginSpec.location() / "?.lua").toUserOutput(); const QString searchPath = (pluginSpec.location() / "?.lua").toUserOutput();
lua["package"]["path"] = searchPath.toStdString(); lua["package"]["path"] = searchPath.toStdString();

View File

@@ -138,4 +138,9 @@ ExtensionSystem::IPlugin::ShutdownFlag LuaPluginSpec::stop()
void LuaPluginSpec::kill() {} void LuaPluginSpec::kill() {}
bool LuaPluginSpec::printToOutputPane() const
{
return d->pluginTable.get_or("printToOutputPane", false);
}
} // namespace Lua } // namespace Lua

View File

@@ -52,6 +52,9 @@ public:
bool delayedInitialize() override; bool delayedInitialize() override;
ExtensionSystem::IPlugin::ShutdownFlag stop() override; ExtensionSystem::IPlugin::ShutdownFlag stop() override;
void kill() override; void kill() override;
public:
bool printToOutputPane() const;
}; };
} // namespace Lua } // namespace Lua

View File

@@ -23,6 +23,7 @@ Qtc = {}
---@field hooks? Hooks The hooks of the plugin. ---@field hooks? Hooks The hooks of the plugin.
---@field Mimetypes? string XML MIME-info for registering additional or adapting built-in MIME types. ---@field Mimetypes? string XML MIME-info for registering additional or adapting built-in MIME types.
---@field JsonWizardPaths? string[] A list of paths relative to the plugin location or paths to the Qt resource system that are searched for template-based wizards. ---@field JsonWizardPaths? string[] A list of paths relative to the plugin location or paths to the Qt resource system that are searched for template-based wizards.
---@field printToOutputPane? boolean Whether the `print(...)` function should print to the output pane or not. ( Default: false )
QtcPlugin = {} QtcPlugin = {}
---@class QtcPluginDependency ---@class QtcPluginDependency

View File

@@ -17,4 +17,5 @@ return {
{ Name = "Lua", Version = "13.0.82", Required = true } { Name = "Lua", Version = "13.0.82", Required = true }
}, },
setup = function() require 'tests'.setup() end, setup = function() require 'tests'.setup() end,
printToOutputPane = true,
} --[[@as QtcPlugin]] } --[[@as QtcPlugin]]