aboutsummaryrefslogtreecommitdiffstats
path: root/doc/qtcreator/src/editors/creator-code-completion.qdoc
blob: c21ebef7b3ea7ba110bb9512367004c73ce45c4d (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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Creator documentation.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Free Documentation License Usage
** Alternatively, this file may be used under the terms of the GNU Free
** Documentation License version 1.3 as published by the Free Software
** Foundation and appearing in the file included in the packaging of
** this file. Please review the following information to ensure
** the GNU Free Documentation License version 1.3 requirements
** will be met: https://www.gnu.org/licenses/fdl-1.3.html.
**
****************************************************************************/

/*!
    \previouspage creator-checking-code-syntax.html
    \page creator-completing-code.html
    \nextpage creator-indenting-code.html

    \title Completing Code

    As you write code, \QC suggests properties, IDs, and code snippets to
    complete the code. It provides a list of context-sensitive suggestions to
    the statement currently under your cursor. Press \key Tab
    or \key Enter to accept the selected suggestion and complete the code.

    \if defined(qtcreator)
    The following image displays suggestions for completing C++ code:

    \image qtcreator-codecompletion.png
    \endif

    The following image displays suggestions for completing QML code:

    \image qml-code-completion.png

    To open the list of suggestions at any time, press \key {Ctrl+Space}.
    If only one option is available, \QC inserts it automatically.

    When completion is invoked manually, \QC completes the common prefix of the
    list of suggestions. This is especially useful for classes with several
    similarly named members. To disable this functionality, uncheck
    \uicontrol {Autocomplete common prefix} in the code completion preferences.
    Select \uicontrol Tools > \uicontrol Options > \uicontrol {Text Editor}
    > \uicontrol Completion.

    By default, code completion does not consider case. To apply full or
    first-letter case-sensitivity, select \uicontrol Full or
    \uicontrol {First Letter} in the \uicontrol {Case-sensitivity} field.

    \section1 Summary of Available Types

    The following table lists available types for code completion and icon used
    for each.

    \table
        \header
            \li  Icon
            \li  Description
        \if defined(qtcreator)
        \row
            \li \inlineimage completion/class.png
            \li A class
        \row
            \li \inlineimage completion/enum.png
            \li An enum
        \row
            \li \inlineimage completion/enumerator.png
            \li An enumerator (value of an enum)
        \row
            \li \inlineimage completion/func.png
            \li A function
        \row
            \li \inlineimage completion/func_priv.png
            \li A private function
        \row
            \li \inlineimage completion/func_prot.png
            \li A protected function
        \row
            \li \inlineimage completion/var.png
            \li A variable
        \row
            \li \inlineimage completion/var_priv.png
            \li A private variable
        \row
            \li \inlineimage completion/var_prot.png
            \li A protected variable
        \row
            \li \inlineimage completion/signal.png
            \li A signal
        \row
            \li \inlineimage completion/slot.png
            \li A slot
        \row
            \li \inlineimage completion/slot_priv.png
            \li A private slot
        \row
            \li \inlineimage completion/slot_prot.png
            \li A protected slot
        \row
            \li \inlineimage completion/keyword.png
            \li A C++ keyword
        \row
            \li \inlineimage completion/snippet.png
            \li A C++ code snippet
        \endif
        \row
            \li \inlineimage completion/element.png
            \li A QML type
        \row
            \li \inlineimage completion/qmlsnippet.png
            \li A QML code snippet
        \if defined(qtcreator)
        \row
            \li \inlineimage completion/macro.png
            \li A macro
        \row
            \li \inlineimage completion/namespace.png
            \li A namespace
        \endif
    \endtable

    \section1 Completing Code Snippets

    Code snippets can consist of multiple variables that you specify values for.
    Select an item in the list and press \key Tab or \key Enter to complete the
    code. Press \key Tab to move between the variables and specify values for
    them. When you specify a value for a variable, all instances of the variable
    within the snippet are renamed.

    \if defined(qtcreator)
    The following image shows a C++ code snippet:

    \image qmldesigner-code-completion.png "Completing C++ code"
    \endif

    The following image shows a QML code snippet:

    \image qml-code-snippet.png

    \section2 Editing Code Snippets

    Code snippets specify code constructs. You can add, modify,
    and remove snippets in the snippet editor. To open the editor, select
    \uicontrol Tools > \uicontrol Options > \uicontrol {Text Editor} >
    \uicontrol Snippets.

    \if defined(qtcreator)
    The following image shows built-in C++ code snippets:

    \image qtcreator-edit-code-snippets.png "C++ snippet options"
    \endif

    The following image shows built-in QML code snippets:

    \image qml-snippet-editor.png

    \QC provides you with built-in snippets in the following categories:

    \list

        \li Text snippets, which can contain any text string. For example, code
            comments

        \if defined(qtcreator)
        \li C++ code snippets, which specify C++ code constructs

        \li CMake code snippets that you can use when editing \c CMakeLists.txt
            files in the CMake editor
        \endif

        \li QML code snippets, which specify QML code constructs

        \if defined(qtcreator)
        \li Nim code snippets, which specify Nim code constructs
        \endif

    \endlist

    \section2 Adding and Editing Snippets

    Select a snippet in the list to edit it in the snippet editor. To add a new
    snippet, select \uicontrol Add. Specify a trigger and, if the trigger is
    already in use, an optional variant, which appear in the list of suggestions
    when you write code.
    \if defined(qtcreator)
    Also specify a text string or C++ or QML code construct
    in the snippet editor, depending on the snippet category. You can use
    \l{Using Qt Creator Variables}{predefined variables} in snippets.
    \else
    Also specify a text string or QML code in the snippet editor, depending on
    the snippet category.
    \endif

    The snippet editor provides you with:

    \list

        \li Highlighting

        \li Indentation

        \li Parentheses matching

        \li Basic code completion

    \endlist

    Specify the variables for the snippets in the following format:

    \code
    $variable$
    \endcode

    \if defined(qtcreator)
    Specify \QC variables in the following format:

    \code
    %{variable}
    \endcode

    For example, the following variable expands to the name of the current
    project: \c {%{CurrentProject:Name}}.

    Use unique variable names within a snippet, because all instances of a
    variable are renamed when you specify a value for it.

    To determine the case of values you enter in snippets, use the following
    modifiers:

    \list

        \li \c {:c} converts the initial letter of the string to upper case

        \li \c {:l} converts the string to lower case

        \li \c {:u} converts the string to upper case

    \endlist

    For example, add the following line to the \c class snippet to specify that
    the function name is converted to all lower case characters regardless of
    how you specify the value of the \c{$name$} variable:

    \code
        void $name:l$() {}
    \endcode

    \image qtcreator-snippet-modifiers.png
    \endif

    The snippet editor does not check the syntax of the snippets that you edit
    or add. However, when you use the snippets, the code editor marks any
    errors by underlining them in red.

    To discard the changes you made to a built-in snippet, select
    \uicontrol {Revert Built-in}.

    \section2 Removing Snippets

    Several similar built-in snippets might be provided for different use cases.
    To make the list of suggestions shorter when you write code, remove the
    built-in snippets that you do not need. If you need them later, you can
    restore them.

    To remove snippets, select a snippet in the list, and then select
    \uicontrol Remove. To restore the removed snippets, select
    \uicontrol {Restore Removed Built-ins}.

    \section2 Resetting Snippets

    To remove all added snippets and to restore all removed snippets, select
    \uicontrol {Reset All}.

    \note If you now select \uicontrol OK or \uicontrol Apply, you permanently
    lose all your own snippets.

    \if defined(qtcreator)
    \section1 Completing Nim Code

    You can use the \l{https://nim-lang.org/docs/nimsuggest.html}{Nimsuggest}
    tool to query \c {.nim} source files and obtain suggestions for code
    completion.

    To use Nimsuggest, you must install it on the development PC. Then select
    \uicontrol Tools > \uicontrol Options > \uicontrol Nim > \uicontrol Tools,
    and enter the path to the tool executable in the \uicontrol Path field.
    \endif
*/