-
Notifications
You must be signed in to change notification settings - Fork 409
Fix: indent comments and verbatim envs correctly
#3087
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
Conversation
|
Thanks for the in-depth description and suggested fix!
I don't mind having the issue and suggested fix raised in the same thread, so no worries! I think you reasoned perfectly here; although I won't claim that everyone else agrees on that 😅
It seems this is a breaking change. Some people may be customed to having comments being automatically indented. However, I'm inclined to agree that it is a desired behaviour. Can you explain again why you want the comments to start at the beginning of the line? I mean, you already say this is relevant for
Glad for the kind words and thanks for contributing! |
|
A minor comment, since I already notice you are almost following my commit message convention - I don't hold contributors to this convention, so feel free to ignore me! I try to adhere to convention commits, and as such, I prefer to use lower case in the commit type - thus, instead of "Fix: indent ...", I would prefer "fix: indent ...". If you were to rebase and fix that, then also feel free to add an exclamation mark to indicate breaking change, so "fix!: indent ...". |
If the comment doesn't align with the start of the line, it ends up in the
👍 Noted – I will save us all an extra step and follow this convention in future contributions. So I will rebase accordingly, but before I do, I want to ask again about the failing test – would you like me to include a corrected version of the reference file in the new commit? Or would you like to deal with the test separately? |
Ok, makes sense. Now, should we have a different behaviour for
Great; yes, please deal with the test. I agree that you can just update the reference file. |
I agree. I think the proposed implementation is going to be flexible and intuitive enough that it won't inconvenience most people. Because of the action of
Sounds good – I will shuffle things around and rebase shortly. |
|
Ok, done! |
|
Wait, sorry, I changed the input file, not the reference file. I will correct shortly. |
|
Great, thanks; I'll merge this when you've corrected the input -> reference-thing. |
|
Well that was embarrassing – I had made that mistake on another branch several days ago, and I just remembered I had forgotten to fix it. But we should be good to go now! |
|
Great! Thanks again! |
A Side Effect
Before
VimtexIndent()returns the number of spaces by which the current line ought to be indented, it strips comments from the line usings:clean_line(). This is necessary for proper indentation on the basis of environments, delimiters, and so on, because we don't want indentation to be influenced by a commented-out\beginor\endcommand, a commented-out delimiter, or whatever else.A side effect of the current implementation is that the conditional expression
if l:line =~# '^\s*%'(on line 55) is never true. (For good measure, I tested this oncsquotes.sty, which is 2499 lines long and contains a lot of commented lines. I put anechomsgwithin the conditional and usedgg=G, and no message was printed.) Because comments are removed before that point (at line 47), for any line that begins with a%,l:lineis empty. As a result, commented lines sometimes do not use the indentation of the previous line, which the comment at line 54 indicates is the intended behavior.Affected Use Cases
Generally, this quirk in the code is not consequential. I had never noticed it until recently, and even then I thought it was the intended behavior, until I saw the comment on line 54. But one case where the current implementation can be a bummer (and where I first noticed it) is if you are editing a
.dtxfile. Suppose I were to start by inserting the following:Suppose also I have
formatoptionsoand/orrset. If I have my cursor at the end of the last line of the above code, and I pressoin normal mode or<Return>in insert mode, then the following occurs:This is initially an inconvenience because you have to manually realign the comment character with the start of the line. But, alas, the real bummer begins once you've swallowed the minor inconvenience and keep going. If ever after you press
oin normal mode or<Return>in insert mode, the comment character will again be indented as above. Worse (or, at least, more unpredictable) is if you insert anindentkeyscharacter like}. Suppose I've realigned the comment character in the above code and I continue editing like so:As soon as you insert
}, this happens:(Note: for this example to work, you need to have returned to normal mode at some point. If, for example, you realign the comment character by pressing
CTRL-Din insert mode,indentkeysrespects your manual indentation. But if you do something more labor-intensive like using<<to realign the comment character in normal mode, or you exit insert mode for some other reason after usingCTRL-D, the above behavior will occur).The cause is simply that, because the commented lines are considered to be empty, they are indented by one
shiftwidthafter the opening brace at the end of\NewDocumentCommandlike normal, rather than just inheriting the indent of the previous line, as intended. Every time we pressoin normal mode or<Return>in insert mode, or we insert anindentkeyscharacter, the line will be (re)indented to oneshiftwidth. And as long as the lines continued to be commented, the indent is never changed. So the current implementation does make it rather inconvenient to edit.dtxfiles.It should be noted (at the risk of obsessively documenting the issue ...) that the bug affects indentation in
verbatimenvironments as well, although I doubt anyone is at much risk of being inconvenienced by it. Suppose I were to start typing the following:Again, if I have the
oand/orroptions turned on, and I pressoin normal mode or<Return>in insert mode, the following occurs:The cause is that the last line is considered to be a blank line, and therefore it receives the indentation of the 'previous' line, which in a
verbatimenvironment is considered to be the line that begins the environment. In the code above, that indentation is zero.Usually, a new line in a
verbatimenvironment is given the indent of the (actual) previous line afteroin normal mode or<Return>in insert mode, so this behavior is unique to commented lines. Because there is no distinction between comments and code in averbatimenvironment, I figured that there should be no difference in how they are indented in averbatimenvironment, so this PR 'fixes' (at least according to that reasoning) indentation inverbatimenvironments as well.Suggested Fix
Despite the verbosity of the foregoing discussion (sorry), my suggested fix is simple. We can start by setting
l:linetogetline(a:lnum), and then setl:linetos:clean_line(l:line)after we've checked forverbatimenvironments and commented lines. I think this is organized and readable, and it guarantees that everything other thanverbatimenvironments and commented lines will be indented in the expected way.Because this PR affects how comments are indented, it causes the
tikzindentation test to fail. If you want to accept the PR, we would have to alter the reference file -- but I haven't touched that in this PR because I don't know what your workflow is for altering tests. If you end up wanting to approve the PR, let me know how you'd like me to proceed.(Also, novice contributor note: I reasoned that, if I had a fix in mind, it would be more helpful to suggest the fix rather than opening a bug report and saying 'fix this please'. If I got it wrong, let me know!)
Thanks for your work, as always, and I hope this is helpful!