refactor: remove redundant state in preview (#1761)

This commit is contained in:
Laura
2022-03-10 16:34:02 +01:00
committed by GitHub
parent 2032f4316f
commit a7517b4b2d

View File

@@ -434,6 +434,7 @@ previewers.vimgrep = defaulter(function(opts)
local cwd = opts.cwd or vim.loop.cwd() local cwd = opts.cwd or vim.loop.cwd()
local jump_to_line = function(self, bufnr, lnum) local jump_to_line = function(self, bufnr, lnum)
pcall(vim.api.nvim_buf_clear_namespace, bufnr, ns_previewer, 0, -1)
if lnum and lnum > 0 then if lnum and lnum > 0 then
pcall(vim.api.nvim_buf_add_highlight, bufnr, ns_previewer, "TelescopePreviewLine", lnum - 1, 0, -1) pcall(vim.api.nvim_buf_add_highlight, bufnr, ns_previewer, "TelescopePreviewLine", lnum - 1, 0, -1)
pcall(vim.api.nvim_win_set_cursor, self.state.winid, { lnum, 0 }) pcall(vim.api.nvim_win_set_cursor, self.state.winid, { lnum, 0 })
@@ -441,8 +442,6 @@ previewers.vimgrep = defaulter(function(opts)
vim.cmd "norm! zz" vim.cmd "norm! zz"
end) end)
end end
self.state.last_set_bufnr = bufnr
end end
return previewers.new_buffer_previewer { return previewers.new_buffer_previewer {
@@ -451,16 +450,6 @@ previewers.vimgrep = defaulter(function(opts)
return Path:new(from_entry.path(entry, true)):normalize(cwd) return Path:new(from_entry.path(entry, true)):normalize(cwd)
end, end,
setup = function()
return { last_set_bufnr = nil }
end,
teardown = function(self)
if self.state and self.state.last_set_bufnr and vim.api.nvim_buf_is_valid(self.state.last_set_bufnr) then
vim.api.nvim_buf_clear_namespace(self.state.last_set_bufnr, ns_previewer, 0, -1)
end
end,
get_buffer_by_name = function(_, entry) get_buffer_by_name = function(_, entry)
return from_entry.path(entry, true) return from_entry.path(entry, true)
end, end,
@@ -471,10 +460,6 @@ previewers.vimgrep = defaulter(function(opts)
return return
end end
if self.state.last_set_bufnr then
pcall(vim.api.nvim_buf_clear_namespace, self.state.last_set_bufnr, ns_previewer, 0, -1)
end
-- Workaround for unnamed buffer when using builtin.buffer -- Workaround for unnamed buffer when using builtin.buffer
if entry.bufnr and (p == "[No Name]" or vim.api.nvim_buf_get_option(entry.bufnr, "buftype") ~= "") then if entry.bufnr and (p == "[No Name]" or vim.api.nvim_buf_get_option(entry.bufnr, "buftype") ~= "") then
local lines = vim.api.nvim_buf_get_lines(entry.bufnr, 0, -1, false) local lines = vim.api.nvim_buf_get_lines(entry.bufnr, 0, -1, false)