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

conform: autoformat on save only for specified filetypes

Open dam9000 opened this issue 2 years ago • 5 comments

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 avatar Mar 04 '24 21:03 dam9000

@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 avatar Mar 05 '24 06:03 fredrikaverpil

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

dam9000 avatar Mar 05 '24 09:03 dam9000

I like this idea but honestly have no opinion and don't quite understand what "intensive" means in this context.

feoh avatar Mar 12 '24 21:03 feoh

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

dam9000 avatar Mar 12 '24 22:03 dam9000

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.

feoh avatar Mar 12 '24 22:03 feoh

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 ?

tjdevries avatar Mar 15 '24 14:03 tjdevries

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.

dam9000 avatar Mar 15 '24 15:03 dam9000

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?

tjdevries avatar Mar 15 '24 15:03 tjdevries

foce pushed: reversed the logic and add c, cpp to the default disabled filetypes

dam9000 avatar Mar 15 '24 16:03 dam9000

local disable_filetypes = { c = true, cpp = true }
return {
  timeout_ms = 500,
  lsp_fallback = not disable_filetypes[vim.bo[bufnr].filetype],
}

tjdevries avatar Mar 15 '24 16:03 tjdevries

force pushed: changed as suggested, and tested that it works as expected.

dam9000 avatar Mar 15 '24 17:03 dam9000

For what it's worth I love this idea :)

feoh avatar Mar 15 '24 17:03 feoh