2

Background

I'm a beginner to neovim and is trying to configure it. But I met with a problem. I referred to some blogs and tried to configure lsp. Some suggests that I should set the event for nvim-lspconfig as "VeryLazy".

event = { "VeryLazy" },

But after doing this, my lspconfig doesn't work.

If I change the event as

event = { "BufReadPre", "BufNewFile" },

Every thing is OK.

Here is what happened

If I set my lsp.lua this way(verylazy version):

return {
    {
        "williamboman/mason.nvim",
        lazy = true,
        config = function()
            require("mason").setup()
        end,
    },

    {
        "williamboman/mason-lspconfig.nvim",
        lazy = true,
        dependencies = { "williamboman/mason.nvim" },
        config = function()
            require("mason-lspconfig").setup({
                ensure_installed = {
                    "pyright",      -- Python
                    "clangd",       -- C++
                    "lua_ls",       -- Lua
                    "rust_analyzer",-- Rust
                    "marksman"      -- Markdown
                },
            })
        end,
    },

    {
        "neovim/nvim-lspconfig",
        event = { "VeryLazy" },
        dependencies = {"williamboman/mason-lspconfig.nvim"},
        config = function()
        local lspconfig = require("lspconfig")

        -- Python 
        lspconfig.pyright.setup({
            filetypes = { "python" },
            settings = {
                python = {
                    analysis = {
                        typeCheckingMode = "strict",
                        autoSearchPaths = true,
                        useLibraryCodeForTypes = true,
                    },
                },
            },
        })

        -- C++ 
        lspconfig.clangd.setup({
            filetypes = { "c", "cpp", "objc", "objcpp" },
            cmd = { "clangd", "--background-index" },
        })

        -- Lua 配置
        lspconfig.lua_ls.setup({
            filetypes = { "lua" },
            settings = {
                Lua = {
                    runtime = {
                        version = 'LuaJIT',
                    },
                    diagnostics = {
                        globals = { 'vim' },
                    },
                    workspace = {
                        library = vim.api.nvim_get_runtime_file("", true),
                    },
                    telemetry = {
                        enable = false,
                    },
                },
            },
        })

        -- Rust 
        lspconfig.rust_analyzer.setup({
            filetypes = { "rust" },
            settings = {
                ["rust-analyzer"] = {
                    cargo = {
                        loadOutDirsFromCheck = true,
                    },
                    procMacro = {
                        enable = true,
                    },
                },
            },
        })

        -- Markdown 
        lspconfig.marksman.setup({
            filetypes = { "markdown" },
            cmd = { "marksman", "server" },
        })
        end,
    },

}

Then the result is: enter image description here But if I change it to (BufNewPre-BufNewFile version):

return {
    {
        "williamboman/mason.nvim",
        lazy = true,
        config = function()
            require("mason").setup()
        end,
    },

    {
        "williamboman/mason-lspconfig.nvim",
        lazy = true,
        dependencies = { "williamboman/mason.nvim" },
        config = function()
            require("mason-lspconfig").setup({
                ensure_installed = {
                    "pyright",      -- Python
                    "clangd",       -- C++
                    "lua_ls",       -- Lua
                    "rust_analyzer",-- Rust
                    "marksman"      -- Markdown
                },
            })
        end,
    },

    {
        "neovim/nvim-lspconfig",
        event = { "BufReadPre", "BufNewFile" },
        dependencies = {"williamboman/mason-lspconfig.nvim"},
        config = function()
        local lspconfig = require("lspconfig")

        -- Python
        lspconfig.pyright.setup({
            filetypes = { "python" },
            settings = {
                python = {
                    analysis = {
                        typeCheckingMode = "strict",
                        autoSearchPaths = true,
                        useLibraryCodeForTypes = true,
                    },
                },
            },
        })

        -- C++
        lspconfig.clangd.setup({
            filetypes = { "c", "cpp", "objc", "objcpp" },
            cmd = { "clangd", "--background-index" },
        })

        -- Lua 
        lspconfig.lua_ls.setup({
            filetypes = { "lua" },
            settings = {
                Lua = {
                    runtime = {
                        version = 'LuaJIT',
                    },
                    diagnostics = {
                        globals = { 'vim' },
                    },
                    workspace = {
                        library = vim.api.nvim_get_runtime_file("", true),
                    },
                    telemetry = {
                        enable = false,
                    },
                },
            },
        })

        -- Rust
        lspconfig.rust_analyzer.setup({
            filetypes = { "rust" },
            settings = {
                ["rust-analyzer"] = {
                    cargo = {
                        loadOutDirsFromCheck = true,
                    },
                    procMacro = {
                        enable = true,
                    },
                },
            },
        })

        -- Markdown 
        lspconfig.marksman.setup({
            filetypes = { "markdown" },
            cmd = { "marksman", "server" },
        })
        end,
    },

}

