-
-
Notifications
You must be signed in to change notification settings - Fork 225
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feature: add folding to LSP #650
Comments
From the LSP spec, it looks like it is possible to add that. https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_foldingRange The change would be applied here: templ/cmd/templ/lspcmd/proxy/server.go Lines 716 to 722 in ad69501
First, you'd need to get the source code of the current templ file from the in-memory cache, as per templ/cmd/templ/lspcmd/proxy/server.go Lines 728 to 735 in ad69501
Then, you'd build on @alehechka's recent work in 22f761a, where he added tracking the positions of HTML elements in the parser tree. With those changes, it's now possible to walk through the templ object model, find the locations of things you want to fold (e.g. HTML elements etc) and return back the list of folding ranges in the LSP. The structure of a templ file is laid out in https://github.com/a-h/templ/blob/main/parser/v2/structure.go - once parsed, those are the nodes you're looking for in the doc. This won't be high on the priority list for me, but I'd be happy to take a PR for the change. Anyone reading, let me know if you're going to try. If you want to build this feature, and aren't sure how LSP stuff works, I did a talk at Gophercon UK about it, and I talk about how it works in templ, so do check it out: https://www.youtube.com/watch?v=EkK8Jxjj95s |
@a-h Reading the links you provided, I can't help but feeling this would be covered by the "html intellisense" that's proposed here: #498 @alehechka what do you think? Is it worth adding just the folding or is it better to have the full HTML capabilities here that should include folding? |
I believe that this won't come by html intellisense being integrated. We will want do our own folding since we will want to allow folding of html, go code and custom templ code. So proxying the html lsp won't suffice here. |
I'll play around with this, but I don't have experience working with LSPs, so no promises |
@a-h I was able to make good progress, so I'm now confident I'll have a PR soon. still have to write all the tests, and figure out if I'll be able to add folding to Go code, but the rest is looking good. |
Great work! Hopefully go folding will be a case of proxying gopls |
Unfortunately that gif I posted was a blunter, turned out my templ build was not working and that is the default folding VSCode does when there's no LSP (I I'll not delete my comment to perpetuate my shame 😄 ) But I'm not too far off. My only issue is that the way I did is eating the last line (see pictures) so my issue is that I send the line number of the closing bracket, on the column right before it and seems VS code is just ignoring it and consuming the entire line. You can see the folded shows only |
@joerdav Yes, That's what I'll try to do, I guess i just have to make sure I just pic the foldings of gopls that correspond to Templs TemplateFileGoExpression nodes |
Related to this, for treesitter users folds can be defined by treesitter. I've got a PR for this open, and can confirm it works with nvim. |
I believe the LSP can add folding support. I believe this is very useful when you have long html tags. Folding them can make one read the code more easily.
The text was updated successfully, but these errors were encountered: