ocamlformat icon indicating copy to clipboard operation
ocamlformat copied to clipboard

Question: Why the ocamlformat plugin for vim stop me from storing me code?

Open VON0000 opened this issue 1 year ago • 6 comments

i am running Linux Mint My ocaml version is 4.13.1 My ocamlformat version is 0.26.2 My vim version is 9.1

i add this to my vimrc ''' call plug#begin('~/.vim/plugged')

Plug 'rgrinberg/vim-ocaml'

call plug#end()

autocmd BufWritePre .ml,.mli execute ':silent !ocamlformat % --inplace' | execute 'edit!' ''' But after that, if i do sth like ':w' or ':wq', any new code i write that is not saved will be deleted. And the file is saved as the last version before coding.

ocamlformat works well in terminal.

VON0000 avatar Jun 26 '24 05:06 VON0000

BufWritePre is called before the file is saved to disk. Ocamlformat operates on the old file and the edit! overwrite any new change. Did you find these instructions somewhere ?

This should work with BufWritePost instead. Though, a dedicated formatter plugin for vim would work better as it will handle different languages and errors.

Julow avatar Jun 26 '24 08:06 Julow

sorry for bothering you, but I have tried this one "autocmd BufWritePost .ml,.mli execute ':silent !ocamlformat % --inplace' | execute 'edit!'" It can store my change now but it could not format the code.

VON0000 avatar Jun 26 '24 08:06 VON0000

Is here an error? Having a nice display of the errors is hard to do in Vim, you'd probably have more luck with a formatter plugin. I do not have formatting-on-save in my setup so I cannot suggest a specific plugin.

Julow avatar Jun 26 '24 09:06 Julow

There is no error or warning in terminal. I tried to print the log of vim. ah0IeT4wwb It seems that no log connected to ocamlformat is printed.

I also will try to find some formatter plugin for vim.

VON0000 avatar Jun 26 '24 09:06 VON0000

You can run :!ocamlformat % --inplace, which should show the same errors if any.

Julow avatar Jun 26 '24 09:06 Julow

:!ocamlformat % --inplace works well in vim

VON0000 avatar Jun 26 '24 09:06 VON0000