Skip to content

Instantly share code, notes, and snippets.

@alanwsmith
Created June 11, 2021 21:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alanwsmith/1e2c5d1fd95e55229e059fc3dcc5eed9 to your computer and use it in GitHub Desktop.
Save alanwsmith/1e2c5d1fd95e55229e059fc3dcc5eed9 to your computer and use it in GitHub Desktop.
Is this a safe way to preview files in Neovim with a Lua plugin? Is there a better way?
local search_buffer, search_window
local document_buffer, document_window
local storage_dir = "/Users/alans/Desktop/neovim-test-files/"
local function open_file()
local file_name = vim.api.nvim_buf_get_lines(search_buffer, 0, 1, true)
-- I do scrubbing to make sure the file is valid but
-- have removed it to focus on the preview part of the example
local file_path = storage_dir..'/'..file_name[1]
vim.api.nvim_set_current_win(document_window)
vim.api.nvim_command('edit ' .. file_path)
vim.api.nvim_set_current_win(search_window)
end
local function search_for_file()
search_buffer = vim.api.nvim_create_buf(false, true)
search_window = vim.api.nvim_open_win(search_buffer, true,
{style = "minimal",relative='win', row=20, col=4, width=42, height=1}
)
vim.api.nvim_command('au CursorMoved,CursorMovedI <buffer> lua require"preview_file".open_file()')
document_buffer = vim.api.nvim_create_buf(false, true)
document_window = vim.api.nvim_open_win(document_buffer, true,
{style = "minimal",relative='win', row=23, col=4, width=42, height=5}
)
vim.api.nvim_set_current_win(search_window)
end
return {
open_file = open_file,
search_for_file = search_for_file
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment