stevearc / conform.nvim

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

feature request: add `pyright`'s `pyright.organizeimports` #466

Closed carschandler closed 3 months ago

carschandler commented 3 months ago

Did you check existing requests?

Describe the feature

pyright (and basedpyright) provides a pyright.organizeimports (basedpyright.organizeimports) command that organizes Python imports. It would be nice to be able to simply specify my Python config in conform like

python = { "pyright_organize_imports", "black" }

Provide background

My current workflow is to use the :PyrightOrganizeImports command provided by lspconfig, but it would be much nicer to have this as part of conform so that it weaves into the auto-formatting workflow. I'd like to achieve this without having to install a separate program just for organizing imports. It's just a couple of lines of lua in lspconfig that define the command, but given that this is technically part of the LSP, I'm not sure if this disqualifies it from being part of conform.

This seems pretty straightforward to implement... if someone could point me in the right direction for the idiomatic way to implement a simple lua function as the command, I'd be happy to open a PR!

What is the significance of this feature?

nice to have

Additional details

Command from lspconfig:

local function organize_imports()
  local params = {
    command = 'pyright.organizeimports',
    arguments = { vim.uri_from_bufnr(0) },
  }

  local clients = util.get_lsp_clients { -- This is just a one-liner function that could be extracted
    bufnr = vim.api.nvim_get_current_buf(),
    name = 'pyright',
  }
  for _, client in ipairs(clients) do
    client.request('workspace/executeCommand', params, nil, 0)
  end
end
stevearc commented 3 months ago

Commands are not supported as formatters, due to the fact that they have a radically different interface. See #193 for more

There are other tools (such as isort) that can sort your imports for you; I would recommend using one of those.