Skip to content

Commit

Permalink
feat: Use smarter variable finding - closes #27
Browse files Browse the repository at this point in the history
Only supported for some languages, extending this is undocumented for
now.
  • Loading branch information
andrewferrier committed Apr 27, 2024
1 parent 0c5f80e commit 052693a
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 17 deletions.
23 changes: 15 additions & 8 deletions lua/debugprint/filetypes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ local shell = {
right_var = '}"',
find_treesitter_variable = function(opts)
if opts.node:type() == "variable_name" then
return opts.node_text
return opts.get_node_text(opts.node)
else
return nil
end
Expand All @@ -36,11 +36,10 @@ local js = {
mid_var = '", ',
right_var = ")",
find_treesitter_variable = function(opts)
if
opts.node:type() == "identifier"
or opts.node:type() == "shorthand_property_identifier_pattern"
then
return opts.node_text
if opts.node:type() == "property_identifier" then
return opts.get_node_text(opts.node:parent())
elseif opts.node:type() == "identifier" then
return opts.get_node_text(opts.node)
else
return nil
end
Expand Down Expand Up @@ -151,8 +150,16 @@ return {
mid_var = "' .. vim.inspect(",
right_var = "))",
find_treesitter_variable = function(opts)
if opts.node:type() == "identifier" then
return opts.node_text
if opts.node:type() == "dot_index_expression" then
return opts.get_node_text(opts.node)
elseif
opts.node:parent()
and opts.node:parent():type() == "dot_index_expression"
and opts.node:prev_named_sibling()
then
return opts.get_node_text(opts.node:parent())
elseif opts.node:type() == "identifier" then
return opts.get_node_text(opts.node)
else
return nil
end
Expand Down
10 changes: 1 addition & 9 deletions lua/debugprint/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -176,20 +176,12 @@ end
M.find_treesitter_variable = function(filetype_config)
local obj = {}

obj.get_node_text = get_node_text
obj.node = get_node_at_cursor()

if obj.node == nil then
return nil
else
obj.node_text = get_node_text(obj.node)
obj.parent_node = obj.node:parent()

if obj.parent_node then
obj.parent_node_text = get_node_text(obj.parent_node)
else
obj.parent_node_text = nil
end

if vim.list_contains(vim.tbl_keys(filetype_config), "find_treesitter_variable") then
return filetype_config.find_treesitter_variable(obj)
else
Expand Down
44 changes: 44 additions & 0 deletions tests/debugprint.lua
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,50 @@ describe("can handle treesitter identifiers", function()

assert.are.same(vim.api.nvim_win_get_cursor(0), { 2, 10 })
end)

it("special case nested (lua)", function()
debugprint.setup()

local filename = init_file({
"function x()",
" local xyz = {}",
" xyz.abc = 123",
"end",
}, "lua", 3, 10)

feedkeys("g?v")

check_lines({
"function x()",
" local xyz = {}",
" xyz.abc = 123",
" print('DEBUGPRINT[1]: "
.. filename
.. ":3: xyz.abc=' .. vim.inspect(xyz.abc))",
"end",
})

assert.are.same(vim.api.nvim_win_get_cursor(0), { 3, 10 })
end)

it("special case nested (javascript)", function()
debugprint.setup()

local filename = init_file({
"let x = {}",
"x.abc = 123",
}, "javascript",2, 4)

feedkeys("g?v")

check_lines({
"let x = {}",
"x.abc = 123",
'console.warn("DEBUGPRINT[1]: ' .. filename .. ':2: x.abc=", x.abc)',
})

assert.are.same(vim.api.nvim_win_get_cursor(0), { 2, 4 })
end)
end)

describe("visual selection", function()
Expand Down

0 comments on commit 052693a

Please sign in to comment.