Skip to content

Commit

Permalink
feat: add support for inline virtual text
Browse files Browse the repository at this point in the history
  • Loading branch information
theHamsta committed May 6, 2023
1 parent ab988db commit 9061ab6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,19 @@ require("nvim-dap-virtual-text").setup {
--- @param buf number
--- @param stackframe dap.StackFrame https://microsoft.github.io/debug-adapter-protocol/specification#Types_StackFrame
--- @param node userdata tree-sitter node identified as variable definition of reference (see `:h tsnode`)
--- @param options nvim_dap_virtual_text_options Current options for nvim-dap-virtual-text
--- @return string|nil A text how the virtual text should be displayed or nil, if this variable shouldn't be displayed
display_callback = function(variable, _buf, _stackframe, _node)
return variable.name .. ' = ' .. variable.value
display_callback = function(variable, buf, stackframe, node, options)
if options.virt_text_pos == 'inline' then
return ' = ' .. variable.value
else
return variable.name .. ' = ' .. variable.value
end
end,
-- position of virtual text, see `:h nvim_buf_set_extmark()`, default tries to inline the virtual text. Use 'eol' to set to end of line
virt_text_pos = vim.fn.has 'nvim-0.10' == 1 and 'inline' or 'eol',

-- experimental features:
virt_text_pos = 'eol', -- position of virtual text, see `:h nvim_buf_set_extmark()`
all_frames = false, -- show virtual text for all stack frames not only current. Only works for debugpy on my machine.
virt_lines = false, -- show virtual lines instead of virtual text (will flicker!)
virt_text_win_col = nil -- position the virtual text at a fixed window column (starting from the first text column) ,
Expand Down
11 changes: 8 additions & 3 deletions lua/nvim-dap-virtual-text.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ local options = {
error_prefix = ' ',
info_prefix = ' ',
-- position of virtual text, see `:h nvim_buf_set_extmark()`
virt_text_pos = 'eol',
virt_text_pos = vim.fn.has 'nvim-0.10' == 1 and 'inline' or 'eol',
-- show virtual lines instead of virtual text (will flicker!)
virt_lines = false,
virt_lines_above = true,
Expand All @@ -77,10 +77,15 @@ local options = {
--- @param buf number
--- @param stackframe dap.StackFrame https://microsoft.github.io/debug-adapter-protocol/specification#Types_StackFrame
--- @param node userdata tree-sitter node identified as variable definition of reference (see `:h tsnode`)
--- @param options nvim_dap_virtual_text_options Current options for nvim-dap-virtual-text
--- @return string|nil A text how the virtual text should be displayed or nil, if this variable shouldn't be displayed
--- @diagnostic disable-next-line: unused-local
display_callback = function(variable, buf, stackframe, node)
return variable.name .. ' = ' .. variable.value
display_callback = function(variable, buf, stackframe, node, options)
if options.virt_text_pos == 'inline' then
return ' = ' .. variable.value
else
return variable.name .. ' = ' .. variable.value
end
end,
}

Expand Down
7 changes: 4 additions & 3 deletions lua/nvim-dap-virtual-text/virtual_text.lua
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ function M.set_virtual_text(stackframe, options)
local has_changed = options.highlight_changed_variables
and (evaluated.value ~= (last_value and last_value.value))
and (options.highlight_new_as_changed or last_value)
local text = options.display_callback(evaluated, buf, stackframe, node)
local text = options.display_callback(evaluated, buf, stackframe, node, options)
if text then
if options.commented then
text = vim.o.commentstring:gsub('%%s', { ['%s'] = text })
Expand Down Expand Up @@ -196,15 +196,16 @@ function M.set_virtual_text(stackframe, options)
{ virt_lines = { content }, virt_lines_above = options.virt_lines_above }
)
else
local inline = options.virt_text_pos == 'inline'
local line_text = api.nvim_buf_get_lines(buf, line, line + 1, true)[1]
local win_col = math.max(options.virt_text_win_col or 0, #line_text + 1)
for i, virt_text in ipairs(content) do
local node_range = { virt_text.node:range() }
if i < #content then
if i < #content and not inline then
virt_text[1] = virt_text[1] .. options.separator
end
virt_text.node = nil
vim.api.nvim_buf_set_extmark(buf, hl_namespace, node_range[1], node_range[2], {
vim.api.nvim_buf_set_extmark(buf, hl_namespace, node_range[inline and 3 or 1], node_range[inline and 4 or 2], {
end_line = node_range[3],
end_col = node_range[4],
hl_mode = 'combine',
Expand Down

0 comments on commit 9061ab6

Please sign in to comment.