From ed7f7b1e5a1aa96d912c42de58dbb74ac4a63469 Mon Sep 17 00:00:00 2001 From: Shubham Pawar Date: Sat, 7 Dec 2024 09:13:52 +0530 Subject: [PATCH] chore: adding snacks and dooing nvim --- .config/nvim-lazy/lua/plugins/ai.lua | 2 +- .config/nvim-lazy/lua/plugins/treesitter.lua | 52 ++++++ .config/nvim/after/plugin/autocommands.lua | 10 ++ .config/nvim/lua/plugins/autopairs.lua | 2 +- .config/nvim/lua/plugins/avante.lua | 2 +- .config/nvim/lua/plugins/config/autopairs.lua | 150 +++++++++++++++++- .config/nvim/lua/plugins/dadbod.lua | 2 +- .config/nvim/lua/plugins/dooing.lua | 15 ++ .config/nvim/lua/plugins/faster.lua | 1 - .config/nvim/lua/plugins/git-conflicts.lua | 6 + .config/nvim/lua/plugins/mini/files.lua | 25 ++- .config/nvim/lua/plugins/mini/pairs.lua | 1 + .config/nvim/lua/plugins/mini/surround.lua | 2 +- .config/nvim/lua/plugins/snacks.lua | 48 ++++++ .config/nvim/lua/plugins/surround.lua | 2 +- .config/nvim/lua/plugins/tree-sitter.lua | 68 +++++--- .config/nvim/plugin/lastplace.lua.bkp | 45 ------ 17 files changed, 345 insertions(+), 88 deletions(-) create mode 100644 .config/nvim-lazy/lua/plugins/treesitter.lua create mode 100644 .config/nvim/lua/plugins/dooing.lua delete mode 100644 .config/nvim/lua/plugins/faster.lua create mode 100644 .config/nvim/lua/plugins/git-conflicts.lua create mode 100644 .config/nvim/lua/plugins/snacks.lua delete mode 100644 .config/nvim/plugin/lastplace.lua.bkp diff --git a/.config/nvim-lazy/lua/plugins/ai.lua b/.config/nvim-lazy/lua/plugins/ai.lua index bbd7b6b..09bd1f7 100644 --- a/.config/nvim-lazy/lua/plugins/ai.lua +++ b/.config/nvim-lazy/lua/plugins/ai.lua @@ -144,7 +144,7 @@ return { env = { url = "https://glhf.chat", api_key = function() - get_age_credentials("glhf.age") + return get_age_credentials("glhf.age") end, chat_url = "/api/openai/v1/chat/completions", }, diff --git a/.config/nvim-lazy/lua/plugins/treesitter.lua b/.config/nvim-lazy/lua/plugins/treesitter.lua new file mode 100644 index 0000000..aff8973 --- /dev/null +++ b/.config/nvim-lazy/lua/plugins/treesitter.lua @@ -0,0 +1,52 @@ +---@type LazySpec +return { + "nvim-treesitter/nvim-treesitter", + opts = { + textobjects = { + move = { + enable = true, + goto_next_start = { + ["]f"] = "@function.outer", + ["]c"] = "@class.outer", + ["]a"] = "@parameter.inner", + ["]l"] = "@loop.*", + }, + goto_next_end = { + ["]F"] = "@function.outer", + ["]C"] = "@class.outer", + ["]A"] = "@parameter.inner", + ["]L"] = "@loop.*", + }, + goto_previous_start = { + ["[f"] = "@function.outer", + ["[c"] = "@class.outer", + ["[a"] = "@parameter.inner", + }, + goto_previous_end = { + ["[F"] = "@function.outer", + ["[C"] = "@class.outer", + ["[A"] = "@parameter.inner", + }, + goto_next = { + ["]i"] = "@conditional.outer", + }, + goto_previous = { + ["[i"] = "@conditional.outer", + }, + }, + swap = { + enable = true, + swap_next = { + ["k"] = { query = "@block.outer", desc = "Swap next block" }, + ["f"] = { query = "@function.outer", desc = "Swap next function" }, + ["a"] = { query = "@parameter.inner", desc = "Swap next argument" }, + }, + swap_previous = { + ["K"] = { query = "@block.outer", desc = "Swap previous block" }, + ["F"] = { query = "@function.outer", desc = "Swap previous function" }, + ["A"] = { query = "@parameter.inner", desc = "Swap previous argument" }, + }, + }, + }, + }, +} diff --git a/.config/nvim/after/plugin/autocommands.lua b/.config/nvim/after/plugin/autocommands.lua index daa75fb..5fa6d50 100644 --- a/.config/nvim/after/plugin/autocommands.lua +++ b/.config/nvim/after/plugin/autocommands.lua @@ -40,6 +40,16 @@ vim.api.nvim_create_autocmd('TermOpen', { end, }) +vim.api.nvim_create_autocmd('FileType', { + group = au_term, + pattern = 'toggleterm', + desc = 'fix: to disable foldexpr for terminal to fix this issue. Takes too much time to open terminal.', + callback = function() + vim.opt_local.foldexpr = '' + vim.opt_local.foldmethod = 'manual' + end, +}) + vim.api.nvim_create_autocmd('FileType', { group = au_buffer, pattern = 'lazy', diff --git a/.config/nvim/lua/plugins/autopairs.lua b/.config/nvim/lua/plugins/autopairs.lua index 9da76cd..a184973 100644 --- a/.config/nvim/lua/plugins/autopairs.lua +++ b/.config/nvim/lua/plugins/autopairs.lua @@ -2,6 +2,6 @@ return { 'windwp/nvim-autopairs', event = 'InsertEnter', - enabled = false, + enabled = true, config = require('plugins.config.autopairs').config, } diff --git a/.config/nvim/lua/plugins/avante.lua b/.config/nvim/lua/plugins/avante.lua index d4013bb..0b94b14 100644 --- a/.config/nvim/lua/plugins/avante.lua +++ b/.config/nvim/lua/plugins/avante.lua @@ -146,7 +146,7 @@ return { env = { url = 'https://glhf.chat', api_key = function() - get_age_credentials 'glhf.age' + return get_age_credentials 'glhf.age' end, chat_url = '/api/openai/v1/chat/completions', }, diff --git a/.config/nvim/lua/plugins/config/autopairs.lua b/.config/nvim/lua/plugins/config/autopairs.lua index 3ce8ea3..1bbc1ca 100644 --- a/.config/nvim/lua/plugins/config/autopairs.lua +++ b/.config/nvim/lua/plugins/config/autopairs.lua @@ -1,19 +1,165 @@ local M = {} -M.config = function(_, opts) +local function rule_add_spaces_between_paren() + local npairs = require 'nvim-autopairs' + local Rule = require 'nvim-autopairs.rule' + local cond = require 'nvim-autopairs.conds' + + local brackets = { { '(', ')' }, { '[', ']' }, { '{', '}' } } + npairs.add_rules({ + -- Rule for a pair with left-side ' ' and right side ' ' + Rule(' ', ' ') + -- Pair will only occur if the conditional function returns true + :with_pair(function(opts) + -- We are checking if we are inserting a space in (), [], or {} + local pair = opts.line:sub(opts.col - 1, opts.col) + return vim.tbl_contains({ + brackets[1][1] .. brackets[1][2], + brackets[2][1] .. brackets[2][2], + brackets[3][1] .. brackets[3][2], + }, pair) + end) + :with_move(cond.none()) + :with_cr(cond.none()) + -- We only want to delete the pair of spaces when the cursor is as such: ( | ) + :with_del(function(opts) + local col = vim.api.nvim_win_get_cursor(0)[2] + local context = opts.line:sub(col - 1, col + 2) + return vim.tbl_contains({ + brackets[1][1] .. ' ' .. brackets[1][2], + brackets[2][1] .. ' ' .. brackets[2][2], + brackets[3][1] .. ' ' .. brackets[3][2], + }, context) + end), + }) + -- For each pair of brackets we will add another rule + for _, bracket in pairs(brackets) do + npairs.add_rules({ + -- Each of these rules is for a pair with left-side '( ' and right-side ' )' for each bracket type + Rule(bracket[1] .. ' ', ' ' .. bracket[2]) + :with_pair(cond.none()) + :with_move(function(opts) + return opts.char == bracket[2] + end) + :with_del(cond.none()) + :use_key(bracket[2]) + -- Removes the trailing whitespace that can occur without this + :replace_map_cr(function(_) + return '2xiO' + end), + }) + end +end + +local function rule_add_space_on_equal() + local Rule = require 'nvim-autopairs.rule' + local cond = require 'nvim-autopairs.conds' + Rule('=', '') + :with_pair(cond.not_inside_quote()) + :with_pair(function(opts) + local last_char = opts.line:sub(opts.col - 1, opts.col - 1) + if last_char:match '[%w%=%s]' then + return true + end + return false + end) + :replace_endpair(function(opts) + local prev_2char = opts.line:sub(opts.col - 2, opts.col - 1) + local next_char = opts.line:sub(opts.col, opts.col) + next_char = next_char == ' ' and '' or ' ' + if prev_2char:match '%w$' then + return ' =' .. next_char + end + if prev_2char:match '%=$' then + return next_char + end + if prev_2char:match '=' then + return '=' .. next_char + end + return '' + end) + :set_end_pair_length(0) + :with_move(cond.none()) + :with_del(cond.none()) +end +-- https://github.com/rstacruz/vim-closer/blob/master/autoload/closer.vim +local get_closing_for_line = function(line) + local i = -1 + local clo = '' + + while true do + i, _ = string.find(line, '[%(%)%{%}%[%]]', i + 1) + if i == nil then + break + end + local ch = string.sub(line, i, i) + local st = string.sub(clo, 1, 1) + + if ch == '{' then + clo = '}' .. clo + elseif ch == '}' then + if st ~= '}' then + return '' + end + clo = string.sub(clo, 2) + elseif ch == '(' then + clo = ')' .. clo + elseif ch == ')' then + if st ~= ')' then + return '' + end + clo = string.sub(clo, 2) + elseif ch == '[' then + clo = ']' .. clo + elseif ch == ']' then + if st ~= ']' then + return '' + end + clo = string.sub(clo, 2) + end + end + + return clo +end + +local function rule_expand_multiple_on_enter(autopairs) + local Rule = require 'nvim-autopairs.rule' + + autopairs.remove_rule '(' + autopairs.remove_rule '{' + autopairs.remove_rule '[' + + autopairs.add_rule(Rule('[%(%{%[]', '') + :use_regex(true) + :replace_endpair(function(opts) + return get_closing_for_line(opts.line) + end) + :end_wise(function(opts) + -- Do not endwise if there is no closing + return get_closing_for_line(opts.line) ~= '' + end)) +end + +function M.config(_, opts) local options = vim.tbl_deep_extend('force', { disable_filetype = { 'TelescopePrompt' }, ignored_next_char = '[%w%.]', - check_ts = true, + check_ts = false, map_c_w = true, fast_wrap = {}, }, opts) local npairs = require 'nvim-autopairs' npairs.setup(options) + + rule_add_spaces_between_paren() + rule_add_space_on_equal() + -- rule_expand_multiple_on_enter(npairs) + local ok, cmp = pcall(require, 'cmp') if ok then local cmp_autopairs = require 'nvim-autopairs.completion.cmp' cmp.event:on('confirm_done', cmp_autopairs.on_confirm_done()) end end + return M diff --git a/.config/nvim/lua/plugins/dadbod.lua b/.config/nvim/lua/plugins/dadbod.lua index 55c91f5..b58d053 100644 --- a/.config/nvim/lua/plugins/dadbod.lua +++ b/.config/nvim/lua/plugins/dadbod.lua @@ -12,7 +12,7 @@ return { 'DBUIFindBuffer', }, keys = { - { 'td', 'DBUIToggle', desc = 'Toggle DBUI' }, + { 'tD', 'DBUIToggle', desc = 'Toggle DBUI' }, }, init = function() -- Your DBUI configuration diff --git a/.config/nvim/lua/plugins/dooing.lua b/.config/nvim/lua/plugins/dooing.lua new file mode 100644 index 0000000..1bc171b --- /dev/null +++ b/.config/nvim/lua/plugins/dooing.lua @@ -0,0 +1,15 @@ +---@type LazySpec +return { + 'atiladefreitas/dooing', + keys = { 'td' }, + cmd = { 'Dooing' }, + opts = { + -- Keymaps + keymaps = { + toggle_window = 'td', + }, + }, + config = function(_, opts) + require('dooing').setup(opts) + end, +} diff --git a/.config/nvim/lua/plugins/faster.lua b/.config/nvim/lua/plugins/faster.lua deleted file mode 100644 index e9904a8..0000000 --- a/.config/nvim/lua/plugins/faster.lua +++ /dev/null @@ -1 +0,0 @@ -return { 'pteroctopus/faster.nvim', lazy = false, version = "*", opts = {} } diff --git a/.config/nvim/lua/plugins/git-conflicts.lua b/.config/nvim/lua/plugins/git-conflicts.lua new file mode 100644 index 0000000..3a1cccb --- /dev/null +++ b/.config/nvim/lua/plugins/git-conflicts.lua @@ -0,0 +1,6 @@ +---@type LazySpec +return { + 'akinsho/git-conflict.nvim', + version = '*', + config = true, +} diff --git a/.config/nvim/lua/plugins/mini/files.lua b/.config/nvim/lua/plugins/mini/files.lua index 29798ed..1e52e07 100644 --- a/.config/nvim/lua/plugins/mini/files.lua +++ b/.config/nvim/lua/plugins/mini/files.lua @@ -87,7 +87,7 @@ return { end, }) local events = { - ['lsp-file-operations.did-rename'] = { { 'MiniFilesActionRename', 'MiniFilesActionMove' }, 'Renamed' }, + -- ['lsp-file-operations.did-rename'] = { { 'MiniFilesActionRename', 'MiniFilesActionMove' }, 'Renamed' }, ['lsp-file-operations.will-create'] = { 'MiniFilesActionCreate', 'Create' }, ['lsp-file-operations.will-delete'] = { 'MiniFilesActionDelete', 'Delete' }, } @@ -113,17 +113,16 @@ return { }) end - -- vim.api.nvim_create_autocmd('User', { - -- pattern = 'MiniFilesActionRename', - -- group = au_group, - -- desc = 'LSP Rename file', - -- callback = function(event) - -- local ok, rename = pcall(require, 'lsp-file-operations.did-rename') - -- if not ok then - -- return - -- end - -- rename.callback({ old_name = event.data.from, new_name = event.data.to }) - -- end, - -- }) + vim.api.nvim_create_autocmd('User', { + pattern = 'MiniFilesActionRename', + group = au_group, + desc = 'LSP Rename file', + callback = function(event) + if not Snacks then + return + end + Snacks.rename.on_rename_file(event.data.from, event.data.to) + end, + }) end, } diff --git a/.config/nvim/lua/plugins/mini/pairs.lua b/.config/nvim/lua/plugins/mini/pairs.lua index a8037b0..6621d19 100644 --- a/.config/nvim/lua/plugins/mini/pairs.lua +++ b/.config/nvim/lua/plugins/mini/pairs.lua @@ -2,6 +2,7 @@ return { 'echasnovski/mini.pairs', event = { 'InsertEnter', 'CmdlineEnter' }, + enabled = false, opts = { modes = { insert = true, command = true, terminal = false }, -- skip autopair when next character is one of these diff --git a/.config/nvim/lua/plugins/mini/surround.lua b/.config/nvim/lua/plugins/mini/surround.lua index 80e98a5..50bf03d 100644 --- a/.config/nvim/lua/plugins/mini/surround.lua +++ b/.config/nvim/lua/plugins/mini/surround.lua @@ -2,7 +2,7 @@ return { 'echasnovski/mini.surround', version = '*', - enabled = true, + enabled = false, keys = { { 'gz', '', desc = '+surround' }, { 'ys', mode = { 'n', 'v' }, desc = 'Surround add' }, diff --git a/.config/nvim/lua/plugins/snacks.lua b/.config/nvim/lua/plugins/snacks.lua new file mode 100644 index 0000000..612b6af --- /dev/null +++ b/.config/nvim/lua/plugins/snacks.lua @@ -0,0 +1,48 @@ +---@type LazySpec +return { + 'folke/snacks.nvim', + priority = 1000, + lazy = false, + opts = { + bigfile = { enabled = true }, + notifier = { enabled = true }, + quickfile = { enabled = true }, + statuscolumn = { enabled = false }, + words = { enabled = true }, + }, + + keys = { + { + 'gg', + function() + Snacks.lazygit() + end, + desc = 'Lazygit', + }, + { + 'gL', + function() + Snacks.git.blame_line() + end, + desc = 'Git Blame Line', + }, + { + ']]', + function() + Snacks.words.jump(vim.v.count1) + end, + desc = 'Next Reference', + mode = { 'n', 't' }, + }, + { + '[[', + function() + Snacks.words.jump(-vim.v.count1) + end, + desc = 'Prev Reference', + mode = { 'n', 't' }, + }, + + -- { "lR", function() Snacks.rename.rename_file() end, desc = "Rename File" }, + }, +} diff --git a/.config/nvim/lua/plugins/surround.lua b/.config/nvim/lua/plugins/surround.lua index e3ce04d..b07cbb7 100644 --- a/.config/nvim/lua/plugins/surround.lua +++ b/.config/nvim/lua/plugins/surround.lua @@ -1,7 +1,7 @@ ---@type LazySpec return { 'kylechui/nvim-surround', - enabled = false, + enabled = true, config = true, -- config = function() -- require("nvim-surround").setup() diff --git a/.config/nvim/lua/plugins/tree-sitter.lua b/.config/nvim/lua/plugins/tree-sitter.lua index 1d5011d..cd768b6 100644 --- a/.config/nvim/lua/plugins/tree-sitter.lua +++ b/.config/nvim/lua/plugins/tree-sitter.lua @@ -1,26 +1,52 @@ +---@type LazySpec return { - 'nvim-treesitter/nvim-treesitter', - event = { 'BufReadPre', 'BufNewFile' }, - lazy = vim.fn.argc(-1) == 0, - version = false, -- last release is way too old and doesn't work on Windows - build = ':TSUpdate', - init = function() - require 'nvim-treesitter.query_predicates' - end, - cmd = { 'TSUpdateSync', 'TSUpdate', 'TSInstall' }, - dependencies = { - 'nvim-treesitter/nvim-treesitter-textobjects', - 'andymass/vim-matchup', - 'windwp/nvim-ts-autotag', - { - 'nvim-treesitter/nvim-treesitter-context', - opts = { - mode = 'cursor', - max_lines = 3, + { + 'nvim-treesitter/nvim-treesitter', + event = { 'BufReadPre', 'BufNewFile' }, + lazy = vim.fn.argc(-1) == 0, + version = false, -- last release is way too old and doesn't work on Windows + build = ':TSUpdate', + init = function() + require 'nvim-treesitter.query_predicates' + end, + cmd = { 'TSUpdateSync', 'TSUpdate', 'TSInstall' }, + dependencies = { + 'nvim-treesitter/nvim-treesitter-textobjects', + 'andymass/vim-matchup', + 'windwp/nvim-ts-autotag', + { + 'nvim-treesitter/nvim-treesitter-context', + opts = { + mode = 'cursor', + max_lines = 3, + }, }, }, + config = function() + require 'plugins.config.tree-sitter' + end, + }, + { + 'Wansmer/treesj', + keys = { + { 'm', 'TSJToggle' }, + { + 'M', + function() + require('treesj').toggle({ split = { recursive = true } }) + end, + desc = 'Toggle Treesitter Join(recursive)', + }, + { 'j', 'TSJJoin' }, + { 's', 'TSJSplit' }, + }, + dependencies = { 'nvim-treesitter/nvim-treesitter' }, -- if you install parsers with `nvim-treesitter` + cmd = { 'TSJToggle', 'TSJSplit', 'TSJJoin' }, + opts = { + use_default_keymaps = false, + }, + config = function(_, opts) + require('treesj').setup(opts) + end, }, - config = function() - require 'plugins.config.tree-sitter' - end, } diff --git a/.config/nvim/plugin/lastplace.lua.bkp b/.config/nvim/plugin/lastplace.lua.bkp deleted file mode 100644 index 7a576e4..0000000 --- a/.config/nvim/plugin/lastplace.lua.bkp +++ /dev/null @@ -1,45 +0,0 @@ --- adapted from https://github.com/ethanholz/nvim-lastplace/blob/main/lua/nvim-lastplace/init.lua -local ignore_buftype = { 'quickfix', 'nofile', 'help' } -local ignore_filetype = { 'gitcommit', 'gitrebase', 'svn', 'hgcommit' } - -local function run() - if vim.tbl_contains(ignore_buftype, vim.bo.buftype) then - return - end - - if vim.tbl_contains(ignore_filetype, vim.bo.filetype) then - -- reset cursor to first line - vim.cmd [[normal! gg]] - return - end - - -- If a line has already been specified on the command line, we are done - -- nvim file +num - if vim.fn.line '.' > 1 then - return - end - - local last_line = vim.fn.line [['"]] - local buff_last_line = vim.fn.line '$' - - -- If the last line is set and the less than the last line in the buffer - if last_line > 0 and last_line <= buff_last_line then - local win_last_line = vim.fn.line 'w$' - local win_first_line = vim.fn.line 'w0' - -- Check if the last line of the buffer is the same as the win - if win_last_line == buff_last_line then - -- Set line to last line edited - vim.cmd [[normal! g`"]] - -- Try to center - elseif buff_last_line - last_line > ((win_last_line - win_first_line) / 2) - 1 then - vim.cmd [[normal! g`"zz]] - else - vim.cmd [[normal! G'"]] - end - end -end - -vim.api.nvim_create_autocmd({ 'BufWinEnter', 'FileType' }, { - group = vim.api.nvim_create_augroup('nvim-lastplace', {}), - callback = run, -})