Skip to content

feat(fmt): sort object keys with the sort-keys setting - #360

Open
simonvanlierde wants to merge 5 commits into
mainfrom
fmt-sort-keys
Open

simonvanlierde wants to merge 5 commits into
mainfrom
fmt-sort-keys

Conversation

@simonvanlierde

Copy link
Copy Markdown
Owner

Adds sort-keys to jsonvet fmt, so it can replace the pretty-format-json hook without losing comments.

  • [format] and per-file-format take sort-keys, off by default. Keys sort at every depth by code point after quotes and escapes are read, with a stable sort, so duplicate keys keep their order.
  • A comment above a key or at the end of its line moves with the key. The comments before the first key stay at the top, and the comments after the last key and in an empty object stay where they are.
  • Range formatting in the language server and --fix never sort. Whole-file formatting in the language server follows the setting.

Fixes #352

Tested: the repo check passes (697 tests). The new tests fail with sorting off, with the header handling off, and with a sort that reverses duplicates. The format fuzz target ran for 10 minutes with sorting on: 1,761,285 runs and no crash.

Not checked: I did not add the setting to the playground, which passes format options to wasm one by one. A file where a suppression hides a problem at a key that sorting moves is still left as it is, because the suppression check counts tokens by position; I did not teach it to follow moved keys.

- Add `sort-keys` to `[format]` and `per-file-format`, off by default: it is the main thing
  the pretty-format-json hook does that `jsonvet fmt` did not.
- Sort keys at every depth by code point after reading quotes and escapes, as Python's
  `sorted` does, with a stable sort so duplicate keys keep their order.
- A comment above a key or at the end of its line moves with the key. The comments before the
  first key are kept apart, in `Tree::heads`, so they stay at the top; the comments after the
  last key and in an empty object were never attached to a key and stay too. Keeping them out of
  `Container` holds the fmt memory peak where it was.
- The language server turns sorting off for range formatting, which cannot move members across
  the object; `--fix` never formats, so it never sorts.
- A key that carries a suppressed problem can move, which the suppression check reads as a
  change of what is hidden, so such a file is left as it is.
- The fuzz target runs its second option set with sorting on.

Fixes #352

Signed-off-by: Simon van Lierde <90462640+simonvanlierde@users.noreply.github.com>
…orting

- A block comment before the first key on its own line, such as /* about b */ "b": 1, was taken into the object's head comments, so after sorting it sat in front of whichever key came first. Only the comments on the lines before the first key stay at the top now.

Signed-off-by: Simon van Lierde <90462640+simonvanlierde@users.noreply.github.com>
* origin/main:
  test(playground): set the 5,000-line text without page.fill (#359)
  docs(editors): add Zed setup (#358)

Signed-off-by: Simon van Lierde <90462640+simonvanlierde@users.noreply.github.com>

# Conflicts:
#	docs/roadmap.md
@github-actions

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Playground screenshots for 7dd83d4, desktop and phone in light and dark: download the zip (kept 14 days).

- The Windows checkout gives docs/format.md CRLF line ends, so splitting on a fenced block found nothing and test (windows-latest) panicked with an index out of bounds.

Signed-off-by: Simon van Lierde <90462640+simonvanlierde@users.noreply.github.com>
- The escaped-pair case only compared a pair with U+FFFF, so a wrong joined code point still sorted last and 10 key_order mutants survived. A pair between its raw neighbours, and escapes that form no pair, now catch them.

- The early returns in Tree::sort_keys (one-member objects and arrays, objects already in order, an empty head list) change no output, so .cargo/mutants.toml excludes their mutants.

Signed-off-by: Simon van Lierde <90462640+simonvanlierde@users.noreply.github.com>

This branch was successfully deployed

1 active deployment
preview — 7dd83d4d Deployed Dec 2, 2026 by simonvanlierde via preview #445
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.

Sort keys in jsonvet fmt, keeping comments with their keys

1 participant