Tips & Tricks

Get Typing Superpowers

CLion does its best to help you develop at the speed of thought. Code generation takes care of the repetitive tasks; code completion saves your time typing, while code navigation lets you move around the code instantly. Talking about quicker code writing, code generation (for getters/setters, constructors/destructors), generate definitions, implement and override functions and create from usage can make you significantly more productive.

The latest CLion EAP adds a new member to the family: Complete Statement.

Complete Statement helps you create syntactically correct code constructs by inserting the necessary syntax elements and getting you in position to start typing the next statement. CLion will insert parentheses, braces, semicolons, quotes, etc. for you as necessary so you avoid moving the caret manually.

To use Complete Statement, press Ctrl+Shift+Enter on Linux/Windows, or ⇧⌘⏎ on OS X.

Let’s take a closer look at some scenarios that now can be sped up. Are you ready for gifs? :)

Let’s start from the very beginning: creating a new namespace. Type the keyword (with the help of completion) and the name, and use Complete Statement to add the braces automatically:

Now that we have a namespace, let’s declare classes, structures, enums or enum classes. Again, there is no need to type ‘{ … };’ – Complete Statement will insert them for you:

Let’s move forward and declare some function and ask CLion to create a definition (Alt+Enter):

Now that we have a definition stub, we can fill it with some code. If you use Complete Statement, control statements like if, if ... else, for, while, do, switch will be completed with the missing braces, as will usual statements. The correct #include directive can be added with a quick-fix (Alt+Enter):

Notice how correct formatting is applied to the resulting code when Complete Statement is used.

While iterating, live templates can be handy to generate code:

However, if you still decide to type it by hand, to introduce some changes to this universal loop template, Complete Statement will again add all the necessary parentheses and braces and position the caret inside the loop, so that you can immediately start implementing the body.

What if there is more than one place to position the caret? Let’s check one more trick for getting a for loop quicker: press the Complete Statement shortcut several times and let the caret “travel” across the for loop so that you can fill the proper part:

If you need to get a function definition, don’t use the Complete Statement after you’ve written the function signature, since it will only add “;” at the end of the line. Complete Statement can’t guess it and so doesn’t add braces by default. Use generate definitions or ‘create from usage’, or a simple completion to get braces automatically and the caret positioned inside the function body, so that you can start implementing it immediately:

That’s it! Doesn’t it look handy?! Try it out for yourself and see how much faster your typing can get with CLion’s help.

Since Complete Statement functionality is rather new, there may be bugs (stable version is planned for CLion 2016.2). Please do share invalid cases with us so that we can fix them before the release. Check it right now. Thanks!

The CLion Team

image description