Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add equalprg #596

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

heygarrett
Copy link

@heygarrett heygarrett commented Dec 11, 2024

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

@github-actions github-actions bot requested a review from stevearc December 11, 2024 01:29
Copy link
Owner

@stevearc stevearc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may appear to work in the simple case, but it will cause errors as soon as you try to run it with more than one formatter. Lua function formatters need to

  1. not modify the buffer
  2. pass the formatted text back as an argument to the callback

@heygarrett
Copy link
Author

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

@stevearc
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feature request: a new formatter for Vim's own C-indenting
2 participants