stevearc / conform.nvim

Lightweight yet powerful formatter plugin for Neovim
MIT License
3.01k stars 156 forks source link

feat(shfmt): add automatic indentation detection #481

Closed mehalter closed 3 months ago

mehalter commented 3 months ago

This adds the automatic setting of -i so that shfmt follows the current indentation settings of neovim. Whether it is manually set by the user or set through external factors such as .editorconfig

I also checked to see if this is done elsewhere for other formatters and found a couple others which I was able to improve the "correctness" of their indentation size calculations

EDIT: I was thinking about this and it seems like having the effective shiftwidth value is quite nice. And because this seems to be "contextual information" it seemed natural to put it in the conform.Context type since it is virtually free to calculate performance wise. Let me know if you disagree. Another approach would be to make some sort of utility module that is imported by formatters on an as needed basis that calculates this. This is basically the same as vim.fn.shiftwidth (:h shiftwidth()) with the only difference being that it calculates it specifically for the buffer in the context rather than the "current buffer". This allowed for a very nice refactor across the formatters that utilize this field and is available for future formatters. Also nice to make sure we have a common method for calculating indentation size.

Also shiftwidth and tabstop are always integers according to the neovim docs so no need to have default fallbacks or check for nil

mehalter commented 3 months ago

If this is a common task, it might be useful to provide this maybe as a utility so formatters can easily calculate it on demand or we could even put this as part of the formatter context that is passed to the args and prepend_args functions

Mainly just the part that does the logic for calculating the correct indentation size. Something like expandtab is not necessary. But how the tab key is converted to some number of spaces is not based just on tabstop

stevearc commented 3 months ago

Nice QOL improvement for people writing formatters. Thanks for the well reasoned PR!