Skip to content

Commit

Permalink
Improove window options configurations
Browse files Browse the repository at this point in the history
## Changes Description

This commit adds the ability to customize window options `vim.wo` for
windows Overseer creates (e.g. the task_list, the output, etc.). It also
fixes a problem where pre-defined window options are reset after the
main buffer changes on the window.
  • Loading branch information
Townk committed Jun 3, 2024
1 parent 819bb88 commit 7dff5f8
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 40 deletions.
32 changes: 32 additions & 0 deletions lua/overseer/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ local default_config = {
separator = "────────────────────────────────────────",
-- Default direction. Can be "left", "right", or "bottom"
direction = "left",
-- You can set window options (see `:help vim.wo`) to be used when creating
-- the Task List window, these are the defaults used:
win_opts = {
listchars = "tab:> ",
winfixwidth = true,
winfixheight = true,
number = false,
relativenumber = false,
foldcolumn = "0",
signcolumn = "no",
statuscolumn = "",
wrap = false,
spell = false,
},
-- Set keymap to false to remove default behavior
-- You can add custom keymaps here as well (anything vim.keymap.set accepts)
bindings = {
Expand All @@ -51,6 +65,24 @@ local default_config = {
["<C-j>"] = "ScrollOutputDown",
["q"] = "Close",
},
-- When the Task List is positioned at the bottom, it will show its output
-- window on the right side of the split. The next configuration table
-- tweaks options for such window
output = {
-- You can set window options (see `:help vim.wo`) to be used when
-- creating the Task List Output window, these are the defaults used:
win_opts = {
number = false,
relativenumber = false,
cursorline = false,
cursorcolumn = false,
foldcolumn = "0",
signcolumn = "no",
statuscolumn = "",
spell = false,
list = false,
},
},
},
-- See :help overseer-actions
actions = {},
Expand Down
27 changes: 24 additions & 3 deletions lua/overseer/task_list/sidebar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,24 @@ function Sidebar:_get_preview_wins()
return ret
end

---@param winid number
---@param bufnr number
---@param window_opts vim.wo
function Sidebar:_update_window_buf(winid, bufnr, window_opts)
if vim.api.nvim_win_get_buf(winid) ~= bufnr then
vim.api.nvim_win_set_buf(winid, bufnr)
-- The documentation of `vim.api.nvim_win_set_buf` states that it will
-- "Set the current buffer in a window, without side effects", but
-- unfortunately, some window options are not kept when a "new" buffer is
-- set to the window. For instance, the `winhighlight` and the experimental
-- `statuscolumn` options may change with a new buffer, therefore, to make
-- the appearance of the window consistent, we re-apply its window options
-- when switching its current buffer.
util.set_window_opts(window_opts, winid)
util.scroll_to_end(winid)
end
end

function Sidebar:update_preview()
local winids = self:_get_preview_wins()
if vim.tbl_isempty(winids) then
Expand All @@ -240,9 +258,12 @@ function Sidebar:update_preview()
end

for _, winid in ipairs(winids) do
if vim.api.nvim_win_get_buf(winid) ~= display_buf then
vim.api.nvim_win_set_buf(winid, display_buf)
util.scroll_to_end(winid)
-- We need to use different window options between the floating preview
-- window and the output one
if vim.wo[winid].previewwindow then
self:_update_window_buf(winid, display_buf, config.task_win.win_opts)
else
self:_update_window_buf(winid, display_buf, config.task_list.output.win_opts)
end
end
end
Expand Down
19 changes: 15 additions & 4 deletions lua/overseer/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,15 @@ M.scroll_to_end = function(winid)
vim.api.nvim_set_option_value("scrolloff", scrolloff, { scope = "local", win = winid })
end

---@param win_opts vim.wo
---@param winid? number
M.set_window_opts = function(win_opts, winid)
winid = winid or 0
for k, v in pairs(win_opts) do
vim.api.nvim_set_option_value(k, v, { scope = "local", win = winid })
end
end

---@param bufnr number
---@param ns number
---@param highlights table
Expand Down Expand Up @@ -265,10 +274,12 @@ end

---Set the appropriate window options for a terminal buffer
M.set_term_window_opts = function(winid)
winid = winid or 0
vim.api.nvim_set_option_value("number", false, { scope = "local", win = winid })
vim.api.nvim_set_option_value("relativenumber", false, { scope = "local", win = winid })
vim.api.nvim_set_option_value("signcolumn", "no", { scope = "local", win = winid })
M.set_window_opts({
number = false,
relativenumber = false,
signcolumn = "no",
statuscolumn = "",
}, winid)
end

---@generic T : any
Expand Down
36 changes: 3 additions & 33 deletions lua/overseer/window.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,6 @@ local function watch_for_win_closed()
})
end

local min_win_opts = {
number = false,
relativenumber = false,
cursorline = false,
cursorcolumn = false,
foldcolumn = "0",
signcolumn = "no",
spell = false,
list = false,
}
---@param winid integer
local function set_minimal_win_opts(winid)
for k, v in pairs(min_win_opts) do
vim.api.nvim_set_option_value(k, v, { scope = "local", win = winid })
end
end

---@param direction "left"|"right"|"bottom"
---@param existing_win integer
local function create_overseer_window(direction, existing_win)
Expand Down Expand Up @@ -68,34 +51,21 @@ local function create_overseer_window(direction, existing_win)
vim.bo[outbuf].bufhidden = "wipe"
end
util.go_buf_no_au(outbuf)
set_minimal_win_opts(0)
util.set_window_opts(config.task_list.output.win_opts, 0)
util.go_win_no_au(winid)
vim.w.overseer_output_win = output_win
watch_for_win_closed()
end

util.go_buf_no_au(bufnr)
local default_opts = {
listchars = "tab:> ",
winfixwidth = true,
winfixheight = true,
number = false,
signcolumn = "no",
foldcolumn = "0",
relativenumber = false,
wrap = false,
spell = false,
}
for k, v in pairs(default_opts) do
vim.api.nvim_set_option_value(k, v, { scope = "local", win = 0 })
end
util.set_window_opts(config.task_list.win_opts, 0)
vim.api.nvim_win_set_width(0, layout.calculate_width(nil, config.task_list))
if direction == "bottom" then
vim.api.nvim_win_set_height(0, layout.calculate_height(nil, config.task_list))
end
-- Set the filetype only after we enter the buffer so that FileType autocmds
-- behave properly
vim.bo[bufnr].filetype = "OverseerList"
vim.api.nvim_set_option_value("filetype", "OverseerList", { buf = bufnr })

util.go_win_no_au(my_winid)
return winid
Expand Down

0 comments on commit 7dff5f8

Please sign in to comment.