Skip to content

Commit 052693a

Browse files
committed
feat: Use smarter variable finding - closes #27
Only supported for some languages, extending this is undocumented for now.
1 parent 0c5f80e commit 052693a

File tree

3 files changed

+60
-17
lines changed

3 files changed

+60
-17
lines changed

lua/debugprint/filetypes.lua

+15-8
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ local shell = {
1414
right_var = '}"',
1515
find_treesitter_variable = function(opts)
1616
if opts.node:type() == "variable_name" then
17-
return opts.node_text
17+
return opts.get_node_text(opts.node)
1818
else
1919
return nil
2020
end
@@ -36,11 +36,10 @@ local js = {
3636
mid_var = '", ',
3737
right_var = ")",
3838
find_treesitter_variable = function(opts)
39-
if
40-
opts.node:type() == "identifier"
41-
or opts.node:type() == "shorthand_property_identifier_pattern"
42-
then
43-
return opts.node_text
39+
if opts.node:type() == "property_identifier" then
40+
return opts.get_node_text(opts.node:parent())
41+
elseif opts.node:type() == "identifier" then
42+
return opts.get_node_text(opts.node)
4443
else
4544
return nil
4645
end
@@ -151,8 +150,16 @@ return {
151150
mid_var = "' .. vim.inspect(",
152151
right_var = "))",
153152
find_treesitter_variable = function(opts)
154-
if opts.node:type() == "identifier" then
155-
return opts.node_text
153+
if opts.node:type() == "dot_index_expression" then
154+
return opts.get_node_text(opts.node)
155+
elseif
156+
opts.node:parent()
157+
and opts.node:parent():type() == "dot_index_expression"
158+
and opts.node:prev_named_sibling()
159+
then
160+
return opts.get_node_text(opts.node:parent())
161+
elseif opts.node:type() == "identifier" then
162+
return opts.get_node_text(opts.node)
156163
else
157164
return nil
158165
end

lua/debugprint/utils.lua

+1-9
Original file line numberDiff line numberDiff line change
@@ -176,20 +176,12 @@ end
176176
M.find_treesitter_variable = function(filetype_config)
177177
local obj = {}
178178

179+
obj.get_node_text = get_node_text
179180
obj.node = get_node_at_cursor()
180181

181182
if obj.node == nil then
182183
return nil
183184
else
184-
obj.node_text = get_node_text(obj.node)
185-
obj.parent_node = obj.node:parent()
186-
187-
if obj.parent_node then
188-
obj.parent_node_text = get_node_text(obj.parent_node)
189-
else
190-
obj.parent_node_text = nil
191-
end
192-
193185
if vim.list_contains(vim.tbl_keys(filetype_config), "find_treesitter_variable") then
194186
return filetype_config.find_treesitter_variable(obj)
195187
else

tests/debugprint.lua

+44
Original file line numberDiff line numberDiff line change
@@ -996,6 +996,50 @@ describe("can handle treesitter identifiers", function()
996996

997997
assert.are.same(vim.api.nvim_win_get_cursor(0), { 2, 10 })
998998
end)
999+
1000+
it("special case nested (lua)", function()
1001+
debugprint.setup()
1002+
1003+
local filename = init_file({
1004+
"function x()",
1005+
" local xyz = {}",
1006+
" xyz.abc = 123",
1007+
"end",
1008+
}, "lua", 3, 10)
1009+
1010+
feedkeys("g?v")
1011+
1012+
check_lines({
1013+
"function x()",
1014+
" local xyz = {}",
1015+
" xyz.abc = 123",
1016+
" print('DEBUGPRINT[1]: "
1017+
.. filename
1018+
.. ":3: xyz.abc=' .. vim.inspect(xyz.abc))",
1019+
"end",
1020+
})
1021+
1022+
assert.are.same(vim.api.nvim_win_get_cursor(0), { 3, 10 })
1023+
end)
1024+
1025+
it("special case nested (javascript)", function()
1026+
debugprint.setup()
1027+
1028+
local filename = init_file({
1029+
"let x = {}",
1030+
"x.abc = 123",
1031+
}, "javascript",2, 4)
1032+
1033+
feedkeys("g?v")
1034+
1035+
check_lines({
1036+
"let x = {}",
1037+
"x.abc = 123",
1038+
'console.warn("DEBUGPRINT[1]: ' .. filename .. ':2: x.abc=", x.abc)',
1039+
})
1040+
1041+
assert.are.same(vim.api.nvim_win_get_cursor(0), { 2, 4 })
1042+
end)
9991043
end)
10001044

10011045
describe("visual selection", function()

0 commit comments

Comments
 (0)