summaryrefslogtreecommitdiff
path: root/doc/qtcreator/src/editors/creator-code-refactoring.qdoc
blob: f333c92bc424477301a64342ea0c49ef5dc46030 (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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
/****************************************************************************
**
** Copyright (C) 2019 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.
**
****************************************************************************/

/*!
    \contentspage index.html
    \page creator-editor-refactoring.html
    \previouspage creator-editor-locator.html
    \if defined(qtdesignstudio)
    \nextpage creator-editor-options.html
    \else
    \nextpage creator-beautifier.html
    \endif

    \title Refactoring

    Refactor code to:

    \list
        \li Improve internal quality of your application
        \li Improve performance and extensibility
        \li Improve code readability and maintainability
        \li Simplify code structure
    \endlist

    \if defined(qtcreator)
    \section1 Finding Symbols

    To find instances of a specific symbol in your Qt C++ project, place the
    cursor on the symbol in the editor and select \uicontrol Tools >
    \uicontrol {C++} > \uicontrol {Find References to Symbol Under Cursor} or
    press \key {Ctrl+Shift+U}.

    \note You can also select \uicontrol Edit > \uicontrol {Find/Replace} >
    \uicontrol {Advanced Find} > \uicontrol {C++ Symbols} to search for
    classes, functions, enums, and declarations either from files listed as
    part of the project or from all files that are used by the code, such as
    include files.
    \image qtcreator-search-cpp-symbols.png
    \endif

    \section1 Finding QML Types

    To find instances of a specific QML type in a project, place the cursor on
    the type and select \uicontrol Tools > \uicontrol {QML/JS} >
    \uicontrol {Find References to Symbol Under Cursor} or press
    \key {Ctrl+Shift+U}.

    \section1 Viewing Search Results

    \QC searches from the following locations:

    \list
        \li Files listed as a part of the project
        \li Files directly used by the project files (for example, generated
            files)
        \if defined(qtcreator)
        \li Header files of used frameworks and libraries
        \endif
    \endlist

    The \uicontrol {Search Results} pane shows the location
    and number of search hits in the current project.

    \if defined(qtcreator)
    \image qtcreator-refactoring-find.png
    \else
    \image qml-find-usages.png
    \endif

    You can browse the search results in the following ways:

    \list
        \li To go directly to an instance, double-click the instance in the
            \uicontrol {Search Results} pane.
        \li To move between instances, click the \inlineimage next.png
            (\uicontrol {Next Item}) button and \inlineimage prev.png
            (\uicontrol {Previous Item}) button in the
            \uicontrol {Search Results} pane.
        \li To expand and collapse the list of all instances, click the
            \inlineimage qtcreator-expand.png
            (\uicontrol {Expand All}) button.
        \li To clear the search results, click the
            \inlineimage clean_pane_small.png
            (\uicontrol Clear) button.
        \li To start a new search, click the
            \inlineimage qtcreator-new-search-icon.png
            (\uicontrol {New Search}) button.
    \endlist

    \section1 Renaming Symbols

    You can rename symbols in all files in a project.
    \if defined(qtcreator)
    When you rename a class, you can also change filenames that match the
    class name.

    To rename a specific symbol in a Qt project, place the cursor
     on it and select \uicontrol Tools > \uicontrol {C++} >
    \uicontrol {Rename Symbol Under Cursor} or press \key {Ctrl+Shift+R}.
    \endif

    To rename a QML type in a project, select \uicontrol Tools >
    \uicontrol {QML/JS} > \uicontrol {Rename Symbol Under Cursor}
    or press \key {Ctrl+Shift+R}.

    The \uicontrol {Search Results} pane shows the location
    and number of instances of the symbol in the current project.

    \if defined(qtcreator)
    \image qtcreator-refactoring-replace.png
    \else
    \image qml-rename-symbol.png
    \endif

    To replace all selected instances, enter the name of the new symbol
    in the \uicontrol {Replace with} text box, and select \uicontrol Replace.
    To omit an instance, deselect the check box next to the instance.

    \note This action replaces all selected instances of the symbol in
    all files listed in the \uicontrol {Search Results} pane. You cannot
    undo this action.

    \if defined(qtcreator)
    If the symbol is a class, select the \uicontrol {Rename files} check
    box to also change the filenames that match the class name.

    \note Renaming local symbols does not open the \uicontrol {Search Results}
    pane. The instances of the symbol are highlighted in code and you can edit
    the symbol. All instances of the local symbol are changed as you type.
    \endif

    \section1 Column Editing

    To apply a change to several rows in a column simultaneously, press
    \key Alt, select the rows, and enter or remove text. The changes are made
    simultaneously at the cursor position on all the selected rows.

    \section1 Applying Refactoring Actions

    \QC allows you to quickly and conveniently apply actions to refactor your
    code by selecting them in a context menu. The actions available depend on
    the position of the cursor in the code editor.

    \if defined(qtcreator)
    To apply refactoring actions to C++ code, right-click an operand,
    conditional statement, string, or name to open a context menu.
    \endif
    To apply refactoring actions to QML code, right-click an item ID or name.

    In the context menu, select \uicontrol {Refactoring} and then select a
    refactoring action.

    You can also press \key {Alt+Enter} to open a context menu that contains
    refactoring actions available in the current cursor position.

    \if defined(qtcreator)
    If you use the \l{Parsing C++ Files with the Clang Code Model}
    {Clang code model} to parse the C++ files, the
    \l{http://clang.llvm.org/diagnostics.html}{Clang fix-it hints}
    that have been integrated into \QC are also available to you. In addition to
    the standard ways of activating refactoring actions, you can select the
    actions that are applicable on a line in the context menu in the left margin
    of the code editor.

   \section2 Refactoring C++ Code

   You can apply the following types of refactoring actions to C++ code:

   \list
        \li Change binary operands
        \li Simplify if and while conditions (for example, move declarations out
            of if conditions)
        \li Modify strings (for example, set the encoding for a string to
            Latin-1, mark strings translatable, and convert symbol names to
            camel case)
        \li Create variable declarations
        \li Create function declarations and definitions
   \endlist

    The following table summarizes the refactoring actions for C++ code. The
    action is available when the cursor is in the position described in the
    Activation column.

    \table
        \header
            \li Refactoring Action
            \li Description
            \li Activation
        \row
            \li Add Curly Braces
            \li Adds curly braces to an if statement that does not contain a
                compound statement. For example, rewrites

    \code
    if (a)
        b;
    \endcode

                as

    \code
    if (a) {
        b;
    }
    \endcode
            \li \c if
        \row
            \li Move Declaration out of Condition
            \li Moves a declaration out of an if or while condition to simplify
                the condition. For example, rewrites

    \code
    if (Type name = foo()) {}
    \endcode

                as

    \code
    Type name = foo;
    if (name) {}
    \endcode
            \li Name of the introduced variable
        \row
            \li Rewrite Condition Using ||
            \li Rewrites the expression according to De Morgan's laws. For
                example, rewrites:
    \code
    !a && !b
    \endcode

                as

    \code
    !(a || b)
    \endcode
            \li \c &&
        \row
            \li Rewrite Using \e operator
            \li Rewrites an expression negating it and using the inverse
                operator. For example, rewrites:

            \list

    \li    \code
    a op b
    \endcode

                as

    \code
    !(a invop b)
    \endcode

    \li    \code
    (a op b)
    \endcode

                as

    \code
    !(a invop b)
    \endcode

    \li   \code
    !(a op b)
    \endcode

                as

    \code
    (a invob b)
    \endcode

    \endlist

            \li \c {<=}, \c {<}, \c {>}, \c {>=}, \c {==} or \c {!=}
        \row
            \li Split Declaration
            \li Splits a simple declaration into several declarations. For
                example, rewrites:
    \code
    int *a, b;
    \endcode

                as

    \code
    int *a;
    int b;
    \endcode
            \li Type name or variable name
        \row
            \li Split if Statement
            \li Splits an if statement into several statements. For example,
                rewrites:

    \code
    if (something && something_else) {
    }
    \endcode

                as

    \code
    if (something) {
       if (something_else) {
       }
    }
    \endcode

                and

    \code
    if (something || something_else)
        x;
    \endcode

                with

    \code
    if (something)
        x;
    else if (something_else)
        x;
    \endcode

            \li \c && or \c ||
        \row
            \li Swap Operands
            \li Rewrites an expression in the inverse order using the inverse
                operator. For example, rewrites:
    \code
    a op b
    \endcode

                as
    \code
    b flipop a
    \endcode
            \li \c {<=}, \c {<}, \c {>},  \c {>=}, \c {==}, \c {!=}, \c {&&}
                or \c {||}
        \row
            \li Convert to Decimal
            \li Converts an integer literal to decimal representation
            \li Numeric literal
        \row
            \li Convert to Hexadecimal
            \li Converts an integer literal to hexadecimal representation
            \li Numeric literal
        \row
            \li Convert to Octal
            \li Converts an integer literal to octal representation
            \li Numeric literal
        \row
            \li Convert to Objective-C String Literal
            \li Converts a string literal to an Objective-C string literal if
                the file type is Objective-C(++). For example, rewrites the
                following strings

    \code
    "abcd"
    QLatin1String("abcd")
    QLatin1Literal("abcd")
    \endcode

                as

    \code
    @"abcd"
    \endcode
            \li String literal
        \row
            \li Enclose in QLatin1Char()
            \li Sets the encoding for a character to Latin-1, unless the
                character is already enclosed in QLatin1Char, QT_TRANSLATE_NOOP,
                tr, trUtf8, QLatin1Literal, or QLatin1String. For example,
                rewrites

    \code
    'a'
    \endcode

                as

    \code
    QLatin1Char('a')
    \endcode
            \li String literal
        \row
            \li Enclose in QLatin1String()
            \li Sets the encoding for a string to Latin-1, unless the string is
                already enclosed in QLatin1Char, QT_TRANSLATE_NOOP, tr, trUtf8,
                QLatin1Literal, or QLatin1String. For example, rewrites
    \code
    "abcd"
    \endcode

                as

    \code
    QLatin1String("abcd")
    \endcode

            \li String literal

        \row
            \li Mark as Translatable
            \li Marks a string translatable. For example, rewrites \c "abcd"
                with one of the following options, depending on which of them is
                available:

    \code
    tr("abcd")
    QCoreApplication::translate("CONTEXT", "abcd")
    QT_TRANSLATE_NOOP("GLOBAL", "abcd")
    \endcode

            \li String literal

        \row
            \li Add Definition in ...
            \li Inserts a definition stub for a function declaration either in
                the header file (inside or outside the class) or in the
                implementation file. For free functions, inserts the definition
                after the declaration of the function or in the implementation
                file. Qualified names are minimized when possible, instead of
                always being fully expanded.

                For example, rewrites

    \code
    Class Foo {
        void bar();
    };
    \endcode

                as (inside class)

    \code
    Class Foo {
        void bar() {

        }
    };
    \endcode

                as (outside class)

    \code
    Class Foo {
        void bar();
    };

    void Foo::bar()
    {

    }
    \endcode

                as (in implementation file)

    \code
    // Header file
    Class Foo {
        void bar();
    };

    // Implementation file
    void Foo::bar()
    {

    }
    \endcode

            \li Function name
        \row
            \li Add \c Function Declaration
            \li Inserts the member function declaration that matches the member
                function definition into the class declaration. The function can
                be \c {public}, \c {protected}, \c {private}, \c {public slot},
                \c {protected slot}, or \c {private slot}.
            \li Function name
        \row
            \li Switch with Next/Previous Parameter
            \li Moves a parameter down or up one position in a parameter list.
            \li Parameter in the declaration or definition of a function
        \row
            \li Extract Function
            \li Moves the selected code to a new function and replaces the block
                of code with a call to the new function. Enter a name for the
                function in the \uicontrol {Extract Function Refactoring}
                dialog.
            \li Block of code selected
        \row
            \li Extract Constant as Function Parameter
            \li Replaces the selected literal and all its occurrences with the
                function parameter \c{newParameter}. The parameter
                \c{newParameter} will have the original literal as the default
                value.
            \li Block of code selected
        \row
            \li Add Local Declaration
            \li Adds the type of an assignee, if the type of the right-hand
                side of the assignment is known. For example, rewrites

    \code
    a = foo();
    \endcode

                as

    \code
    Type a = foo();
    \endcode

                where Type is the return type of \c {foo()}

            \li Assignee

        \row
            \li Convert to Camel Case
            \li Converts a symbol name to camel case, where elements of the name
                are joined without delimiter characters and the initial
                character of each element is capitalized. For example, rewrites
                \c an_example_symbol as \c anExampleSymbol and
                \c AN_EXAMPLE_SYMBOL as \c AnExampleSymbol
            \li Identifier
        \row
            \li Complete Switch Statement
            \li Adds all possible cases to a switch statement of the type
                \c enum
            \li \c switch
        \row
            \li Generate Missing Q_PROPERTY Members
            \li Adds missing members to a \c Q_PROPERTY:
            \list
                \li \c read function
                \li \c write function, if there is a WRITE
                \li \c {onChanged} signal, if there is a NOTIFY
                \li data member with the name \c {m_<propertyName>}
            \endlist
            \li \c Q_PROPERTY
        \row
            \li Apply Changes
            \li Keeps function declarations and definitions synchronized by
                checking for the matching declaration or definition when you
                edit a function signature and by applying the changes to the
                matching code.
            \li Function signature. When this action is available, a light bulb
                icon appears: \inlineimage refactormarker.png
        \row
            \li Add #include for undeclared or forward declared identifier
            \li Adds an \c {#include} directive to the current file to make the
                definition of a symbol available.
            \li Undeclared identifier
        \row
            \li Reformat Pointers or References
            \li Reformats declarations with pointers or references according
                to the code style settings for the current project. In case no
                project is open, the current global code style settings are
                used.

                For example, rewrites:

    \code
    char*s;
    \endcode

                as

    \code
    char *s;
    \endcode

                When applied to selections, all suitable declarations in the
                selection are rewritten.

            \li Declarations with pointers or references and selections
                containing such declarations
        \row
            \li Create Getter and Setter Member Functions
            \li Creates either both getter and setter member functions for
                member variables or only a getter or setter.
            \li Member variable in class definition
        \row
            \li Move Function Definition
            \li Moves a function definition to the implementation file, outside
                the class or back to its declaration. For example, rewrites:
    \code
    class Foo
    {
      void bar()
      {
          // do stuff here
      }
    };
    \endcode

                as
    \code
    class Foo
    {
      void bar();
    };

    void Foo::bar() {
        // do stuff here
    }
    \endcode

            \li Function signature
        \row
            \li Move All Function Definitions
            \li Moves all function definitions to the implementation file or
                outside the class. For example, rewrites:
    \code
    class Foo
    {
      void bar()
      {
          // do stuff here
      }
      void baz()
      {
          // do stuff here
      }
    };
    \endcode

                as

    \code
    class Foo
    {
      void bar();
      void baz();
    };

    void Foo::bar() {
        // do stuff here
    }

    void Foo::baz() {
        // do stuff here
    }
    \endcode

            \li Class name
        \row
            \li Assign to Local Variable
            \li Adds a local variable which stores the return value of a
                function call or a new expression. For example, rewrites:

    \code
    QString s;
    s.toLatin1();
    \endcode

                as

    \code
    QString s;
    QByteArray latin1 = s.toLatin1();
    \endcode

                and

    \code
    new Foo;
    \endcode

                as

    \code
    Foo * localFoo = new Foo;
    \endcode

            \li Function call or class name
        \row
            \li Insert (Pure) Virtual Functions
            \li Select an insertion mode:
            \list
                \li Insert only declarations.
                \li Insert declarations and the corresponding definitions inside the class.
                \li Insert declarations and the corresponding definitions outside the class.
                \li Insert declarations and the corresponding definitions in the implementation file
                (only if an implementation file exists).
            \endlist
            \image qtcreator-refactoring-virtual-function-dialog.png

            \li Class or base class name
        \row
            \li Optimize for-Loop
            \li Rewrites post increment operators as pre increment operators and
                post decrement operators as pre decrement operators. It also
                moves other than string or numeric literals and id expressions
                from the condition of a for loop to its initializer. For
                example, rewrites:

    \code
    for (int i = 0; i < 3 * 2; i++)
    \endcode

                as

    \code
    for (int i = 0, total = 3 * 2; i < total; ++i)
    \endcode
            \li \c for

        \row
            \li Escape String Literal as UTF-8
            \li Escapes non-ASCII characters in a string literal to hexadecimal
                escape sequences. String Literals are handled as UTF-8.
            \li String literal

        \row
            \li Unescape String Literal as UTF-8
            \li Unescapes octal or hexadecimal escape sequences in a string
                literal. String Literals are handled as UTF-8.
            \li String literal

        \row
            \li Convert to Stack Variable
            \li Converts the selected pointer to a stack variable. For example,
                rewrites:

    \code
    QByteArray *foo = new QByteArray("foo");
    foo->append("bar");
    \endcode

                as

    \code
    QByteArray foo("foo");
    foo.append("bar");
    \endcode

                This operation is limited to work only within function scope.
                Also, the coding style for pointers and references is not
                respected yet.
            \li Pointer Variable

        \row
            \li Convert to Pointer
            \li Converts the selected stack variable to a pointer. For example,
                rewrites:

    \code
    QByteArray foo = "foo";
    foo.append("bar");
    \endcode

                as

    \code
    QByteArray *foo = new QByteArray("foo");
    foo->append("bar");
    \endcode

                This operation is limited to work only within function scope.
                Also, the coding style for pointers and references is not
                respected yet.
            \li Stack Variable
        \row
            \li Convert connect() to Qt 5 Style
            \li Converts a Qt 4 QObject::connect() to Qt 5 style.
            \li QObject::connect() (Qt 4 style)
    \endtable
    \endif

    \section2 Refactoring QML Code

    You can apply the following types of refactoring actions to QML code:

    \list
        \li Rename IDs
        \li Split initializers
        \li Move a QML type into a separate file to reuse it in other .qml files
    \endlist

    The following table summarizes the refactoring actions for QML code. The
    action is available when the cursor is in the position described in the
    Activation column.

    \table
        \header
            \li Refactoring Action
            \li Description
            \li Activation

        \row
            \li Move Component into Separate File
            \li Moves a QML type into a separate file. Give the new component a
                name and select whether properties are set for the new component
                or for the original one.

                \image qtcreator-move-component-into-separate-file.png

            \li QML type name. This action is also available in the
                \uicontrol {Form Editor} in the Design mode.
        \row
            \li Split Initializer
            \li Reformats a one-line type into a multi-line type. For example,
                rewrites

    \code
    Item { x: 10; y: 20; width: 10 }
    \endcode

                as

    \code
    Item {
        x: 10;
        y: 20;
        width: 10
    }
    \endcode

            \li QML type property
        \row
            \li Wrap Component in Loader
            \li Wraps the type in a Component type and loads it dynamically in a
                Loader type. This is usually done to improve startup time.
            \li QML type name
        \row
            \li Add a message suppression comment
            \li Prepends the line with an annotation comment that stops the
                message from being generated.
            \li Error, warning or hint from static analysis

    \endtable
   */