My nvim configuration
| 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 = |
| 18 | vim.tbl_deep_extend("force", client.config.settings, { python = { pythonPath = path } }) |
| 19 | end |
| 20 | client:notify("workspace/didChangeConfiguration", { settings = nil }) |
| 21 | end |
| 22 | end |
| 23 | |
| 24 | ---@type vim.lsp.Config |
| 25 | return { |
| 26 | cmd = { "pyright-langserver", "--stdio" }, |
| 27 | filetypes = { "python" }, |
| 28 | root_markers = { |
| 29 | "pyproject.toml", |
| 30 | "setup.py", |
| 31 | "setup.cfg", |
| 32 | "requirements.txt", |
| 33 | "Pipfile", |
| 34 | "pyrightconfig.json", |
| 35 | "manage.py", |
| 36 | ".git", |
| 37 | }, |
| 38 | settings = { |
| 39 | python = { |
| 40 | analysis = { |
| 41 | autoSearchPaths = true, |
| 42 | useLibraryCodeForTypes = true, |
| 43 | diagnosticMode = "openFilesOnly", |
| 44 | }, |
| 45 | }, |
| 46 | }, |
| 47 | on_attach = function(client, bufnr) |
| 48 | vim.api.nvim_buf_create_user_command(bufnr, "LspPyrightOrganizeImports", function() |
| 49 | local params = { |
| 50 | command = "pyright.organizeimports", |
| 51 | arguments = { vim.uri_from_bufnr(bufnr) }, |
| 52 | } |
| 53 | |
| 54 | -- Using client.request() directly because "pyright.organizeimports" is private |
| 55 | -- (not advertised via capabilities), which client:exec_cmd() refuses to call. |
| 56 | -- https://github.com/neovim/neovim/blob/c333d64663d3b6e0dd9aa440e433d346af4a3d81/runtime/lua/vim/lsp/client.lua#L1024-L1030 |
| 57 | client.request("workspace/executeCommand", params, nil, bufnr) |
| 58 | end, { |
| 59 | desc = "Organize Imports", |
| 60 | }) |
| 61 | vim.api.nvim_buf_create_user_command(bufnr, "LspPyrightSetPythonPath", set_python_path, { |
| 62 | desc = "Reconfigure pyright with the provided python path", |
| 63 | nargs = 1, |
| 64 | complete = "file", |
| 65 | }) |
| 66 | end, |
| 67 | } |