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 01204a0
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 37 deletions.
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,31 @@ 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) ,
-- e.g. 80 to position at column 80, see `:h nvim_buf_set_extmark()`
}
```

With support for inline virtual text (nvim 0.10), `virt_text_pos = 'inline'`

![image](https://user-images.githubusercontent.com/7189118/236633778-5e18c02c-4415-46a4-b903-6ee06764ef2a.png)


With `highlight_changed_variables = false, all_frames = false`

![current_frame](https://user-images.githubusercontent.com/7189118/81495691-5d937400-92b2-11ea-8995-17daeda593cc.gif)
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
56 changes: 28 additions & 28 deletions test_cases/issue_36_value_not_updated.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
# See Issue #36: Value not updated after changing the value of a parameter

from operator import itemgetter
class Solution:
def twoSum(self, nums: list[int], target: int) -> list[int]:
if len(nums) < 2:
return nums
arr = list(sorted(enumerate(nums), key=itemgetter(1)))
i = 0
j = len(nums) - 1
while i < j:
arri = arr[i]
arrj = arr[j]
sum_ = arri[1] + arrj[1]
if sum_ == target:
return [arri[0], arrj[0]]
elif sum_ > target:
j -= 1
else:
i += 1
return []
arr = [2, 7, 11, 15]
target = 9
Solution().twoSum(arr, target) == [0, 1]
from operator import itemgetter


class Solution:
def twoSum, nums: list[int], target: int) -> list[int]:
if len(nums) < 2:
return nums

arr = list(sorted(enumerate(nums), key=itemgetter(1)))
i = 0
j = len(nums) - 1

while i < j:
arri = arr[i]
arrj = arr[j]
sum_ = arri[1] + arrj[1]
if sum_ == target:
return [arri[0], arrj[0]]
elif sum_ > target:
j -= 1
else:
i += 1
return []

arr = [2, 7, 11, 15]
target = 9

Solution().twoSum(arr, target) == [0, 1]

0 comments on commit 01204a0

Please sign in to comment.