irongit

My nvim configuration

hygo-nvim/lsp/basedpyright.lua
83 lines2.6 KBLua
1---@brief
2---
3--- https://detachhead.github.io/basedpyright
4---
5--- `basedpyright`, a static type checker and language server for python
6
7local 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 = "basedpyright",
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 or {}, { 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
22end
23
24---@type vim.lsp.Config
25return {
26 cmd = { "basedpyright-langserver", "--stdio" },
27 filetypes = { "python" },
28 root_markers = {
29 "pyrightconfig.json",
30 "pyproject.toml",
31 "setup.py",
32 "setup.cfg",
33 "requirements.txt",
34 "Pipfile",
35 ".git",
36 },
37 settings = {
38 basedpyright = {
39 analysis = {
40 typeCheckingMode = "standard",
41 diagnosticMode = "workspace",
42 autoSearchPaths = true,
43 useLibraryCodeForTypes = true,
44 diagnosticSeverityOverrides = {
45 reportMissingTypeStubs = false,
46 reportUnusedVariable = false,
47 reportUnusedParameter = false,
48 reportMissingParameterType = false,
49 reportUnknownParameterType = false,
50 reportUnknownMemberType = false,
51 reportUnknownVariableType = false,
52 reportUnknownArgumentType = false,
53 reportAny = false,
54 },
55 },
56 },
57 },
58 on_attach = function(client, bufnr)
59 if client.server_capabilities.documentSymbolProvider then
60 require("nvim-navic").attach(client, bufnr)
61 end
62
63 vim.api.nvim_buf_create_user_command(bufnr, "LspPyrightOrganizeImports", function()
64 local params = {
65 command = "basedpyright.organizeimports",
66 arguments = { vim.uri_from_bufnr(bufnr) },
67 }
68
69 -- Using client.request() directly because "basedpyright.organizeimports" is private
70 -- (not advertised via capabilities), which client:exec_cmd() refuses to call.
71 -- https://github.com/neovim/neovim/blob/c333d64663d3b6e0dd9aa440e433d346af4a3d81/runtime/lua/vim/lsp/client.lua#L1024-L1030
72 client.request("workspace/executeCommand", params, nil, bufnr)
73 end, {
74 desc = "Organize Imports",
75 })
76
77 vim.api.nvim_buf_create_user_command(bufnr, "LspPyrightSetPythonPath", set_python_path, {
78 desc = "Reconfigure basedpyright with the provided python path",
79 nargs = 1,
80 complete = "file",
81 })
82 end,
83}