conform: autoformat on save only for specified filetypes
Fixes: #686
Enable the autoformat on save only for specified filetypes. By default this is only for lua, similar as LSP is by default only enabled for lua. Based on conform recipe: https://github.com/stevearc/conform.nvim/blob/master/doc/recipes.md
In my view having the autoformat on save enabled by default for all filetypes is a bit too intrusive, hence this PR.
@dam9000 nice! I will personally create a global toggle which toggles formatting on/off instead. Maybe that would be a better approach here too, as it will become very opinionated around which filetypes to enable.
@fredrikaverpil right, there are multiple ways to customize the write_on_save logic and there are many examples in the conform recipes. Including all of them would be an overkill. It's true the selection of when to enable/disable will be opinionated that's why it's good to provide a place where this can be customized. I think the important thing is to change the format_on_save from a hardcoded always enable table to a function to give the user a hint and a place where to customize this. Also I think enabling it for lua is a reasonable default, because most likely the only lua files user will be editing is the kickstart init.lua itself, which has a well defined style in .stylua.toml. The same cannot be said for all the other file types that someone works on.
I like this idea but honestly have no opinion and don't quite understand what "intensive" means in this context.
@feoh I said "intrusive" not "intensive". English is not my primary language so maybe the term used was not the best choice although I think it describes my feeling exactly:
- intrusive: causing disruption or annoyance through being unwelcome or uninvited.
By that I mean the situation I was put in - I updated the kickstart config to the newest version (after the rewrite), went to edit my usual source code files and was taken by surprise that they all got reformatted, that was an unexpected and unwelcome change. I don't know if any editor out there enables "reformat on save" by default, that surely has to be something that a person will want to enable explicitly, only after he makes sure that the formatting style matches the expectation. So I first needed to find out what exactly is causing the reformat and then find out a way to limit it to only lua and that resulted in this PR.
your English is fantastic as always! My reading comprehension was at fault :-) to be honest I haven't been bitten by this yet in personally when I'm programming I really appreciate having my code auto format for me on save but perhaps I'm in the minority. In any case, thank you for your contribution and I'm going to leave this one for others to comment and or possibly act on.
I think I would rather just link the recipes in the init.lua for people to go explore. I think for starting off, it's nice that it just always applies the LSP formatting if you have an LSP installed. For many languages this is just "the right" choice IMO.
What do you think @dam9000 ?
Maybe it can be nice for when you are starting a project from scratch. But when editing existing code the style will likely not match and if the files are in git then you get a bunch of diffs all over the place. Also some languages such python have a standardized coding style while for C/C++ there are uncounted number of styles and each person (or team) will defend the style that they prefer. So for C and C++ I think this should definitely not be enabled by default. In my view this should be a opt-in as no other editor that I'm aware of has this enabled by default, I checked: sublime, jetbrains, vs, vs code. Also I expect most people trying out Neovim/kickstart will already have some existing projects so let's not take them by surprise with such default behaviour.
What if we do the reverse, and we make it easy to disable a language (we don't have to populate the list with anything, we can just show them that they would need to add it)?
I think this might be a nice middle ground?
foce pushed: reversed the logic and add c, cpp to the default disabled filetypes
local disable_filetypes = { c = true, cpp = true }
return {
timeout_ms = 500,
lsp_fallback = not disable_filetypes[vim.bo[bufnr].filetype],
}
force pushed: changed as suggested, and tested that it works as expected.
For what it's worth I love this idea :)