From 11829946e6cf8a24c753c27d4fb0db0d5c46a9aa Mon Sep 17 00:00:00 2001 From: Tovi Jaeschke-Rogers Date: Tue, 24 Jun 2025 11:59:20 +0930 Subject: [PATCH] feat: add refactor plugin --- .config/nvim/lua/core/autocmd.lua | 58 +++++++++++++++++++++----- .config/nvim/lua/core/remaps.lua | 20 +++++++++ .config/nvim/lua/plugins/hardtime.lua | 1 + .config/nvim/lua/plugins/refactor.lua | 9 ++++ .config/nvim/lua/plugins/telescope.lua | 34 --------------- 5 files changed, 77 insertions(+), 45 deletions(-) create mode 100644 .config/nvim/lua/plugins/refactor.lua diff --git a/.config/nvim/lua/core/autocmd.lua b/.config/nvim/lua/core/autocmd.lua index f0fbbea..6e36ece 100644 --- a/.config/nvim/lua/core/autocmd.lua +++ b/.config/nvim/lua/core/autocmd.lua @@ -1,18 +1,20 @@ +vim.api.nvim_set_hl(0, "YankColor", { fg = "#C8C093", bg = "#000000", ctermfg = 59, ctermbg = 41 }) + local aucmd_dict = { VimEnter = { { callback = function() local function find_project_root() - local current_dir = vim.fn.getcwd() + local current_dir = vim.fn.getcwd() - while current_dir ~= "/" do - if vim.fn.isdirectory(current_dir .. "/.git") == 1 or vim.fn.filereadable(current_dir .. "/.nvim.lua") then - return current_dir + while current_dir ~= "/" do + if vim.fn.isdirectory(current_dir .. "/.git") == 1 or vim.fn.filereadable(current_dir .. "/.nvim.lua") then + return current_dir + end + current_dir = vim.fn.fnamemodify(current_dir, ":h") end - current_dir = vim.fn.fnamemodify(current_dir, ":h") - end - return vim.fn.getcwd() + return vim.fn.getcwd() end local project_root = find_project_root() @@ -21,14 +23,14 @@ local aucmd_dict = { if vim.fn.filereadable(project_specific_conf_file) == 1 then vim.cmd("source " .. project_specific_conf_file) print("Sourced project specific config file: " .. project_specific_conf_file) - end + end end } }, FileType = { { - -- Set tabstop to 2 for Dart, Vue, JavaScript, TypeScript, and JSON files + -- Set tabstop to 2 for Dart, Vue, JavaScript, TypeScript, and JSON files pattern = "html,dart,vue,javascript,typescript,typescriptreact,json,markdown,css,sass", callback = function() vim.opt_local.tabstop = 2 @@ -38,7 +40,7 @@ local aucmd_dict = { }, { pattern = 'dart', - callback = function () + callback = function() vim.bo.commentstring = '// %s' end }, @@ -61,6 +63,27 @@ local aucmd_dict = { }, }, + BufReadPost = { + { + callback = function(event) + local exclude = { 'gitcommit', 'commit', 'gitrebase' } + local buf = event.buf + if + vim.tbl_contains(exclude, vim.bo[buf].filetype) + or vim.b[buf].lazyvim_last_loc + then + return + end + vim.b[buf].lazyvim_last_loc = true + local mark = vim.api.nvim_buf_get_mark(buf, '"') + local lcount = vim.api.nvim_buf_line_count(buf) + if mark[1] > 0 and mark[1] <= lcount then + pcall(vim.api.nvim_win_set_cursor, 0, mark) + end + end, + }, + }, + BufNewFile = { { -- Set syntax for Dockerfiles @@ -70,6 +93,20 @@ local aucmd_dict = { end }, }, + + TextYankPost = { + { + callback = function() + vim.hl.on_yank { higroup = "YankColor", timeout = 250 } + end + }, + }, + + VimResized = { + { + command="wincmd =" + }, + }, } for event, opt_tbls in pairs(aucmd_dict) do @@ -77,4 +114,3 @@ for event, opt_tbls in pairs(aucmd_dict) do vim.api.nvim_create_autocmd(event, opt_tbl) end end - diff --git a/.config/nvim/lua/core/remaps.lua b/.config/nvim/lua/core/remaps.lua index 6410bb4..6e558bf 100644 --- a/.config/nvim/lua/core/remaps.lua +++ b/.config/nvim/lua/core/remaps.lua @@ -62,3 +62,23 @@ vim.keymap.set("t", "", "", { silent = true }) vim.keymap.set("n", "e", function() vim.cmd("edit notes.ignore.md") end) vim.keymap.set("n", ".a", function() vim.cmd("edit app/api/.env") end) vim.keymap.set("n", ".f", function() vim.cmd("edit app/frontend/.env.development.local") end) + +-- Delete a buffer, without closing the window, see https://stackoverflow.com/q/4465095/6064933 +vim.keymap.set("n", [[\d]], "bprevious bdelete #", { + silent = true, + desc = "delete current buffer", +}) + +vim.keymap.set("n", [[\D]], function() + local buf_ids = vim.api.nvim_list_bufs() + local cur_buf = vim.api.nvim_win_get_buf(0) + + for _, buf_id in pairs(buf_ids) do + -- do not Delete unlisted buffers, which may lead to unexpected errors + if vim.api.nvim_get_option_value("buflisted", { buf = buf_id }) and buf_id ~= cur_buf then + vim.api.nvim_buf_delete(buf_id, { force = true }) + end + end +end, { + desc = "delete other buffers", +}) diff --git a/.config/nvim/lua/plugins/hardtime.lua b/.config/nvim/lua/plugins/hardtime.lua index 24082e0..14b8e4c 100644 --- a/.config/nvim/lua/plugins/hardtime.lua +++ b/.config/nvim/lua/plugins/hardtime.lua @@ -1,5 +1,6 @@ return { "m4xshen/hardtime.nvim", + enabled = false, lazy = false, dependencies = { "MunifTanjim/nui.nvim", diff --git a/.config/nvim/lua/plugins/refactor.lua b/.config/nvim/lua/plugins/refactor.lua new file mode 100644 index 0000000..3f09d47 --- /dev/null +++ b/.config/nvim/lua/plugins/refactor.lua @@ -0,0 +1,9 @@ +return { + "ThePrimeagen/refactoring.nvim", + dependencies = { + "nvim-lua/plenary.nvim", + "nvim-treesitter/nvim-treesitter", + }, + lazy = false, + opts = {}, +} diff --git a/.config/nvim/lua/plugins/telescope.lua b/.config/nvim/lua/plugins/telescope.lua index ed14135..43cfa4c 100644 --- a/.config/nvim/lua/plugins/telescope.lua +++ b/.config/nvim/lua/plugins/telescope.lua @@ -169,39 +169,5 @@ return { end) vim.keymap.set("n", "u", "Telescope undo") - - keymap.set("n", "/", function() - builtin.current_buffer_fuzzy_find({ - attach_mappings = function(prompt_bufnr, map) - -- Override the default select action - map('i', '', function() - local selection = action_state.get_selected_entry() - local prompt = action_state.get_current_line() - - actions.close(prompt_bufnr) - - -- Set the search register to the prompt text - if prompt and prompt ~= "" then - vim.fn.setreg('/', prompt) - end - - -- Jump to the selected result with proper error handling - if selection then - -- Try different possible field names for line/column - local line = selection.lnum or selection.line_number or selection.row or 1 - local col = selection.col or selection.column or selection.start_col or 0 - - -- Ensure we have valid numbers - if type(line) == "number" and type(col) == "number" then - -- Vim uses 1-based indexing for lines, 0-based for columns - vim.api.nvim_win_set_cursor(0, { line, col }) - end - end - end) - - return true - end, - }) - end, {}) end, }