Why the event “VeryLazy” make my lsp-config not work

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”.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>event = { "VeryLazy" },
</code>
<code>event = { "VeryLazy" }, </code>
event = { "VeryLazy" },

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

If I change the event as

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>event = { "BufReadPre", "BufNewFile" },
</code>
<code>event = { "BufReadPre", "BufNewFile" }, </code>
event = { "BufReadPre", "BufNewFile" },

Every thing is OK.

Here is what happened

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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,
},
}
</code>
<code>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, }, } </code>
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:

But if I change it to (BufNewPre-BufNewFile version):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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,
},
}
</code>
<code>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, }, } </code>
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:

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>.
├── 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
</code>
<code>. ├── 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 </code>
.
├── 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 :

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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,
},
}
</code>
<code>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, }, } </code>
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!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật