From 9e1e6df9791d778605d01c726d9be30926dc753d Mon Sep 17 00:00:00 2001 From: chayleaf Date: Tue, 21 Feb 2023 08:03:51 +0700 Subject: [PATCH] update vim config --- home/common/vim.nix | 167 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 143 insertions(+), 24 deletions(-) diff --git a/home/common/vim.nix b/home/common/vim.nix index 78d485a..4795f29 100644 --- a/home/common/vim.nix +++ b/home/common/vim.nix @@ -1,5 +1,6 @@ { config, pkgs, ... }: { + imports = [ ./options.nix ]; programs.neovim = { enable = true; defaultEditor = true; @@ -22,9 +23,7 @@ syntax on au FileType markdown set colorcolumn=73 textwidth=72 au FileType gitcommit set colorcolumn=73 - - lua << EOF - EOF + highlight NormalFloat guibg=NONE ''; viAlias = true; vimAlias = true; @@ -52,33 +51,123 @@ require("nvim-tree").setup({ -- :help nvim-tree-setup }) + local opts = { noremap=true, silent=true } + vim.keymap.set('n', '', require("nvim-tree.api").tree.toggle, opts) + require("which-key").register({[''] = { + require("nvim-tree.api").tree.toggle, 'Toggle NvimTree' + }}, {mode='n', noremap=true, silent=true}) ''; } vim-sleuth luasnip { plugin = nvim-cmp; config = lua '' - local cmp = require('cmp') - cmp.setup { - snippet = { - expand = function(args) - require('luasnip').lsp_expand(args.body) - end, - }, - mapping = { - [''] = cmp.mapping.select_prev_item(), - [''] = cmp.mapping.select_next_item(), - [''] = cmp.mapping.complete(), - [''] = cmp.mapping.close(), - [''] = cmp.mapping.confirm { select = true }, - }, - sources = cmp.config.sources({ - { name = 'nvim_lsp' }, - { name = 'luasnip' }, - }), - } + local cmp = require('cmp') + local function border(hl_name) + return { + { "╭", hl_name }, + { "─", hl_name }, + { "╮", hl_name }, + { "│", hl_name }, + { "╯", hl_name }, + { "─", hl_name }, + { "╰", hl_name }, + { "│", hl_name }, + } + end + cmp.setup { + snippet = { + expand = function(args) + require('luasnip').lsp_expand(args.body) + end, + }, + view = { + + }, + window = { + completion = { + border = border "CmpBorder", + winhighlight = "Normal:CmpPmenu,CursorLine:PmenuSel,Search:None", + }, + documentation = { + border = border "CmpDocBorder", + }, + }, + formatting = { + format = function(_, vim_item) + local icons = require("lspkind") + vim_item.kind = string.format("%s %s", icons[vim_item.kind], vim_item.kind) + return vim_item + end, + }, + mapping = { + [''] = cmp.mapping.select_prev_item(), + [''] = cmp.mapping.select_next_item(), + [''] = cmp.mapping.complete(), + [''] = cmp.mapping.close(), + [''] = cmp.mapping.confirm { + behavior = cmp.ConfirmBehavior.Replace, + select = false, + }, + [''] = cmp.mapping(function(fallback) + if cmp.visible() then + cmp.select_next_item() + elseif require("luasnip").expand_or_jumpable() then + vim.fn.feedkeys(vim.api.nvim_replace_termcodes('luasnip-expand-or-jump', true, true, true), "") + else + fallback() + end + end, { + "i", + "s", + }), + [''] = cmp.mapping(function(fallback) + if cmp.visible() then + cmp.select_prev_item() + elseif require('luasnip').jumpable(-1) then + vim.fn.feedkeys(vim.api.nvim_replace_termcodes('luasnip-jump-prev', true, true, true), "") + else + fallback() + end + end, { + "i", + "s", + }), + }, + sources = cmp.config.sources({ + { name = 'nvim_lsp' }, + { name = 'luasnip' }, + }), + } ''; } + lspkind-nvim cmp_luasnip cmp-nvim-lsp + { plugin = nvim-autopairs; + config = lua '' + require('nvim-autopairs').setup({ + disable_filetype = { "TelescopePrompt" , "vim" }, + }) + local cmp_autopairs = require('nvim-autopairs.completion.cmp') + local cmp = require('cmp') + cmp.event:on( + 'confirm_done', + cmp_autopairs.on_confirm_done() + ) + ''; } + { plugin = comment-nvim; + config = lua '' + require('Comment').setup() + local opts = { noremap=true, silent=true } + vim.keymap.set('n', '/', require("Comment.api").toggle.linewise.current, opts) + vim.keymap.set('v', '/', "lua require('Comment.api').toggle.linewise(vim.fn.visualmode())", opts) + require("which-key").register({['/'] = { + require("Comment.api").toggle.linewise.current, 'Comment current line' + }}, {mode='n', noremap=true, silent=true}) + require("which-key").register({['/'] = { + "lua require('Comment.api').toggle.linewise(vim.fn.visualmode())", 'Comment current line' + }}, {mode='v', noremap=true, silent=true}) + + ''; } { plugin = nvim-lspconfig; config = lua '' -- Mappings. @@ -88,6 +177,12 @@ vim.keymap.set('n', '[d', vim.diagnostic.goto_prev, opts) vim.keymap.set('n', ']d', vim.diagnostic.goto_next, opts) vim.keymap.set('n', 'q', vim.diagnostic.setloclist, opts) + require("which-key").register({ + ['e'] = { vim.diagnostic.open_float, 'Show diagnostics in a floating window.' }, + ['[d'] = { vim.diagnostic.goto_prev, 'Move to the previous diagnostic in the current buffer.' }, + [']d'] = { vim.diagnostic.goto_next, 'Get the next diagnostic closest to the cursor position.' }, + ['q'] = { vim.diagnostic.setloclist, 'Add buffer diagnostics to the location list.' }, + }, {mode='n', noremap=true, silent=true}) -- Use an on_attach function to only map the following keys -- after the language server attaches to the current buffer @@ -113,6 +208,25 @@ vim.keymap.set('n', 'ca', vim.lsp.buf.code_action, bufopts) vim.keymap.set('n', 'gr', vim.lsp.buf.references, bufopts) vim.keymap.set('n', 'f', function() vim.lsp.buf.format { async = true } end, bufopts) + require("which-key").register({ + ['gD'] = { vim.lsp.buf.declaration, 'Jumps to the declaration of the symbol under the cursor.' }, + ['gd'] = { vim.lsp.buf.definition, 'Jumps to the definition of the symbol under the cursor.' }, + ['K'] = { vim.lsp.buf.hover, 'Displays hover information about the symbol under the cursor in a floating window.' }, + ['gi'] = { vim.lsp.buf.implementation, 'Lists all the implementations for the symbol under the cursor in the quickfix window.' }, + [''] = { vim.lsp.buf.signature_help, 'Displays signature information about the symbol under the cursor in a floating window.' }, + ['wa'] = { vim.lsp.buf.add_workspace_folder, 'Add a folder to the workspace folders.' }, + ['wr'] = { vim.lsp.buf.remove_workspace_folder, 'Remove a folder from the workspace folders.' }, + ['wl'] = { function() + print(vim.inspect(vim.lsp.buf.list_workspace_folders())) + end, 'List workspace folders.' }, + ['D'] = { vim.lsp.buf.type_definition, 'Jumps to the definition of the type of the symbol under the cursor.' }, + ['rn'] = { vim.lsp.buf.rename, 'Rename old_fname to new_fname' }, + ['ca'] = { vim.lsp.buf.code_action, 'Selects a code action available at the current cursor position.' }, + ['gr'] = { vim.lsp.buf.references, 'Lists all the references to the symbol under the cursor in the quickfix window.' }, + ['f'] = { function() + vim.lsp.buf.format { async = true } + end, 'Formats a buffer.' }, + }, {mode='n', noremap=true, silent=true, buffer=bufnr}) end local lsp_flags = { @@ -191,8 +305,13 @@ ["rust-analyzer"] = {} } } - ''; - } + ''; } + { plugin = which-key-nvim; + config = lua '' + vim.o.timeout = true + vim.o.timeoutlen = 500 + require("which-key").setup { } + ''; } ]; }; }