function setup_keymap_lsp() -- -- Global mappings. -- -- See `:help vim.diagnostic.*` for documentation on any of the below functions -- vim.keymap.set("n", "e", vim.diagnostic.open_float) -- vim.keymap.set("n", "[d", vim.diagnostic.goto_prev) -- vim.keymap.set("n", "]d", vim.diagnostic.goto_next) -- vim.keymap.set("n", "q", vim.diagnostic.setloclist) vim.api.nvim_create_autocmd("LspAttach", { group = vim.api.nvim_create_augroup("UserLspConfig", {}), callback = function(ev) -- Enable completion triggered by vim.bo[ev.buf].omnifunc = "v:lua.vim.lsp.omnifunc" -- Buffer local mappings. -- See `:help vim.lsp.*` for documentation on any of the below functions local opts = { buffer = ev.buf } vim.keymap.set("n", "gt", vim.lsp.buf.type_definition, opts) vim.keymap.set("n", "gD", vim.lsp.buf.declaration, opts) vim.keymap.set("n", "gd", vim.lsp.buf.definition, opts) vim.keymap.set("n", "gi", vim.lsp.buf.implementation, opts) vim.keymap.set("n", "gr", vim.lsp.buf.references, opts) vim.keymap.set("n", "K", vim.lsp.buf.hover, opts) vim.keymap.set("n", "", vim.lsp.buf.signature_help, opts) vim.keymap.set("n", "wa", vim.lsp.buf.add_workspace_folder, opts) vim.keymap.set("n", "wr", vim.lsp.buf.remove_workspace_folder, opts) vim.keymap.set("n", "wl", function() print(vim.inspect(vim.lsp.buf.list_workspace_folders())) end, opts) vim.keymap.set("n", "gR", vim.lsp.buf.rename, opts) vim.keymap.set({ "n", "v" }, "ga", vim.lsp.buf.code_action, opts) vim.keymap.set("n", "g=", function() vim.lsp.buf.format { async = true } end, opts) end, }) end local lua_ls_opts = { settings = { Lua = { runtime = { -- Tell the language server which version of Lua you're using (most likely LuaJIT in the case of Neovim) version = "LuaJIT", }, diagnostics = { -- Get the language server to recognize the `vim` global globals = { "vim" }, }, workspace = { -- Make the server aware of Neovim runtime files library = vim.api.nvim_get_runtime_file("", true), }, -- Do not send telemetry data containing a randomized but unique identifier telemetry = { enable = false, }, }, }, } return { { "neovim/nvim-lspconfig", enabled = custom.full_feature, dependencies = { "hrsh7th/nvim-cmp", "hrsh7th/cmp-nvim-lsp", "hrsh7th/cmp-buffer", "hrsh7th/cmp-path", "hrsh7th/cmp-cmdline", "L3MON4D3/LuaSnip", "williamboman/mason.nvim", "williamboman/mason-lspconfig.nvim", "mfussenegger/nvim-lint", "mhartington/formatter.nvim", }, event = { "BufReadPre", "BufNewFile", "VeryLazy" }, cmd = { "LspInfo", "LspInstall", "LspUninstall" }, config = function (_, opts) local mason = require("mason") local mason_lsp = require("mason-lspconfig") local luasnip = require("luasnip") local cmp = require("cmp") mason.setup() mason_lsp.setup() mason_lsp.setup_handlers { function (server_name) -- default handler (optional) require("lspconfig")[server_name].setup {} end, ["lua_ls"] = function () require("lspconfig")["lua_ls"].setup(lua_ls_opts) end } cmp.setup({ snippet = { expand = function(args) luasnip.lsp_expand(args.body) end, }, mapping = cmp.mapping.preset.insert({ [""] = cmp.mapping.scroll_docs(-4), [""] = cmp.mapping.scroll_docs(4), [""] = cmp.mapping.complete(), [""] = cmp.mapping.abort(), [""] = cmp.mapping.confirm({ select = true }), }), sources = cmp.config.sources({ { name = "nvim_lsp" }, { name = "buffer" }, { name = "luasnip" }, }), }) cmp.setup.cmdline(":", { mapping = cmp.mapping.preset.cmdline(), sources = cmp.config.sources({ { name = "path" } }, { { name = "cmdline" } }) }) setup_keymap_lsp() end, keys = { { "gs", "LspStart", mode = "n" }, { "gS", "LspStop", mode = "n" }, { "gI", "LspInfo", mode = "n" }, { "ge", vim.diagnostic.open_float, mode = "n" }, { "gp", vim.diagnostic.goto_prev, mode = "n" }, { "gn", vim.diagnostic.goto_next, mode = "n" }, { "gq", vim.diagnostic.setloclist, mode = "n" } }, }, { "folke/trouble.nvim", dependencies = { "nvim-tree/nvim-web-devicons" }, event = "VeryLazy", keys = { { "xx", "TroubleToggle", mode = "n" }, { "xw", "TroubleToggle workspace_diagnostics", mode = "n" }, { "xd", "TroubleToggle document_diagnostics", mode = "n" }, { "xq", "TroubleToggle quickfix", mode = "n" }, { "xl", "TroubleToggle loclist", mode = "n" }, { "xr", "TroubleToggle lsp_references", mode = "n" }, }, opts = { }, }, }