wip: Add some stuff for nvim_file

This commit is contained in:
TJ DeVries
2020-09-24 23:23:03 -04:00
parent 145777396b
commit a38bb06e7d
3 changed files with 70 additions and 6 deletions

View File

@@ -235,9 +235,7 @@ function Picker:clear_extra_rows(results_bufnr)
end end
local empty_lines = utils.repeated_table(worst_line, "") local empty_lines = utils.repeated_table(worst_line, "")
vim.api.nvim_buf_set_lines(results_bufnr, 0, worst_line, false, empty_lines) pcall(vim.api.nvim_buf_set_lines, results_bufnr, 0, worst_line, false, empty_lines)
log.trace("Worst Line after process_complete: %s", worst_line, results_bufnr)
end end
end end

View File

@@ -238,7 +238,11 @@ end
previewers.vim_buffer = defaulter(function(_) previewers.vim_buffer = defaulter(function(_)
return previewers.new { return previewers.new {
setup = function() return { last_set_bufnr = nil } end, setup = function()
return {
last_set_bufnr = nil
}
end,
teardown = function(self) teardown = function(self)
if self.state and self.state.last_set_bufnr then if self.state and self.state.last_set_bufnr then
@@ -250,6 +254,10 @@ previewers.vim_buffer = defaulter(function(_)
-- TODO: Consider using path here? Might not work otherwise. -- TODO: Consider using path here? Might not work otherwise.
local filename = entry.filename local filename = entry.filename
if filename == nil then
filename = entry.path
end
if filename == nil then if filename == nil then
local value = entry.value local value = entry.value
filename = vim.split(value, ":")[1] filename = vim.split(value, ":")[1]
@@ -259,13 +267,15 @@ previewers.vim_buffer = defaulter(function(_)
return return
end end
log.trace("Previewing File: %s", filename) log.info("Previewing File:", filename)
local bufnr = vim.fn.bufnr(filename) local bufnr = vim.fn.bufnr(filename)
if bufnr == -1 then if bufnr == -1 then
-- TODO: Is this the best way to load the buffer?... I'm not sure tbh -- TODO: Is this the best way to load the buffer?... I'm not sure tbh
bufnr = vim.fn.bufadd(bufnr) bufnr = vim.fn.bufadd(filename)
vim.fn.bufload(bufnr) vim.fn.bufload(bufnr)
vim.cmd([[doautocmd filetypedetect BufRead ]] .. filename)
end end
self.state.last_set_bufnr = bufnr self.state.last_set_bufnr = bufnr
@@ -285,6 +295,8 @@ previewers.vim_buffer = defaulter(function(_)
vim.api.nvim_win_set_cursor(status.preview_win, {entry.lnum, 0}) vim.api.nvim_win_set_cursor(status.preview_win, {entry.lnum, 0})
-- print("LNUM:", entry.lnum) -- print("LNUM:", entry.lnum)
end end
log.info("Previewed bufnr", bufnr)
end, end,
} }
end, {}) end, {})
@@ -423,6 +435,58 @@ previewers.vim_buffer_or_bat = defaulter(function(_)
} }
end, {}) end, {})
previewers.nvim_file = defaulter(function(_)
return previewers.new {
preview_fn = function(_, entry, status)
local filename = entry.filename
if filename == nil then
filename = entry.path
end
-- if filename == nil then
-- local value = entry.value
-- filename = vim.split(value, ":")[1]
-- end
if filename == nil then
log.info("Could not find file from entry", entry)
return
end
local win_id = status.preview_win
local bufnr = vim.fn.bufnr(filename)
if bufnr == -1 then
bufnr = vim.api.nvim_create_buf(false, true)
-- vim.api.nvim_buf_set_name(bufnr, filename)
vim.api.nvim_win_set_buf(win_id, bufnr)
vim.api.nvim_win_set_option(status.preview_win, 'wrap', false)
if false then
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, vim.fn.readfile(filename))
vim.api.nvim_buf_set_option(bufnr, 'filetype', 'lua')
else
vim.api.nvim_buf_call(bufnr, function()
vim.cmd(":noauto view " .. filename)
vim.api.nvim_command("doautocmd filetypedetect BufRead " .. vim.fn.fnameescape(filename))
end)
end
vim.api.nvim_command("doautocmd filetypedetect BufRead " .. vim.fn.fnameescape(filename))
-- print("FT:", vim.api.nvim_buf_get_option(bufnr, 'filetype'))
else
vim.api.nvim_win_set_buf(win_id, bufnr)
vim.api.nvim_win_set_option(status.preview_win, 'wrap', false)
vim.api.nvim_win_set_option(status.preview_win, 'winhl', 'Normal:Normal')
end
-- vim.api.nvim_buf_set_option(bufnr, 'filetype', 'lua')
-- vim.cmd([[doautocmd filetypedetect BufRead ]] .. vim.fn.fnameescape(filename))
end,
}
end)
previewers.Previewer = Previewer previewers.Previewer = Previewer
return previewers return previewers

View File

@@ -139,6 +139,8 @@ end)()
-- x() -- x()
-- x.new { example = 3 }() -- x.new { example = 3 }()
function utils.make_default_callable(f, default_opts) function utils.make_default_callable(f, default_opts)
default_opts = default_opts or {}
return setmetatable({ return setmetatable({
new = function(opts) new = function(opts)
opts = vim.tbl_extend("keep", opts, default_opts) opts = vim.tbl_extend("keep", opts, default_opts)