ocaml-ppx / ocamlformat

Auto-formatter for OCaml code
MIT License
606 stars 168 forks source link

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

Open VON0000 opened 1 week ago

VON0000 commented 1 week ago

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.

Julow commented 1 week ago

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.

VON0000 commented 1 week ago

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.

Julow commented 1 week ago

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.

VON0000 commented 1 week ago

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.

Julow commented 1 week ago

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

VON0000 commented 1 week ago

:!ocamlformat % --inplace works well in vim