Skip to content

feature request: use Ruff native language server for "ruff_format" #502

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

Closed
1 task done
steakhutzeee opened this issue Jul 26, 2024 · 7 comments
Closed
1 task done
Labels
enhancement New feature or request

Comments

@steakhutzeee
Copy link

steakhutzeee commented Jul 26, 2024

Did you check existing requests?

  • I have searched the existing issues

Describe the feature

Actually ruff_format formatter does not work with the native ruff language server but only with the CLI.

Provide background

See issue on Ruff astral-sh/ruff#12514

What is the significance of this feature?

strongly desired

Additional details

My actual config is:

local M = {
  'stevearc/conform.nvim'
}

function M.config()
  require("conform").setup({
    formatters_by_ft = {
      -- python = {'ruff_organize_imports', 'ruff_format'},
    },
    format_after_save = {
      lsp_format = "fallback",
    },
    notify_on_error = true,
  })
end

return M

I'm not specifying any formatter in order to fall back to the LSP. This way I can format with the native Ruff server but I can't organize imports.

EDIT: Also added the following autocommand to sort imports:

vim.api.nvim_create_autocmd({ "BufWritePost" }, {
  pattern = { "*.py" },
  callback = function()
    vim.lsp.buf.code_action {
      context = {
        only = { 'source.organizeImports.ruff' },
      },
      apply = true,
    }
  end,
})

Maybe this can be integrated in Conform somehow?

@steakhutzeee steakhutzeee added the enhancement New feature or request label Jul 26, 2024
@stevearc
Copy link
Owner

I don't think I understand what you are asking for. If you want to format using the ruff CLI, that's what the ruff_fix, ruff_format, and ruff_organize_imports formatters will do. If you want to format using the ruff LSP, then just attach the LSP and specify that conform should use LSP formatting. If you want to do organize imports + LSP formatting like in the issue you linked, you can do

python = { "ruff_organize_imports", lsp_format = "last" }

Does this cover your use case?

@stevearc stevearc added the question Further information is requested label Mar 19, 2025
@steakhutzeee
Copy link
Author

steakhutzeee commented Mar 19, 2025

Thanks!

Was not aware of the "last" option. Silly question, and if I want to use the lsp to organize the imports instead of the CLI also?

In the issue I linked it was discouraged to fallback on lsp for formatting and use CLI for imports sorting. That's why I use the autocommand that calls the lsp for imports sorting also.

Also, how I format after save in this case with my config from above?

@github-actions github-actions bot removed the question Further information is requested label Mar 19, 2025
@stevearc
Copy link
Owner

You cannot use a LSP code action as a conform formatter. See #61 for an explanation of why LSP formatting is different from formatting CLI tools and why we have to handle it differently. Code actions have all the same issues as LSP formatting (operate on the current state of the buffer, modify the buffer directly instead of producing diffs). This makes them impossible to compose nicely with other formatters, and would require special handling.

It is possible that we could at some point add support for code actions as a formatting step that runs before LSP formatting, but it would be a significant amount of work and tricky to design a clean API. Until that happens, no code action formatters.

@steakhutzeee
Copy link
Author

Thank you.

So my config should become:

local M = {
  'stevearc/conform.nvim'
}

function M.config()
  require("conform").setup({
    formatters_by_ft = {
      python = { 'ruff_organize_imports'},
      fish = {'fish_indent'},
    },
    format_after_save = {
      lsp_format = "last",
    },
    notify_on_error = true,
  })
end

return M

Or I need to also specify python = { "ruff_organize_imports", lsp_format = "last" } as you mentioned above?

@stevearc
Copy link
Owner

Either one should work

@steakhutzeee
Copy link
Author

Out of curiosity. and if I would to use format on save only for some file types and not all of them?

@stevearc
Copy link
Owner

You can customize the format-on-save logic by providing a function. Here is a recipe with a bunch of ideas: https://github.com/stevearc/conform.nvim/blob/master/doc/recipes.md#autoformat-with-extra-features

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants