aboutsummaryrefslogtreecommitdiffstats
path: root/doc/qtcreatordev/src/lua-extensions.qdoc
blob: e5bf56a0406a00ecf50c1411722234473ce2ebdb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

/*!
    \page lua-extensions.html
    \title Creating Lua-Based Extensions

    \QC can be extended with Lua scripts. The included Lua engine is based on Lua 5.4.6.

    \section1 Writing Lua Extensions

    To create a new Lua extension, choose \uicontrol {File} > \uicontrol {New Project} >
    \uicontrol {Library} > \uicontrol {\QC Lua Plugin}.

    To test your new extension, start your project. Your \uicontrol {Application Output}
    should show \e {Hello from Lua!}.

    \section1 Lua Extension Specification

    A Lua extension consists of a Lua script with the same name as the folder it is in.
    This is necessary for the extension to be loaded.

    This script defines the specification of the extension, such as its display name, vendor, copyright.

    \code
        --- 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]]
    \endcode

    \section1 The Setup Function

    The setup function is called when the extension is loaded. This is where you can set up the
    functionality of your extension. Since the specification file is parsed with very limited
    permissions, you need to require a module where you implement the actual functionality.

    \code
        --- 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,
        }
    \endcode

    \code
        --- 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
        }
    \endcode


    \section1 Asynchronous Operations

    Some of the built-in operations work asynchronously. To handle this, use the Async module.

    \code
        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)
    \endcode

    \section1 Interactive Help

    When you open a .lua file in the editor the first time, you are asked to download the Lua
    Language Server. This is extremely useful as it gives you context sensitive help and
    auto-completion.

    \section1 \QC Lua API

    The \QC Lua API is available to Lua extensions via a number of modules that you can import
    using the \c {require} function. C++ extensions may provide additional modules. One example of that
    is the LanguageServer Extension that provides a module for creating Language Server clients.

    You can find the API documentation files for the Lua modules in your \QC installation.
    On \macos you can find them in \c{Qt Creator.app/Contents/Resources/lua/meta}.

    \annotatedlist lua-modules

    \section1 Extending the Lua API with C++

    To add functionality to the Lua Interface, you need to register a new module with the Lua Engine.

    \code
    #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;
            });
         }
    };
    \endcode

    You can then access \c{MyCppExtension.myFunction} from your Lua scripts like this:

    \code
    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))
    \endcode

    For more information on how to register C++ functionality, see \l {sol2}.

    \section1 Examples

    \section2 Language Server

    The \QC LuaLanguageClient Plugin provides support for registering your own Language Server
    clients. You can find an example of how to use this in the \QC Extension "Lua Language Server"
    and "Rust Language Server".

    \sa {Creating C++-Based Plugins}
*/

/*!
    \page lua-extension-meta-core
    \ingroup lua-modules
    \title Core.lua
    \brief Access and interact with the core functionality of \QC.

    \quotefile ../../../src/plugins/lua/meta/core.lua
*/

/*!
    \page lua-extension-meta-action
    \ingroup lua-modules
    \title action.lua
    \brief Create user interface actions in \QC.

    \quotefile ../../../src/plugins/lua/meta/action.lua
*/

/*!
    \page lua-extension-meta-async
    \ingroup lua-modules
    \title async.lua
    \brief Handle asynchronouse operations with the async/await Lua API.

    \quotefile ../../../src/plugins/lua/meta/async.lua
*/

/*!
    \page lua-extension-meta-fetch
    \ingroup lua-modules
    \title fetch.lua
    \brief Fetch data from the internet.

    \quotefile ../../../src/plugins/lua/meta/fetch.lua
*/

/*!
    \page lua-extension-meta-gui
    \ingroup lua-modules
    \title gui.lua
    \brief Create user interfaces.

    \quotefile ../../../src/plugins/lua/meta/gui.lua
*/

/*!
    \page lua-extension-meta-lsp
    \ingroup lua-modules
    \title lsp.lua
    \brief Register Language Server clients.

    \quotefile ../../../src/plugins/lua/meta/lsp.lua
*/

/*!
    \page lua-extension-meta-messagemanager
    \ingroup lua-modules
    \title messagemanager.lua
    \brief Display messages to the user.

    \quotefile ../../../src/plugins/lua/meta/messagemanager.lua
*/

/*!
    \page lua-extension-meta-process
    \ingroup lua-modules
    \title process.lua
    \brief Run external processes.

    \quotefile ../../../src/plugins/lua/meta/process.lua
*/

/*!
    \page lua-extension-meta-qt
    \ingroup lua-modules
    \title qt.lua
    \brief Access Qt functionality.

    \quotefile ../../../src/plugins/lua/meta/qt.lua
*/

/*!
    \page lua-extension-meta-qtc
    \ingroup lua-modules
    \title qtc.lua
    \brief Access and extend \QC.

    \quotefile ../../../src/plugins/lua/meta/qtc.lua
*/

/*!
    \page lua-extension-meta-settings
    \ingroup lua-modules
    \title settings.lua
    \brief Read and write settings.

    \quotefile ../../../src/plugins/lua/meta/settings.lua
*/

/*!
    \page lua-extension-meta-simpletypes
    \ingroup lua-modules
    \title simpletypes.lua
    \brief Access simple types.

    \quotefile ../../../src/plugins/lua/meta/simpletypes.lua
*/

/*!
    \page lua-extension-meta-utils
    \ingroup lua-modules
    \title utils.lua
    \brief Common utility functions and classes.

    \quotefile ../../../src/plugins/lua/meta/utils.lua
*/

/*!
    \page lua-extension-meta-texteditor
    \ingroup lua-modules
    \title texteditor.lua
    \brief Access and manipulate text editors.

    \quotefile ../../../src/plugins/lua/meta/texteditor.lua
*/