feat: add equalprg
Closes #555
Vim and Neovim have built-in formatting functionality:
-
:help equalprg -
:help C-indenting
This PR adds equalprg as a formatter which does the following:
- Store current cursor position in a variable
-
gg(<range-start>Gif range is provided) -
=G(=<range-end>Gif range is provided) - Restore cursor position from variable
Ah, I didn't realize the buffer can't be modified. Are there any existing formatters that use a Lua function but act directly on text in a buffer that I could look at as an example? I wonder if using a temporary file would work. 🤔
There aren't any other formatters that do something like that right now. I agree that a temporary buffer is probably the way to go for this, but it'll likely be a bit tricky. Usually the FileType autocmd will set the indentexpr or equalprg on the buffer, but if you try to set the filetype on a temp buffer it won't work as expected during format_on_save or format_after_save because we're already in an autocmd. You'll likely need to manually copy over the relevant option values to the temp buffer.