fix: preview for unnamed buffers (#434)

This commit is contained in:
Simon Hauser
2021-01-15 16:42:26 +01:00
committed by GitHub
parent e08a5b1331
commit 00e898a1f9
2 changed files with 24 additions and 15 deletions

View File

@@ -20,7 +20,8 @@ previewers.file_maker = function(filepath, bufnr, opts)
local ft = opts.use_ft_detect and pfiletype.detect(filepath) local ft = opts.use_ft_detect and pfiletype.detect(filepath)
if opts.bufname ~= filepath then if opts.bufname ~= filepath then
path.read_file_async(filepath, vim.schedule_wrap(function(data) path.read_file_async(vim.fn.expand(filepath), vim.schedule_wrap(function(data)
if not vim.api.nvim_buf_is_valid(bufnr) then return end
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, vim.split(data, '[\r]?\n')) vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, vim.split(data, '[\r]?\n'))
if opts.callback then opts.callback(bufnr) end if opts.callback then opts.callback(bufnr) end
@@ -165,6 +166,16 @@ previewers.cat = defaulter(function(_)
end, {}) end, {})
previewers.vimgrep = defaulter(function(_) previewers.vimgrep = defaulter(function(_)
local jump_to_line = function(self, bufnr, lnum)
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_win_set_cursor, self.state.winid, {lnum, 0})
vim.api.nvim_buf_call(bufnr, function() vim.cmd"norm! zz" end)
end
self.state.last_set_bufnr = bufnr
end
return previewers.new_buffer_previewer { return previewers.new_buffer_previewer {
setup = function() setup = function()
return { return {
@@ -183,7 +194,6 @@ previewers.vimgrep = defaulter(function(_)
end, end,
define_preview = function(self, entry, status) define_preview = function(self, entry, status)
local lnum = entry.lnum or 0
local p = from_entry.path(entry, true) local p = from_entry.path(entry, true)
if p == nil or p == '' then return end if p == nil or p == '' then return end
@@ -191,18 +201,17 @@ previewers.vimgrep = defaulter(function(_)
pcall(vim.api.nvim_buf_clear_namespace, self.state.last_set_bufnr, ns_previewer, 0, -1) pcall(vim.api.nvim_buf_clear_namespace, self.state.last_set_bufnr, ns_previewer, 0, -1)
end end
conf.buffer_previewer_maker(p, self.state.bufnr, { -- Workaround for unnamed buffer when using builtin.buffer
bufname = self.state.bufname, if p == '[No Name]' and entry.bufnr then
callback = function(bufnr) local lines = vim.api.nvim_buf_get_lines(entry.bufnr, 0, -1, false)
if lnum ~= 0 then vim.api.nvim_buf_set_lines(self.state.bufnr, 0, -1, false, lines)
pcall(vim.api.nvim_buf_add_highlight, bufnr, ns_previewer, "TelescopePreviewLine", lnum - 1, 0, -1) jump_to_line(self, self.state.bufnr, entry.lnum)
pcall(vim.api.nvim_win_set_cursor, self.state.winid, {lnum, 0}) else
vim.api.nvim_buf_call(bufnr, function() vim.cmd"norm! zz" end) conf.buffer_previewer_maker(p, self.state.bufnr, {
end bufname = self.state.bufname,
callback = function(bufnr) jump_to_line(self, bufnr, entry.lnum) end
self.state.last_set_bufnr = bufnr })
end end
})
end end
} }
end, {}) end, {})

View File

@@ -259,7 +259,7 @@ previewers.vimgrep = defaulter(function(opts)
local height = vim.api.nvim_win_get_height(win_id) local height = vim.api.nvim_win_get_height(win_id)
local p = from_entry.path(entry, true) local p = from_entry.path(entry, true)
if p == nil or p == '' then return end if p == nil or p == '' or p == '[No Name]' then return end
local lnum = entry.lnum or 0 local lnum = entry.lnum or 0
local context = math.floor(height / 2) local context = math.floor(height / 2)