-
Notifications
You must be signed in to change notification settings - Fork 395
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
Check for numbers after markers when setting fold level #3052
Conversation
Thanks, this seems like a valid and good contribution! I had a couple of minor comments to the code. Also, a couple of comments to your text:
True.
Haha, good to hear!
Yes; although I've started to shift towards syntax based folding with tree-sitter (only possible with neovim, though). I find marker based folding to be sort of a "poor man's" solution when a filetype does not have any very sensible default. With treesitter, it seems we are getting closer to good folding for many more syntaxes, which is something I like quite a lot.
Thanks for recognizing the matter of taste here. Personally, I like having the markers in the foldtext to make it clear that this is indeed a marker based fold. I recognize that this is clearly a subjective choice, which is one of the reasons I've tried to make it relatively easy to customize for anyone who wants to. Notice, I think you can make the customization even simpler: let g:vimtex_fold_types = #{markers: #{
\ text: { line, level -> foldtext() }
\}}
Good point.
Yes, thank you, I would very much appreciate that! I think the best place to add this is under the section "FOLDING" and/or under the docs for
Thanks! Glad you find VimTeX useful and I'm happy to see your contribution! |
That's helpful to hear your thoughts on marker-based folding – although I submit an example of a
Again, I wouldn't have thought of this – glad to learn something!
Great, I will get to work on that. Thanks again! |
* Use ternary operator in return value * Only allow one digit after marker * Use '..' rather than '.' for concatenation
Glad to share my thoughts :)
My pleasure!
Looking forward to seeing it. And thanks to you for this; I've merged it now with a minor fix on top. |
The Problem
Vim's built-in capability for folding code using markers (at least if one is using the default markers, as the Vim documentation recommends) is unsatisfactory when editing LaTeX documents, because it is not so unlikely that a line of LaTeX code will contain three closing braces and be misconstrued by Vim as the end of a fold. VimTeX's folding mechanism improves on this situation by only recognizing markers in comments (although if one uses the default markers, even this can sometimes have unexpected results, as dicussed in e.g. #1502 and #1515). However, VimTeX's folding mechanism falls short compared to Vim's in that VimTeX does not recognize numbers after markers, which are used in Vim to manually specify the fold level.
This difference between VimTeX's folding and Vim's native folding can have not only unexpected but also unfortunate consequences. I've uploaded an example file with various nested folds (It is supposed to imitate a personal
sty
file, although please note I had to upload it as atxt
file, since GitHub wouldn't accept atex
file. Also, disclaimer: I don't necessarily endorse the folds in the example file with respect to style/organization; I just wanted to make sure there was some good nesting in there. :)) Here's how the file looks at first glance with Vim's built-in folding:But here's how it looks with VimTeX's folding:
Because Vim allows you to manually specify the fold level, not every opening marker needs to be followed by a closing marker (which is nice, because it's less messy). My general policy, for example, is that I only add a closing marker when I want there to be white space visible after the fold. The result, in this file, is that no opening marker of fold level 1 is followed by a closing marker (except at the end of the document; that way, you can add things to the end of the document outside of the last fold), but every opening marker of fold level greater than 1 is followed by a corresponding closing fold marker. (I think you follow this sort of style in many, though not all, of your VimTeX source files). What happens, then, because VimTeX doesn't recognize the numbers following the fold markers, is that the fold level increases for each
{{{1
, without there being a closing marker to decrease the fold level again. So the fold level gets bigger and bigger, and everything gets subsumed under 'Options'.A Solution
Essentially, my code just looks for a number after the marker specified by the user (or the default marker) by appending a capture group to the existing regexes and using
matchlist
to get the number. If there is a number, the thelevel
function returns e.g. '>2' if the marker is an opening marker, and e.g. '<2' if the marker is a closing marker. So we get the expected, Vim-like behavior, and VimTeX users can specify theg:vimtex_fold_types.markers
option in the way that they always have. Here's how it looks at first glance:The
foldtext
I personally feel dissatisfied with VimTeX's
foldtext
for markers. The markers themselves don't mean anything, so they shouldn't be part of the text, and there's also no way to see what level each fold is. I considered including an alternativefoldtext
in my PR, but then I decided I was getting myself involved in matters of taste... Others, including you, may well like the text returned by the currentfoldtext
. And people like me, if they want e.g. the default Vimfoldtext
, can very easily get it like so:The only recommendation I might make would be to inform people in the documentation that they can specify their own
foldtext
by adding a funcref to the appropriate dictionary in theirg:vimtex_fold_types
configuration, as above. Someone could only figure this out by reading the code (or reading this post, I suppose). If you want to add that tip to the documentation (I can see reasons not to), I would be happy to write something and submit it as a separate PR, if that would save you some time.I appreciate all of your work on VimTeX, and I hope this code can be helpful. Thank you!