From b9255a95e280ff1a23aa01689e5261ed205cf18f Mon Sep 17 00:00:00 2001 From: Filip Wandzio Date: Thu, 30 Oct 2025 19:45:37 +0100 Subject: Fix lsp for moden nvim api Signed-off-by: Filip Wandzio --- init.lua | 373 +++++++++++++++++++++++---------------------------------------- 1 file changed, 135 insertions(+), 238 deletions(-) (limited to 'init.lua') diff --git a/init.lua b/init.lua index 9942d1b..5eae671 100644 --- a/init.lua +++ b/init.lua @@ -1,6 +1,7 @@ ---@diagnostic disable: undefined-global, +--@diagnostic disable: undefined-global vim.loader.enable() + vim.g.netrw_banner = 0 vim.g.netrw_liststyle = 3 vim.g.netrw_altv = 0 @@ -25,7 +26,6 @@ vim.opt.hlsearch = true vim.opt.incsearch = true vim.opt.updatetime = 50 vim.opt.laststatus = 0 --- vim.opt.termguicolors = true vim.opt.signcolumn = "number" vim.opt.clipboard = "unnamedplus" vim.opt.fillchars = "eob: " @@ -34,90 +34,44 @@ vim.opt.lazyredraw = false vim.opt.updatetime = 300 vim.opt.timeoutlen = 500 --- -- Format on save if any attached LSP supports formatting -vim.api.nvim_create_autocmd( - "BufWritePre", - { - callback = function() - local buf = vim.api.nvim_get_current_buf() - local clients = vim.lsp.get_clients({bufnr = buf}) - - for _, client in ipairs(clients) do - if client.server_capabilities.documentFormattingProvider then - vim.lsp.buf.format({silent = true}) - return - end +vim.api.nvim_create_autocmd("BufWritePre", { + callback = function() + local buf = vim.api.nvim_get_current_buf() + local clients = vim.lsp.get_clients({bufnr = buf}) + for _, client in ipairs(clients) do + if client.server_capabilities.documentFormattingProvider then + vim.lsp.buf.format({async = false}) + return end - -- No formatting-capable client: do nothing end - } -) + end +}) local run_on_save = vim.api.nvim_create_augroup("RunPfOnSave", {clear = true}) local formatters = { { - patterns = { - "*.js", - "*.ts", - "*.jsx", - "*.json", - "*.tsx", - "*.css", - "*.html", - "*.yaml", - "*.md", - "*.svelte", - "*.lua" - }, - command = function(file) - vim.fn.system({"prettier", "--write", file}) - end + patterns = {"*.js","*.ts","*.jsx","*.json","*.tsx","*.css","*.html","*.yaml","*.md","*.svelte","*.lua"}, + command = function(file) vim.fn.system({"prettier", "--write", file}) end }, - { - patterns = {"*.tex"}, - command = function(file) - vim.fn.system({"pdflatex", "--shell-escape", file}) - end - }, - { - patterns = {"*.c", "*.h"}, - command = function(file) - vim.fn.system({"clang-format", "-i", file}) - end - } + {patterns = {"*.tex"}, command = function(file) vim.fn.system({"pdflatex", "--shell-escape", file}) end}, + {patterns = {"*.c","*.h"}, command = function(file) vim.fn.system({"clang-format", "-i", file}) end} } for _, fmt in ipairs(formatters) do - vim.api.nvim_create_autocmd( - "BufWritePost", - { - group = run_on_save, - pattern = fmt.patterns, - callback = function(args) - local filepath = args.match or args.file - pcall(fmt.command, filepath) - end - } - ) + vim.api.nvim_create_autocmd("BufWritePost", { + group = run_on_save, pattern = fmt.patterns, + callback = function(args) pcall(fmt.command, args.match or args.file) end + }) end --- Auto-delete Netrw hidden buffers vim.api.nvim_create_augroup("AutoDeleteNetrwHiddenBuffers", {clear = true}) -vim.api.nvim_create_autocmd( - "FileType", - { - group = "AutoDeleteNetrwHiddenBuffers", - pattern = "netrw", - callback = function() - vim.opt_local.bufhidden = "wipe" - vim.opt_local.buflisted = false - end - } -) +vim.api.nvim_create_autocmd("FileType", { + group = "AutoDeleteNetrwHiddenBuffers", pattern = "netrw", + callback = function() vim.opt_local.bufhidden = "wipe"; vim.opt_local.buflisted = false end +}) function ToggleVExplorer() local expl_buf = vim.t.expl_buf_num local expl_win = expl_buf and vim.fn.bufwinnr(expl_buf) or -1 - if expl_win ~= -1 then vim.cmd(expl_win .. "wincmd c") vim.t.expl_buf_num = nil @@ -128,58 +82,26 @@ function ToggleVExplorer() end end -local autopairs = { - ["("] = ")", - ["["] = "]", - ["{"] = "}", - ["<"] = ">" -} - +local autopairs = { ["("]=")", ["["]="]", ["{"]="}", ["<"]=">" } for open, close in pairs(autopairs) do - vim.api.nvim_set_keymap("i", open, open .. close .. "", {noremap = true, silent = true}) + vim.api.nvim_set_keymap("i", open, open..close.."", {noremap=true, silent=true}) end function set_highlights() local highlights = { - {"GitGutterAdd", {bg = "none"}}, - {"GitGutterDelete", {bg = "none"}}, - {"GitGutterChange", {bg = "none"}}, - {"GitGutterChangeAdd", {bg = "none"}}, - {"GitGutterChangeDelete", {bg = "none"}}, - {"NonText", {fg = "none"}}, - {"EndOfBuffer", {fg = "none", bg = "none"}}, - {"SignColumn", {bg = "none"}}, - {"StatusLine", {bg = "none", fg = "none"}}, - {"StatusLineNc", {bg = "none", fg = "none"}}, - {"CursorColumn", {fg = "none"}}, - {"CursorLine", {bg = "none"}}, - {"VertSplit", {bg = "none"}}, - {"DiagnosticError", {bg = "none"}}, - {"DiagnosticWarn", {bg = "none"}}, - {"DiagnosticInfo", {bg = "none"}}, - {"DiagnosticHint", {bg = "none"}}, - {"Pmenu", {bg = "none"}}, - {"DiagnosticVirtualTextError", {bg = "none"}}, - {"DiagnosticVirtualTextWarn", {bg = "none"}}, - {"DiagnosticVirtualTextInfo", {bg = "none"}}, - {"DiagnosticVirtualTextHint", {bg = "none"}}, - {"LspFloatWinNormal", {bg = "none"}}, - {"NormalFloat", {bg = "none"}}, - {"LineNr", {bg = "none"}}, - {"DiagnosticVirtualError", {bg = "none"}}, - {"DiagnosticVirtualWarn", {bg = "none"}}, - {"DiagnosticVirtualInfo", {bg = "none"}}, - {"DiagnosticVirtualHint", {bg = "none"}} + {"GitGutterAdd",{bg="none"}},{"GitGutterDelete",{bg="none"}},{"GitGutterChange",{bg="none"}}, + {"NonText",{fg="none"}},{"EndOfBuffer",{fg="none",bg="none"}},{"SignColumn",{bg="none"}}, + {"StatusLine",{bg="none",fg="none"}},{"StatusLineNc",{bg="none",fg="none"}}, + {"CursorColumn",{fg="none"}},{"CursorLine",{bg="none"}},{"VertSplit",{bg="none"}}, + {"DiagnosticError",{bg="none"}},{"DiagnosticWarn",{bg="none"}},{"DiagnosticInfo",{bg="none"}}, + {"DiagnosticHint",{bg="none"}},{"Pmenu",{bg="none"}},{"LspFloatWinNormal",{bg="none"}}, + {"NormalFloat",{bg="none"}},{"LineNr",{bg="none"}} } - - for _, hl in ipairs(highlights) do - vim.api.nvim_set_hl(0, hl[1], hl[2]) - end + for _, hl in ipairs(highlights) do vim.api.nvim_set_hl(0, hl[1], hl[2]) end end vim.cmd("colorscheme quiet") vim.o.background = "dark" - function color_mode() if vim.o.background == "dark" then vim.o.background = "light" @@ -193,109 +115,107 @@ end local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim" if not vim.loop.fs_stat(lazypath) then - vim.fn.system( - { - "git", - "clone", - "--filter=blob:none", - "https://github.com/folke/lazy.nvim.git", - "--branch=stable", - lazypath - } - ) + vim.fn.system({"git","clone","--filter=blob:none","https://github.com/folke/lazy.nvim.git","--branch=stable",lazypath}) end - vim.opt.rtp:prepend(lazypath) -require("lazy").setup( +require("lazy").setup({ + {"ibhagwan/fzf-lua", opts = {}}, + {"airblade/vim-gitgutter"}, + { - { - "ibhagwan/fzf-lua", - opts = {} - }, - {"airblade/vim-gitgutter"}, - { - "neovim/nvim-lspconfig", - lazy = false, - dependencies = { - {"ms-jpq/coq_nvim", branch = "coq"}, - {"ms-jpq/coq.artifacts", branch = "artifacts"} - }, - init = function() - vim.g.coq_settings = { - auto_start = "shut-up", - display = { - icons = { - mode = "none" - }, - ghost_text = { - enabled = false - } - } - } + "neovim/nvim-lspconfig", + config = function() + local capabilities = require("cmp_nvim_lsp").default_capabilities() + local function get_root(patterns) + local root = vim.fs.dirname(vim.fs.find(patterns, { upward = true })[1]) + return root or vim.fn.getcwd() + end + local on_attach = function(_, bufnr) + local opts = {buffer = bufnr, silent = true} + vim.keymap.set("n", "gd", vim.lsp.buf.definition, opts) + vim.keymap.set("n", "K", vim.lsp.buf.hover, opts) + vim.keymap.set("n", "gr", vim.lsp.buf.references, opts) + vim.keymap.set("n", "ca", vim.lsp.buf.code_action, opts) end - } - } -) - -local lsp_servers = { - clangd = {filetypes = {"c", "h"}}, - svelte = {filetypes = {"svelte"}}, - ts_ls = {filetypes = { - "typescript", - "typescriptreact", - "javascript", - "javascriptreact"} - }, - rust_analyzer = { - filetypes = {"rust"}, - imports = { - granularity = { - group = "module", - }, - prefix = "self", - }, - cargo = { - buildScripts = { - enable = true, - }, - }, - procMacro = { - enable = true - }, - } -} - -local function server_maps(opts) - local buf = opts.buffer -end - - - -local capabilities = vim.lsp.protocol.make_client_capabilities() -capabilities = require("coq").lsp_ensure_capabilities(capabilities) -for server_name, config in pairs(lsp_servers) do - vim.api.nvim_create_autocmd("FileType", { - pattern = config.filetypes, - callback = function() - vim.lsp.start({ - name = server_name, - cmd = vim.lsp.get_server_capabilities(server_name).cmd or { server_name }, - root_dir = vim.fs.dirname(vim.api.nvim_buf_get_name(0)), + vim.lsp.config.clangd = { + cmd = {"clangd"}, + root_dir = get_root({"compile_commands.json", ".git", "Makefile"}), + capabilities = capabilities, + on_attach = on_attach, + } + vim.lsp.config.rust_analyzer = { + cmd = {"rust-analyzer"}, + root_dir = get_root({"Cargo.toml", ".git"}), + capabilities = capabilities, + on_attach = on_attach, + } + vim.lsp.config.ts_ls = { + cmd = {"typescript-language-server","--stdio"}, + root_dir = get_root({"package.json", ".git"}), capabilities = capabilities, - settings = config.settings, - on_attach = function(client, bufnr) - server_maps({buffer = bufnr}) + on_attach = on_attach, + } + + vim.api.nvim_create_autocmd("FileType", { + pattern = {"c","cpp","h","rs","typescript","javascript"}, + callback = function() + local ft = vim.bo.filetype + if ft == "c" or ft == "cpp" or ft == "h" then + vim.lsp.start(vim.lsp.config.clangd) + elseif ft == "rust" then + vim.lsp.start(vim.lsp.config.rust_analyzer) + elseif ft == "typescript" or ft == "javascript" then + vim.lsp.start(vim.lsp.config.ts_ls) + end end }) end - }) -end - - -set_highlights() + }, + { + "hrsh7th/nvim-cmp", + dependencies = { + "hrsh7th/cmp-nvim-lsp","hrsh7th/cmp-buffer","hrsh7th/cmp-path", + "L3MON4D3/LuaSnip","saadparwaiz1/cmp_luasnip","onsails/lspkind.nvim", + { + "rafamadriz/friendly-snippets", + config = function() require("luasnip.loaders.from_vscode").lazy_load() end + }, + }, + config = function() + local cmp = require("cmp") + local luasnip = require("luasnip") + local lspkind = require("lspkind") + cmp.setup({ + snippet = {expand = function(args) luasnip.lsp_expand(args.body) end}, + mapping = cmp.mapping.preset.insert({ + [""] = cmp.mapping.complete(), + [""] = cmp.mapping.confirm({select=true}), + [""] = cmp.mapping(function(fb) + if cmp.visible() then cmp.select_next_item() + elseif luasnip.expand_or_jumpable() then luasnip.expand_or_jump() + else fb() end + end, {"i","s"}), + [""] = cmp.mapping(function(fb) + if cmp.visible() then cmp.select_prev_item() + elseif luasnip.jumpable(-1) then luasnip.jump(-1) + else fb() end + end, {"i","s"}), + }), + sources = cmp.config.sources({ + {name="nvim_lsp", keyword_length = 2},{name="luasnip"},{name="buffer", keyword_length = 3},{name="path"} + }), + formatting = { + format = lspkind.cmp_format({ + mode = "symbol_text", maxwidth = 50, ellipsis_char = "..." + }) + } + }) + end + } +}) vim.g.mapleader = " " vim.keymap.set("n", "e", ToggleVExplorer, {desc = "Toggle VExplorer"}) vim.keymap.set("n", "z", [[:%s/\<\>//gI]]) @@ -310,43 +230,20 @@ vim.keymap.set("n", "f", "FzfLua files") vim.keymap.set("n", "F", "FzfLua live_grep", {noremap = true, silent = true}) vim.keymap.set("n", "b", "FzfLua buffers") vim.keymap.set("n", "d", vim.lsp.buf.definition, {silent = true}) -vim.keymap.set( - "n", - "df", - function() - vim.diagnostic.open_float() - end, - {silent = true} -) +vim.keymap.set("n", "df", vim.diagnostic.open_float, {silent = true}) vim.keymap.set("n", "k", vim.lsp.buf.hover, {silent = true}) -vim.keymap.set( - "n", - "ca", - function() - vim.lsp.buf.code_action() - end, - {silent = true} -) +vim.keymap.set("n", "ca", vim.lsp.buf.code_action, {silent = true}) vim.keymap.set("n", "r", color_mode, {desc = "Change mode light/dark"}) -vim.keymap.set( - "n", - "t", - function() - vim.cmd("terminal") - vim.cmd("startinsert") - end, - {desc = "Open terminal in current window"} -) +vim.keymap.set("n", "t", function() + vim.cmd("terminal") + vim.cmd("startinsert") +end, {desc = "Open terminal"}) vim.keymap.set("v", "J", ":m '>+1gv=gv") vim.keymap.set("v", "K", ":m '<-2gv=gv") +vim.keymap.set("t", "", function() + vim.cmd([[stopinsert]]) + vim.cmd([[b#]]) +end, {noremap = true, silent = true}) +set_highlights() -vim.keymap.set( - "t", - "", - function() - vim.cmd([[stopinsert]]) - vim.cmd([[b#]]) - end, - {noremap = true, silent = true} -) -- cgit v1.2.3