-
Notifications
You must be signed in to change notification settings - Fork 46
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
neovim: use ruff to format the current buffer #119
Comments
You may need to use null-ls plugin.
Then add this configuration
|
I have the same issue. It seems that currently only was to use ruff's import sorting is to call code action and select sorting import as described #61. It's too much for simple formatting. I've been relying on venerable isort for the job. It would be nice to see the progress so that I can get rid of isort in the end. |
Thanks for opening this issue! Let me give a bit of background into these different LSP capabilities. With that difference in mind, let's look at what Ruff provides. Ruff is first and foremost a linter (formatter is in progress) which provides diagnostics and a way to auto-fix most of it. The way we provide the auto-fix capability using the Language Server Protocol (LSP) is to use code actions. Why code actions and not formatting request? Because code actions allows us to provide the capability to fix/refactor only certain part of the document while formatting request acts on the entire document. In the future, when the formatter is complete, we will provide the formatting capability which will run the formatter on the whole document. Now, let's look at how to solve the problems which is being mentioned in this issue. First, let's look at the capabilities the :lua vim.print(vim.lsp.get_active_clients({name = 'ruff_lsp'})[1].server_capabilities)
{
codeActionProvider = {
codeActionKinds = { "quickfix", "source.fixAll", "source.organizeImports", "source.fixAll.ruff", "source.organizeImports.ruff" },
resolveProvider = true
},
executeCommandProvider = {
commands = { "ruff.applyAutofix", "ruff.applyOrganizeImports" }
},
hoverProvider = false,
textDocumentSync = {
change = 2,
openClose = true,
save = true,
willSave = false,
willSaveWaitUntil = false
},
workspace = {
fileOperations = vim.empty_dict(),
workspaceFolders = {
changeNotifications = true,
supported = true
}
}
} How to apply all of the auto-fixes available in the current buffer?vim.lsp.buf.code_action {
context = {
only = { 'source.fixAll.ruff' }
},
apply = true,
} Note that this will only apply for the rules configured in your configuration file. The isort rule How to organize the imports only (aka run the import sorter)?vim.lsp.buf.code_action {
context = {
only = { 'source.organizeImports.ruff' }
},
apply = true,
} As mentioned by @pythops, you can use
TipsIf you notice in the server capabilities, it provides some commands which can be executed for the current document. This can be used to define native Ex-commands in Neovim when configuring using require('lspconfig').ruff_lsp.setup {
init_options = {
settings = {
-- ...
},
},
commands = {
RuffAutofix = {
function()
vim.lsp.buf.execute_command {
command = 'ruff.applyAutofix',
arguments = {
{ uri = vim.uri_from_bufnr(0) },
},
}
end,
description = 'Ruff: Fix all auto-fixable problems',
},
RuffOrganizeImports = {
function()
vim.lsp.buf.execute_command {
command = 'ruff.applyOrganizeImports',
arguments = {
{ uri = vim.uri_from_bufnr(0) },
},
}
end,
description = 'Ruff: Format imports',
},
},
} Whichever method you use, defining commands or using code actions, you can bind it to a key and/or run it automatically on save using autocmds ( References: |
@dhruvmanila thanks a lot for taking the time to write this detailed answer! much clearer now |
We have previously explained this in: - #214 (comment) - #209 (comment) - #57 (comment) - #119 (comment) We should cover this in the README to avoid mismatched expectations.
@dhruvmanila Thanks for the detailed solution, I just came here exactly for that. Just a suggestion:
Considering that using Having to scour closed issues in a repo instead of having something work out of the box unnecessarily diminishes the user experience of this otherwise fantastic tool. |
We have previously explained this in: - astral-sh/ruff-lsp#214 (comment) - astral-sh/ruff-lsp#209 (comment) - astral-sh/ruff-lsp#57 (comment) - astral-sh/ruff-lsp#119 (comment) We should cover this in the README to avoid mismatched expectations.
I've been searching for 1 hour how to use ruff to format the current buffer and haven't found any example of config that works.
This issue has been closed, even though
:lua vim.lsp.buf.format()
does not work as expected.Could anyone share a config to make ruff format the current buffer? :) Thanks!
The text was updated successfully, but these errors were encountered: