Search code examples
pythonneovimpyright

Neovim and Pyright: limited auto-completion of Python code


Newbie to Neovim and Pyright. I learned from this tutorial: https://youtu.be/zHTeCSVAFNY?si=6G-b4caNl0Bx2xmG

As a result, I am happy to see this snippet auto-completing a for-loop:

snippet in neovim for adding a python for loop

But I am not seeing any auto-completion in other cases. Example 1: no auto-completion of the name of the variable "list" when writing for value in.... Example 2: although my python code includes import os at start of file, neovim does not suggest any os.walk auto-completion when writing for root, dirs, files in os.w...:

enter image description here

I am wondering whether it is a limitation of my configuration or if it is a limitation of Pyright.

I would be grateful for your opinions and advices.

Infos about my sytem:

  • Linux Mint, release 6
  • Python 3.11
  • Neovim v0.10.2

As an answer to InSync's comment (4 nov 2024), I am adding now three lua plugin files (lsp-config.lua, none-ls.lua and completions.lua) for your information.

lsp-config.lua:

return {
    {
        "williamboman/mason.nvim", -- installation
        lazy = false,
        config = function()
            require("mason").setup() -- setup
        end,
    },
    {
        "williamboman/mason-lspconfig.nvim", -- installation
        lazy = false,
        opts = {
            auto_install = true, -- instead of something like ensure_installed = { "lua_ls", "pyright" },
        },
    },
    {
        "neovim/nvim-lspconfig",
        lazy = false,
        config = function()
            local capabilities = require("cmp_nvim_lsp").default_capabilities()
            local lspconfig = require("lspconfig")
            lspconfig.html.setup({
                capabilities = capabilities,
            })
            lspconfig.lua_ls.setup({
                capabilities = capabilities,
            })
            lspconfig.pyright.setup({
                capabilities = capabilities,
            })

            vim.keymap.set("n", "K", vim.lsp.buf.hover, {})
            vim.keymap.set("n", "<leader>gd", vim.lsp.buf.definition, {})
            vim.keymap.set("n", "<leader>gr", vim.lsp.buf.references, {})
            vim.keymap.set("n", "<leader>ca", vim.lsp.buf.code_action, {})
        end,
    },
}

none-ls.lua:

return {
    "nvimtools/none-ls.nvim", -- calls none-ls through short github url
    config = function()
        local null_ls = require("null-ls")
        null_ls.setup({
            sources = {
                null_ls.builtins.formatting.stylua, -- formatter for lua installed with Mason
                null_ls.builtins.formatting.black, -- formatter for python installed with Mason
                null_ls.builtins.formatting.isort, -- formatter for python installed with Mason
            },
        })
        vim.keymap.set("n", "<leader>gf", vim.lsp.buf.format, {}) -- for instance, adds double quotes instead of single quotes when the relevant language server advices it
    end,
}

completions.lua:

return {
    { "hrsh7th/cmp-nvim-lsp" },
    {
        "L3MON4D3/LuaSnip",
        dependencies = {
            "saadparwaiz1/cmp_luasnip",
            "rafamadriz/friendly-snippets",
        },
    },
    {
        "hrsh7th/nvim-cmp",
        config = function()
            local cmp = require("cmp")
            require("luasnip.loaders.from_vscode").lazy_load()

            cmp.setup({
                snippet = {
                    expand = function(args)
                        require("luasnip").lsp_expand(args.body) -- For `luasnip` users
                    end,
                },
                window = {
                    completion = cmp.config.window.bordered(),
                    documentation = cmp.config.window.bordered(),
                },
                mapping = cmp.mapping.preset.insert({
                    ["<C-b>"] = cmp.mapping.scroll_docs(-4),
                    ["<C-f>"] = cmp.mapping.scroll_docs(4),
                    ["<C-Space>"] = cmp.mapping.complete(),
                    ["<C-e>"] = cmp.mapping.abort(),
                    ["<CR>"] = cmp.mapping.confirm({ select = true }),
                }),
                sources = cmp.config.sources({
                    -- { name = 'nvim_lsp' },
                    { name = "luasnip" }, -- For luasnip users.
                }, {
                    { name = "buffer" },
                }),
            })
        end,
    },
}

Solution

  • Solution is: uncomment the line -- { name = 'nvim_lsp' }, in above file completions.lua. This way, Pyright is enabled to make auto-completion suggestions. Source: a comment beginning with "For anyone that..." below the video https://youtu.be/iXIwm4mCpuc?si=fBTLwIr3gUr__8-K