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

fix(formatexpr)!: do not fallback to the built-in formatter #238

Merged
merged 1 commit into from
Dec 8, 2023

Conversation

wookayin
Copy link
Contributor

@wookayin wookayin commented Dec 7, 2023

This makes the behavior of formatexpr more consistent with
vim.lsp.formatexpr; do not run vim's builtin formatter.

Problem: When conform's formatexpr is used and (range or buffer) but
conform can't do formatting and there is no LSP server with the
formatting capabilities, it will fall back to the (wrong) built-in
formatting, which might result in simply concatenating all the words.

This is a breaking change (as a part of v5.0 with breaking change),
reverting the behavior introduced in 278bcd8 (#55). /cc @cryptomilk

This makes the behavior of formatexpr more consistent with
`vim.lsp.formatexpr`; do not run vim's builtin formatter.

Problem: When conform's formatexpr is used and (range or buffer) but
conform can't do formatting and there is no LSP formatter with the
formatting capabilities, it will fall back to the (wrong) built-in
formatting, which might result in simply concatenating all the words.

This is a breaking change, reverting the behavior introduced in stevearc#55.
@wookayin
Copy link
Contributor Author

wookayin commented Dec 7, 2023

If one still would like to fallback to the built-in formatter, they can write a custom formatexpr function like (although not recommended):

function _G.formatexpr()
    if #require('conform').list_formatters_for_buffer() > 0 or
       #require("conform.lsp_format").get_format_clients() > 0
        return ('conform').formatexpr()  -- uses conform or LSP
    end
    return 1  -- fallback to the built-in formatexpr, see :help formatoptions
end

But ideally formatexpr should be set locally per buffer using LspAttach, FileType autocmds, rather than globally. Example:

  vim.api.nvim_create_autocmd('FileType', {
    pattern = vim.tbl_keys(require("conform").formatters_by_ft),
    group = vim.api.nvim_create_augroup('conform_formatexpr', { clear = true }),
    callback = function()
      vim.opt_local.formatexpr = 'v:lua.require("conform").formatexpr()'
    end,
  })

@wookayin
Copy link
Contributor Author

wookayin commented Dec 8, 2023

@stevearc Any comments? I wish this were included in v5.0 together as we bump up the major version, because it's a breaking change.

@stevearc
Copy link
Owner

stevearc commented Dec 8, 2023

I think you make a good point: gw is always available for people that want the default behavior, and not falling back to the default formatexpr makes the behavior more consistent with the LSP formatexpr, which I generally support. The more conform's API behaves like LSP, the fewer surprises users will encounter.

I would argue that this isn't really a breaking change, it's just a behavior change. Nobody's config is going to throw errors because of this.

@stevearc stevearc merged commit 48bc999 into stevearc:master Dec 8, 2023
7 checks passed
@wookayin
Copy link
Contributor Author

wookayin commented Dec 8, 2023

Thank you for merging this!

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.

None yet

2 participants