From 17da210bc3452cc422c180c529bd36e6b069e134 Mon Sep 17 00:00:00 2001 From: Std-Enigma Date: Sat, 22 Jun 2024 19:16:16 +0330 Subject: [PATCH 1/4] feat: added trouble.nvim integration --- lua/spectre/actions.lua | 8 +++++++- lua/spectre/config.lua | 1 + 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lua/spectre/actions.lua b/lua/spectre/actions.lua index d6b141a..dc12ab1 100644 --- a/lua/spectre/actions.lua +++ b/lua/spectre/actions.lua @@ -93,11 +93,17 @@ end M.send_to_qf = function() local entries = M.get_all_entries() - vim.cmd([[copen]]) vim.fn.setqflist(entries, 'r') vim.fn.setqflist({}, 'r', { title = string.format('Result Search: [%s]', state.query.search_query), }) + local trouble_avail, _ = pcall(require, "trouble") + local status = trouble_avail and state.user_config.use_trouble_qf + if status then + vim.cmd([[Trouble quickfix win.relative=win focus=true]]) + else + vim.cmd([[copen]]) + end return entries end diff --git a/lua/spectre/config.lua b/lua/spectre/config.lua index 8593bf5..36fd2c9 100644 --- a/lua/spectre/config.lua +++ b/lua/spectre/config.lua @@ -216,6 +216,7 @@ local config = { }, }, replace_vim_cmd = 'cdo', + use_trouble_qf = false, is_open_target_win = true, is_insert_mode = false, is_block_ui_break = false, From b80deea8857e74a9843f4d8af59b042aa94edde9 Mon Sep 17 00:00:00 2001 From: Std-Enigma Date: Tue, 25 Jun 2024 13:18:11 +0330 Subject: [PATCH 2/4] docs: updated README add more information about trouble.nvim integration --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 409f539..191c119 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ You may also need to install the following: - [BurntSushi/ripgrep](https://github.com/BurntSushi/ripgrep) (finder) - [devicons](https://github.com/kyazdani42/nvim-web-devicons) (icons) - [sed](https://www.gnu.org/software/sed/) (replace tool) +- [trouble.nvim](https://github.com/folke/trouble.nvim) (improved quickfix list) ### MacOs @@ -282,6 +283,7 @@ require('spectre').setup({ } }, replace_vim_cmd = "cdo", + use_trouble_qf = false, -- use trouble.nvim as quickfix list is_open_target_win = true, --open file on opener window is_insert_mode = false, -- start open panel on is_insert_mode is_block_ui_break = false -- mapping backspace and enter key to avoid ui break From 0bc41bfe955080dd0105bcdda487dcba698f5ee7 Mon Sep 17 00:00:00 2001 From: Std-Enigma Date: Tue, 25 Jun 2024 15:14:05 +0330 Subject: [PATCH 3/4] format: config.lua --- lua/spectre/config.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/spectre/config.lua b/lua/spectre/config.lua index 36fd2c9..9e54776 100644 --- a/lua/spectre/config.lua +++ b/lua/spectre/config.lua @@ -9,7 +9,7 @@ local config = { namespace_status = api.nvim_create_namespace('SEARCH_PANEL_STATUS'), namespace_result = api.nvim_create_namespace('SEARCH_PANEL_RESULT'), - lnum_UI = 8, -- total line for ui you can edit it + lnum_UI = 8, -- total line for ui you can edit it line_result = 10, -- line begin result -- result_padding = '│ ', @@ -126,7 +126,7 @@ local config = { map = 'rd', cmd = "lua require('spectre.actions').run_delete_line()", desc = 'delete line', - } + }, }, find_engine = { ['rg'] = { From 9770e20402d4887e21e24bb07b19eb21781db70f Mon Sep 17 00:00:00 2001 From: Std-Enigma Date: Tue, 25 Jun 2024 16:06:23 +0330 Subject: [PATCH 4/4] format: actions.lua --- lua/spectre/actions.lua | 85 ++++++++++++++++++----------------------- 1 file changed, 38 insertions(+), 47 deletions(-) diff --git a/lua/spectre/actions.lua b/lua/spectre/actions.lua index dc12ab1..ee87ca9 100644 --- a/lua/spectre/actions.lua +++ b/lua/spectre/actions.lua @@ -97,7 +97,7 @@ M.send_to_qf = function() vim.fn.setqflist({}, 'r', { title = string.format('Result Search: [%s]', state.query.search_query), }) - local trouble_avail, _ = pcall(require, "trouble") + local trouble_avail, _ = pcall(require, 'trouble') local status = trouble_avail and state.user_config.use_trouble_qf if status then vim.cmd([[Trouble quickfix win.relative=win focus=true]]) @@ -229,61 +229,53 @@ M.run_delete_line = function(entries) local error_item = 0 state.status_line = 'Run Replace.' local replacer_creator = state_utils.get_replace_creator() - local replacer = replacer_creator:new(state_utils.get_replace_engine_config(), - { - on_done = function(result) - if result.ref then - done_item = done_item + 1 - local value = result.ref - state.status_line = 'Delete line: ' .. done_item .. ' Error:' .. error_item - for _, display_lnum in ipairs(value.display_lnums) do - M.set_entry_finish(display_lnum) - api.nvim_buf_set_extmark( - state.bufnr, - config.namespace, - display_lnum, - 0, - { virt_text = { { '󰄲 DONE', 'String' } }, virt_text_pos = 'eol' } - ) - end + local replacer = replacer_creator:new(state_utils.get_replace_engine_config(), { + on_done = function(result) + if result.ref then + done_item = done_item + 1 + local value = result.ref + state.status_line = 'Delete line: ' .. done_item .. ' Error:' .. error_item + for _, display_lnum in ipairs(value.display_lnums) do + M.set_entry_finish(display_lnum) + api.nvim_buf_set_extmark( + state.bufnr, + config.namespace, + display_lnum, + 0, + { virt_text = { { '󰄲 DONE', 'String' } }, virt_text_pos = 'eol' } + ) end - end, - on_error = function(result) - if result.ref then - error_item = error_item + 1 - local value = result.ref - state.status_line = 'Delete line: ' .. done_item .. ' Error:' .. error_item - for _, display_lnum in ipairs(value.display_lnums) do - M.set_entry_finish(display_lnum) - api.nvim_buf_set_extmark( - state.bufnr, - config.namespace, - display_lnum, - 0, - { virt_text = { { '󰄱 ERROR', 'Error' } }, virt_text_pos = 'eol' } - ) - end + end + end, + on_error = function(result) + if result.ref then + error_item = error_item + 1 + local value = result.ref + state.status_line = 'Delete line: ' .. done_item .. ' Error:' .. error_item + for _, display_lnum in ipairs(value.display_lnums) do + M.set_entry_finish(display_lnum) + api.nvim_buf_set_extmark( + state.bufnr, + config.namespace, + display_lnum, + 0, + { virt_text = { { '󰄱 ERROR', 'Error' } }, virt_text_pos = 'eol' } + ) end - end, - } - ) + end + end, + }) local groupby_filename = {} for _, value in pairs(entries) do if not groupby_filename[value.filename] then groupby_filename[value.filename] = { filename = value.filename, lnums = { value.lnum }, - display_lnums = { value.display_lnum } + display_lnums = { value.display_lnum }, } else - table.insert( - groupby_filename[value.filename].lnums, - value.lnum - ) - table.insert( - groupby_filename[value.filename].display_lnums, - value.display_lnum - ) + table.insert(groupby_filename[value.filename].lnums, value.lnum) + table.insert(groupby_filename[value.filename].display_lnums, value.display_lnum) end end @@ -297,7 +289,6 @@ M.run_delete_line = function(entries) end end - M.select_template = function() if not state.user_config.open_template or #state.user_config.open_template == 0 then vim.notify('You need to set open_template on setup function.')