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 authored and vhyrro committed Nov 27, 2023
1 parent a80c025 commit 81f6330
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lua/neorg/modules/core/qol/toc/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ module.config.public = {
-- If `true`, will close the Table of Contents after an entry in the table
-- is picked.
close_after_use = false,

-- If `true`, the width of the Table of Contents window will automatically
-- fit its longest line
fit_width = true,
}

module.public = {
Expand Down Expand Up @@ -227,6 +231,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 +273,13 @@ 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)

if module.config.public.fit_width then
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
end

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 81f6330

Please sign in to comment.