Nate
Nate

Reputation: 420

File not saving after BufWritePre command?

I'm attempting to use a tiny autocmd to format my code before saving:

autocmd BufWritePre *.cs :OmniSharpCodeFormat

I expected this to run the code formatter, and then save the output. Instead, it runs the code formatter, which correctly changes the code in my buffer, and yet the saved file is the original pre-formatted buffer.

Not quite sure what I'm doing wrong here! Open to any thoughts or suggestions.

Upvotes: 0

Views: 583

Answers (2)

Nate
Nate

Reputation: 420

It looks like the method is indeed still asynchronous - I was looking for ways to try to await the asynchronous completion, but it seems like they added a callback:

function! s:CBCodeFormat() abort
  noautocmd write
  set nomodified
endfunction
autocmd BufWritePre *.cs call OmniSharp#actions#format#Format(function('s:CBCodeFormat'))

Upvotes: 1

doopNudles
doopNudles

Reputation: 705

To quote from here:

Yes :OmniSharpCodeFormat is asynchronous now, so it's not going to work in a BufWritePre.

That was posted nearly two years ago, but it's probably still asynchronous now.

Upvotes: 1

Related Questions