From 3e9747e24347e75a2963e339068121ffc96957ac Mon Sep 17 00:00:00 2001 From: Nurdaulet Kurenshe Date: Sat, 2 Mar 2024 22:09:58 +0600 Subject: [PATCH 1/3] Add file preview --- lua/spectre/actions.lua | 87 +++++++++++++++++++++++++++++++++++++++++ lua/spectre/config.lua | 30 ++++++++------ 2 files changed, 105 insertions(+), 12 deletions(-) diff --git a/lua/spectre/actions.lua b/lua/spectre/actions.lua index 388d7ee..7be3ae9 100644 --- a/lua/spectre/actions.lua +++ b/lua/spectre/actions.lua @@ -140,6 +140,93 @@ M.run_current_replace = function() vim.notify("Not found any entry to replace.") end end +local function readFileSync(path) + local uv = require('luv') + local fd = assert(uv.fs_open(path, "r", 438)) + local stat = assert(uv.fs_fstat(fd)) + local data = assert(uv.fs_read(fd, stat.size, 0)) + assert(uv.fs_close(fd)) + return data +end +local function spliteFile(inputstr, sep) + if sep == nil then + sep = "\n" + end + local t = {} + for str in string.gmatch(inputstr, "(.-)\n") do + table.insert(t, str) + end + return t +end +local function getFileRange(file, entry) + local content = {} + local h_pos; + local cnt = 0; + + for k, v in pairs(file) do + if k >= entry.lnum - 5 and k <= entry.lnum + 5 then + table.insert(content, v) + cnt = cnt + 1 + end + if k == entry.lnum then + h_pos = cnt; + end + end + return content, h_pos +end + +M.show_file_preview = function() + local entry = M.get_current_entry() + if entry then + local data = readFileSync(entry.filename) + local file = spliteFile(data) + + local content, h_pos = getFileRange(file, entry) + local win_width, win_height = vim.lsp.util._make_floating_popup_size(content, {}) + local query_length = string.len(state.query.search_query) + + local bufnr = vim.api.nvim_create_buf(false, true) + + api.nvim_buf_set_option(bufnr, "bufhidden", "wipe") + api.nvim_buf_set_lines(bufnr, 0, -1, true, content) + api.nvim_buf_add_highlight(bufnr, -1, + state.user_config.highlight.search, + h_pos - 1, entry.col - 1, entry.col + query_length - 1) + + local help_win = vim.api.nvim_open_win(bufnr, false, { + style = "minimal", + title = " " .. entry.filename .. " ", + title_pos = 'center', + relative = 'cursor', + width = win_width, + height = win_height, + col = 0, + row = 1, + border = "rounded" + }) + + api.nvim_win_set_option(help_win, 'winblend', 0) + api.nvim_buf_set_keymap(bufnr, 'n', '', 'lua vim.api.nvim_win_close(' .. help_win .. ', true)', + { noremap = true }) + + api.nvim_create_autocmd({ + 'CursorMovedI', + 'CursorMoved', + 'CursorMovedI', + 'BufHidden', + 'BufLeave', + 'InsertEnter', + 'WinScrolled', + 'BufDelete', + }, { + callback = function() + pcall(vim.api.nvim_win_close, help_win, true) + end, + }) + else + vim.notify("Not found any entry.") + end +end local is_running = false diff --git a/lua/spectre/config.lua b/lua/spectre/config.lua index 8011f9f..facce15 100644 --- a/lua/spectre/config.lua +++ b/lua/spectre/config.lua @@ -2,15 +2,15 @@ local api = vim.api ---@class SpectreConfig local config = { - filetype = "spectre_panel", - namespace = api.nvim_create_namespace("SEARCH_PANEL"), - namespace_ui = api.nvim_create_namespace("SEARCH_PANEL_UI"), - namespace_header = api.nvim_create_namespace("SEARCH_PANEL_HEADER"), - namespace_status = api.nvim_create_namespace("SEARCH_PANEL_STATUS"), - namespace_result = api.nvim_create_namespace("SEARCH_PANEL_RESULT"), + filetype = "spectre_panel", + namespace = api.nvim_create_namespace("SEARCH_PANEL"), + namespace_ui = api.nvim_create_namespace("SEARCH_PANEL_UI"), + namespace_header = api.nvim_create_namespace("SEARCH_PANEL_HEADER"), + 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 - line_result = 10, -- line begin result + lnum_UI = 8, -- total line for ui you can edit it + line_result = 10, -- line begin result -- result_padding = '│ ', -- color_devicons = true, @@ -18,9 +18,9 @@ local config = { -- result_padding = '¦ ', -- line_sep = '├──────────────────────────────────────', - line_sep_start = '┌──────────────────────────────────────────────────────', - result_padding = '│ ', - line_sep = '└──────────────────────────────────────────────────────', + line_sep_start = '┌──────────────────────────────────────────────────────', + result_padding = '│ ', + line_sep = '└──────────────────────────────────────────────────────', color_devicons = true, open_cmd = 'vnew', live_update = false, @@ -31,6 +31,7 @@ local config = { filename = "SpectreFile", filedirectory = "SpectreDir", search = "SpectreSearch", + line = "SpectreLine", border = "SpectreBorder", replace = "SpectreReplace" }, @@ -91,6 +92,11 @@ local config = { cmd = "lua require('spectre').toggle_live_update()", desc = "update when vim writes to file" }, + ['run_current_replace'] = { + map = "tp", + cmd = "lua require('spectre.actions').show_file_preview()", + desc = "preview file" + }, -- only work if the find_engine following have that option ['toggle_ignore_case'] = { map = "ti", @@ -200,7 +206,7 @@ local config = { replace_vim_cmd = "cdo", is_open_target_win = true, is_insert_mode = false, - is_block_ui_break = false, + is_block_ui_break = false, open_template = {} } From ebf7bf6c0f20fd344716b2c46f3461d0f2447137 Mon Sep 17 00:00:00 2001 From: Nurdaulet Kurenshe Date: Sat, 2 Mar 2024 22:12:17 +0600 Subject: [PATCH 2/3] Add highlighting to result lnum --- lua/spectre/highlight.lua | 3 ++- lua/spectre/ui.lua | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lua/spectre/highlight.lua b/lua/spectre/highlight.lua index aa0f520..0ad3d2f 100644 --- a/lua/spectre/highlight.lua +++ b/lua/spectre/highlight.lua @@ -5,9 +5,10 @@ M.set_hl = function() vim.api.nvim_set_hl(0, 'SpectreBody', { link = "String" }) vim.api.nvim_set_hl(0, 'SpectreFile', { link = "Keyword" }) vim.api.nvim_set_hl(0, 'SpectreDir', { link = "Comment" }) + vim.api.nvim_set_hl(0, 'SpectreLine', { link = "Comment" }) vim.api.nvim_set_hl(0, 'SpectreSearch', { link = "DiffChange" }) vim.api.nvim_set_hl(0, 'SpectreBorder', { link = "Comment" }) - vim.api.nvim_set_hl(0, 'SpectreReplace', { link = "DiffDelete" }) + vim.api.nvim_set_hl(0, 'SpectreReplace', { link = "DiffAdd" }) end vim.api.nvim_create_autocmd('ColorScheme', { diff --git a/lua/spectre/ui.lua b/lua/spectre/ui.lua index dc668e4..240f7bc 100644 --- a/lua/spectre/ui.lua +++ b/lua/spectre/ui.lua @@ -30,6 +30,9 @@ M.render_line = function(bufnr, namespace, text_opts, view_opts, regex) api.nvim_buf_set_lines(bufnr, text_opts.lnum, end_lnum, false, { view_opts.padding_text .. text_opts.item_line .. " " .. diff.text, }) + api.nvim_buf_add_highlight(bufnr, namespace, + cfg.highlight.line, + text_opts.lnum, view_opts.padding , view_opts.padding + item_line_len) else api.nvim_buf_set_lines(bufnr, text_opts.lnum, end_lnum, false, { view_opts.padding_text .. diff.text, From f643c9a06efa3fc79b847fd1a890d2634e32f97b Mon Sep 17 00:00:00 2001 From: Nurdaulet Kurenshe Date: Sat, 2 Mar 2024 22:35:50 +0600 Subject: [PATCH 3/3] Try to fix tests --- tests/minimal.vim | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/minimal.vim b/tests/minimal.vim index 048c978..af2659c 100644 --- a/tests/minimal.vim +++ b/tests/minimal.vim @@ -11,6 +11,9 @@ set nobackup lua << EOF --_G.__is_log = true require('spectre.init') +require('spectre').setup({ + lnum_for_results = false +}) require('tests/helper').init() EOF