From 5ab828d9115d3738e7e7183fe1cd9524bc0e4b6c Mon Sep 17 00:00:00 2001 From: wongxy Date: Fri, 15 Apr 2022 02:16:12 +0800 Subject: [PATCH] feat: handle colorcolumn automatically, no more configuration I'm dizzy, it took me 2 hours to solve the logic problem --- plugin/virt-column.lua | 48 ++++++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/plugin/virt-column.lua b/plugin/virt-column.lua index a3bfc18..f5a524d 100644 --- a/plugin/virt-column.lua +++ b/plugin/virt-column.lua @@ -1,5 +1,3 @@ ---- vim.g.virtcolumn: equal to global colorcolumn ---- vim.b.virtcolumn: equal to local colorcolumn --- vim.g.virtcolumn_char: ▕ by default local api = vim.api @@ -18,26 +16,39 @@ local function _refresh() return end - local textwidth = vim.opt.textwidth:get() - local virtcolumns = vim.split(vim.b.virtcolumn or vim.g.virtcolumn or vim.o.cc, ",") + local virtcolumn = vim.b.virtcolumn or vim.w.virtcolumn + + 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[] local items = {} - for _, virtcolumn in ipairs(virtcolumns) do - if virtcolumn and virtcolumn ~= "" then - if vim.startswith(virtcolumn, "+") then + local textwidth = vim.opt.textwidth:get() + for _, c in ipairs(vim.split(virtcolumn, ",")) do + local item + if c and c ~= "" then + if vim.startswith(c, "+") then if textwidth ~= 0 then - table.insert(items, textwidth + tonumber(virtcolumn:sub(2))) + item = textwidth + tonumber(c:sub(2)) end elseif vim.startswith(virtcolumn, "-") then if textwidth ~= 0 then - table.insert(items, textwidth - tonumber(virtcolumn:sub(2))) + item = textwidth - tonumber(c:sub(2)) end else - table.insert(items, tonumber(virtcolumn)) + item = tonumber(c) end end + if item and item > 0 then + table.insert(items, item) + end end table.sort(items, function(a, b) return a > b @@ -101,17 +112,22 @@ local function refresh(args) end local function set_hl() - vim.cmd [[ - hi clear ColorColumn - hi default link VirtColumn NonText - ]] + local cc_bg = api.nvim_get_hl_by_name("ColorColumn", true).background + if cc_bg then + api.nvim_set_hl(0, "VirtColumn", { fg = cc_bg, default = true }) + else + vim.cmd [[hi default link VirtColumn NonText]] + end end set_hl() local group = api.nvim_create_augroup("virtcolumn", {}) api.nvim_create_autocmd( - { "WinScrolled", "TextChanged", "TextChangedI", "BufWinEnter", "InsertLeave" }, - { group = group, callback = refresh } + { "WinScrolled", "TextChanged", "TextChangedI", "BufWinEnter", "InsertLeave", "FileChangedShellPost" }, + { + group = group, + callback = refresh, + } ) api.nvim_create_autocmd("OptionSet", { group = group, callback = refresh, pattern = "colorcolumn" }) api.nvim_create_autocmd("ColorScheme", { group = group, callback = set_hl })