Skip to content

Commit

Permalink
feat: add support for inline virtual text (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
vishalbalaji authored Jul 16, 2024
1 parent 85855b3 commit 08bd34b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
1 change: 1 addition & 0 deletions doc/colorizer.txt
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ user_default_options *colorizer.user_default_options*
-- parsers can contain values used in |user_default_options|
sass = { enable = false, parsers = { css }, }, -- Enable sass colors
virtualtext = "■",
virtualtext_inline = false, -- Show the virtualtext inline with the color
-- update color values even if buffer is not focused
always_update = false
}
Expand Down
3 changes: 3 additions & 0 deletions lua/colorizer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ local CURRENT_BUF = 0
-- -- parsers can contain values used in |user_default_options|
-- sass = { enable = false, parsers = { css }, }, -- Enable sass colors
-- virtualtext = "■",
-- virtualtext_inline = false, -- Show the virtualtext inline with the color
-- -- update color values even if buffer is not focused
-- always_update = false
-- }
Expand All @@ -157,6 +158,7 @@ local CURRENT_BUF = 0
--@field tailwind boolean|string
--@field sass table
--@field virtualtext string
--@field virtualtext_inline? boolean
--@field always_update boolean
local USER_DEFAULT_OPTIONS = {
RGB = true,
Expand All @@ -172,6 +174,7 @@ local USER_DEFAULT_OPTIONS = {
tailwind = false,
sass = { enable = false, parsers = { css = true } },
virtualtext = "",
virtualtext_inline = false,
always_update = false,
}

Expand Down
18 changes: 15 additions & 3 deletions lua/colorizer/buffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,23 @@ function buffer.add_highlight(buf, ns, line_start, line_end, data, options)
for linenr, hls in pairs(data) do
for _, hl in ipairs(hls) do
local hlname = create_highlight(hl.rgb_hex, mode)
buf_set_virtual_text(buf, ns, linenr, hl.range[2], {
end_col = hl.range[2],

local start_col = hl.range[2]
local opts = {
virt_text = { { options.virtualtext or "", hlname } },
hl_mode = "combine",
})
priority = 0,
}

if options.virtualtext_inline then
start_col = hl.range[1]
opts.virt_text_pos = "inline"
opts.virt_text = { { (options.virtualtext or "") .. " ", hlname } }
end

opts.end_col = start_col

buf_set_virtual_text(buf, ns, linenr, start_col, opts)
end
end
end
Expand Down

0 comments on commit 08bd34b

Please sign in to comment.