Skip to content

feature request: lua function formatter #653

@itmecho

Description

@itmecho

Did you check existing requests?

  • I have searched the existing issues

Describe the feature

I would like to be able to pass a lua function to the formatter list by filetype. Either that or configure a custom formatter that is a lua function instead of an external command.

Provide background

I would like to run the following lua command when formatting go files:

vim.lsp.buf.code_action({
  filter = function(a)
    return a.kind == 'source.organizeImports'
  end,
  apply = true,
})

At the moment I can do this by having an extra check in my key binding but it would be nice to be able to run custom lua code as a formatter.

Current lazy.nvim solution:

return {
  'stevearc/conform.nvim',
  ...
  keys = {
    {
      '<leader>cf',
      function()
        if vim.api.nvim_buf_get_option(0, 'filetype') == 'go' then
          vim.lsp.buf.code_action({
            filter = function(a)
              return a.kind == 'source.organizeImports'
            end,
            apply = true,
          })
        end
        require('conform').format({ bufnr = 0, timeout_ms = 10000 })
      end,
      desc = 'Conform format',
    },
  },
}

What is the significance of this feature?

nice to have

Additional details

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions