Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show context. fix #110 #210

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions lua/spectre/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,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', '<Esc>', '<CMD>lua vim.api.nvim_win_close(' .. help_win .. ', true)<CR>',
{ 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

Expand Down
10 changes: 8 additions & 2 deletions lua/spectre/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ 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'),

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,
Expand Down Expand Up @@ -91,6 +92,11 @@ local config = {
cmd = "<cmd>lua require('spectre').toggle_live_update()<CR>",
desc = 'update when vim writes to file',
},
['run_current_replace'] = {
map = "tp",
cmd = "<cmd>lua require('spectre.actions').show_file_preview()<CR>",
desc = "preview file"
},
-- only work if the find_engine following have that option
['toggle_ignore_case'] = {
map = 'ti',
Expand Down
3 changes: 3 additions & 0 deletions lua/spectre/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,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,
Expand Down
3 changes: 3 additions & 0 deletions tests/minimal.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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

Loading