CurlyBlocks — Add Missing Curly Braces
CurlyBlocks adds curly braces to if, else if, else, for, while, and
do...while statements whose body is a single statement written without
braces.
Example
// Before
if (x > 0) console.log(x);
// After
if (x > 0) {
console.log(x);
}
Usage
- Run CurlyBlocks: Add Missing Curly Braces from the Command Palette, the
editor context menu, or with
Ctrl+Shift+C (Cmd+Shift+C on macOS).
- With no selection, the whole document is processed. With one or more
selections, only the selected ranges are processed.
- Indentation follows the editor's tab/space settings.
Features
- Adds braces to single-statement
if / else if / else / for / while /
do...while bodies.
- Leaves already-braced blocks untouched, so running it repeatedly is safe.
- Skips control keywords that appear inside strings, template literals,
comments, and regular-expression literals.
- Leaves a body untouched when its extent is ambiguous rather than risk
changing the meaning of the code.
Supported languages
By default the command runs on these language identifiers:
javascript, javascriptreact, typescript, typescriptreact, c, cpp,
csharp, java, go, rust, php, dart, groovy, kotlin, swift,
scala.
Change the list with the curlyBlocks.languages setting.
Settings
| Setting |
Description |
curlyBlocks.languages |
Language identifiers the command is allowed to run on. |
Limitations
- The formatter is text-based, not a full parser. It wraps a body only when its
boundary is unambiguous; bodies inside the parenthesised header of a statement
are not reformatted.
try / catch / finally already require braces in JavaScript, so they are
not modified.