# Improve the handling of \`zig fmt: off/on\` · gitcafe/zig

[View on GitCafe](https://git.cafe/gitcafe/zig/commit/c98f792ff8ed7af0d5e427836600c2ed7b30965e)

Repository: [gitcafe/zig](https://git.cafe/gitcafe/zig)

Visibility: public

Requested revision: c98f792ff8ed7af0d5e427836600c2ed7b30965e

Requested commit: c98f792ff8ed7af0d5e427836600c2ed7b30965e

Commit: c98f792ff8ed7af0d5e427836600c2ed7b30965e

Tree: 5de21dd53c87442e41e5734d065101ec64dc3f2f

Author: yvt

Committer: Andrew Kelley

## Message

```
Improve the handling of `zig fmt: off/on`

This commit reworks the handling of `zig fmt: off/on`. A motivating
example is shown below:

    const c = d;
    // zig fmt: off
    // comment
    const a = b;
    // zig fmt: on

Before processing the decl `const a = b;`, `renderRoot` looks for
`zig fmt: off` that appears between this decl and the previous one. If
it finds one, it searches for the next `zig fmt: on` that re-enables
reformatting (or EOF if none was found), and copies the input code
between `zig fmt: off` and `zig fmt: on` to the output stream. After
that, it proceeds to the next decl.

The important thing to notice here is that `renderTopLevelDecl` emits
line comment tokens that follow the decl. Therefore, when copying code,
we must be careful not to copy the line comment tokens that already have
been written to the output stream. The original code failed to take this
fact into consideration. It did skip `// zig fmt: off`, but not the
remaining ones. As a result, when the above example is fed as input, it
duplicated the line `// comment`.

```

## Parents

- [7139eef4cfc7c75655f9951849a994b116e96abe](https://git.cafe/gitcafe/zig/commit/7139eef4cfc7c75655f9951849a994b116e96abe?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zig/tree/c98f792ff8ed7af0d5e427836600c2ed7b30965e?format=markdown)
