このページでは

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]]

セットアップ関数

セットアップ関数は、拡張機能が読み込まれたときに呼び出されます。ここで、拡張機能の機能を設定することができます。仕様ファイルの解析には非常に制限された権限しか与えられていないため、実際の機能を実装するモジュールを require する必要があります。

--- 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 Language Serverのダウンロードを求められます。これは、コンテキストに応じたヘルプや自動補完機能を提供してくれるため、非常に便利です。

Qt Creator Lua API

Qt Creator のLua APIは、require 関数を使用してインポートできるいくつかのモジュールを介して、Lua拡張機能から利用可能です。C++拡張機能は、追加のモジュールを提供する場合があります。その一例として、Language Serverクライアントを作成するためのモジュールを提供するLanguageServer Extensionがあります。

Lua モジュールの API ドキュメントファイルは、Qt Creator のインストールディレクトリ内にあります。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

Language Server クライアントを登録します。

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プラグインは、独自のLanguage Serverクライアントを登録するためのサポートを提供します。この使用例については、Qt Creator 拡張機能の「Lua Language Server」および「Rust Language Server」を参照してください。

「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.