Skip to content
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

Autoformat only normal mode #136

Closed
wants to merge 2 commits into from

Conversation

lelutin
Copy link
Collaborator

@lelutin lelutin commented Jan 30, 2021

This change resolves this issue I've been experiencing for months about the characters that keep flipping before the line is auto-formatted.

However, changes how the plugin currently behaves. So I'd like to get at least a tiny bit of feedback about the change.

Basically what this will change is that line re-splitting will not happen automatically as you type anymore. You will need to use gq to explicitely request a reformatting of lines so that long lines get split out to shorter ones.

Note that auto-alignment of hash arrows will still happen as you type.

Closes: #135

@russellshackleford
Copy link

I can confirm this does what it claims on self-compiled vim-8.2-2107. However, I'm on the fence about whether I would even want this fix because it's just going to cause all manner of testing errors for long lines that are just over the limit and not really noticeable to the naked eye whereas I can spot the weird flipping and fix it inline. ¯\_(ツ)_/¯

We currently have a bug caused by the auto formatting that happens when
we reach textwidth. The last two characters are getting swapped before
the line is formatted. This leads to many frustrating situations.

To avoid this situation we want to avoid formatting when we have not
explicitly called a formatting function such as 'gq' in normal mode.
@lelutin lelutin force-pushed the autoformat_only_normal_mode branch from 2d76469 to 53dd41b Compare August 29, 2024 02:56
@lelutin
Copy link
Collaborator Author

lelutin commented Aug 29, 2024

rebased this patch series on top of current master so that it's easier to review and also to get CI results -- which apparently fail, good to know.

I'll try to get around to working some more on this series and hopefully get it merged this week 😖

the autoformat function is called by vim when textwidth (if defined) is
exceeded. during these calls, the Fallback part of the fuction ends up
calling out to "normal! gww" which tends to reverse the last two
characters.

this is caused by how gww works, in relationship to how formatexpr is
called during insert mode -- gww tries to bring the cursor back to where
it was before, but the cursor position did not exist in the first place
(since the last typed character was not inserted into the buffer yet
before calling formatexpr)

This change works around the issue by entirely disabling autoformatting
when not in normal mode. It means that only using gq in normal mode will
be able to reformat the text.
@lelutin lelutin force-pushed the autoformat_only_normal_mode branch from 53dd41b to cf5fb02 Compare August 29, 2024 03:03
@lelutin lelutin closed this Sep 8, 2024
@lelutin lelutin deleted the autoformat_only_normal_mode branch September 8, 2024 01:41
@lelutin
Copy link
Collaborator Author

lelutin commented Sep 8, 2024

I gave another try today at figuring this out and ....blah vimscript is really obsucre.

From the examples that I've seen so far when formatexpr is implemented, they basically grab the selection from the buffer with getline() and then iterate over those to modify what's needed and accumulate the result in a variable before replacing the lines with the result.
So what we're doing with calling out to gww is a bit out of the ordinary.

[n]vim's builtin xml formatexpr is also explicitly excluding formatting in non-normal mode.

After reworking the commits a tiny bit and making sure I run tests, I've merged this in (rebased so it doesn't appear as merged here).

in the future if we want to make formatting happen on non-normal mode, we'll have to re-implement the puppet#format#Format() function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

with textwidth enabled, typing past the textwidth limit inverts the last two characters before formatting
2 participants