fix(statuscolumn): correct line numbers & respect more options

This commit is contained in:
Folke Lemaitre 2023-10-17 08:28:12 +02:00
parent 42ba1af40f
commit 315df373f2
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040

View file

@ -86,11 +86,13 @@ end
function M.statuscolumn() function M.statuscolumn()
local win = vim.g.statusline_winid local win = vim.g.statusline_winid
if vim.wo[win].signcolumn == "no" then
return ""
end
local buf = vim.api.nvim_win_get_buf(win) local buf = vim.api.nvim_win_get_buf(win)
local is_file = vim.bo[buf].buftype == ""
local show_signs = vim.wo[win].signcolumn ~= "no"
local components = { "", "", "" } -- left, middle, right
if show_signs then
---@type Sign?,Sign?,Sign? ---@type Sign?,Sign?,Sign?
local left, right, fold local left, right, fold
for _, s in ipairs(M.get_signs(buf, vim.v.lnum)) do for _, s in ipairs(M.get_signs(buf, vim.v.lnum)) do
@ -100,28 +102,34 @@ function M.statuscolumn()
left = s left = s
end end
end end
if vim.v.virtnum ~= 0 then if vim.v.virtnum ~= 0 then
left = nil left = nil
end end
vim.api.nvim_win_call(win, function() vim.api.nvim_win_call(win, function()
if vim.fn.foldclosed(vim.v.lnum) >= 0 then if vim.fn.foldclosed(vim.v.lnum) >= 0 then
fold = { text = vim.opt.fillchars:get().foldclose or "", texthl = "Folded" } fold = { text = vim.opt.fillchars:get().foldclose or "", texthl = "Folded" }
end end
end) end)
-- Left: mark or non-git sign
local nu = "" components[1] = M.icon(M.get_mark(buf, vim.v.lnum) or left)
if vim.wo[win].number and vim.v.virtnum == 0 then -- Right: fold icon or git sign (only if file)
nu = vim.wo[win].relativenumber and vim.v.relnum ~= 0 and vim.v.relnum or vim.v.lnum components[3] = is_file and M.icon(fold or right) or ""
end end
return table.concat({ -- Numbers in Neovim are weird
M.icon(M.get_mark(buf, vim.v.lnum) or left), -- They show when either number or relativenumber is true
[[%=]], local is_num = vim.wo[win].number
nu .. " ", local is_relnum = vim.wo[win].relativenumber
M.icon(fold or right), if (is_num or is_relnum) and vim.v.virtnum == 0 then
}, "") if vim.v.relnum == 0 then
components[2] = is_num and "%l" or "%r" -- the current line
else
components[2] = is_relnum and "%r" or "%l" -- other lines
end
components[2] = "%=" .. components[2] .. " " -- right align
end
return table.concat(components, "")
end end
function M.fg(name) function M.fg(name)