conform.nvim icon indicating copy to clipboard operation
conform.nvim copied to clipboard

feat: add equalprg

Open heygarrett opened this issue 1 year ago • 2 comments

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:

  1. Store current cursor position in a variable
  2. gg (<range-start>G if range is provided)
  3. =G (=<range-end>G if range is provided)
  4. Restore cursor position from variable

heygarrett avatar Dec 11 '24 01:12 heygarrett

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. 🤔

heygarrett avatar Dec 11 '24 14:12 heygarrett

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.

stevearc avatar Dec 21 '24 04:12 stevearc