From b402f795414b80d3057a38468649233847ee3a71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Noah=20H=C3=A5kansson?= Date: Tue, 30 Jan 2024 10:38:13 +0100 Subject: [PATCH] plugins, keymaps + fix formatting --- init.lua | 18 ++++---- lazy-lock.json | 25 ++++++----- lua/custom/keymaps.lua | 36 ++++++++------- lua/custom/plugins/bqf.lua | 2 +- lua/custom/plugins/none-ls.lua | 17 +++++++- lua/custom/plugins/persisted.lua | 65 ++++++++++++++++++++++++++++ lua/custom/plugins/ssr.lua | 28 ++++++++++++ lua/custom/plugins/undotree.lua | 32 ++++++++++++++ lua/kickstart/plugins/autoformat.lua | 8 ++-- lua/utils/formatting.lua | 32 ++++++++++++++ 10 files changed, 220 insertions(+), 43 deletions(-) create mode 100644 lua/custom/plugins/persisted.lua create mode 100644 lua/custom/plugins/ssr.lua create mode 100644 lua/custom/plugins/undotree.lua create mode 100644 lua/utils/formatting.lua diff --git a/init.lua b/init.lua index 116ca90c..90f0d972 100644 --- a/init.lua +++ b/init.lua @@ -90,7 +90,7 @@ require('lazy').setup({ -- Useful status updates for LSP -- NOTE: `opts = {}` is the same as calling `require('fidget').setup({})` - { 'j-hui/fidget.nvim', opts = {} }, + { 'j-hui/fidget.nvim', opts = {} }, -- Additional lua configuration, makes nvim stuff amazing! 'folke/neodev.nvim', @@ -115,7 +115,7 @@ require('lazy').setup({ }, -- Useful plugin to show you pending keybinds. - { 'folke/which-key.nvim', opts = {} }, + { 'folke/which-key.nvim', opts = {} }, { -- Adds git related signs to the gutter, as well as utilities for managing changes 'lewis6991/gitsigns.nvim', @@ -358,10 +358,10 @@ vim.keymap.set('n', 'k', "v:count == 0 ? 'gk' : 'k'", { expr = true, silent = tr vim.keymap.set('n', 'j', "v:count == 0 ? 'gj' : 'j'", { expr = true, silent = true }) -- Diagnostic keymaps -vim.keymap.set('n', '[d', vim.diagnostic.goto_prev, { desc = 'Go to previous diagnostic message' }) -vim.keymap.set('n', ']d', vim.diagnostic.goto_next, { desc = 'Go to next diagnostic message' }) -vim.keymap.set('n', 'e', vim.diagnostic.open_float, { desc = 'Open floating diagnostic message' }) -vim.keymap.set('n', 'q', vim.diagnostic.setloclist, { desc = 'Open diagnostics list' }) +vim.keymap.set('n', 'lj', vim.diagnostic.goto_next, { desc = 'Go to next diagnostic message' }) +vim.keymap.set('n', 'lk', vim.diagnostic.goto_prev, { desc = 'Go to previous diagnostic message' }) +vim.keymap.set('n', 'le', vim.diagnostic.open_float, { desc = 'Open floating diagnostic message' }) +vim.keymap.set('n', 'lq', vim.diagnostic.setloclist, { desc = 'Open diagnostics list' }) -- require additional custom keymaps require('custom.keymaps') @@ -589,13 +589,15 @@ end -- document existing key chains require('which-key').register({ ['c'] = { name = '[C]ode', _ = 'which_key_ignore' }, - ['d'] = { name = '[D]ocument', _ = 'which_key_ignore' }, + -- ['d'] = { name = '[D]ocument', _ = 'which_key_ignore' }, ['g'] = { name = '[G]it', _ = 'which_key_ignore' }, -- ['h'] = { name = 'Git [H]unk', _ = 'which_key_ignore' }, ['r'] = { name = '[R]ename', _ = 'which_key_ignore' }, ['s'] = { name = '[S]earch', _ = 'which_key_ignore' }, - ['t'] = { name = '[T]oggle', _ = 'which_key_ignore' }, + -- ['t'] = { name = '[T]oggle', _ = 'which_key_ignore' }, -- ['w'] = { name = '[W]orkspace', _ = 'which_key_ignore' }, + ['l'] = { name = '[L]sp', _ = 'which_key_ignore' }, + ['u'] = { name = '[U]ndoTree', _ = 'which_key_ignore' }, }) -- register which-key VISUAL mode -- required for visual hs (hunk stage) to work diff --git a/lazy-lock.json b/lazy-lock.json index 99c597b3..92b3208d 100644 --- a/lazy-lock.json +++ b/lazy-lock.json @@ -7,10 +7,10 @@ "cmp_luasnip": { "branch": "master", "commit": "05a9ab28b53f71d1aece421ef32fee2cb857a843" }, "copilot.lua": { "branch": "master", "commit": "b03617a6dc4bc88b65ab5deac1631da9a9c2dcaf" }, "dial.nvim": { "branch": "master", "commit": "27eb570085db2ef44bff4f620d3806039184651c" }, - "dressing.nvim": { "branch": "master", "commit": "0e88293ce3459f4bb310125f3366304af6dc7990" }, + "dressing.nvim": { "branch": "master", "commit": "6f212262061a2120e42da0d1e87326e8a41c0478" }, "eyeliner.nvim": { "branch": "main", "commit": "c540d58bf52aa979d4cca639c60387ae0c0ccf88" }, "fidget.nvim": { "branch": "main", "commit": "1d1042d418ee8cb70d68f1e38db639844331c093" }, - "friendly-snippets": { "branch": "main", "commit": "aced40b66b7bae9bc2c37fd7b11841d54727a7b0" }, + "friendly-snippets": { "branch": "main", "commit": "b8fae73a479ae0a1c54f5c98fa687ae8a0addc53" }, "gitsigns.nvim": { "branch": "main", "commit": "2c2463dbd82eddd7dbab881c3a62cfbfbe3c67ae" }, "harpoon": { "branch": "master", "commit": "ccae1b9bec717ae284906b0bf83d720e59d12b91" }, "indent-blankline.nvim": { "branch": "master", "commit": "12e92044d313c54c438bd786d11684c88f6f78cd" }, @@ -18,8 +18,8 @@ "lualine.nvim": { "branch": "master", "commit": "7d131a8d3ba5016229e8a1d08bf8782acea98852" }, "mason-lspconfig.nvim": { "branch": "main", "commit": "b9084b1f42f790d6230dc66dbcb6bcc35b148552" }, "mason-nvim-dap.nvim": { "branch": "main", "commit": "3614a39aae98ccd34124b072939d6283853b3dd2" }, - "mason.nvim": { "branch": "main", "commit": "dcd0ea30ccfc7d47e879878d1270d6847a519181" }, - "neodev.nvim": { "branch": "main", "commit": "64b2a51b02c6f2ae177c745e4d8bc801a339fe09" }, + "mason.nvim": { "branch": "main", "commit": "c43eeb5614a09dc17c03a7fb49de2e05de203924" }, + "neodev.nvim": { "branch": "main", "commit": "345ed5843405126464119154f394d4298c1314c7" }, "none-ls.nvim": { "branch": "main", "commit": "912f81829e9ab4ee3d54c3ea5a304c264f0003bc" }, "nvim-autopairs": { "branch": "master", "commit": "096d0baecc34f6c5d8a6dd25851e9d5ad338209b" }, "nvim-bqf": { "branch": "main", "commit": "bdc2a4e5bb670b3c0e33ada9c0eec636d93a0748" }, @@ -28,20 +28,23 @@ "nvim-dap": { "branch": "master", "commit": "9adbfdca13afbe646d09a8d7a86d5d031fb9c5a5" }, "nvim-dap-go": { "branch": "main", "commit": "a5cc8dcad43f0732585d4793deb02a25c4afb766" }, "nvim-dap-ui": { "branch": "master", "commit": "d845ebd798ad1cf30aa4abd4c4eff795cdcfdd4f" }, - "nvim-lspconfig": { "branch": "master", "commit": "8917d2c830e04bf944a699b8c41f097621283828" }, + "nvim-lspconfig": { "branch": "master", "commit": "021906284dcfb938bc236f8295af2650c60cb807" }, "nvim-navic": { "branch": "master", "commit": "8649f694d3e76ee10c19255dece6411c29206a54" }, "nvim-surround": { "branch": "main", "commit": "703ec63aa798e5e07d309b35e42def34bebe0174" }, - "nvim-tree.lua": { "branch": "master", "commit": "7bdb220d0fe604a77361e92cdbc7af1b8a412126" }, - "nvim-treesitter": { "branch": "master", "commit": "6f586875e35e2b02358efa467317634c766fd1d4" }, + "nvim-tree.lua": { "branch": "master", "commit": "f39f7b6fcd3865ac2146de4cb4045286308f2935" }, + "nvim-treesitter": { "branch": "master", "commit": "e9602f41192ba51842b4be4d02c718a64cd57acf" }, "nvim-treesitter-textobjects": { "branch": "master", "commit": "19a91a38b02c1c28c14e0ba468d20ae1423c39b2" }, - "nvim-web-devicons": { "branch": "master", "commit": "b427ac5f9dff494f839e81441fb3f04a58cbcfbc" }, - "onedark.nvim": { "branch": "master", "commit": "14e5de43cf1ff761c280d1ff5b9980897f5b46c7" }, - "plenary.nvim": { "branch": "master", "commit": "663246936325062427597964d81d30eaa42ab1e4" }, - "schemastore.nvim": { "branch": "main", "commit": "85009f1efcea501d40fb7f575319516d5f93feaf" }, + "nvim-web-devicons": { "branch": "master", "commit": "aaec87dbdaa776bfa0a13c8694bec9bcb7454719" }, + "onedark.nvim": { "branch": "master", "commit": "1230aaf2a427b2c5b73aba6e4a9a5881d3e69429" }, + "persisted.nvim": { "branch": "main", "commit": "edd8aa41cd87f9da1b6ef0c584068dea192f65b7" }, + "plenary.nvim": { "branch": "master", "commit": "4f71c0c4a196ceb656c824a70792f3df3ce6bb6d" }, + "schemastore.nvim": { "branch": "main", "commit": "11d661ae5e08f19b5256661a6491a66fa26cdcfc" }, + "ssr.nvim": { "branch": "main", "commit": "bb323ba621ac647b4ac5638b47666e3ef3c279e1" }, "substitute.nvim": { "branch": "main", "commit": "17ffaeb5a1dc2dbef39cf0865d8a4b6000836714" }, "telescope-fzf-native.nvim": { "branch": "main", "commit": "6c921ca12321edaa773e324ef64ea301a1d0da62" }, "telescope.nvim": { "branch": "0.1.x", "commit": "d90956833d7c27e73c621a61f20b29fdb7122709" }, "typescript-tools.nvim": { "branch": "master", "commit": "c43d9580c3ff5999a1eabca849f807ab33787ea7" }, + "undotree": { "branch": "main", "commit": "80552a0180b49e5ba072c89ae91ce5d4e3aed36b" }, "vim-fugitive": { "branch": "master", "commit": "f116dcc8e21021e6fbfb6b0a9f8f7b9566d933f4" }, "vim-illuminate": { "branch": "master", "commit": "97c1265ff0b67064b6cfdc15bafc50202a537ae2" }, "vim-rhubarb": { "branch": "master", "commit": "ee69335de176d9325267b0fd2597a22901d927b1" }, diff --git a/lua/custom/keymaps.lua b/lua/custom/keymaps.lua index b14b0f19..a44ec78b 100644 --- a/lua/custom/keymaps.lua +++ b/lua/custom/keymaps.lua @@ -4,25 +4,29 @@ local wk = require('which-key') +-- Quality of life +vim.keymap.set('n', 'w', 'w', { noremap = true, silent = true, desc = '[W]rite Buffer' }) +vim.keymap.set('n', 'q', 'q', { noremap = true, silent = true, desc = '[Q]uit Buffer' }) + -- basic navigation wk.register({ - ['w'] = { name = '', _ = 'which_key_ignore' }, + ['d'] = { name = '[D]o ', _ = 'which_key_ignore' }, }) -vim.keymap.set('n', 'wh', 'h', { noremap = true, silent = true, desc = 'move to left window' }) -vim.keymap.set('n', 'wj', 'j', { noremap = true, silent = true, desc = 'move to bottom window' }) -vim.keymap.set('n', 'wk', 'k', { noremap = true, silent = true, desc = 'move to top window' }) -vim.keymap.set('n', 'wl', 'l', { noremap = true, silent = true, desc = 'move to right window' }) -vim.keymap.set('n', 'ws', 's', { noremap = true, silent = true, desc = 'split window horizontally' }) -vim.keymap.set('n', 'wv', 'v', { noremap = true, silent = true, desc = 'split window vertically' }) -vim.keymap.set('n', 'wc', 'c', { noremap = true, silent = true, desc = 'close window' }) -vim.keymap.set('n', 'wq', 'q', { noremap = true, silent = true, desc = 'quit window' }) -vim.keymap.set('n', 'wo', 'o', { noremap = true, silent = true, desc = 'close all other windows' }) -vim.keymap.set('n', 'ww', 'w', { noremap = true, silent = true, desc = 'move to next window' }) -vim.keymap.set('n', 'w+', '+', { noremap = true, silent = true, desc = 'increase window height' }) -vim.keymap.set('n', 'w-', '-', { noremap = true, silent = true, desc = 'decrease window height' }) -vim.keymap.set('n', 'w>', '>', { noremap = true, silent = true, desc = 'increase window width' }) -vim.keymap.set('n', 'w<', '<', { noremap = true, silent = true, desc = 'decrease window width' }) -vim.keymap.set('n', 'w=', '=', { noremap = true, silent = true, desc = 'balance window sizes' }) +vim.keymap.set('n', 'dh', 'h', { noremap = true, silent = true, desc = 'move to left window' }) +vim.keymap.set('n', 'dj', 'j', { noremap = true, silent = true, desc = 'move to bottom window' }) +vim.keymap.set('n', 'dk', 'k', { noremap = true, silent = true, desc = 'move to top window' }) +vim.keymap.set('n', 'dl', 'l', { noremap = true, silent = true, desc = 'move to right window' }) +vim.keymap.set('n', 'ds', 's', { noremap = true, silent = true, desc = 'split window horizontally' }) +vim.keymap.set('n', 'dv', 'v', { noremap = true, silent = true, desc = 'split window vertically' }) +vim.keymap.set('n', 'dc', 'c', { noremap = true, silent = true, desc = 'close window' }) +vim.keymap.set('n', 'dq', 'q', { noremap = true, silent = true, desc = 'quit window' }) +vim.keymap.set('n', 'do', 'o', { noremap = true, silent = true, desc = 'close all other windows' }) +vim.keymap.set('n', 'dw', 'w', { noremap = true, silent = true, desc = 'move to next window' }) +vim.keymap.set('n', 'd+', '+', { noremap = true, silent = true, desc = 'increase window height' }) +vim.keymap.set('n', 'd-', '-', { noremap = true, silent = true, desc = 'decrease window height' }) +vim.keymap.set('n', 'd>', '>', { noremap = true, silent = true, desc = 'increase window width' }) +vim.keymap.set('n', 'd<', '<', { noremap = true, silent = true, desc = 'decrease window width' }) +vim.keymap.set('n', 'd=', '=', { noremap = true, silent = true, desc = 'balance window sizes' }) -- Turn off highlight when pressing Esc vim.keymap.set('n', '', 'noh ', { noremap = false, silent = true }) diff --git a/lua/custom/plugins/bqf.lua b/lua/custom/plugins/bqf.lua index 150b759b..2173d900 100644 --- a/lua/custom/plugins/bqf.lua +++ b/lua/custom/plugins/bqf.lua @@ -12,7 +12,7 @@ function M.config() auto_preview = true, show_title = true, delay_syntax = 50, - wrap = false, + wrap = true, }, func_map = { tab = 't', diff --git a/lua/custom/plugins/none-ls.lua b/lua/custom/plugins/none-ls.lua index 78b6863d..d4f5ec96 100644 --- a/lua/custom/plugins/none-ls.lua +++ b/lua/custom/plugins/none-ls.lua @@ -19,12 +19,25 @@ function M.config() formatting.stylua, formatting.black, formatting.prettier.with({ - extra_filetypes = { 'toml' }, + filetypes = { + 'css', + 'scss', + 'less', + 'html', + 'json', + 'jsonc', + 'yaml', + 'toml', + 'markdown', + 'markdown.mdx', + 'graphql', + 'handlebars', + }, -- extra_args = { "--no-semi", "--single-quote", "--jsx-single-quote" }, }), formatting.eslint_d, diagnostics.eslint_d, - null_ls.builtins.diagnostics.flake8, + diagnostics.flake8, -- diagnostics.flake8, code_actions.eslint_d, }, diff --git a/lua/custom/plugins/persisted.lua b/lua/custom/plugins/persisted.lua new file mode 100644 index 00000000..4b3d54bb --- /dev/null +++ b/lua/custom/plugins/persisted.lua @@ -0,0 +1,65 @@ +-- +-- better session management with git branch support +-- + +local M = { + 'olimorris/persisted.nvim', + lazy = false, +} + +function M.config() + require('persisted').setup({ + save_dir = vim.fn.expand(vim.fn.stdpath('data') .. '/sessions/'), -- directory where session files are saved: Resolves to ~/.local/share/lvim/sessions/ + silent = false, -- silent nvim message when sourcing session file + use_git_branch = true, -- create session files based on the branch of the git enabled repository + autosave = true, -- automatically save session files when exiting Neovim + should_autosave = function() -- function to determine if a session should be autosaved + -- do not autosave if the current filetype is ""(empty buffer), NvimTree or alpha + if vim.bo.filetype == '' or vim.bo.filetype == 'NvimTree' or vim.bo.filetype == 'alpha' then + return false + end + return true + end, + autoload = true, -- automatically load the session for the cwd on Neovim startup + on_autoload_no_session = function() -- function to run when `autoload = true` but there is no session to load + end, + follow_cwd = true, -- change session file name to match current working directory if it changes + allowed_dirs = { + '~/Flexfiles', + '~/projects', + '~/work', + }, -- table of dirs that the plugin will auto-save and auto-load from + ignored_dirs = nil, -- table of dirs that are ignored when auto-saving and auto-loading + telescope = { -- options for the telescope extension + reset_prompt_after_deletion = true, -- whether to reset prompt after session deleted + }, + }) +end + +-- persisted autocommands +local persistedGroup = vim.api.nvim_create_augroup('PersistedHooks', { clear = true }) + +-- close NvimTree and DiffView(git) before saving session +vim.api.nvim_create_autocmd({ 'User' }, { + pattern = 'PersistedSavePre', + group = persistedGroup, + callback = function() + pcall(vim.cmd, 'NvimTreeClose') + pcall(vim.cmd, 'DiffviewClose') + end, +}) +-- close empty buffers before saving session +vim.api.nvim_create_autocmd({ 'User' }, { + pattern = 'PersistedSavePre', + group = persistedGroup, + callback = function() + local buflist = vim.fn.getbufinfo({ buflisted = 1 }) + for _, buf in ipairs(buflist) do + if buf.name == '' then + vim.api.nvim_buf_delete(buf.bufnr, { force = false }) + end + end + end, +}) + +return M diff --git a/lua/custom/plugins/ssr.lua b/lua/custom/plugins/ssr.lua new file mode 100644 index 00000000..a7741645 --- /dev/null +++ b/lua/custom/plugins/ssr.lua @@ -0,0 +1,28 @@ +local M = { + 'cshuaimin/ssr.nvim', + lazy = true, +} + +function M.config() + require('ssr').setup({ + border = 'rounded', + min_width = 50, + min_height = 5, + max_width = 120, + max_height = 25, + adjust_window = true, + keymaps = { + close = 'q', + next_match = 'n', + prev_match = 'N', + replace_confirm = '', + replace_all = '', + }, + }) +end + +vim.keymap.set({ 'n', 'x' }, 'r', function() + require('ssr').open() +end) + +return M diff --git a/lua/custom/plugins/undotree.lua b/lua/custom/plugins/undotree.lua new file mode 100644 index 00000000..4416ecf8 --- /dev/null +++ b/lua/custom/plugins/undotree.lua @@ -0,0 +1,32 @@ +local M = { + 'jiaoshijie/undotree', + dependencies = 'nvim-lua/plenary.nvim', + config = true, + keys = { -- load the plugin only when using it's keybinding: + { 'u', "lua require('undotree').toggle()" }, + }, +} + +function M.config() + require('undotree').setup({ + float_diff = true, -- using float window previews diff, set this `true` will disable layout option + layout = 'left_bottom', -- "left_bottom", "left_left_bottom" + position = 'left', -- "right", "bottom" + ignore_filetype = { 'NvimTree', 'fugitive', 'undotree', 'undotreeDiff', 'qf', 'TelescopePrompt', 'spectre_panel', 'tsplayground' }, + window = { + winblend = 30, + }, + keymaps = { + ['j'] = 'move_next', + ['k'] = 'move_prev', + ['gj'] = 'move2parent', + ['J'] = 'move_change_next', + ['K'] = 'move_change_prev', + [''] = 'action_enter', + ['p'] = 'enter_diffbuf', + ['q'] = 'quit', + }, + }) +end + +return M diff --git a/lua/kickstart/plugins/autoformat.lua b/lua/kickstart/plugins/autoformat.lua index bc56b15b..a0b7e653 100644 --- a/lua/kickstart/plugins/autoformat.lua +++ b/lua/kickstart/plugins/autoformat.lua @@ -60,12 +60,10 @@ return { return end - vim.lsp.buf.format { + vim.lsp.buf.format({ async = false, - filter = function(c) - return c.id == client.id - end, - } + filter = require('utils.formatting').format_filter, + }) end, }) end, diff --git a/lua/utils/formatting.lua b/lua/utils/formatting.lua new file mode 100644 index 00000000..dade80f5 --- /dev/null +++ b/lua/utils/formatting.lua @@ -0,0 +1,32 @@ +local M = {} + +---filter passed to vim.lsp.buf.format +---always selects null-ls if it's available and caches the value per buffer +---@param client table client attached to a buffer +---@return boolean if client matches +function M.format_filter(client) + local filetype = vim.bo.filetype + local n = require('null-ls') + local s = require('null-ls.sources') + local method = n.methods.FORMATTING + local available_formatters = s.get_available(filetype, method) + + if #available_formatters > 0 then + return client.name == 'null-ls' + elseif client.supports_method('textDocument/formatting') then + return true + else + return false + end +end + +---Simple wrapper for vim.lsp.buf.format() to provide defaults +---@param opts table|nil +function M.format(opts) + opts = opts or {} + opts.filter = opts.filter or M.format_filter + + return vim.lsp.buf.format(opts) +end + +return M