feature: add global refresh

This commit is contained in:
Lukas Reineke
2021-11-29 14:43:12 +09:00
parent 95a27c0548
commit 84ffea61a1
3 changed files with 31 additions and 5 deletions
+5 -1
View File
@@ -54,11 +54,13 @@ char *virt-column-char*
3. COMMANDS *virt-column-commands*
------------------------------------------------------------------------------
:VirtColumnRefresh *VirtColumnRefresh*
:VirtColumnRefresh[!] *VirtColumnRefresh*
Refreshes the virtual column
Run this with |autocmd| when the file changes.
With bang (VirtColumnRefresh!) refreshes the virtual column globally.
By default it is run for:
1. |FileChangedShellPost| *
2. |TextChanged| *
@@ -67,6 +69,8 @@ char *virt-column-char*
5. |BufWinEnter| *
6. |WinScrolled| *
7. |OptionSet| colorcolumn
8. |VimEnter| * !
9. |SessionLoadPost| * !
==============================================================================
4. CHANGELOG *virt-column-changelog*
+15
View File
@@ -0,0 +1,15 @@
local M = {}
M.refresh = function(bang)
if bang then
local win = vim.api.nvim_get_current_win()
vim.cmd [[noautocmd windo lua require("virt-column").refresh()]]
if vim.api.nvim_win_is_valid(win) then
vim.api.nvim_set_current_win(win)
end
else
vim.cmd [[lua require("virt-column").refresh()]]
end
end
return M
+11 -4
View File
@@ -18,7 +18,7 @@ M.setup = function(config)
M.config = vim.tbl_deep_extend("force", M.config, config or {})
M.namespace = vim.api.nvim_create_namespace "virt-column"
vim.cmd [[command! VirtColumnRefresh lua require("virt-column").refresh()]]
vim.cmd [[command! -bang VirtColumnRefresh lua require("virt-column.commands").refresh("<bang>" == "!")]]
vim.cmd [[highlight link VirtColumn Whitespace]]
vim.cmd [[highlight clear ColorColumn]]
@@ -27,18 +27,25 @@ M.setup = function(config)
autocmd!
autocmd FileChangedShellPost,TextChanged,TextChangedI,CompleteChanged,BufWinEnter,WinScrolled * VirtColumnRefresh
autocmd OptionSet colorcolumn VirtColumnRefresh
autocmd VimEnter,SessionLoadPost * VirtColumnRefresh!
augroup END
]]
end
M.refresh = function()
local bufnr = 0
local bufnr = vim.api.nvim_get_current_buf()
if not vim.api.nvim_buf_is_loaded(bufnr) then
return
end
local winnr = vim.api.nvim_get_current_win()
local win_start = vim.fn.line "w0"
local win_end = vim.fn.line "w$"
local offset = math.max(win_start - 11, 0)
local range = math.min(win_end + 11, vim.api.nvim_buf_line_count(bufnr))
local lines = vim.api.nvim_buf_get_lines(bufnr, offset, range, false)
local width = vim.api.nvim_win_get_width(0) - ffi.C.curwin_col_off()
local width = vim.api.nvim_win_get_width(winnr) - ffi.C.curwin_col_off()
local textwidth = vim.opt.textwidth:get()
local colorcolumn = vim.opt.colorcolumn:get()
@@ -56,7 +63,7 @@ M.refresh = function()
return a > b
end)
M.clear_buf()
M.clear_buf(bufnr)
for i = 1, #lines, 1 do
local current_offset = 0