How to configure python-lsp-server in NvChad – Python

by
Maya Patel
llama-cpp-python neovim nvim-lspconfig

Quick Fix: Follow the provided documentation. Firstly, add the configuration code regarding lspconfig inside your ~/.config\/nvim\/lua\/custom\/plugins.lua file. After that install lsp server through Mason. Finally, configure lspconfig in a separate file like custom\/plugins.lua to avoid confusion.

The Problem:

A user has been using NvChad for Neovim, inspired by a YouTube video. The user has pylsp installed through Mason but is unable to configure it properly in their lspconfig.lua file, resulting in certain functionalities not working as expected. Guidance is sought to resolve this issue and successfully set up pylsp in NvChad.

The Solutions:

Solution 1: Here is the common pattern on configuring lspconfig on NvChad.

The documentation on NvChad about setting up the lspconfig can be found here.

To configure lspconfig on NvChad, follow these steps:

1. Add the following code to your ~/.config/nvim/lua/custom/plugins.lua file:

-- In order to modify the `lspconfig` configuration:
{
  "neovim/nvim-lspconfig",
   config = function()
      require "plugins.configs.lspconfig"
      require "custom.configs.lspconfig"
   end,
},

2. Install the lsp server via mason by adding the following code to your custom/plugins.lua file:

{
    "williamboman/mason.nvim",
    opts = {
      ensure_installed = {
        "lua-language-server",
        "html-lsp",
        "prettier",
        "stylua",
        "gopls"
      },
    },
  }
}

3. Configure the lspconfig:

  • Create a file for each plugin configuration, e.g. ~/.config/nvim/lua/custom/configs/luaconfig.lua.

  • Add the following code to the configuration file:

    local on_attach = require("plugins.configs.lspconfig").on_attach
    local capabilities = require("plugins.configs.lspconfig").capabilities

    local lspconfig = require "lspconfig"
    local servers = { "gopls"–[[ , "html", "css" ]]}

    for _, lsp in ipairs(servers) do
    lspconfig[lsp].setup
    end

You can also configure the lsp servers according to your liking in this file. For example, here is how the gopls lspconfig setup looks like:

lspconfig.gopls.setup({
  on_attach = on_attach,
  capabilities = capabilities,
  settings = {
    gopls = {
      analyses = {
        unusedparams = true,
      },
      staticcheck = true,
      gofumpt = true,
    },
  },
})

Solution 2: Configure python-lsp-server in NvChad

To configure the python-lsp-server in NvChad, follow these steps:

  1. Open the .config/nvim/lua/custom/configs/lspconfig.lua file in your preferred text editor.
  2. Locate the line that reads local servers = {...}.
  3. Inside the curly braces {...}, add the string "pylsp".
  4. Save the file.

By adding "pylsp" to the servers table, you have instructed NvChad to use the python-lsp-server for Python language support.

Here’s a code snippet demonstrating the updated lspconfig.lua file with the python-lsp-server included:

local servers = {
  "pylsp",  -- Add python-lsp-server for Python
  "sumneko_lua",
  "tsserver",
  "bashls",
  "rust_analyzer",
  "gopls",
  "intelephense",
  "emmet_ls",
  "jsonls",
  "sqlls",
  "cssls",
  "html",
}

With the above configuration, NvChad will now use the python-lsp-server to provide language support for Python files. You should now be able to use LSP features like autocompletion, goto definition, and diagnostics for Python code within Neovim.

Q&A

How to set up Pylsp in NvChad?

Install PyLsp with Mason, configure it in lspconfig.lua, and add ‘pylsp’ to the servers array.

What is the common pattern for configuring LSP in NvChad?

Declare the LSP plugin in plugins.lua, install the server with Mason, and configure it in its own .lua file.

Where to configure PyLsp in NvChad?

Add ‘pylsp’ to the servers array in lspconfig.lua and configure it in a separate .lua file.

Video Explanation:

The following video, titled "The perfect Neovim setup for Python - YouTube", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

... how to set up neovim yourself. #python #neovim #coding My socials: Twitter: https://twitter.com/dreamsofcode_io Discord Server: https ...