Skip to content

[CodeMirror v6] Tidy Feature Works Inconsistently #4093

Closed

Description

@susan-t

p5.js version

No response

What is your operating system?

Mac OS

Web browser and version

Chrome, Firefox

Actual Behavior

In the v6 editor, when tidying code in one file, it works as normal. However it inconsistently works when trying to tidy code in other files. Sometimes nothing is tidied and the feature breaks for all files and sometimes the feature causes all the code to become one big code block.

Screen.Recording.2026-04-22.at.1.36.02.PM.mov

Expected Behavior

On the regular p5.js site, the tidy feature works consistently.

Screen.Recording.2026-04-22.at.1.37.33.PM.mov

Steps to reproduce

Steps:

  1. Open the v6 sandbox editor
  2. Write some code that is formatted incorrectly into each file segment.
  3. Try and tidy up each file.

Snippet:

// Paste your code here :)

Activity

  1. welcome commented on Apr 22, 2026

    @welcome

    Welcome! 👋 Thanks for opening your first issue here! And to ensure the community is able to respond to your issue, be sure to follow the issue template if you haven't already.

  2. dimssu commented on Apr 23, 2026

    @dimssu
    Contributor

    Happy to take this up!

  3. aadityasingh9601 commented on Apr 23, 2026

    @aadityasingh9601

    Hi @susan-t,

    I am also facing the same issue.
    The "Tidy Code" feature works only for the JavaScript file & not working at all for the HTML & CSS files.

    OS -> Windows.
    Web browser -> Brave & MS Edge

  4. dimssu commented on Apr 23, 2026

    @dimssu
    Contributor

    Hi maintainers — I've root-caused this and a fix is ready. The bug is in the CM6 editor's state setup (client/modules/IDE/components/Editor/stateUtils.js): createNewFileState mutates a module-scoped extraKeymaps array by pushing a Shift-Mod-F tidy binding that closes over the current file's mode. Because the array is shared across every file's state, later-created files end up with multiple tidy handlers for the same shortcut (one per mode), so Cmd+Shift+F on e.g. a CSS file ends up running the JS-mode prettier first (throws or mangles) and then cascading through. The first file created tidies correctly; the rest don't. Fix is a minimal, local-array rework plus a regression test. Could I be assigned so I can open a PR? Targeting develop-codemirror-v6.

  5. Geethegreat commented on Apr 24, 2026

    @Geethegreat
    Member

    Hi! I was able to reproduce the bug on the v6 sandbox at https://app-ted0c74.release.editor.p5js.org/ — tidy works fine on the JS file but behaves inconsistently on CSS and HTML files.
    I found the root cause in stateUtils.js. extraKeymaps is a module-level array, and inside createNewFileState a new Shift-Mod-F entry is pushed into it each time a file state is created:
    jsextraKeymaps.push({ key: `Shift-Mod-F`, run: (cmView) => tidyCodeWithPrettier(cmView, mode) });
    Since CodeMirror fires the first matching keymap entry, the tidy function always runs with the first file's mode regardless of which file is currently active. So if JS loads first, Shift-Mod-F always tries to tidy as JavaScript even when you're in CSS or HTML.
    I think the fix is to create a local tidyKeymap inside createNewFileState instead of pushing to the shared extraKeymaps.

  6. yugalkaushik commented on May 9, 2026

    @yugalkaushik
    Contributor

    https://github.com/user-attachments/assets/38334c3c-aaab-4667-9a35-658800d61a42
    Hi, I tried replicating the issue on my end, but everything seems to be working as expected. The tidy code functionality itself works perfectly fine for me, although I was not able to trigger it using the Ctrl + Shift + F shortcut key.
    @susan-t could you please check again and let us know if you are still facing the issue?

  7. susan-t commented on May 9, 2026

    @susan-t
    Author

    @yugalkaushik I was only facing the issue through the v6 sandbox editor!! However, since the website is not up anymore, I'm not sure if it's an issue any longer!

  8. yugalkaushik commented on May 11, 2026

    @yugalkaushik
    Contributor

    https://app-tedc8aa.release.editor.p5js.org/ So this is the new link for the v6 editor. I tested it out and the issue still persists for non-JS files. I'll look into it.

  9. added
    Help WantedWould love additional input or contributions!
    and removed on May 11, 2026
  10. yugalkaushik commented on May 13, 2026

    @yugalkaushik
    Contributor

    @skyash-dev Yeah, it works perfectly fine locally. I just wanted to check whether @dimssu was able to replicate the issue since he created a pull request for the fix.

  11. skyyash commented on May 14, 2026

    @skyyash
    Contributor

    unable to reproduce this locally! (i accidentally deleted the previous comment:/)

  12. added 2 commits that reference this issue on May 29, 2026
  13. khanniie commented on May 29, 2026

    @khanniie
    Member

    Hi everyone! Appreciate the testing and really glad we caught this! I just merged in @dimssu 's fix, would you be able to verify if the issue is now fixed on your ends? If so I will resolve the issue.

  14. skyyash commented on May 30, 2026

    @skyyash
    Contributor

    hi @khanniie!

    i can still reproduce this on the sandbox environment and whats interesting to me is that it is inconsistent only when triggered from edit -> tidy code and works just fine when triggered through ctrl+shift+f...

    recording.mp4

    also i cant reproduce this locally on the cm6 branch.

    recording.mp4
  15. yugalkaushik commented on May 30, 2026

    @yugalkaushik
    Contributor

    @skyash-dev I agree with you as I’m also unable to replicate the issue locally and on my end, tidy is not behaving as expected either.

    preview-cm6.mp4
  16. khanniie commented on May 30, 2026

    @khanniie
    Member

    @yugalkaushik @skyash-dev Thank you both for the testing and quick response! I took a second pass and I think I've fixed it for myself on staging and locally, would you be able to check to see if it's fixed on your ends as well?

  17. skyyash commented on May 31, 2026

    @skyyash
    Contributor

    @khanniie yes it's fixed, kudos!

    curious to know, what was the solution?

  18. khanniie commented on May 31, 2026

    @khanniie
    Member

    @khanniie yes it's fixed, kudos!

    curious to know, what was the solution?

    @skyash-dev thank you!! :D haha I'm still wrapping my head around it myself but I believe the header bar IDE file that was calling tidyCode was holding onto a stale reference of the function (so it was pointing towards the wrong file name and type). The fix was to change the click={cmRef.current.tidyCode} to click={()=>cmRef.current.tidyCode()}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugError or unexpected behaviorsHelp WantedWould love additional input or contributions!

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions