AppCode 2016.2 Help

Completion

Shortcuts and settings

Basic Completion ⌃Space
Smart Type completion ⌃⇧Space
Complete Statement ⌃⇧⏎
Preferences Editor | General | Code Completion

Basic completion

Basic code completion (⌃Space) is the one everyone expects to be there in a decent IDE: complete the name of any class, method or a variable instantly as you type.

Quite natural, isn't it? However AppCode is way more intelligent than just that, though we wouldn't want anyone to think that we know better ) That's why all the smart completions are separated from the basic ones – you got them when you need them, but there's no pressure.

Smart Type completion

Smart Type completion (⌃⇧Space) analyzes the context you are currently working with and prepares the list of choices for you. It filters the list of methods and variables to match the expected expression type. For example, it can suggest a constructor or factory method of a class expected:

ac_Tsmart_completion@2x

If Smart Type completion didn’t suggest you anything, try pressing the same shortcut ⌃⇧Space one more time: AppCode will look up values of the expected type which can be retrieved through a chained method call.

Complete Statement

With Complete Statement (⌃⇧⏎) AppCode inserts parentheses, braces, semicolons, quotes, etc. for you where necessary, and then moves the caret in position where you can start typing the next statement automatically:

Settings

To change the completion behaviour, go to Preferences | Editor | General | Code Completion. By default, completion in AppCode is case-sensitive, but you can change it here at any time:

ac_Tcompletions_settings@2x

Features

There are several completion features specific for AppCode which sometimes can be really helpful. Let’s go through them:

  • When calling a method on a variable you can forget about square brackets – AppCode will add them automatically:
  • Finishing code completion with Tab. It doesn't matter which type of completion you use, basic or Smart Type, the finishing logic described here will work the same way for both of them. When you press Tab instead of on the selected item instead of just inserting the element, AppCode overwrites the following expression with the selected option taking the structure around into account:
  • Finishing code completion with a symbol. Here, by symbol we mean a dot, a colon, a bracket… Finishing completion of an element with one of those characters also completes the statement:
    Note, that before finishing code completion with a symbol, you need to select Insert selected variant by typing dot, space, etc option in completion preferences.
  • CamelHumps. To complete your code even faster, and to limit the search suggestions to exactly what you need, just use "CamelHumps": AppCode identifies parts of compound names composed according to CamelCase, where each part starts with a capital letter:
  • Blocks completion. Even if you mistakenly deleted the block placeholder after completing the function with the block as a parameter, you can invoke Basic or Smart completion, select the block from completion list and expand it by pressing :

Try in action

We will implement a simple cellForRowAtIndexPath method for the UITableView in the UIViewController using different completion kinds and features.

  1. First step is to dequeue the table cell and introduce the needed variable:
    Note that you do not need to care about square brackets in this case – AppCode will insert them automatically.
  2. For this sample let’s use formatted NSString as unique cell identifier. Invoke Smart completion and quickly insert [NSString stringWithFormat]:
    After specifying the id, do not move the caret using the keyboard or mouse – just invoke Complete Statement, and the semicolon will be inserted automatically.
  3. To get the cell instance, let’s create a local variable using Introduce Variable (⌃⌥V and then use Complete Statement to quickly jump to the new line:
  4. As usual, we should check if the cell was already created. And here goes another use-case for Complete Statement: type if and finish completion with ⌃⇧⏎ to jump into the parentheses for specifying an expression, and one more time to jump inside the curly braces:
    Complete Statement can also be used with other control statements, such as for, while, do and switch.
  5. Invoke Smart Completion to allocate the cell and specify the cell type after it:
  6. Oh, wait! We need the cell identifier again, and there is no sense to have a duplicate in the code. Let’s separate it into a variable and then re-use it:
  7. Move the caret to the initWithStyle method, jump to the second parameter using Tab, invoke Basic Completion and finish it with Complete Statement again:
  8. Now we can return the cell from the method – invoke Basic Completion and finish it with Tab instead of :

That’s it! Make use of different completion types and you’ll see the benefits immediately!

See Also

Last modified: 13 September 2016