本页内容

创建基于 Lua 的扩展

Qt Creator 可通过 Lua 脚本进行扩展。内置的 Lua 引擎基于 Lua 5.4.6。

编写 Lua 扩展

要创建新的 Lua 扩展,请选择“File ” > “New Project ” > “Library ” > Qt Creator Lua Plugin。

要测试新扩展,请启动项目。您的“Application Output ”窗口应显示“Hello from Lua!”。

扩展程序位置

Lua 扩展可位于Qt Creator 的任何常规插件位置。要了解Qt Creator 会扫描哪些文件夹以查找插件,请将环境变量QT_LOGGING_RULES 设置为qtc.extensionsystem*=true 。这将启用插件管理器的调试输出,该输出会在Qt Creator 启动时列出插件搜索路径。

要添加搜索路径,请在启动Qt Creator 时使用-pluginpath 命令行参数。

Lua 扩展规范

一个 Lua 扩展由一个 Lua 脚本组成,该脚本的名称与其所在文件夹的名称相同。这是加载该扩展所必需的。

该脚本定义了扩展的规范,例如其显示名称、供应商和版权信息。

--- MyExtension.lua
return {
    Id = "myextension",
    Name = "MyExtension",
    Version = "1.0.0",
    CompatVersion = "1.0.0",
    Vendor = "My Company",
    VendorId = "mycompany",
    Category = "Tests",
    Description = "Describe what your extension does in a sentence.",
    LongDescription = [[
        Tell users more about your extension. This text is rendered as Markdown.
    ]],
    Dependencies = {
        { Name = "Lua",  Version = "15.0.0", Required = true }
    },
    setup = function() print("Hello from Lua!") end,
    printToOutputPane = true,
} --[[@as QtcPlugin]]

Setup 函数

当扩展被加载时,会调用 setup 函数。您可以在此处配置扩展的功能。由于规范文件的解析权限非常有限,您需要引入一个模块来实现实际功能。

--- MyExtension.lua
return {
    Name = "MyExtension",
    Version = "1.0.0",
    ...,
    --- This is the setup function that is called when the extension is loaded.
    --- It requires the 'init' module and calls the setup function from the returned table.
    setup = function() require 'init'.setup() end,
}
--- init.lua
function setup()
    print("Hello from Lua!")
end

-- Returns a table with a single field 'setup' that points to the setup function.
return {
    setup = setup
}

异步操作

部分内置操作以异步方式运行。为处理此类情况,请使用 Async 模块。

local a = require 'async'
local u = require 'Utils'

a.sync(function()
    print("Lets wait for 5 seconds ...")
    a.wait(u.waitms(5000))
    print("... done!")
end)

交互式帮助

当您首次在编辑器中打开 .lua 文件时,系统会提示您下载 Lua 语言服务器。此功能非常实用,可为您提供上下文相关帮助和自动补全功能。

Qt Creator Lua API

Qt Creator 的Lua API可通过多个模块供Lua扩展使用,你可以使用require 函数导入这些模块。C++扩展可能会提供额外的模块。其中一个例子是LanguageServer扩展,它提供了一个用于创建语言服务器客户端的模块。

您可以在Qt Creator 安装目录中找到Lua模块的API文档文件。在macOS上,这些文件位于 Qt Creator.app/Contents/Resources/lua/meta.

Core.lua

访问并操作Qt Creator 的核心功能。

action.lua

在Qt Creator 中创建用户界面操作。

async.lua

使用 async/await Lua API 处理异步操作。

fetch.lua

从互联网获取数据。

gui.lua

创建用户界面。

lsp.lua

注册语言服务器客户端。

messagemanager.lua

向用户显示消息。

process.lua

运行外部进程。

qt.lua

访问 Qt 功能。

qtc.lua

访问并扩展Qt Creator 。

settings.lua

读取和写入设置。

simpletypes.lua

访问简单类型。

texteditor.lua

访问并操作文本编辑器。

utils.lua

常用辅助函数和类。

使用 C++ 扩展 Lua API

要为 Lua 接口添加功能,您需要在 Lua 引擎中注册一个新模块。

#include <lua/luaengine.h>

class MyCppExtension final : public ExtensionSystem::IPlugin
{
    Q_OBJECT
    Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QtCreatorPlugin" FILE "MyCppExtension.json")

public:
    MyCppExtension() {}

private:
    void initialize() final {
        // The registered function will be called when the Lua module 'MyCppExtension' is required.
        // The returned table will be returned from the require call in Lua.
        ::Lua::registerProvider("MyCppExtension", [](sol::state_view lua) -> sol::object {
            sol::table result = lua.create_table();
            result["myFunction"] = [](int a, int b) { return a + b; };
            return result;
        });
     }
};

然后,您就可以像这样从 Lua 脚本中访问 `MyCppExtension.myFunction `:

local MyCppExtension = require 'MyCppExtension'
--- MyCppExtension is now a table with a single field 'myFunction', as it is returned from the
--- C++ function registered via 'Lua::registerProvider(...)'.
print(MyCppExtension.myFunction(1, 2))

有关如何注册 C++ 功能的更多信息,请参阅sol2。

示例

语言服务器

Qt Creator 的LuaLanguageClient插件支持注册您自己的语言服务器客户端。您可以在Qt Creator 扩展的“Lua语言服务器”和“Rust语言服务器”中找到相关使用示例。

另请参阅 《创建基于 C++ 的插件》。

Copyright © The Qt Company Ltd. and other contributors. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.