Skip to content

Commit

Permalink
feat(toc): auto adjust toc vsplit width upon creation
Browse files Browse the repository at this point in the history
  • Loading branch information
champignoom committed Nov 27, 2023
1 parent 3f531c3 commit 2a26745
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lua/neorg/modules/core/qol/toc/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,16 @@ module.public = {
end,
}

local function get_max_virtcol()
local n_line = vim.fn.line('$')
local result = 1
for i = 1, n_line do
-- FIXME: for neovim <=0.9.*, virtcol() doesn't accept winid argument
result = math.max(result, vim.fn.virtcol({i, '$'}))
end
return result
end

module.on_event = function(event)
if event.split_type[2] ~= module.name then
return
Expand Down Expand Up @@ -259,6 +269,11 @@ module.on_event = function(event)
vim.api.nvim_win_set_option(window, "conceallevel", 0)
module.public.update_toc(namespace, toc_title, event.buffer, event.window, buffer, window)

local max_virtcol_1bex = get_max_virtcol()
local current_winwidth = vim.fn.winwidth(window)
local new_winwidth = math.min(current_winwidth, math.max(30, max_virtcol_1bex-1))
vim.cmd(("vertical resize %d"):format(new_winwidth + 1)) -- +1 for margin

local close_buffer_callback = function()
-- Check if buffer exists before deleting it
if vim.api.nvim_buf_is_loaded(buffer) then
Expand Down

0 comments on commit 2a26745

Please sign in to comment.