Skip to content

fix: scope CM6 Tidy keymap per file state (#4093)#4097

Open
dimssu wants to merge 1 commit intoprocessing:develop-codemirror-v6from
dimssu:fix/issue-4093-tidy-cm6
Open

fix: scope CM6 Tidy keymap per file state (#4093)#4097
dimssu wants to merge 1 commit intoprocessing:develop-codemirror-v6from
dimssu:fix/issue-4093-tidy-cm6

Conversation

@dimssu
Copy link
Copy Markdown

@dimssu dimssu commented Apr 23, 2026

Issue:

Fixes #4093

In the CM6 editor (develop-codemirror-v6), the Tidy shortcut (Cmd/Ctrl+Shift+F) works on the first file but misbehaves on subsequent files — sometimes nothing happens, sometimes the file is reformatted as "one big block."

Root cause: createNewFileState in client/modules/IDE/components/Editor/stateUtils.js pushed a Shift-Mod-F binding into a module-scoped extraKeymaps array, closing over the current file's mode. Because later-created file states snapshot that shared array, files created after the first end up with multiple tidy handlers (one per previously-initialized file's mode). On a CSS file, the JS-mode tidy runs first → prettier's babel parser throws on CSS, or (for files it can parse) mangles the content before the correct-mode tidy ever runs.

The Edit → Tidy Code menu click path was unaffected because it computes the mode from file.name at call time.

Changes:

  • stateUtils.js: stop mutating the module-level extraKeymaps. Build the tidy binding as a per-state local array (fileTidyKeymap) and include it in the state's keymaps. Handler now returns true so CM6 consumes the shortcut.
  • stateUtils.test.js: new regression test. Creates JS, CSS, and HTML file states in order, dispatches Shift-Ctrl-F via runScopeHandlers, and asserts each file is tidied with its own mode. Test fails on the previous code (prettier throws parsing CSS as JS) and passes on the fix.

Testing:

Manually verified that pressing Cmd+Shift+F now tidies correctly in sketch.js, style.css, and index.html regardless of which file is opened first.

I have verified that this pull request:

  • has no linting errors (npm run lint)
  • has no test errors (npm run test — Editor suite; 9/9 pass)
  • has no typecheck errors (npm run typecheck)
  • is from a uniquely-named feature branch and is up to date with the `develop-codemirror-v6` branch.
  • is descriptively named and links to an issue number, i.e. `Fixes [CodeMirror v6] Tidy Feature Works Inconsistently #4093`
  • meets the standards outlined in the accessibility guidelines (no UI changes)

createNewFileState mutated a module-scoped extraKeymaps array by pushing
a Shift-Mod-F binding that closed over the current file's mode. Because
the array was shared across every file's state, later-created files
accumulated multiple tidy handlers — Cmd+Shift+F on a CSS/HTML file ran
prettier with the JS parser first, either throwing or mangling output.

Move the tidy binding into a per-state local array and return true from
the handler so CM6 consumes the shortcut. Add a regression test that
creates JS/CSS/HTML states in order and verifies each tidies with its
own mode.
@yugalkaushik
Copy link
Copy Markdown
Contributor

@dimssu Thank you for the PR. Please share the video preview for the fix

@aadityasingh9601
Copy link
Copy Markdown

Hi @dimssu, I think we can't raise a PR for an issue with the "Awaiting Maintainer Approval" label. I think you should first let a maintainer approve the issue, only then raise a PR.

Thanks!

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.

3 participants