Not for normies
fix(nvim): pyright
2 files changed, +64 -3
+1-0nvim/after/ftplugin/python.lua
| @@ -0,0 +1 @@ | ||
| 1 | +vim.keymap.set("n", "K", vim.lsp.buf.hover, { buffer = true, desc = "LSP Hover" }) |
+63-3nvim/lsp/pyright.lua
| @@ -1,5 +1,65 @@ | ||
| 1 | +---@brief | |
| 2 | +--- | |
| 3 | +--- https://github.com/microsoft/pyright | |
| 4 | +--- | |
| 5 | +--- `pyright`, a static type checker and language server for python | |
| 6 | + | |
| 7 | +local function set_python_path(command) | |
| 8 | + local path = command.args | |
| 9 | + local clients = vim.lsp.get_clients { | |
| 10 | + bufnr = vim.api.nvim_get_current_buf(), | |
| 11 | + name = 'pyright', | |
| 12 | + } | |
| 13 | + for _, client in ipairs(clients) do | |
| 14 | + if client.settings then | |
| 15 | + client.settings.python = vim.tbl_deep_extend('force', client.settings.python, { pythonPath = path }) | |
| 16 | + else | |
| 17 | + client.config.settings = vim.tbl_deep_extend('force', client.config.settings, { python = { pythonPath = path } }) | |
| 18 | + end | |
| 19 | + client:notify('workspace/didChangeConfiguration', { settings = nil }) | |
| 20 | + end | |
| 21 | +end | |
| 22 | + | |
| 23 | +---@type vim.lsp.Config | |
| 1 | 24 | return { |
| 2 | - cmd = { "pyright" }, | |
| 3 | - filetypes = { "python" }, | |
| 4 | - root_markers = { "requirements.txt" }, | |
| 25 | + cmd = { 'pyright-langserver', '--stdio' }, | |
| 26 | + filetypes = { 'python' }, | |
| 27 | + root_markers = { | |
| 28 | + 'pyproject.toml', | |
| 29 | + 'setup.py', | |
| 30 | + 'setup.cfg', | |
| 31 | + 'requirements.txt', | |
| 32 | + 'Pipfile', | |
| 33 | + 'pyrightconfig.json', | |
| 34 | + '.git', | |
| 35 | + }, | |
| 36 | + settings = { | |
| 37 | + python = { | |
| 38 | + analysis = { | |
| 39 | + autoSearchPaths = true, | |
| 40 | + useLibraryCodeForTypes = true, | |
| 41 | + diagnosticMode = 'openFilesOnly', | |
| 42 | + }, | |
| 43 | + }, | |
| 44 | + }, | |
| 45 | + on_attach = function(client, bufnr) | |
| 46 | + vim.api.nvim_buf_create_user_command(bufnr, 'LspPyrightOrganizeImports', function() | |
| 47 | + local params = { | |
| 48 | + command = 'pyright.organizeimports', | |
| 49 | + arguments = { vim.uri_from_bufnr(bufnr) }, | |
| 50 | + } | |
| 51 | + | |
| 52 | + -- Using client.request() directly because "pyright.organizeimports" is private | |
| 53 | + -- (not advertised via capabilities), which client:exec_cmd() refuses to call. | |
| 54 | + -- https://github.com/neovim/neovim/blob/c333d64663d3b6e0dd9aa440e433d346af4a3d81/runtime/lua/vim/lsp/client.lua#L1024-L1030 | |
| 55 | + client.request('workspace/executeCommand', params, nil, bufnr) | |
| 56 | + end, { | |
| 57 | + desc = 'Organize Imports', | |
| 58 | + }) | |
| 59 | + vim.api.nvim_buf_create_user_command(bufnr, 'LspPyrightSetPythonPath', set_python_path, { | |
| 60 | + desc = 'Reconfigure pyright with the provided python path', | |
| 61 | + nargs = 1, | |
| 62 | + complete = 'file', | |
| 63 | + }) | |
| 64 | + end, | |
| 5 | 65 | } |