Skip to content

clang-format: work around vim/vim#5930 when positioning cursor #145

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

Merged
merged 4 commits into from
Apr 27, 2020

Conversation

sam-mccall
Copy link
Contributor

This bug (and a related predecessor) cause :goto and line2bytes()
to do the wrong thing when vim textprops are used.
This manifests as "cursor jumps around randomly after formatting" when using
plugins that use textprops for diagnostics, highlighting etc.

We happen to have all the code in an array when we need to do cursor coordinate
conversions, so we do those in vimscript instead of querying vim.

Without this patch, The newly added test fails on affected versions of vim
including both vim master and debian's 8.1.2269 (in different ways).

Vim bug tracker entry:
vim/vim#5930
Similar workaround in first-party vim integration:
llvm/llvm-project@591be7e

This bug (and a related predecessor) cause `:goto` and `line2bytes()`
to do the wrong thing when vim textprops are used.
This manifests as "cursor jumps around randomly after formatting" when using
plugins that use textprops for diagnostics, highlighting etc.

We happen to have all the code in an array when we need to do cursor coordinate
conversions, so we do those in vimscript instead of querying vim.

Without this patch, The newly added test fails on affected versions of vim
including both vim master and debian's 8.1.2269 (in different ways).

Vim bug tracker entry:
vim/vim#5930
Similar workaround in first-party vim integration:
llvm/llvm-project@591be7e
Comment on lines 127 to 133
" line2byte counts bytes from 1, and col counts from 1, so -2
let l:cursor_pos = line2byte(line('.')) + col('.') - 2
" Avoid line2byte: https://github.com/vim/vim/issues/5930
let l:cursor_pos = col('.') - 1
for l:i in range(1, line('.') - 1)
let l:cursor_pos += len(l:lines[l:i - 1]) + 1
endfor
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider refactoring this into an s:Func. Later it might make a nice helper in itself in maktaba#buffer#.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing! I'm not sure how useful these are apart from as a workaround for this bug, I'll let you make the call on moving them.

@dbarnett
Copy link
Contributor

Thanks for digging into this and fixing!

@sam-mccall
Copy link
Contributor Author

Thanks for the review, and sorry about the hacky workarounds!

Copy link
Contributor

@dbarnett dbarnett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, huge readability win. 2 optional nits now that I've had a better look at the algorithm. Either way, just ping me when you're ready and I'll merge it.

sam-mccall and others added 2 commits April 27, 2020 12:44
Co-Authored-By: David Barnett <david@mumind.me>
@sam-mccall
Copy link
Contributor Author

@dbarnett Sorry for the long turnaround, and thanks for the review - this should be good to go.

@dbarnett dbarnett merged commit d8dd427 into google:master Apr 27, 2020
% int f() {<CR>
| int i=1;<CR>
| return 1234567890; }<CR>
:call prop_type_add('keyword', {})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, these make the vroom tests blow up on older versions of vim: https://travis-ci.org/github/google/vim-codefmt/jobs/680186570.

Can you change those to only conditionally add the props if vim supports them? And then IIUC the rest of the test should pass regardless on older vims, just won't be covering your workaround logic.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, sorry about that. Sent #150

dbarnett added a commit that referenced this pull request Jul 29, 2020
Fixes clangformat.vroom failing against some versions of vim since #145
with `E117: Unknown function: prop_type_add`. Moves all the intricate
cursor position checks from clangformat.vroom into a separate file
clangformat-cursor.vroom so the flow of the basic testing scenarios
makes makes more sense and is less brittle.
dbarnett added a commit that referenced this pull request Jul 29, 2020
Fixes clangformat.vroom failing against some versions of vim since #145
with `E117: Unknown function: prop_type_add`. Moves all the intricate
cursor position checks from clangformat.vroom into a separate file
clangformat-cursor.vroom so the flow of the basic testing scenarios
makes makes more sense and is less brittle.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants