Skip to content

feat: add scrap format - #144

Merged
skipbit merged 10 commits into
mainfrom
feat/format-command
Nov 22, 2026
Merged

skipbit merged 10 commits into
mainfrom
feat/format-command

Conversation

@skipbit

@skipbit skipbit commented Nov 22, 2026 •

Copy link
Copy Markdown
Owner

Summary

Adds scrap format [<path>...], which formats in place, with the clang-format found on PATH, the files of the project that the paths name, or all of them. --check comes in a later change.

$ scrap format
Using clang-format '/usr/bin/clang-format' (version 18.1.3)
$ scrap format tools/gen.cpp tools
warning: leaving out 'tools/gen.cpp', which scrap format does not cover
warning: leaving out 'tools', which holds nothing scrap format covers
  • Paths. The project is searched for from the first path, or from the directory holding it when it names a file. Every path is resolved through the file system first, so a path spelled in another letter case on a file system that ignores case, or a path through a symbolic link, names the file it reaches. A path that names nothing, and a path in another project, nested ones included, are errors, reported as given before any file is formatted:
    error: 'src/nope.cpp' does not exist
    hint: pass a file or directory inside the project, or omit the path to format the whole project
    error: '/tmp/x.cpp' is not in the project at '/home/me/proj'
    hint: run scrap format once for each project
    
  • Files. The files come from filesToFormat and selectFiles. A named file outside them is left out with a warning, and so is a named directory below which none of them lie. When nothing is left, the command exits with 0 without looking for clang-format. A project with no target still has its files below src/ and include/ formatted.
  • clang-format. It is looked for on PATH, and named with its version before any file is formatted. When there is none:
    error: clang-format not found in PATH
    hint: install clang-format and make sure it is on PATH
    
    The style of [format] is passed to --style as written.
  • Failures. A file clang-format fails on is reported first, with what clang-format wrote below it, and the other files are still formatted; the command then exits with 1. A clang-format that cannot be started stops the command at the first file, as it would fail every file the same way.
    error: failed to format 'src/a.cpp'
    Invalid value for -style
    
    A clang-format that fails and writes nothing is reported with its exit status, such as error: failed to format 'src/a.cpp': clang-format exited with status 1.

formatFile no longer returns the failure of a check that could not read the file back, which only checkFile can meet, so the command handles only what a format can return. Each kind of failure is rendered by its own function, chosen with std::visit, so a kind added without one fails the build.

The README describes the command and [format].

Tests

  • ProjectPathsTest, 12 tests: where the search starts for a file and a directory, the root named as a directory, a path that names nothing (first or later, through .., or below a file), a path that cannot be examined, an empty path, a project that cannot be found, a path in a nested project and outside any project, a symbolic link, and a path in another letter case. The last one runs only on a file system that ignores case.
  • ProjectPathsTextTest, 6 tests, and FormatTextTest, 8 tests: the messages above, a signal, an exit status with nothing written, a clang-format that could not be started or did not give its version, and escaping what clang-format wrote.
  • FileSelectionTest, 1 more test: a named directory that selects nothing is listed once, in the order named.
  • CliE2ETest, 11 tests. Eight run a script that stands in for clang-format on PATH: formatting the project and leaving out a file outside it, named files and directories, a named directory that holds nothing to format, a path that names nothing, passing the style, an unknown key in [format], no clang-format, a failing file followed by the next, and a clang-format that cannot be started. Three run the clang-format of the PATH the tests run with, and skip when there is none: following .clang-format, following the style of scrap.toml instead, and a style clang-format rejects. In the checks of this pull request they ran on the Linux jobs of build-and-test and on diff-coverage, and skipped on macOS and in the package jobs.

Each of these fails at least one test when taken out: starting the search from the directory of a file, the check that later paths are in the project, resolving paths through the file system, treating a path below a file as naming nothing, reporting a path that cannot be examined as given, listing a directory that selects nothing and its warning, the exit status with nothing written, going on after a failed file, the warning, passing the style, the exit without clang-format when nothing is left, stopping at a clang-format that cannot be started, the line naming clang-format, and going on to the next file after one is formatted.

A format never reads the file back, so it no longer returns the failure
of a check that could not read it.
The project is searched for from the first path, or from the directory
holding it when it names a file. Every path is resolved through the
filesystem, so a path spelled in another letter case where case is
ignored, or through a link, names the file it reaches. A path that
names nothing, or that lies in another project, is reported.
scrap format [<path>...] formats in place, with the clang-format found
on PATH, the files of the project that the paths name, or all of them.
The clang-format used is named with its version first. A named file the
command does not cover is left out with a warning. A file clang-format
fails on is reported with what clang-format wrote, and the other files
are still formatted.
Each kind of failure has its own renderer, chosen with std::visit, so a
kind added without one fails the build. The failures of clang-format
share one renderer that takes the name of the tool, and the line for an
empty path argument is written in one place.
scrap names the clang-format it found by the directory PATH lists. On
macOS the temporary directory is reached through a symbolic link, so a
canonical path is not what scrap names.
A named directory that holds nothing scrap format covers is left out
with a warning, as a named file it does not cover is, so a format that
had nothing to do there does not pass in silence.
When clang-format fails and writes nothing, the failure says the status
it exited with, the one thing left to tell why.
A path argument that cannot be examined is shown as it was typed, as a
path that names nothing already is, not joined to the working
directory.
@skipbit
skipbit merged commit 130232e into main Nov 22, 2026
29 checks passed
@skipbit
skipbit deleted the feat/format-command branch November 22, 2026 14:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant