zed icon indicating copy to clipboard operation
zed copied to clipboard

Implement syntactic fold for comments for JS, TS, and TSX

Open Daquisu opened this issue 6 months ago • 4 comments

This PR implements the first five steps of #31395:

  1. Create folds.scm for JavaScript, TypeScript, TSX for comments only (for now) Although the comments are already queried by highlights.scm and text_objects.scm, they are semantically different from querying comments to make them foldable. In the future, we can easily add folds to other syntax structures with this new file. Related: nvim-treesitter uses a folds.scm for each language. TypeScript example.
  1. Modify language.rs to add new fields related to folds_query (e.g. add folds_query to the Grammar struct).
  1. Create function in multi_buffer.rs to query fold information according to folds.scm on demand.
  1. Optimize fold actions to minimize number of folds queries done (e.g. when calling fold_all).
  1. Also optimize the fold indicators in the UI.

The optimizations done were done by either:

i. Precomputing a map of fold starting row to fold range (done in fold actions in general).

ii. Minimizing the bytes parsed by the query (done in the UI).

Manual test:

https://github.com/user-attachments/assets/f744bcfd-2499-456c-ab24-d8a139d2a10f

Release Notes:

  • Implemented syntactic fold for comments for JavaScript, TypeScript, and TSX.

Daquisu avatar May 30 '25 16:05 Daquisu

Seems like we should add the implementation for how folds.scm will be used first before we go defining the folds queries themselves.

maxdeviant avatar May 30 '25 16:05 maxdeviant

Hey, I appreciate that you're taking the time to do this. However, I'd much prefer it if you would implement syntax based folding as a single PR, rather than a bunch of small PR's. This will greatly reduce the review burden on us, and allow easy reverting/changing/reworking. My suggestion is that you mark this PR as a draft, and continue working on adding support for the folds.scm files. Thanks again!

probably-neb avatar May 30 '25 16:05 probably-neb

Thanks for the quick feedback. I was uncertain about the best PR size in this case, so your comment helps a lot.

I will change the PR to draft while I work on it.

Daquisu avatar May 30 '25 17:05 Daquisu

The PR is ready for review.

Sorry about closing it earlier, somehow I misclicked and did not notice.

Daquisu avatar Jun 04 '25 01:06 Daquisu

I will close it as things start to conflict and it's been 3 weeks since the last feedback. Overall, looks like a nice idea though.

SomeoneToIgnore avatar Jul 01 '25 10:07 SomeoneToIgnore