Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions .cursor/rules/java-style.mdc
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ End-of-line braces show up in a minority of files (`MemBuf`, `Buffer`, `BinaryWr

A single-statement `if` or `else` may be unbraced. The statement goes on the next line, indented one level. Never `if (x) return;`.

`for` and `while` are braced even when the body is one statement. Brace new loops.
A single-statement `for` or `while` may be unbraced the same way. Brace a loop when the body is longer than one statement.

After a `for` or `while`, leave a blank line before the next statement. That applies to a one-line body and to a braced body. Skip the blank line when the loop is the last thing in the method: the closing brace of the method comes next.
A braced block always gets one blank line after it before the next statement, unless it is the last thing in its enclosing block. A braceless single-line loop or `if` follows the grouping rules below.

```java
for (String filename : files)
Expand All @@ -78,6 +78,18 @@ for (String folderName : folders.keySet())
}
```

## Blank lines

Group the lines that do one step, and put one blank line between steps. A run of the same kind of statement stays together. When that run switches to a different kind, add a blank line.

A long run of the same kind of statement is not one step just because the lines look alike. Split where a new stage begins, when later lines are built from the results above.

A braced block, anything that ends in `}`, gets one blank line after it before the next statement, unless it is the last thing in its enclosing block. Nested blocks that end together share that one blank line. A braced loop with a `return` after it gets that blank too, even in a short method. The parts of one construct stay joined, with no blank between them: `if`/`else` (including `else if`), `try`/`catch`/`finally`, and `do`/`while`. So `} else {`, `} catch (...) {`, `} finally {`, and `} while (...);` attach to the block before them. The blank line comes after the whole construct.

A braceless single-line `if`, `for`, or `while` may skip the blank. Tiny guards stay together. A step's lines stay grouped, and a blank line separates steps. A chain of `if`/`return` stays together, and the fallback `return` gets a blank line above it. Back-to-back short similar blocks, and `if` pairs that read like `if`/`else`, get no blank between them.

Put a blank line before a `return`, except when it is the only line in its block, or when it follows one or two short statements. A braceless loop in a very short block may return on the next line. A braced loop may not.

## Methods, signatures, tests

Leave one blank line between methods. The formatter preserves a blank line that is already there and will not insert one you forgot.
Expand Down
Loading