-
Notifications
You must be signed in to change notification settings - Fork 44
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
Usage alongside pyright: ruff_lsp executes command of pyright, vice versa #295
Comments
I think this is more of a question to the neovim community than to ruff-lsp. neovim's API |
I've realized this is more or less the same issue as here and here. At first, I came up with a hack like: vim.lsp.handlers["workspace/executeCommand"] = function(err)
if err and err.message == "Unsupported command" then
return
end
end However, this seems fragile (the fact that I do not handle anything other than the Regarding your recommendation, is that from |
Hey, thanks for the detailed issue report.
Yes, the reason is as explained by @xulongwu4. I would recommend you to use either one of the server for import sorting whichever you prefer. For ruff-lsp, you could use the following snippet to execute a command only through the ruff-lsp client: ---@param name string
---@return lsp.Client
local function lsp_client(name)
return assert(
vim.lsp.get_active_clients({ bufnr = vim.api.nvim_get_current_buf(), name = name })[1],
('No %s client found for the current buffer'):format(name)
)
end
local servers = {
ruff_lsp = {
init_options = {
settings = {
-- ...
},
},
commands = {
RuffAutofix = {
function()
lsp_client('ruff_lsp').request("workspace/executeCommand", {
command = 'ruff.applyAutofix',
arguments = {
{ uri = vim.uri_from_bufnr(0) },
},
})
end,
description = 'Ruff: Fix all auto-fixable problems',
},
RuffOrganizeImports = {
function()
lsp_client('ruff_lsp').request("workspace/executeCommand", {
command = 'ruff.applyOrganizeImports',
arguments = {
{ uri = vim.uri_from_bufnr(0) },
},
})
end,
description = 'Ruff: Format imports',
},
},
},
} Similar snippet can be created for Pyright as well although I think the command is defined directly in the I hope this answers your query. I'll mark this as completed but feel free to ask any further questions or doubts. |
I was talking about |
@dhruvmanila I copy-pasted your code and there were some errors. I found they're syntax errors. For example, there are no Having followed @xulongwu4 suggestion, I inspected nvim-lspconfig source code and modified the code as follow: local function lsp_client(name)
return assert(
-- it's `get_active_clients`
vim.lsp.get_active_clients({ bufnr = vim.api.nvim_get_current_buf(), name = name })[1],
("No %s client found for the current buffer"):format(name)
)
end
-- ...
commands = {
RuffOrganizeImports = {
function()
-- hardcode the method instead
lsp_client("ruff_lsp").request("workspace/executeCommand", {
command = "ruff.applyOrganizeImports",
arguments = {
{ uri = vim.uri_from_bufnr(0) },
},
})
end,
description = "Ruff: Format imports",
},
}, Another solution that also works: -- I prefer creating a user command inside on_attach instead of `commands` as it is documented to be deprecated soon.
on_attach = function(client, bufnr)
common_on_attach(client, bufnr)
-- Disable hover in favor of Pyright
client.server_capabilities.hoverProvider = false
-- Steal the idea from `eslint.lua`
local ruff_lsp_client = require("lspconfig.util").get_active_client_by_name(bufnr, "ruff_lsp")
local request = function(method, params)
ruff_lsp_client.request(method, params, nil, bufnr)
end
local organize_imports = function()
request("workspace/executeCommand", {
command = "ruff.applyOrganizeImports",
arguments = {
{ uri = vim.uri_from_bufnr(bufnr) },
},
})
end
vim.api.nvim_create_user_command(
"RuffOrganizeImports",
organize_imports,
{ desc = "Ruff: Organize Imports" }
)
end, Yeah, you two indeed introduced to me a robust solution over my original hack to the issue! |
Ah, sorry. I always use nightly where the new APIs are present.
Glad to hear that :) |
The issue
Inside a Python file, if I run
PyrightOrganizeImports
, which is a command frompyright
, pyright will sort the imports, but ruff will throw an errorruff_lsp: -32603: KeyError: 'pyright.organizeimports'
.The same thing happens when I try to run
RuffOrganizeImports
, which is a user-command I created forruff.applyOrganizeImports
(more on that below), ruff will sort the imports, but pyright will throw an errorpyright: 1: Unsupported command
.I suppose the reason for this is that both servers try to execute a command.
How could I constrain a command to a server, so only that server executes the command and not the others?
Lsp settings
Ruff version: 0.0.41
The text was updated successfully, but these errors were encountered: