Skip to content

Commit

Permalink
Merge pull request #50 from Isrothy/winopt
Browse files Browse the repository at this point in the history
feat: Allow users to override window options and buffer optinos
  • Loading branch information
Isrothy authored Jul 22, 2024
2 parents 32cd758 + 9eca6eb commit fb81d02
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 16 deletions.
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,47 @@ vim.g.neominimap = {
bottom = 0,
right = 0,
},

--- Override default window options.
--- Can be either a table or a function that takes the winid of
--- the window to which a minimap is attached and returns a table.
---@type table | fun(winid: integer) : table
winopt = {},

--- Override default buffer options.
--- Can be either a table or a function that takes the bufnr of
--- the buffer from which a minimap is generated and returns a table.
---@type table | fun(bufnr: integer) : table
bufopt = {}
}
```

The default `winopt` is:

```lua
{
winhighlight = "Normal:NeominimapBackground,FloatBorder:NeominimapBorder,CursorLine:NeominimapCursorLine",
wrap = false,
foldcolumn = "0",
signcolumn = "no",
statuscolumn = "",
number = false,
relativenumber = false,
scrolloff = 99999,
sidescrolloff = 0,
winblend = 0,
cursorline = true,
spell = false,
}
```

The default `bufopt` is:

```lua
{
buftype = "nofile",
swapfile = false,
bufhidden = "hide",
}
```

Expand Down
21 changes: 18 additions & 3 deletions lua/neominimap/buffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,24 @@ M.create_minimap_buffer = function(bufnr)
logger.log(string.format("Created a new buffer %d for minimap of buffer %d", mbufnr, bufnr), vim.log.levels.TRACE)
M.set_minimap_bufnr(bufnr, mbufnr)

vim.bo[mbufnr].buftype = "nofile"
vim.bo[mbufnr].swapfile = false
vim.bo[mbufnr].bufhidden = "hide"
local bufopt = {
buftype = "nofile",
swapfile = false,
bufhidden = "hide",
}

local user_opt = type(config.bufopt) == "function" and config.bufopt(bufnr) or config.bufopt
if type(user_opt) == "table" then
bufopt = vim.tbl_deep_extend("force", bufopt, user_opt)
else
logger.log_and_notify(
string.format("Invalid type for bufopt: expected table, got %s", type(user_opt)),
vim.log.levels.ERROR
)
end
for k, v in pairs(bufopt) do
vim.bo[mbufnr][k] = v
end

vim.b[bufnr].update_minimap_text = util.debounce(
vim.schedule_wrap(function()
Expand Down
4 changes: 4 additions & 0 deletions lua/neominimap/config/internal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ local M = {}
---@field z_index integer
---@field window_border string | string[]
---@field margin Neominimap.InternalMargin
---@field winopt table | fun(winid: integer) : table
---@field bufopt table | fun(bufnr: integer) : table

---@class Neominimap.InternalDiagnosticConfig
---@field enabled boolean
Expand Down Expand Up @@ -92,6 +94,8 @@ M.default_config = {
},
z_index = 1, -- The z-index the floating window will be on
window_border = "single", -- The border style of the floating window (accepts all usual options)
winopt = {},
bufopt = {},
}

return M
2 changes: 2 additions & 0 deletions lua/neominimap/config/meta.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ local M = {}
---@field z_index integer?
---@field window_border (string | string[])?
---@field margin Neominimap.Margin?
---@field winopt (table | fun(winid: integer) : table)?
---@field bufopt (table | fun(bufnr: integer) : table)?

---@class Neominimap.DiagnosticConfig
---@field enabled boolean?
Expand Down
36 changes: 36 additions & 0 deletions lua/neominimap/config/validator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,40 @@ local function validate_margin(value)
return true
end

local function validate_winopt(value)
if value ~= nil then
if type(value) == "function" then
return true
elseif type(value) == "table" then
return true
else
vim.notify(
string.format("Invalid type for winopt: expected function or table, got %s", type(value)),
vim.log.levels.ERROR
)
return false
end
end
return true
end

local function validate_bufopt(value)
if value ~= nil then
if type(value) == "function" then
return true
elseif type(value) == "table" then
return true
else
vim.notify(
string.format("Invalid type for bufopt: expected function or table, got %s", type(value)),
vim.log.levels.ERROR
)
return false
end
end
return true
end

---@param config any
---@return boolean
M.validate_user_config = function(config)
Expand All @@ -205,6 +239,8 @@ M.validate_user_config = function(config)
and validate_diagnostic(config.diagnostic)
and validate_treesitter(config.treesitter)
and validate_margin(config.margin)
and validate_winopt(config.winopt)
and validate_bufopt(config.bufopt)
end
return true
end
Expand Down
40 changes: 27 additions & 13 deletions lua/neominimap/window.lua
Original file line number Diff line number Diff line change
Expand Up @@ -199,19 +199,33 @@ M.create_minimap_window = function(winid)
local mwinid = util.noautocmd(api.nvim_open_win)(mbufnr, false, win_cfg)
M.set_minimap_winid(winid, mwinid)

vim.wo[mwinid].winhighlight =
"Normal:NeominimapBackground,FloatBorder:NeominimapBorder,CursorLine:NeominimapCursorLine"
vim.wo[mwinid].wrap = false
vim.wo[mwinid].foldcolumn = "0"
vim.wo[mwinid].signcolumn = "no"
vim.wo[mwinid].statuscolumn = ""
vim.wo[mwinid].number = false
vim.wo[mwinid].relativenumber = false
vim.wo[mwinid].scrolloff = 99999 -- To center minimap
vim.wo[mwinid].sidescrolloff = 0
vim.wo[mwinid].winblend = 0
vim.wo[mwinid].cursorline = true
vim.wo[mwinid].spell = false
local winopt = {
winhighlight = "Normal:NeominimapBackground,FloatBorder:NeominimapBorder,CursorLine:NeominimapCursorLine",
wrap = false,
foldcolumn = "0",
signcolumn = "no",
statuscolumn = "",
number = false,
relativenumber = false,
scrolloff = 99999, -- To center minimap
sidescrolloff = 0,
winblend = 0,
cursorline = true,
spell = false,
}

local user_opt = type(config.winopt) == "function" and config.winopt(winid) or config.winopt
if type(user_opt) == "table" then
winopt = vim.tbl_deep_extend("force", winopt, user_opt)
else
logger.log_and_notify(
string.format("Invalid type for winopt: expected table, got %s", type(user_opt)),
vim.log.levels.ERROR
)
end
for k, v in pairs(winopt) do
vim.wo[mwinid][k] = v
end

logger.log(string.format("Minimap window %d created for window %d", mwinid, winid), vim.log.levels.TRACE)
return mwinid
Expand Down

0 comments on commit fb81d02

Please sign in to comment.