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

last edit mark is tampered after ClangFormat #106

Open
shreejun opened this issue May 12, 2020 · 3 comments
Open

last edit mark is tampered after ClangFormat #106

shreejun opened this issue May 12, 2020 · 3 comments

Comments

@shreejun
Copy link

Select a line in visual mode and do :ClangFormat. It formats only the selected line as expected. Move around the cursor to other lines, and then press `. in order to jump to the line formatted earlier. It jumps to the end of the file instead.

@danpassmore
Copy link

danpassmore commented Feb 9, 2023

I'm facing this same issue when enabling the option that automatically formats upon leaving insert mode (g:clang_format#auto_format_on_insert_leave). In such cases, I often navigate away and then want to come back to that last edit using "g;", but it instead brings me to the end of the file.

In short, it seems like running any of these clang-format commands (automatically or manually) pollutes the vim change list, when I would prefer it didn't. (I'm guessing (pure speculation) that these commands overwrite the entire buffer with one that is equivalent everywhere except where you asked for formatting, which would cause vim to add an entry to its change list pointing at the last line of the file).

@danpassmore
Copy link

I think we need to use the builtin ":keepjumps" command somehow to achieve what we want.

See here:
https://stackoverflow.com/questions/17431246/prevent-vim-from-remembering-a-change

@danpassmore
Copy link

danpassmore commented Feb 9, 2023

I think I figured out how to fix this issue:

In this function:

function! clang_format#replace_ranges(ranges, ...) abort

I added the "keepjumps" keyword to two nearby commands that edit the file (as shown below):

Before:

    if line('$') > len(splitted)
        execute len(splitted) .',$delete' '_'
    endif
    call setline(1, splitted)

After:

    if line('$') > len(splitted)
        execute 'keepjumps' . len(splitted) .',$delete' '_'
    endif
    keepjumps call setline(1, splitted)

This appears to give the desired result in both my scenario and the scenario posted by shreejun.

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

No branches or pull requests

2 participants