Skip to content

Commit

Permalink
fix #385 use virtual_lines for fastwrap
Browse files Browse the repository at this point in the history
  • Loading branch information
zztrieuzz committed Sep 19, 2023
1 parent f611dc2 commit 7b3eb9b
Showing 1 changed file with 36 additions and 21 deletions.
57 changes: 36 additions & 21 deletions lua/nvim-autopairs/fastwrap.lua
Expand Up @@ -15,6 +15,7 @@ local default_config = {
highlight = 'Search',
highlight_grey = 'Comment',
manual_position = true,
use_virt_lines = true
}

M.ns_fast_wrap = vim.api.nvim_create_namespace('autopairs_fastwrap')
Expand Down Expand Up @@ -69,12 +70,12 @@ M.show = function(line)
local key = config.keys:sub(index, index)
index = index + 1
if not config.manual_position and (
utils.is_quote(char)
or (
utils.is_close_bracket(char)
and utils.is_in_quotes(line, col, prev_char)
utils.is_quote(char)
or (
utils.is_close_bracket(char)
and utils.is_in_quotes(line, col, prev_char)
)
)
)
then
offset = 0
end
Expand Down Expand Up @@ -111,8 +112,8 @@ M.show = function(line)
vim.api.nvim_buf_clear_namespace(0, M.ns_fast_wrap, row, row + 1)
for _, pos in pairs(list_pos) do
local hl_mark = {
{ pos = pos.pos - 1, key = config.before_key },
{ pos = pos.pos + 1, key = config.after_key },
{ pos = pos.pos - 1, key = config.before_key },
{ pos = pos.pos + 1, key = config.after_key },
}
if config.manual_position and (char == pos.key or char == string.upper(pos.key)) then
M.highlight_wrap(hl_mark, row, col, #line)
Expand Down Expand Up @@ -172,22 +173,36 @@ end

M.highlight_wrap = function(tbl_pos, row, col, end_col)
local bufnr = vim.api.nvim_win_get_buf(0)
if config.highlight_grey then
vim.highlight.range(
bufnr,
M.ns_fast_wrap,
config.highlight_grey,
{ row, col },
{ row, end_col },
{}
)
end
for _, pos in ipairs(tbl_pos) do
vim.api.nvim_buf_set_extmark(bufnr, M.ns_fast_wrap, row, pos.pos - 1, {
virt_text = { { pos.key, config.highlight } },
virt_text_pos = 'overlay',
if config.use_virt_lines then
local virt_lines = {}
local start = 0
for _, pos in ipairs(tbl_pos) do
virt_lines[#virt_lines + 1] = { string.rep(' ', pos.pos - start - 1), 'Normal' }
virt_lines[#virt_lines + 1] = { pos.key, config.highlight }
start = pos.pos
end
vim.api.nvim_buf_set_extmark(bufnr, M.ns_fast_wrap, row, 0, {
virt_lines = { virt_lines },
hl_mode = 'blend',
})
else
if config.highlight_grey then
vim.highlight.range(
bufnr,
M.ns_fast_wrap,
config.highlight_grey,
{ row, col },
{ row, end_col },
{}
)
end
for _, pos in ipairs(tbl_pos) do
vim.api.nvim_buf_set_extmark(bufnr, M.ns_fast_wrap, row, pos.pos - 1, {
virt_text = { { pos.key, config.highlight } },
virt_text_pos = 'overlay',
hl_mode = 'blend',
})
end
end
end

Expand Down

0 comments on commit 7b3eb9b

Please sign in to comment.