From 21107f040b1579c1139ef0ca2ce993603d24826f Mon Sep 17 00:00:00 2001 From: Kyren223 Date: Sun, 27 Apr 2025 14:12:18 +0300 Subject: Massive nvim refactor, migrated from nvim-lspconfig to NVIM 0.11 lsp/* also migrated most LSP clients to nix rather than mason --- .config/nvim/lua/keymaps.lua | 75 +++++++++++++- .config/nvim/lua/language-servers.lua | 155 ---------------------------- .config/nvim/lua/lsp-keymaps.lua | 68 ------------ .config/nvim/lua/lsp.lua | 96 +++++------------ .config/nvim/lua/plugins/auto-save.lua | 5 + .config/nvim/lua/plugins/blink-cmp.lua | 44 ++++++++ .config/nvim/lua/plugins/noice.lua | 2 +- .config/nvim/lua/plugins/nvim-cmp.lua | 24 ++--- .config/nvim/lua/plugins/nvim-lspconfig.lua | 22 +--- .config/nvim/lua/plugins/snacks.lua | 2 +- 10 files changed, 160 insertions(+), 333 deletions(-) delete mode 100644 .config/nvim/lua/language-servers.lua delete mode 100644 .config/nvim/lua/lsp-keymaps.lua create mode 100644 .config/nvim/lua/plugins/blink-cmp.lua (limited to '.config/nvim/lua') diff --git a/.config/nvim/lua/keymaps.lua b/.config/nvim/lua/keymaps.lua index 94e4223..86547a2 100644 --- a/.config/nvim/lua/keymaps.lua +++ b/.config/nvim/lua/keymaps.lua @@ -15,6 +15,7 @@ end local function yank_filepath_to_clipboard(os) return function() local filepath = vim.fn.expand('%:p') + -- TODO: simplify bcz idc about windows anymore if os == 'linux' then local home_dir = vim.fn.expand('~') filepath = filepath:gsub(home_dir, '~') @@ -58,8 +59,8 @@ set({ 'n', 'x' }, 'p', 'lua RemoveClipboardCR()p', { noremap = true }) set({ 'n', 'x' }, 'P', 'lua RemoveClipboardCR()P', { noremap = true }) -- Move lines around -set('n', '', ":m .+1", { noremap = true }) -set('n', '', ":m .-2", { noremap = true }) +set('n', '', ':m .+1', { noremap = true }) +set('n', '', ':m .-2', { noremap = true }) set('v', '', ":m '>+1gv", { noremap = true }) set('v', '', ":m '<-2gv", { noremap = true }) @@ -99,3 +100,73 @@ set('n', 'x', 'source %', { desc = 'Execute the current -- Reformats file using lsp set('n', '=', vim.lsp.buf.format, { desc = 'Format File' }) + +-- LSP keymaps +local severity = vim.diagnostic.severity +local function next_diagnostic(diagnostic_severity) + return function() + require('lspsaga.diagnostic'):goto_next({ severity = diagnostic_severity }) + end +end +local function prev_diagnostic(diagnostic_severity) + return function() + require('lspsaga.diagnostic'):goto_prev({ severity = diagnostic_severity }) + end +end +local function cursor_diagnostics() + vim.diagnostic.open_float({ + scope = 'cursor', + border = 'single', + }) +end + +local keymaps = { + { 'n', 'gd', 'Lspsaga goto_definition', { desc = '[G]oto [D]efinition' } }, + { 'n', 'gu', 'Lspsaga finder', { desc = '[G]oto [U]sages' } }, + { { 'n', 'i' }, '', vim.lsp.buf.signature_help, { desc = 'Show [P]arameters' } }, + { 'n', 'K', 'Lspsaga hover_doc', { desc = 'Documentation' } }, + { 'n', 'R', 'Lspsaga rename', { desc = '[R]ename' } }, + { { 'n', 'i' }, '', 'Lspsaga code_action', { desc = 'Code Actions' } }, + { 'n', 'ca', 'Lspsaga code_action', { desc = '[C]ode [A]ctions' } }, + { 'n', 'e', next_diagnostic(severity.ERROR), { desc = 'Goto [E]rror' } }, + { 'n', 'E', prev_diagnostic(severity.ERROR), { desc = 'Goto [E]rror (prev)' } }, + { 'n', 'w', next_diagnostic(severity.WARN), { desc = 'Goto [W]arning' } }, + { 'n', 'W', prev_diagnostic(severity.WARN), { desc = 'Goto [W]arning (prev)' } }, + { 'n', 'D', cursor_diagnostics, { desc = '[D]iagnostics under cursor' } }, +} + +for _, ft in ipairs({ 'c', 'h', 'cpp', 'hpp' }) do + keymaps[ft] = { + { 'n', 'H', 'ClangdSwitchSourceHeader', { desc = '[H]eader and Source Switcher' } }, + { 'n', 'K', 'lua require("pretty_hover").hover()', { desc = 'Documentation Hover' } }, + } +end + +local function set_keymaps(tbl, bufnr) + for _, keymap in ipairs(tbl) do + local mode = keymap[1] + local lhs = keymap[2] + local rhs = keymap[3] + local opts = keymap[4] or {} + opts.buffer = bufnr + vim.keymap.set(mode, lhs, rhs, opts) + end +end + +vim.api.nvim_create_autocmd('LspAttach', { + desc = 'Register LSP keymaps', + group = vim.api.nvim_create_augroup('lsp-keymaps', { clear = true }), + callback = function(args) + local bufnr = args.buf + + local filetype = vim.bo[bufnr].filetype + set_keymaps(keymaps, bufnr) + for ft, tbl in pairs(keymaps) do + if type(ft) == 'number' or ft ~= filetype then + goto continue + end + set_keymaps(tbl, bufnr) + ::continue:: + end + end, +}) diff --git a/.config/nvim/lua/language-servers.lua b/.config/nvim/lua/language-servers.lua deleted file mode 100644 index f7e28c1..0000000 --- a/.config/nvim/lua/language-servers.lua +++ /dev/null @@ -1,155 +0,0 @@ --- NOTE: a table or true to enable, false to disable -return { - lua_ls = { - settings = { - diagnostics = { globals = { 'vim' } }, - telemetry = { enabled = false }, - hint = { enable = true }, - workspace = { - library = { - [vim.fn.expand('$VIMRUNTIME/lua')] = true, - [vim.fn.stdpath('config') .. '/lua'] = true, - }, - }, - }, - }, -- lua - clangd = { - mason = false, - cmd = { - 'clangd', - '--background-index', - '--clang-tidy', - '--header-insertion=iwyu', - '--completion-style=detailed', - '--function-arg-placeholders', - '--fallback-style=llvm', - }, - root_dir = function(filename) - return require('lspconfig/util').root_pattern( - '.clang-tidy', - '.clang-format', - 'compile_commands.json', - '.git' - )(filename) or vim.fn.getcwd() - end, - init_options = { - usePlaceholders = true, - completeUnimported = true, - clangdFileStatus = true, - }, - }, -- c/cpp - neocmake = { - root_dir = function(filename) - return require('lspconfig/util').root_pattern( - 'CMakePresets.json', - 'CTestConfig.cmake', - 'build', - 'cmake', - '.git' -- NOTE: git is last due to monorepos (so it tries to use build/cmake first) - )(filename) or vim.fn.getcwd() - end, - }, - pyright = { - settings = { - python = { - pythonPath = '~/python/venv/bin/python', - }, - }, - }, -- python - bashls = true, -- bash - taplo = true, -- toml - lemminx = true, -- xml - yamlls = true, -- yaml - jsonls = true, -- json - html = true, -- html - cssls = true, -- css - ts_ls = true, -- javascript/typescript - astro = true, -- astro framework - mdx_analyzer = true, -- markdown with JS I think - -- marksman = true, -- Markdown LSP - markdown_oxide = true, -- Markdown LSP but better - gopls = { - cmd = { 'gopls' }, - filetypes = { 'go', 'gomod', 'gowork', 'gotmpl' }, - root_dir = function(filename) - return require('lspconfig/util').root_pattern('go.work', 'go.mod', '.git')(filename) - end, - settings = { - gopls = { - gofumpt = true, - codelenses = { - gc_details = false, - generate = true, - regenerate_cgo = true, - run_govulncheck = true, - test = true, - tidy = true, - upgrade_dependency = true, - vendor = true, - }, - hints = { - assignVariableTypes = false, - compositeLiteralFields = false, - compositeLiteralTypes = true, - constantValues = true, - functionTypeParameters = false, - parameterNames = false, - rangeVariableTypes = true, - }, - analyses = { - fieldalignment = true, - nilness = true, - unusedparams = true, - unusedwrite = true, - useany = true, - }, - usePlaceholders = false, - completeUnimported = true, - staticcheck = true, - directoryFilters = { '-.git', '-.vscode', '-.idea', '-.vscode-test', '-node_modules' }, - semanticTokens = true, - }, - }, - }, -- Go LSP - jdtls = true, -- java - -- sqlls = true, - -- sqls = { - -- on_attach = function(client, bufnr) - -- require('sqls').on_attach(client, bufnr) - -- end, - -- settings = { - -- sqls = { - -- connections = { - -- { - -- driver = 'sqlite3', - -- dataSourceName = '/home/kyren/projects/eko/server.db', - -- }, - -- }, - -- }, - -- }, - -- }, - zls = { - mason = false, - cmd = { 'zls' }, - settings = { - zls = { - enable_build_on_save = true, - build_on_save_args = {}, - enable_argument_placeholders = false, - warn_style = true, - - inlay_hints_show_variable_type_hints = true, - inlay_hints_show_struct_literal_field_type = true, - inlay_hints_show_parameter_name = true, - inlay_hints_show_builtin = true, - inlay_hints_hide_redundant_param_names = true, - inlay_hints_hide_redundant_param_names_last_token = true, - }, - }, - }, -- zig - nil_ls = {}, -- nix - asm_lsp = { - mason = false, - cmd = { 'asm-lsp' }, - }, -} diff --git a/.config/nvim/lua/lsp-keymaps.lua b/.config/nvim/lua/lsp-keymaps.lua deleted file mode 100644 index 1721881..0000000 --- a/.config/nvim/lua/lsp-keymaps.lua +++ /dev/null @@ -1,68 +0,0 @@ -local severity = vim.diagnostic.severity -local function next_diagnostic(diagnostic_severity) - return function() - require('lspsaga.diagnostic'):goto_next({ severity = diagnostic_severity }) - end -end -local function prev_diagnostic(diagnostic_severity) - return function() - require('lspsaga.diagnostic'):goto_prev({ severity = diagnostic_severity }) - end -end -local function cursor_diagnostics() - vim.diagnostic.open_float({ - scope = 'cursor', - border = 'single', - }) -end - -local keymaps = { - { 'n', 'gd', 'Lspsaga goto_definition', { desc = '[G]oto [D]efinition' } }, - { 'n', 'gu', 'Lspsaga finder', { desc = '[G]oto [U]sages' } }, - { { 'n', 'i' }, '', vim.lsp.buf.signature_help, { desc = 'Show [P]arameters' } }, - { 'n', 'K', 'Lspsaga hover_doc', { desc = 'Documentation' } }, - { 'n', 'R', 'Lspsaga rename', { desc = '[R]ename' } }, - { { 'n', 'i' }, '', 'Lspsaga code_action', { desc = 'Code Actions' } }, - { 'n', 'ca', 'Lspsaga code_action', { desc = '[C]ode [A]ctions' } }, - { 'n', 'e', next_diagnostic(severity.ERROR), { desc = 'Goto [E]rror' } }, - { 'n', 'E', prev_diagnostic(severity.ERROR), { desc = 'Goto [E]rror (prev)' } }, - { 'n', 'w', next_diagnostic(severity.WARN), { desc = 'Goto [W]arning' } }, - { 'n', 'W', prev_diagnostic(severity.WARN), { desc = 'Goto [W]arning (prev)' } }, - { 'n', 'D', cursor_diagnostics, { desc = '[D]iagnostics under cursor' } }, -} - -for _, ft in ipairs({ 'c', 'h', 'cpp', 'hpp' }) do - keymaps[ft] = { - { 'n', 'H', 'ClangdSwitchSourceHeader', { desc = '[H]eader and Source Switcher' } }, - { 'n', 'K', 'lua require("pretty_hover").hover()', { desc = 'Documentation Hover' } }, - } -end - -local function set_keymaps(tbl, bufnr) - for _, keymap in ipairs(tbl) do - local mode = keymap[1] - local lhs = keymap[2] - local rhs = keymap[3] - local opts = keymap[4] or {} - opts.buffer = bufnr - vim.keymap.set(mode, lhs, rhs, opts) - end -end - -local M = {} - ---- Registers LSP keymaps for the given buffer ----@param bufnr number: the id of the buffer -M.setup = function(bufnr) - local filetype = vim.bo[bufnr].filetype - set_keymaps(keymaps, bufnr) - for ft, tbl in pairs(keymaps) do - if type(ft) == 'number' or ft ~= filetype then - goto continue - end - set_keymaps(tbl, bufnr) - ::continue:: - end -end - -return M diff --git a/.config/nvim/lua/lsp.lua b/.config/nvim/lua/lsp.lua index 89fbc58..1e9cdec 100644 --- a/.config/nvim/lua/lsp.lua +++ b/.config/nvim/lua/lsp.lua @@ -1,73 +1,23 @@ -local lspconfig = require('lspconfig') -local capabilities = require('cmp_nvim_lsp').default_capabilities() - -local servers = require('language-servers') -for name, config in pairs(servers) do - if config == false then - goto continue - end - if config == true then - ---@diagnostic disable-next-line: cast-local-type - config = {} - end - - -- NOTE: there must be a better way to do it, but it will do for now - if name == 'markdown_oxide' then - ---@diagnostic disable-next-line: cast-local-type - config = vim.tbl_deep_extend('force', {}, { - capabilities = vim.tbl_deep_extend('force', capabilities, { - workspace = { - didChangeWatchedFiles = { - dynamicRegistration = true, - }, - }, - }), - }, config) - else - ---@diagnostic disable-next-line: cast-local-type - config = vim.tbl_deep_extend('force', {}, { - capabilities = capabilities, - }, config) - end - - lspconfig[name].setup(config) - ::continue:: -end - -vim.api.nvim_create_autocmd('LspAttach', { - callback = function(args) - local bufnr = args.buf - local client = assert(vim.lsp.get_client_by_id(args.data.client_id), 'must have valid client') - - local settings = servers[client.name] - if type(settings) ~= 'table' then - ---@diagnostic disable-next-line: cast-local-type - settings = {} - end - - -- Override server capabilities - if settings.server_capabilities then - for k, v in pairs(settings.server_capabilities) do - if v == vim.NIL then - v = nil - end - client.server_capabilities[k] = v - end - end - - -- Workaround for semantic tokens apparently? - -- https://www.lazyvim.org/extras/lang/go - -- https://github.com/golang/go/issues/54531#issuecomment-1464982242 - if client.name == 'gopls' and not client.server_capabilities.semanticTokensProvider then - local semantic = client.config.capabilities.textDocument.semanticTokens - client.server_capabilities.semanticTokensProvider = { - full = true, - legend = { tokenModifiers = semantic.tokenModifiers, tokenTypes = semantic.tokenTypes }, - range = true, - } - end - - -- Register buffer local LSP keymaps - require('lsp-keymaps').setup(bufnr) - end, -}) +return { + asm_lsp = true, + astro = true, + basedpyright = true, -- python + bashls = true, + clangd = true, + cssls = true, + gopls = true, + html = true, + jsonls = true, + lemminx = true, -- xml + lua_ls = true, + markdown_oxide = true, + marksman = false, -- markdown + mdx_analyzer = true, + neocmake = true, + nil_ls = true, -- nix + tailwindcss = true, + taplo = true, -- toml + ts_ls = true, -- typescript and yavashit + yamlls = true, + zls = true, -- zig +} diff --git a/.config/nvim/lua/plugins/auto-save.lua b/.config/nvim/lua/plugins/auto-save.lua index 5da836b..99ff70e 100644 --- a/.config/nvim/lua/plugins/auto-save.lua +++ b/.config/nvim/lua/plugins/auto-save.lua @@ -25,6 +25,11 @@ return { return { -- execution_message = { enabled = false }, removed condition = function(buf) + -- TODO: oil issues might be bcz of autosaving? + -- Maybe I should exclude Oil ft? + -- Might be unrelated tho + -- Oil error is "cannot save, action already in progress" + -- Save only if the buffer is not a "special buffer" return vim.fn.getbufvar(buf, '&buftype') == '' end, diff --git a/.config/nvim/lua/plugins/blink-cmp.lua b/.config/nvim/lua/plugins/blink-cmp.lua new file mode 100644 index 0000000..51d910b --- /dev/null +++ b/.config/nvim/lua/plugins/blink-cmp.lua @@ -0,0 +1,44 @@ +return { + 'saghen/blink.cmp', + version = '1.*', + ---@module 'blink.cmp' + ---@type blink.cmp.Config + opts = { + -- 'default' (recommended) for mappings similar to built-in completions (C-y to accept) + -- 'super-tab' for mappings similar to vscode (tab to accept) + -- 'enter' for enter to accept + -- 'none' for no mappings + -- + -- All presets have the following mappings: + -- C-space: Open menu or open docs if already open + -- C-n/C-p or Up/Down: Select next/previous item + -- C-e: Hide menu + -- C-k: Toggle signature help (if signature.enabled = true) + -- + -- See :h blink-cmp-config-keymap for defining your own keymap + keymap = { preset = 'default' }, + + appearance = { + -- 'mono' (default) for 'Nerd Font Mono' or 'normal' for 'Nerd Font' + -- Adjusts spacing to ensure icons are aligned + nerd_font_variant = 'mono', + }, + + -- (Default) Only show the documentation popup when manually triggered + completion = { documentation = { auto_show = false } }, + + -- Default list of enabled providers defined so that you can extend it + -- elsewhere in your config, without redefining it, due to `opts_extend` + sources = { + default = { 'lsp', 'path', 'snippets', 'buffer' }, + }, + + -- (Default) Rust fuzzy matcher for typo resistance and significantly better performance + -- You may use a lua implementation instead by using `implementation = "lua"` or fallback to the lua implementation, + -- when the Rust fuzzy matcher is not available, by using `implementation = "prefer_rust"` + -- + -- See the fuzzy documentation for more information + fuzzy = { implementation = 'prefer_rust_with_warning' }, + }, + opts_extend = { 'sources.default' }, +} diff --git a/.config/nvim/lua/plugins/noice.lua b/.config/nvim/lua/plugins/noice.lua index 6634f48..1420ab5 100644 --- a/.config/nvim/lua/plugins/noice.lua +++ b/.config/nvim/lua/plugins/noice.lua @@ -12,7 +12,7 @@ return { -- override the lsp markdown formatter with Noice ['vim.lsp.util.stylize_markdown'] = true, -- override cmp documentation with Noice (needs the other options to work) - ['cmp.entry.get_documentation'] = true, -- requires hrsh7th/nvim-cmp + -- ['cmp.entry.get_documentation'] = true, -- requires hrsh7th/nvim-cmp }, hover = { enabled = true, diff --git a/.config/nvim/lua/plugins/nvim-cmp.lua b/.config/nvim/lua/plugins/nvim-cmp.lua index 221c756..16b3612 100644 --- a/.config/nvim/lua/plugins/nvim-cmp.lua +++ b/.config/nvim/lua/plugins/nvim-cmp.lua @@ -1,14 +1,14 @@ return { - 'hrsh7th/nvim-cmp', - event = 'InsertEnter', - dependencies = { - 'onsails/lspkind.nvim', -- icons for completion type - 'hrsh7th/cmp-path', -- complete filepaths - 'hrsh7th/cmp-nvim-lsp', -- lsp source for nvim-cmp - 'saadparwaiz1/cmp_luasnip', -- snippets source for nvim-cmp - { 'L3MON4D3/LuaSnip', build = 'make install_jsregexp' }, -- snippets plugin - }, - config = function() - require('cmp_helper') - end, + -- 'hrsh7th/nvim-cmp', + -- event = 'InsertEnter', + -- dependencies = { + -- 'onsails/lspkind.nvim', -- icons for completion type + -- 'hrsh7th/cmp-path', -- complete filepaths + -- 'hrsh7th/cmp-nvim-lsp', -- lsp source for nvim-cmp + -- 'saadparwaiz1/cmp_luasnip', -- snippets source for nvim-cmp + -- { 'L3MON4D3/LuaSnip', build = 'make install_jsregexp' }, -- snippets plugin + -- }, + -- config = function() + -- require('cmp_helper') + -- end, } diff --git a/.config/nvim/lua/plugins/nvim-lspconfig.lua b/.config/nvim/lua/plugins/nvim-lspconfig.lua index 1dd0a77..169f339 100644 --- a/.config/nvim/lua/plugins/nvim-lspconfig.lua +++ b/.config/nvim/lua/plugins/nvim-lspconfig.lua @@ -4,29 +4,9 @@ return { 'neovim/nvim-lspconfig', event = { 'BufReadPre', 'BufNewFile' }, dependencies = { - 'hrsh7th/cmp-nvim-lsp', + -- 'hrsh7th/cmp-nvim-lsp', { 'antosha417/nvim-lsp-file-operations', conifg = true }, - { - 'williamboman/mason-lspconfig.nvim', - build = ':MasonUpdate', - config = function() - local language_servers = require('language-servers') - local ensure_installed = vim.tbl_filter(function(key) - local server = language_servers[key] - if type(server) == 'table' then - if server.mason ~= nil then - return server.mason - end - return true - else - return server - end - end, vim.tbl_keys(language_servers)) - require('mason-lspconfig').setup({ ensure_installed = ensure_installed, auto_install = true }) - end, - }, }, config = function() - require('lsp') end, } diff --git a/.config/nvim/lua/plugins/snacks.lua b/.config/nvim/lua/plugins/snacks.lua index add7435..6095e19 100644 --- a/.config/nvim/lua/plugins/snacks.lua +++ b/.config/nvim/lua/plugins/snacks.lua @@ -21,7 +21,7 @@ return { style = 'compact', filter = function(notif) local exact_filter = { 'No information available', 'No code actions available' } - local contains_filter = { 'fewer lines', 'lines indented', 'lines yanked', 'more lines' } + local contains_filter = { 'fewer lines', 'lines indented', 'lines yanked', 'more lines', 'E486:' } for _, msg in ipairs(exact_filter) do if notif.msg == msg then return false -- cgit v1.3.1