p5.js-web-editor icon indicating copy to clipboard operation
p5.js-web-editor copied to clipboard

[CodeMirror v6] Tidy Feature Works Inconsistently

Open susan-t opened this issue 7 months ago • 16 comments

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:

  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 :)

susan-t avatar Apr 22 '26 17:04 susan-t

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.

welcome[bot] avatar Apr 22 '26 17:04 welcome[bot]

Happy to take this up!

dimssu avatar Apr 23 '26 02:04 dimssu

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

aadityasingh9601 avatar Apr 23 '26 08:04 aadityasingh9601

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.

dimssu avatar Apr 23 '26 19:04 dimssu

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.

Geethegreat avatar Apr 24 '26 04:04 Geethegreat

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 avatar May 09 '26 20:05 yugalkaushik

@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!

susan-t avatar May 09 '26 20:05 susan-t

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.

yugalkaushik avatar May 11 '26 07:05 yugalkaushik

@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.

yugalkaushik avatar May 13 '26 17:05 yugalkaushik

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

skyyash avatar May 14 '26 16:05 skyyash

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.

khanniie avatar May 29 '26 23:05 khanniie

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

skyyash avatar May 30 '26 13:05 skyyash

@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 avatar May 30 '26 13:05 yugalkaushik

@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 avatar May 30 '26 23:05 khanniie

@khanniie yes it's fixed, kudos!

curious to know, what was the solution?

skyyash avatar May 31 '26 05:05 skyyash

@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()}

khanniie avatar May 31 '26 05:05 khanniie