Skip to content

Commit

Permalink
fix: make sure buffer options don't carry over between Hexer and editor
Browse files Browse the repository at this point in the history
  • Loading branch information
theKnightsOfRohan committed Jan 29, 2025
1 parent 168b844 commit 03d1719
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 15 deletions.
47 changes: 34 additions & 13 deletions lua/hexer/display.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ local M = {
width = 420,
height = 69,
},
buf_options = {
buftype = "nofile",
buflisted = false,
modifiable = false,
}
}),
_table = Table({
bufnr = 0,
Expand All @@ -36,7 +41,8 @@ local M = {
---@type HexerItem
data = Parser.parse_input("69"),
}),
_buf_opts = {
_saved_buf = 0,
_saved_buf_opts = {
["buftype"] = "",
["modifiable"] = false,
["readonly"] = false,
Expand All @@ -45,11 +51,10 @@ local M = {

function M.hide_window(self)
self._window:hide()
self:_apply_buf_opts(0)
self:_apply_buf_opts()
end

function M._show_window(self)
self._window:mount()
self._window:show()
self._window:on(Event.BufLeave, function()
self:hide_window()
Expand All @@ -60,10 +65,19 @@ function M._show_window(self)

self._table.bufnr = self._window.bufnr

self:_smodify(function()
vim.api.nvim_buf_set_lines(self._window.bufnr, 0, -1, false, {})
M._table:render()
end)
end

---@param fun function
function M._smodify(self, fun)
vim.api.nvim_set_option_value('modifiable', true, { buf = self._window.bufnr })
vim.api.nvim_buf_set_lines(self._window.bufnr, 0, -1, false, {})
M._table:render()
vim.api.nvim_set_option_value('readonly', false, { buf = self._window.bufnr })
fun()
vim.api.nvim_set_option_value('modifiable', false, { buf = self._window.bufnr })
vim.api.nvim_set_option_value('readonly', true, { buf = self._window.bufnr })
end

---Update the table with the new arg
Expand Down Expand Up @@ -102,9 +116,9 @@ function M._fit_table(self)
end

function M.hexer_open(self, arg)
self:_save_buf_opts(0)
self:_save_buf_opts()

if arg ~= nil then
if arg ~= nil and arg ~= "" then
self:_update_table(arg)
end

Expand All @@ -113,16 +127,23 @@ function M.hexer_open(self, arg)
end

-- BUG: Why do we need these in order to prevent buf options from carrying over?
function M._save_buf_opts(self, buf)
for opt, _ in pairs(self._buf_opts) do
self._buf_opts[opt] = vim.api.nvim_get_option_value(opt, { buf = buf })
function M._save_buf_opts(self)
self._saved_buf = vim.fn.bufnr()
for opt, _ in pairs(self._saved_buf_opts) do
self._saved_buf_opts[opt] = vim.api.nvim_get_option_value(opt, { buf = self._saved_buf })
end
end

function M._apply_buf_opts(self, buf)
for opt, val in pairs(self._buf_opts) do
vim.api.nvim_set_option_value(opt, val, { buf = buf })
function M._apply_buf_opts(self)
for opt, val in pairs(self._saved_buf_opts) do
vim.api.nvim_set_option_value(opt, val, { buf = self._saved_buf })
end
end

function M._setup_window(self)
self._window:mount()
self._window:hide()
vim.api.nvim_set_option_value("buflisted", false, { buf = self._window.bufnr })
end

return M
6 changes: 4 additions & 2 deletions lua/hexer/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ local M = {
_display = require("hexer.display"),
}

M.hexer_open = M._display.hexer_open
M.hexer_close = M._display.hide_window
M.hexer_open = function(self, arg) self._display:hexer_open(arg) end
M.hexer_close = function(self) self._display:hide_window() end

function M.setup()
M._display:_setup_window()

vim.api.nvim_create_user_command("Hexer", function(opts)
M._display:hexer_open(opts.args)
end, { nargs = "?" })
Expand Down

0 comments on commit 03d1719

Please sign in to comment.