5

I want to configure my Python language server (pylsp) in Neovim in a Lua config (init.lua). I just transitioned from a Vimscript to a Lua config with basically zero Lua knowledge and this is causing some headaches. Additionally, I took as my starting point the kickstart.nvim repo so lsp is handled by mason which also is a new tool for me.

The basic setup of pylsp is handled via:

local servers = {
  pylsp = {},

  sumneko_lua = {
    Lua = {
      workspace = { checkThirdParty = false },
      telemetry = { enable = false },
    },
  },
}

-- Setup neovim lua configuration
require('neodev').setup()
--
-- nvim-cmp supports additional completion capabilities, so broadcast that to servers
local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities = require('cmp_nvim_lsp').default_capabilities(capabilities)

-- Setup mason so it can manage external tooling
require('mason').setup()

-- Ensure the servers above are installed
local mason_lspconfig = require 'mason-lspconfig'

mason_lspconfig.setup {
  ensure_installed = vim.tbl_keys(servers),
}

mason_lspconfig.setup_handlers {
  function(server_name)
    require('lspconfig')[server_name].setup {
      capabilities = capabilities,
      on_attach = on_attach,
      settings = servers[server_name],
    }
  end,
}

Based on the sumneko_lua config (4th line in the above code block) as well as e.g. https://github.com/neovim/nvim-lspconfig/blob/master/doc/server_configurations.md#pylsp I tried to e.g. configure pycodestyle by replacing:

  pylsp = {},

with:

  pylsp = {
    plugins = {
      pycodestyle = {
        ignore = {'W391'},
        maxLineLength = 100
      }
    }
  },

However the changes seem to not have any effect. When I open a Lua file and run inside nvim:

:lua print(vim.inspect(vim.lsp.get_active_clients()))

I see that the sumneko_lua variable values are altered. However, when I run the same command in a Python file, I don't even see the 'plugins' and any fields of interest.

So, what am I doing wrong and how can I fix it? Eventually I'm interested in trying out 3rd party plugins listed here but to do that I would have to disable some of those active by default in the config.

1
  • I'm having the exact same problem at the same time as you. It's really weird. It seems not to take any effect.
    – Iguananaut
    Jan 12 at 1:27

1 Answer 1

6

I solved it. You actually need to add a seemingly redundant pylsp key, like:

  pylsp = {
    pylsp = {
        plugins = {
          pycodestyle = {
            ignore = {'W391'},
            maxLineLength = 100
          }
        }
    }
  },

This is because the top-level pylsp refers to the name of the LSP itself, but all the settings for pylsp start with pylsp. (similarly, this is why you see sumneko_lua = { Lua = { ... } } in the defaults).

1
  • 1
    Very nice catch. The trick gave by @nie-dzwiedz in order to display the actual config is great :lua print(vim.inspect(vim.lsp.get_active_clients())) too. Here's another tips: Show only the pylsp Lua's config into a buffer (so you can search edit, whatever...): enew|put=execute(\"lua print(vim.inspect(vim.lsp.get_active_clients({name='pylsp'})))\") enew - will open a new buffer put= - will execute the command with a filter
    – Sylvain
    Sep 21 at 20:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.