feat: handle colorcolumn automatically, no more configuration

I'm dizzy, it took me 2 hours to solve the logic problem
This commit is contained in:
wongxy
2022-04-15 02:16:12 +08:00
parent 356ccaf796
commit 5ab828d911
+32 -16
View File
@@ -1,5 +1,3 @@
--- vim.g.virtcolumn: equal to global colorcolumn
--- vim.b.virtcolumn: equal to local colorcolumn
--- vim.g.virtcolumn_char: ▕ by default --- vim.g.virtcolumn_char: ▕ by default
local api = vim.api local api = vim.api
@@ -18,26 +16,39 @@ local function _refresh()
return return
end end
local textwidth = vim.opt.textwidth:get() local virtcolumn = vim.b.virtcolumn or vim.w.virtcolumn
local virtcolumns = vim.split(vim.b.virtcolumn or vim.g.virtcolumn or vim.o.cc, ",")
local local_cc = vim.wo.cc
if not virtcolumn or local_cc ~= "" then
virtcolumn = local_cc
vim.wo.cc = ""
end
vim.b.virtcolumn = virtcolumn
vim.w.virtcolumn = virtcolumn
---@type number[] ---@type number[]
local items = {} local items = {}
for _, virtcolumn in ipairs(virtcolumns) do local textwidth = vim.opt.textwidth:get()
if virtcolumn and virtcolumn ~= "" then for _, c in ipairs(vim.split(virtcolumn, ",")) do
if vim.startswith(virtcolumn, "+") then local item
if c and c ~= "" then
if vim.startswith(c, "+") then
if textwidth ~= 0 then if textwidth ~= 0 then
table.insert(items, textwidth + tonumber(virtcolumn:sub(2))) item = textwidth + tonumber(c:sub(2))
end end
elseif vim.startswith(virtcolumn, "-") then elseif vim.startswith(virtcolumn, "-") then
if textwidth ~= 0 then if textwidth ~= 0 then
table.insert(items, textwidth - tonumber(virtcolumn:sub(2))) item = textwidth - tonumber(c:sub(2))
end end
else else
table.insert(items, tonumber(virtcolumn)) item = tonumber(c)
end end
end end
if item and item > 0 then
table.insert(items, item)
end
end end
table.sort(items, function(a, b) table.sort(items, function(a, b)
return a > b return a > b
@@ -101,17 +112,22 @@ local function refresh(args)
end end
local function set_hl() local function set_hl()
vim.cmd [[ local cc_bg = api.nvim_get_hl_by_name("ColorColumn", true).background
hi clear ColorColumn if cc_bg then
hi default link VirtColumn NonText api.nvim_set_hl(0, "VirtColumn", { fg = cc_bg, default = true })
]] else
vim.cmd [[hi default link VirtColumn NonText]]
end
end end
set_hl() set_hl()
local group = api.nvim_create_augroup("virtcolumn", {}) local group = api.nvim_create_augroup("virtcolumn", {})
api.nvim_create_autocmd( api.nvim_create_autocmd(
{ "WinScrolled", "TextChanged", "TextChangedI", "BufWinEnter", "InsertLeave" }, { "WinScrolled", "TextChanged", "TextChangedI", "BufWinEnter", "InsertLeave", "FileChangedShellPost" },
{ group = group, callback = refresh } {
group = group,
callback = refresh,
}
) )
api.nvim_create_autocmd("OptionSet", { group = group, callback = refresh, pattern = "colorcolumn" }) api.nvim_create_autocmd("OptionSet", { group = group, callback = refresh, pattern = "colorcolumn" })
api.nvim_create_autocmd("ColorScheme", { group = group, callback = set_hl }) api.nvim_create_autocmd("ColorScheme", { group = group, callback = set_hl })