# Formatting

> How the framework's code is laid out, what clang-format does to code that is laid out any other way, the reasons behind the settings that are not LLVM's, and who runs it.

Source: https://omardev29.github.io/rmp-docs/guidelines/formatting.html

Nobody formats the framework's code by hand. `.clang-format` says how it is laid out, `rmp fmt` lays it out that way, and `rmp test fmt` refuses a file that is not. What follows is what that file decides, why, and what clang-format says about code written any other way — so that you can write it right the first time, and read the diff it makes when you do not.

## Who runs it, and where

| Command | What it does | Where |
|---|---|---|
| `rmp fmt` | formats every file in place | in the framework: `include/`, `src/`, `tests/` and `examples/`; in a game: its own `include/`, `src/` and `tests/`, and not the files that came with the framework: `src/rmp/`, `include/rmp/` and `tests/smoke_test.h` |
| `rmp fmt check` | fails on a file clang-format would change, and changes nothing | the same files |
| `rmp test fmt` | the same, as a stage of `rmp test` | the framework, and its CI's lint job |

A game made with `rmp new` gets the framework's `.clang-format`, so `rmp fmt` lays its code out the same way and `rmp fmt check` refuses a file that is not; neither the game's `rmp test` nor its CI runs them. It leaves the framework's own sources alone because a different clang-format would rewrite them for no reason of the game's.

The version is pinned, and it matters: clang-format one minor version out reformats files that were already right. The framework pins clang-format 22.1.8 in `thirdparty/FROZEN_VERSIONS.md`, and its `tools/versions_check.sh` says so when the one on your `PATH` is another. Each block on this page shown refused is what that version says about it.

## Four spaces, 90 columns, braces on the line

Four spaces and no tab, where LLVM's style, which the file starts from, has two. A brace opens on the line of what it belongs to.

Compiles; `rmp test fmt` refuses it

```cpp
struct Ship {
  int hp = 3;
  float speed = 120.0f;
};
```

```cpp
struct Ship {
    int hp = 3;
    float speed = 120.0f;
};
```

A line is at most 90 columns, not LLVM's 80: wide enough for a declaration of the public headers and the comment after it, which is a shape nearly every line of them has. A longer one is broken where clang-format breaks it:

Compiles; `rmp test fmt` refuses it

```cpp
void announce(const int level, const int lives) { DrawText(TextFormat("Level %d, %d lives left", level, lives), 10, 10, 20, WHITE); }
```

```cpp
void announce(const int level, const int lives) {
    DrawText(TextFormat("Level %d, %d lives left", level, lives), 10, 10, 20, WHITE);
}
```

## No alignment

No column of `=` signs, no comments padded out to line up, no table squared with spaces. A column lined up with a ruler looks tidy in the commit that wrote it and costs something every time after: renaming one field moves twenty lines, the diff of a one-line change is a paragraph, and `git blame` stops pointing at whoever wrote the code and points at whoever moved the spaces. Code takes its shape from its structure, not from padding.

Compiles; `rmp test fmt` refuses it

```cpp
struct Theme {
    Color background    = { 18, 18, 22, 255 }; // behind everything
    Color surface_hover = { 60, 60, 76, 255 };  // a button under the pointer
};
```

```cpp
struct Theme {
    Color background = { 18, 18, 22, 255 }; // behind everything
    Color surface_hover = { 60, 60, 76, 255 }; // a button under the pointer
};
```

One column is left, and clang-format keeps it: the backslashes that carry a macro over several lines line up just past the macro's longest line, so a new longest line moves every one of them.

## A guard on one line

An `if` that leaves early stays on one line, and so does a short loop or a short function. `if (!ready) return;` reads as a guard; a guard spread over three lines stops looking like one. Without braces: a block gets lines of its own.

Compiles; `rmp test fmt` refuses it

```cpp
void step(const bool ready) {
    if (!ready)
        return;
    jump();
}
```

```cpp
void step(const bool ready) {
    if (!ready) return;
    jump();
}
```

A function that fits on a line is written on one, and a block does not start with an empty line:

Compiles; `rmp test fmt` refuses it

```cpp
class Ship {
public:
    [[nodiscard]] int hp() const {

        return _hp;
    }

private:
    int _hp = 3;
};
```

```cpp
class Ship {
public:
    [[nodiscard]] int hp() const { return _hp; }

private:
    int _hp = 3;
};
```

## Classes, namespaces and switches

An access specifier stands at the column of its class, so `public:` and `private:` read as the headings they are:

Compiles; `rmp test fmt` refuses it

```cpp
class Coin {
    public:
    int value = 10;
};
```

```cpp
class Coin {
public:
    int value = 10;
};
```

A namespace does not indent what is in it, and its closing brace says which namespace it closes, however short it is:

Compiles; `rmp test fmt` refuses it

```cpp
namespace layer {
    constexpr unsigned PLAYER = 1U << 0;
}
```

```cpp
namespace layer {
constexpr unsigned PLAYER = 1U << 0;
} // namespace layer
```

A `case` is indented inside its `switch`:

Compiles; `rmp test fmt` refuses it

```cpp
float speed_for(const State state) {
    switch (state) {
    case State::IDLE:
        return 0.0f;
    case State::RUNNING:
        return 120.0f;
    }
    return 0.0f;
}
```

```cpp
float speed_for(const State state) {
    switch (state) {
        case State::IDLE:
            return 0.0f;
        case State::RUNNING:
            return 120.0f;
    }
    return 0.0f;
}
```

## `int &x`, and `{ 1, 2 }`

The `&` of a reference sits against the name, not the type — as the `*` of a pointer does, which is how raylib writes it: `const char *text`. There is no pointer in the framework's own code to show it on ([Pointers and RAII](https://omardev29.github.io/rmp-docs/guidelines/pointers-and-raii.html) says why), but every call into raylib's C has one.

Compiles; `rmp test fmt` refuses it

```cpp
void heal(int& hp, int amount);
```

```cpp
void heal(int &hp, int amount);
```

A braced list keeps a space inside each brace, as raylib's colours are written. LLVM's style closes them up, and every colour of the theme would change.

Compiles; `rmp test fmt` refuses it

```cpp
constexpr Color SKY = Color{135, 206, 235, 255};
```

```cpp
constexpr Color SKY = { 135, 206, 235, 255 };
```

## What clang-format leaves alone

It does not sort the includes. They are in the order they were written, and each one may say after it what it is for; sorting them would scramble that, and in code that speaks to C the order of includes can matter.

```cpp
#include <string_view> // a name on the way in
#include <vector> // the enemies on screen
```

It never rewraps a comment. The framework's file headers carry trees, tables and paragraphs wrapped by hand, and a formatter that rewraps them destroys the part of the code that took longest to write.

And it does not touch the entry-point macros of `rmp/app.h`, which sit between `// clang-format off` and `// clang-format on`: their bodies hold block comments, and every formatter mangles a `/* */` inside a macro's continued lines. They are the only code of the framework's own it is told to leave alone; the vendored libraries and the headers generated from the `.toml` are left out whole, by `.clang-format-ignore`.

## Files

A header is `.h` and a source file `.cpp`, and a header starts with `#pragma once`: every compiler the framework builds with understands it, and it cannot get out of step with the file's name the way an include guard can. clang-tidy has a check that would refuse it, `portability-avoid-pragma-once`, and the framework's `.clang-tidy` turns it off. Nothing checks that a header has it, and every header of the framework does but one: `tests/smoke_test.h`, which the plain C example includes too, has an include guard.

```cpp
#pragma once

namespace game {
inline constexpr int START_LIVES = 3;
} // namespace game
```
