Skip to content

Commit 386231e

Browse files
authored
fix(pyright): command :LspPyrightOrganizeImports fails #3971
Problem: The `pyright.organizeimports` is private, so client:exec_cmd() fails because it refuses to run commands that are not advertised as capabilities. Solution: Call client.request() to side-step the check in client:exec_cmd(). YOLO
1 parent 408cf07 commit 386231e

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

.github/ci/lint.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ _check_lsp_cmd_prefix() {
5353

5454
# Enforce client:exec_cmd().
5555
_check_exec_cmd() {
56-
local exclude='eslint'
56+
local exclude='eslint\|pyright\|basedpyright'
5757
if git grep -P 'workspace.executeCommand' -- 'lsp/*.lua' | grep -v "$exclude" ; then
5858
_fail 'Use client:exec_cmd() instead of calling request("workspace/executeCommand") directly. Example: lsp/pyright.lua'
5959
fi

lsp/basedpyright.lua

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,15 @@ return {
4343
},
4444
on_attach = function(client, bufnr)
4545
vim.api.nvim_buf_create_user_command(bufnr, 'LspPyrightOrganizeImports', function()
46-
client:exec_cmd({
46+
local params = {
4747
command = 'basedpyright.organizeimports',
4848
arguments = { vim.uri_from_bufnr(bufnr) },
49-
})
49+
}
50+
51+
-- Using client.request() directly because "basedpyright.organizeimports" is private
52+
-- (not advertised via capabilities), which client:exec_cmd() refuses to call.
53+
-- https://github.com/neovim/neovim/blob/c333d64663d3b6e0dd9aa440e433d346af4a3d81/runtime/lua/vim/lsp/client.lua#L1024-L1030
54+
client.request('workspace/executeCommand', params, nil, bufnr)
5055
end, {
5156
desc = 'Organize Imports',
5257
})

lsp/pyright.lua

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,15 @@ return {
4343
},
4444
on_attach = function(client, bufnr)
4545
vim.api.nvim_buf_create_user_command(bufnr, 'LspPyrightOrganizeImports', function()
46-
client:exec_cmd({
46+
local params = {
4747
command = 'pyright.organizeimports',
4848
arguments = { vim.uri_from_bufnr(bufnr) },
49-
})
49+
}
50+
51+
-- Using client.request() directly because "pyright.organizeimports" is private
52+
-- (not advertised via capabilities), which client:exec_cmd() refuses to call.
53+
-- https://github.com/neovim/neovim/blob/c333d64663d3b6e0dd9aa440e433d346af4a3d81/runtime/lua/vim/lsp/client.lua#L1024-L1030
54+
client.request('workspace/executeCommand', params, nil, bufnr)
5055
end, {
5156
desc = 'Organize Imports',
5257
})

0 commit comments

Comments
 (0)