Quick Brace/Unbrace

Quick Brace makes it easy to convert single line statements into a brace-enclosed blocks, so you can add new lines without having to manually position the cursor and type extra keystrokes. Unbrace removes the braces from a block that contains a single line statement.

Tip

  • As you write new code, SlickEdit® Core automatically expands statement templates for common block structures (such as if, for, or while) when you type the initial keyword. See Syntax Expansion for more information.

  • Quick Brace and Unbrace do not support code blocks containing multiple statements, but you can use Dynamic Surround and Unsurround instead.

A single line statement is defined as a single line child statement that is not enclosed in braces, for example:

if ( cond ) doSomething();

Hanging single line statements are often broken across two lines in the editor:

if ( cond ) 
     doSomething();

Using Quick Brace/Unbrace

When you use Quick Brace, SlickEdit Core attempts to honor your brace style and indent settings. To use Quick Brace, position the cursor where you would normally type the open brace, and type it. Using the preceding code samples, you could position the cursor as follows:

if ( cond ) <cursor here> doSomething();

or

if ( cond )
<cursor here> doSomething();

After typing the opening brace, the child statement is moved to the next line if necessary, indented according to your indent settings, and the closing brace is inserted automatically. The result on the preceding code sample is:

if ( cond ) {
     doSomething();
}

Tip

TIP Indentation and brace style settings are specified on the Formatting Options screen specific to your language (Window → SlickEdit Preferences → Languages → [Language Category] → [Language] → Formatting). For C-like languages, use the C/C++ Formatting Options screen.

Unbrace does the opposite of Quick Brace, removing the braces from a brace-enclosed block that contains a single line statement and moving the statement to the preceding line that contains the parent statement (unless it is just too long). To use Unbrace, simply delete the opening brace. Using the preceding code example, the result is as follows:

if ( cond ) doSomething();

You can use Unbrace on any brace-enclosed block that contains a single line statement, not just a block that was created with Quick Brace. Unbrace works on any brace style.

Depending on the original brace style and the column location of the open brace, Unbrace either pulls the statement up to the same line or leaves it hanging. The default column threshold is 40, which can be modified by setting the configuration variable def_hanging_statements_after_col (Macro → Set Macro Variable). Set the value to 1 for statements to always remain on the second line. Set the value to a very large number to always pull up statements to the original line. See Setting/Changing Configuration Variables for more information on setting variables.

Quick Brace and Unbrace work for C/C++ and similar languages that support brace blocks. Statements such as if, while, and for are supported, as well as the handling of else clauses for if statements and the splicing of the close brace with a trailing else.

Disabling Quick Brace/Unbrace

Quick Brace/Unbrace is on by default, and can be enabled/disabled on an language-specific basis. To access this option, from the main menu, click Window → SlickEdit Preferences → Languages, expand your language category and language, then click [Language] Formatting Options and select or clear the option Quick brace/unbrace one line statements.