grace/dots

dotfiles/nix configs

git clone https://git.gae.moe/grace/dots

Grace Yoderhx and vcs changesddf0c79

main
2.0 KiB66 linesraw
1---@brief
2---
3--- https://github.com/microsoft/pyright
4---
5--- `pyright`, 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 = '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
21end
22
23---@type vim.lsp.Config
24return {
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    '.jj'
36  },
37  settings = {
38    python = {
39      analysis = {
40        autoSearchPaths = true,
41        useLibraryCodeForTypes = true,
42        diagnosticMode = 'openFilesOnly',
43      },
44    },
45  },
46  on_attach = function(client, bufnr)
47    vim.api.nvim_buf_create_user_command(bufnr, 'LspPyrightOrganizeImports', function()
48      local params = {
49        command = 'pyright.organizeimports',
50        arguments = { vim.uri_from_bufnr(bufnr) },
51      }
52
53      -- Using client.request() directly because "pyright.organizeimports" is private
54      -- (not advertised via capabilities), which client:exec_cmd() refuses to call.
55      -- https://github.com/neovim/neovim/blob/c333d64663d3b6e0dd9aa440e433d346af4a3d81/runtime/lua/vim/lsp/client.lua#L1024-L1030
56      client.request('workspace/executeCommand', params, nil, bufnr)
57    end, {
58      desc = 'Organize Imports',
59    })
60    vim.api.nvim_buf_create_user_command(bufnr, 'LspPyrightSetPythonPath', set_python_path, {
61      desc = 'Reconfigure pyright with the provided python path',
62      nargs = 1,
63      complete = 'file',
64    })
65  end,
66}