Then the result is: enter image description here

I cannot understand the relationship among lspconfig, mason, mason-lsp-config. I think the dependencies I set and the lazy load I design is completely wrong.

I`ll greatly appreciate it if you can tell me why this situation appear.

Some other things you may need(I'm not sure)

My .config/nvim:

.
├── init.lua
├── lazy-lock.json
└── lua
    ├── config
    │   └── lazy.lua
    ├── keymaps.lua
    ├── options.lua
    └── plugins
        ├── colorscheme.lua
        ├── lsp.lua
        ├── lualine.lua
        ├── nvim-cmp.lua
        ├── nvim-tree.lua
        └── telescope.lua

And my configuration for nvim-cmp is :

return {
{
    "hrsh7th/nvim-cmp",
    dependencies = {
        "L3MON4D3/LuaSnip",
        "hrsh7th/cmp-buffer",
        "hrsh7th/cmp-path",
        "hrsh7th/cmp-nvim-lsp",
    },
    event = { "InsertEnter", "CmdlineEnter" }, -- the plugin is on only when I get into insert mode or command mode
    config = function()
        local cmp = require'cmp'
        local luasnip = require'luasnip'

        cmp.setup({
            snippet = {
                expand = function(args)
                    luasnip.lsp_expand(args.body)
                end,
            },
            mapping = {
                ['<C-b>'] = cmp.mapping(cmp.mapping.scroll_docs(-4), { 'i', 'c' }),
                ['<C-f>'] = cmp.mapping(cmp.mapping.scroll_docs(4), { 'i', 'c' }),
                ['<C-Space>'] = cmp.mapping(cmp.mapping.complete(), { 'i', 'c' }),
                ['<C-y>'] = cmp.config.disable,
                ['<C-e>'] = cmp.mapping({
                    i = cmp.mapping.abort(),
                    c = cmp.mapping.close(),
                }),
                ['<CR>'] = cmp.mapping.confirm({ select = true }),
                ['<Tab>'] = cmp.mapping(cmp.mapping.select_next_item(), { 'i', 's' }),
                ['<S-Tab>'] = cmp.mapping(cmp.mapping.select_prev_item(), { 'i', 's' }),
            },
            sources = cmp.config.sources({
                { name = 'nvim_lsp' },
                { name = 'luasnip' },
            }, {
                { name = 'buffer' },
            })
        })

        cmp.setup.cmdline('/', {
            sources = {
                { name = 'buffer' }
            }
        })

        cmp.setup.cmdline(':', {
            sources = cmp.config.sources({
                { name = 'path' }
            }, {
                { name = 'cmdline' }
            })
        })
    end,
},
}

Some other things

Although I referred to some blogs, I did not copy them but tried to write on my own. So these may not seem reasonable. Feel free to offer some other suggestions to my configuration if you want. Thanks!

1 Answer 1

2

My guess is that your LSPs are configured correctly, but they are not automatically started when you open a file. You can check this by running :LspInfo.

Reason

This is because by default lspconfig auto-runs the corresponding LSPs during filetype checking (probably during the BufReadPre event).

At that moment, lspconfig has not been configured fully because configuration occurs during the VeryLazy event which happens after filetype checking.

Workaround

At the end of your config function add vim.cmd("LspStart")


Edit 1:

And to clarify your confusion:

  • lspconfig is just a collection of boilerplate configs for each lsp.
  • mason is just an lsp installer.
  • mason-lspconfig is a bridge between those two, so that mason can automatically configure installed lsps using lspconfig.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.