Rust Vim tooling interrupts undo stack

#1

I have a general workflow where I make a change in a file, save it, then immediately compile it so I can see the compiler errors. When I’m fiddling back and forth I will back out of a change I just made using the ‘undo’ button in Vim when a change just caused a new compiler error.

Because I’m using rustfmt on save this causes an additonal edit, and vim doesn’t seem to be able to back out of my previous change now. Additionally, because vim interprets a “whole file change” hitting ‘undo’ jumps to the top of the file, meaning I lose all context of where I was just working.

My workflow is so jarring and error-prone that I figure I must just be using all of these tools wrong.

How do people write Rust in vim?
Has anyone gotten around my undo-stack issue, or is my back-and-forth habit itself just bad and I need to learn a new way to write code?

I’m using nvim v0.3.1, plugins
rust-lang/rust.vim
autozimu/LanguageClient-neovim

RLS ['rustup', 'run', 'nightly-2018-09-22-x86_64-apple-darwin', 'rls']

0 Likes

#2

In case it helps, I switched to NeoClide and the Rust experience has been great!

Here’s my current Neovim config: https://github.com/dakom/dotfiles/tree/master/.config/nvim

Note that it loads the additional neoclide settings from config/coc.vim

Neoclide, so far, seems to replace all sorts of other combos I was trying (LanguageClient-Neovim, ALE, Syntastic, etc. etc.)

Highly recommend giving it a shot :slight_smile:

(disclaimer: I’m not a seasoned vimer nor rustacean, yet, so take with a grain of salt…)

1 Like

#3

Thanks for the tip! I’ve been using it for a few days now and it’s definitely been better than what I had set up before.

Something that I still can’t figure out is occasionally I write some code that won’t compile, and when I save, coc.vim halts my vim instance with:

[coc.nvim] Error output from rust: Request textDocument/formatting failed.
  Message: Reformat failed to complete successfully
"src/main.rs" 432L, 14916C written
Press ENTER or type command to continue

When I remove formatting on save it goes away, but I want to figure out if it’s possible to still have it. Do you have this issue too, or do you have formatting configured differently?

0 Likes

#4

It takes some time to wrap your head around, but you should know about Vim’s undo-branches feature. Basically whenever you undo some changes, the last “leaf” change is saved, and you can go back to it even after making new changes by using the g- command.

So if you have a file that looks like this:

one
two
three

… and you delete two and add four:

one
three
four

… then undo those two changes bringing it back to:

one
two
three

… then save, which triggers a file wide change to:

one
  two
    three

… you can use the g- command to go back to the last “leaf” edit:

one
three
four

… at which point hitting undo again will undo the edit preceding that one:

one
three

… or hitting g+ will go forward to the next “leaf” edit (the filewide change):

one
  two
    three

See :help undo-branches for more information. It’s capable of what you asked for in the original post, but it’s not particularly convenient for heavy use. Maybe one would get used to it, though.

Personally, I save frequently and I wouldn’t like having rustfmt on save. There are still some corner cases where rustfmt does a bad job and I like auditing those individually with git add -p.

0 Likes