Skip to content

Commit

Permalink
Keep conflicts, spell, and textwidth signs updated in insert mode
Browse files Browse the repository at this point in the history
  • Loading branch information
dstein64 committed Dec 15, 2024
1 parent f075c91 commit 5292e1d
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
36 changes: 36 additions & 0 deletions lua/scrollview/signs/conflicts.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
local api = vim.api
local fn = vim.fn
local scrollview = require('scrollview')
local utils = require('scrollview.utils')
local binary_search = utils.binary_search

local M = {}

Expand Down Expand Up @@ -93,6 +95,40 @@ function M.init(enable)
end
end
end)

api.nvim_create_autocmd('TextChangedI', {
callback = function()
if not scrollview.is_sign_group_active(group) then return end
local bufnr = api.nvim_get_current_buf()
local line = fn.line('.')
local str = fn.getbufline(bufnr, line)[1]
for position, name in pairs(names) do
local expect_sign = nil
if position == TOP then
expect_sign = vim.startswith(str, '<<<<<<< ')
elseif position == MIDDLE then
expect_sign = str == '======='
elseif position == BOTTOM then
expect_sign = vim.startswith(str, '>>>>>>> ')
else
error('Unknown position: ' .. position)
end
local idx = -1
local lines = vim.b[bufnr][name]
if lines ~= nil then
idx = utils.binary_search(lines, line)
if lines[idx] ~= line then
idx = -1
end
end
local has_sign = idx ~= -1
if expect_sign ~= has_sign then
scrollview.refresh()
break
end
end
end
})
end

return M
24 changes: 24 additions & 0 deletions lua/scrollview/signs/spell.lua
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,30 @@ function M.init(enable)
end
end
})

api.nvim_create_autocmd('TextChangedI', {
callback = function()
if not scrollview.is_sign_group_active(group) then return end
local winid = api.nvim_get_current_win()
local bufnr = api.nvim_get_current_buf()
local line = fn.line('.')
local str = fn.getbufline(bufnr, line)[1]
local spellbadword = fn.spellbadword(str)
local expect_sign = spellbadword[1] ~= ''
local idx = -1
local lines = vim.w[winid][name]
if lines ~= nil then
idx = utils.binary_search(lines, line)
if lines[idx] ~= line then
idx = -1
end
end
local has_sign = idx ~= -1
if expect_sign ~= has_sign then
scrollview.refresh()
end
end
})
end

return M
26 changes: 26 additions & 0 deletions lua/scrollview/signs/textwidth.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
local api = vim.api
local fn = vim.fn
local scrollview = require('scrollview')
local utils = require('scrollview.utils')
local binary_search = utils.binary_search

local M = {}

Expand Down Expand Up @@ -73,6 +75,30 @@ function M.init(enable)
scrollview.refresh()
end
})

api.nvim_create_autocmd('TextChangedI', {
callback = function()
if not scrollview.is_sign_group_active(group) then return end
local bufnr = api.nvim_get_current_buf()
local textwidth = api.nvim_buf_get_option(bufnr, 'textwidth')
local line = fn.line('.')
local str = fn.getbufline(bufnr, line)[1]
local line_length = fn.strchars(str, 1)
local expect_sign = textwidth > 0 and line_length > textwidth
local idx = -1
local lines = vim.b[bufnr][name]
if lines ~= nil then
idx = utils.binary_search(lines, line)
if lines[idx] ~= line then
idx = -1
end
end
local has_sign = idx ~= -1
if expect_sign ~= has_sign then
scrollview.refresh()
end
end
})
end

return M

0 comments on commit 5292e1d

Please sign in to comment.