[CodeMirror v6] Tidy Feature Works Inconsistently
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.
https://github.com/user-attachments/assets/c84fbd7b-16b2-4df0-ad64-a6dc9579bd90
Expected Behavior
On the regular p5.js site, the tidy feature works consistently.
https://github.com/user-attachments/assets/11aba491-8344-43ee-9b6c-f41f243ab78a
Steps to reproduce
Steps:
- Open the v6 sandbox editor
- Write some code that is formatted incorrectly into each file segment.
- Try and tidy up each file.
Snippet:
// Paste your code here :)
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.
Happy to take this up!
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
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.
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.
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?
@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!
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.
@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.
unable to reproduce this locally! (i accidentally deleted the previous comment:/)
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.
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...
https://github.com/user-attachments/assets/c9082dff-6a57-4717-8421-1f01583bffc8
also i cant reproduce this locally on the cm6 branch.
https://github.com/user-attachments/assets/c4bd5049-b413-44c9-bf90-e785385f38d0
@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.
https://github.com/user-attachments/assets/ac5a7342-789c-4df8-949f-6031a52a484f
@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?
@khanniie yes it's fixed, kudos!
curious to know, what was the solution?
@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()}