From 67e4663479082532c2116c5e940defda563ad3c6 Mon Sep 17 00:00:00 2001 From: Petr Hrdina Date: Wed, 14 Jan 2026 23:05:58 +0100 Subject: [PATCH] Add LSP config --- lazy-lock.json | 8 ++++ lua/config/lazy.lua | 1 + lua/plugins/lsp.lua | 114 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+) create mode 100644 lua/plugins/lsp.lua diff --git a/lazy-lock.json b/lazy-lock.json index d4d1cbf..5cf2cb9 100644 --- a/lazy-lock.json +++ b/lazy-lock.json @@ -1,8 +1,16 @@ { + "LuaSnip": { "branch": "master", "commit": "3732756842a2f7e0e76a7b0487e9692072857277" }, "catppuccin": { "branch": "main", "commit": "beaf41a30c26fd7d6c386d383155cbd65dd554cd" }, + "cmp-nvim-lsp": { "branch": "main", "commit": "cbc7b02bb99fae35cb42f514762b89b5126651ef" }, "gruvbox.nvim": { "branch": "main", "commit": "a472496e1a4465a2dd574389dcf6cdb29af9bf1b" }, "lazy.nvim": { "branch": "main", "commit": "306a05526ada86a7b30af95c5cc81ffba93fef97" }, + "mason-lspconfig.nvim": { "branch": "main", "commit": "80c0130c5f16b551865a69e832f1feadeedb5fbe" }, + "mason.nvim": { "branch": "main", "commit": "44d1e90e1f66e077268191e3ee9d2ac97cc18e65" }, + "nvim-cmp": { "branch": "main", "commit": "85bbfad83f804f11688d1ab9486b459e699292d6" }, + "nvim-lspconfig": { "branch": "master", "commit": "92ee7d42320edfbb81f3cad851314ab197fa324a" }, + "nvim-tree.lua": { "branch": "master", "commit": "ca8d82fff26cb12ced239713e3222f4a9dcd0da0" }, "nvim-treesitter": { "branch": "main", "commit": "5a7e5638e7d220575b1c22c8a2e099b52231886e" }, + "nvim-web-devicons": { "branch": "master", "commit": "803353450c374192393f5387b6a0176d0972b848" }, "plenary.nvim": { "branch": "master", "commit": "b9fd5226c2f76c951fc8ed5923d85e4de065e509" }, "telescope.nvim": { "branch": "master", "commit": "a8c2223ea6b185701090ccb1ebc7f4e41c4c9784" }, "tokyonight.nvim": { "branch": "main", "commit": "5da1b76e64daf4c5d410f06bcb6b9cb640da7dfd" } diff --git a/lua/config/lazy.lua b/lua/config/lazy.lua index b24d5d5..bd25982 100644 --- a/lua/config/lazy.lua +++ b/lua/config/lazy.lua @@ -19,6 +19,7 @@ vim.opt.rtp:prepend(lazypath) -- Initialize lazy.nvim local plugins = { require("plugins.colorschemes"), -- theme + require("plugins.lsp"), -- LSP require("plugins.telescope"), -- fuzzy finder require("plugins.treesitter"), -- syntax tree } diff --git a/lua/plugins/lsp.lua b/lua/plugins/lsp.lua new file mode 100644 index 0000000..9d494ab --- /dev/null +++ b/lua/plugins/lsp.lua @@ -0,0 +1,114 @@ +return { + -- LSP installer + { "williamboman/mason.nvim", config = true }, + { "williamboman/mason-lspconfig.nvim", config = true }, + + -- LSP configuration + { + "neovim/nvim-lspconfig", + config = function() + -- Initialize Mason + require("mason").setup() + require("mason-lspconfig").setup() + + -- Lua for vim + vim.lsp.config("lua_ls", { + settings = { + Lua = { + diagnostics = { + globals = { "vim" }, -- recognize 'vim' as a global + }, + workspace = { + library = vim.api.nvim_get_runtime_file("", true), -- includes Neovim runtime files + checkThirdParty = false, -- optional, avoids prompts about unknown libraries + }, + telemetry = { enable = false }, -- optional, disable telemetry + }, + }, + }) + vim.lsp.enable("lua_ls") + + -- C/C++ + vim.lsp.config("clangd", {}) + vim.lsp.enable("clangd") + + -- Rust + vim.lsp.config("rust_analyzer", {}) + vim.lsp.enable("rust_analyzer") + + -- Floating diagnostic on hover + vim.api.nvim_create_autocmd("CursorHold", { + callback = function() + vim.diagnostic.open_float(nil, { focusable = false }) + end, + }) + + -- Enable virtual text and signs + vim.diagnostic.config({ + virtual_text = false, + signs = true, + underline = true, + update_in_insert = false, + severity_sort = true, + }) + + + -- Optional: add keymaps + local keymap = vim.keymap + keymap.set("n", "gd", vim.lsp.buf.definition, { desc = "Go to definition" }) + keymap.set("n", "K", vim.lsp.buf.hover, { desc = "Hover documentation" }) + keymap.set("n", "gr", vim.lsp.buf.references, { desc = "References" }) + end, + }, + + -- Completion + { + "hrsh7th/nvim-cmp", + dependencies = { + "hrsh7th/cmp-nvim-lsp", + "L3MON4D3/LuaSnip", + }, + config = function() + local cmp = require("cmp") + cmp.setup({ + snippet = { + expand = function(args) + require("luasnip").lsp_expand(args.body) + end, + }, + mapping = cmp.mapping.preset.insert({ + [""] = cmp.mapping.select_next_item(), + [""] = cmp.mapping.select_prev_item(), + [""] = cmp.mapping.confirm({ select = true }), + }), + sources = cmp.config.sources({ + { name = "nvim_lsp" }, + { name = "buffer" }, + }), + }) + end, + }, + { + "hrsh7th/nvim-cmp", + dependencies = { "hrsh7th/cmp-nvim-lsp", "L3MON4D3/LuaSnip" }, + config = function() + local cmp = require("cmp") + cmp.setup({ + snippet = { + expand = function(args) + require("luasnip").lsp_expand(args.body) + end, + }, + mapping = cmp.mapping.preset.insert({ + [""] = cmp.mapping.select_next_item(), + [""] = cmp.mapping.select_prev_item(), + [""] = cmp.mapping.confirm({ select = true }), + }), + sources = cmp.config.sources({ + { name = "nvim_lsp" }, + { name = "buffer" }, + }), + }) + end, + }, +}