From 3569e4ec8ec81c5d5455061f46a32d23581ed297 Mon Sep 17 00:00:00 2001 From: Fabian Missbrenner Date: Wed, 12 Mar 2025 12:36:38 +0100 Subject: [PATCH] Allow refactoring (plus minor tweaks) --- init.lua | 21 +++++--- lazy-lock.json | 1 + lua/custom/plugins/init.lua | 6 ++- lua/custom/plugins/refactoring.lua | 61 +++++++++++++++++++++++ lua/custom/plugins/treesitter-context.lua | 11 ++-- 5 files changed, 89 insertions(+), 11 deletions(-) create mode 100644 lua/custom/plugins/refactoring.lua diff --git a/init.lua b/init.lua index 5d29a10d..d4757c18 100644 --- a/init.lua +++ b/init.lua @@ -154,7 +154,7 @@ vim.opt.inccommand = 'split' vim.opt.cursorline = true -- Minimal number of screen lines to keep above and below the cursor. --- vim.opt.scrolloff = 10 +vim.opt.scrolloff = 10 -- [[ Basic Keymaps ]] -- See `:help vim.keymap.set()` @@ -384,11 +384,20 @@ require('lazy').setup({ -- You can put your default mappings / updates / etc. in here -- All the info you're looking for is in `:help telescope.setup()` -- - -- defaults = { - -- mappings = { - -- i = { [''] = 'to_fuzzy_refine' }, - -- }, - -- }, + defaults = { + -- mappings = { + -- i = { [''] = 'to_fuzzy_refine' }, + -- }, + layout_strategy = 'vertical', + layout_config = { + vertical = { + width = 0.9, + height = 0.9, + preview_height = 0.6, + preview_cutoff = 0, + }, + }, + }, -- pickers = {} extensions = { ['ui-select'] = { diff --git a/lazy-lock.json b/lazy-lock.json index e6ef4e20..026c8efa 100644 --- a/lazy-lock.json +++ b/lazy-lock.json @@ -22,6 +22,7 @@ "nvim-treesitter-context": { "branch": "master", "commit": "198720b4016af04c9590f375d714d5bf8afecc1a" }, "nvim-web-devicons": { "branch": "master", "commit": "ab4cfee554e501f497bce0856788d43cf2eb93d7" }, "plenary.nvim": { "branch": "master", "commit": "857c5ac632080dba10aae49dba902ce3abf91b35" }, + "refactoring.nvim": { "branch": "master", "commit": "5268cc6add06ba8862492063341a568d18d65fb6" }, "telescope-fzf-native.nvim": { "branch": "main", "commit": "2a5ceff981501cff8f46871d5402cd3378a8ab6a" }, "telescope-ui-select.nvim": { "branch": "master", "commit": "6e51d7da30bd139a6950adf2a47fda6df9fa06d2" }, "telescope.nvim": { "branch": "0.1.x", "commit": "a0bbec21143c7bc5f8bb02e0005fa0b982edc026" }, diff --git a/lua/custom/plugins/init.lua b/lua/custom/plugins/init.lua index c3af7aa7..b6c79977 100644 --- a/lua/custom/plugins/init.lua +++ b/lua/custom/plugins/init.lua @@ -2,8 +2,12 @@ -- I promise not to create any merge conflicts in this directory :) -- -- See the kickstart.nvim README for more information +-- +-- When a file has been detected to have been changed outside of Vim and it has not been changed inside of Vim, automatically read it again. +vim.opt.autoread = true + return { vim.keymap.set('n', 'sm', function() require('telescope.builtin').live_grep { search_dirs = { '/home/ccs/missbrenner/Documents/ns-3/ns3-mmwave-iab/src/mmwave/' } } - end, { desc = '[S]earch [M]mwave files' }), + end, { desc = '[S]earch [M]mwave File Contents' }), } diff --git a/lua/custom/plugins/refactoring.lua b/lua/custom/plugins/refactoring.lua new file mode 100644 index 00000000..a236d7fb --- /dev/null +++ b/lua/custom/plugins/refactoring.lua @@ -0,0 +1,61 @@ +return { + 'ThePrimeagen/refactoring.nvim', + dependencies = { + 'nvim-lua/plenary.nvim', + 'nvim-treesitter/nvim-treesitter', + }, + lazy = false, + config = function() + require('refactoring').setup { + prompt_func_return_type = { + go = false, + java = false, + + cpp = false, + c = false, + h = false, + hpp = false, + cxx = false, + }, + prompt_func_param_type = { + go = false, + java = false, + + cpp = false, + c = false, + h = false, + hpp = false, + cxx = false, + }, + printf_statements = {}, + print_var_statements = {}, + show_success_message = true, -- shows a message with information about the refactor on success + -- i.e. [Refactor] Inlined 3 variable occurrences + } + end, + vim.keymap.set({ 'n', 'x' }, 'cr', function() + return require('refactoring').refactor 'Code Refactor' + end, { expr = true, desc = '[C]ode [R]efactor' }), + vim.keymap.set({ 'n', 'x' }, 'cre', function() + return require('refactoring').refactor 'Extract Function' + end, { expr = true, desc = '[R]efactor [E]xtract Function' }), + vim.keymap.set({ 'n', 'x' }, 'crf', function() + return require('refactoring').refactor 'Extract Function To File' + end, { expr = true, desc = '[R]efactor Extract Function To [F]ile' }), + vim.keymap.set({ 'n', 'x' }, 'crv', function() + return require('refactoring').refactor 'Extract Variable' + end, { expr = true, desc = '[R]efactor Extract [V]ariable' }), + vim.keymap.set({ 'n', 'x' }, 'crI', function() + return require('refactoring').refactor 'Inline Function' + end, { expr = true, desc = '[R]efactor [I]nline Function' }), + vim.keymap.set({ 'n', 'x' }, 'cri', function() + return require('refactoring').refactor 'Inline Variable' + end, { expr = true, desc = '[R]efactor [I]nline Variable' }), + + vim.keymap.set({ 'n', 'x' }, 'crbb', function() + return require('refactoring').refactor 'Extract Block' + end, { expr = true, desc = '[R]efactor Extract [B]lock' }), + vim.keymap.set({ 'n', 'x' }, 'crbf', function() + return require('refactoring').refactor 'Extract Block To File' + end, { expr = true, desc = '[R]efactor Extract [B]lock To [F]ile' }), +} diff --git a/lua/custom/plugins/treesitter-context.lua b/lua/custom/plugins/treesitter-context.lua index 189263e0..d240d3e8 100644 --- a/lua/custom/plugins/treesitter-context.lua +++ b/lua/custom/plugins/treesitter-context.lua @@ -1,15 +1,15 @@ return { - "nvim-treesitter/nvim-treesitter-context", + 'nvim-treesitter/nvim-treesitter-context', config = function() - require'treesitter-context'.setup{ + require('treesitter-context').setup { enable = true, -- Enable this plugin (Can be enabled/disabled later via commands) multiwindow = false, -- Enable multiwindow support. max_lines = 0, -- How many lines the window should span. Values <= 0 mean no limit. min_window_height = 0, -- Minimum editor window height to enable context. Values <= 0 mean no limit. line_numbers = true, multiline_threshold = 20, -- Maximum number of lines to show for a single context - trim_scope = 'outer', -- Which context lines to discard if `max_lines` is exceeded. Choices: 'inner', 'outer' - mode = 'topline', -- Line used to calculate context. Choices: 'cursor', 'topline' + trim_scope = 'inner', -- Which context lines to discard if `max_lines` is exceeded. Choices: 'inner', 'outer' + mode = 'cursor', -- Line used to calculate context. Choices: 'cursor', 'topline' -- Separator between context and content. Should be a single character string, like '-'. -- When separator is set, the context will only show up when there are at least 2 lines above cursorline. separator = nil, @@ -17,4 +17,7 @@ return { on_attach = nil, -- (fun(buf: integer): boolean) return false to disable attaching } end, + vim.keymap.set('n', '[c', function() + require('treesitter-context').go_to_context(vim.v.count1) + end, { silent = true, desc = '[C]ontext (upwards)' }), }