Skip to content

Commit 0c5f80e

Browse files
committed
refactor: Migrate ft-specific treesitter logic into filetypes.lua
Keep this undocumented/unofficial for now until it's a bit more stable.
1 parent dd5ffc2 commit 0c5f80e

File tree

3 files changed

+50
-33
lines changed

3 files changed

+50
-33
lines changed

lua/debugprint/filetypes.lua

+24
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ local shell = {
1212
right = '"',
1313
mid_var = "${",
1414
right_var = '}"',
15+
find_treesitter_variable = function(opts)
16+
if opts.node:type() == "variable_name" then
17+
return opts.node_text
18+
else
19+
return nil
20+
end
21+
end,
1522
}
1623

1724
local docker = vim.deepcopy(shell)
@@ -28,6 +35,16 @@ local js = {
2835
right = '")',
2936
mid_var = '", ',
3037
right_var = ")",
38+
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
44+
else
45+
return nil
46+
end
47+
end,
3148
}
3249

3350
local cs = {
@@ -133,6 +150,13 @@ return {
133150
right = "')",
134151
mid_var = "' .. vim.inspect(",
135152
right_var = "))",
153+
find_treesitter_variable = function(opts)
154+
if opts.node:type() == "identifier" then
155+
return opts.node_text
156+
else
157+
return nil
158+
end
159+
end,
136160
},
137161
["make"] = {
138162
left = '\t@echo >&2 "',

lua/debugprint/init.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ M.debugprint_cache = function(opts)
166166
if opts and opts.prerepeat == true then
167167
if get_filetype_config() and opts.variable == true then
168168
opts.variable_name = utils.get_variable_name(
169-
global_opts.ignore_treesitter or opts.ignore_treesitter
169+
global_opts.ignore_treesitter or opts.ignore_treesitter, get_filetype_config()
170170
)
171171

172172
if opts.variable_name == false then

lua/debugprint/utils.lua

+25-32
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ local get_node_at_cursor = function()
2929
end
3030
end
3131

32+
local get_node_text = function(node)
33+
if vim.treesitter.get_node_text then
34+
-- vim.treesitter.query.get_node_text deprecated as of NeoVim
35+
-- 0.9
36+
return vim.treesitter.get_node_text(node, 0)
37+
else
38+
return vim.treesitter.query.get_node_text(node, 0)
39+
end
40+
end
41+
3242
M.is_modifiable = function()
3343
if
3444
not vim.api.nvim_get_option_value(
@@ -43,15 +53,15 @@ M.is_modifiable = function()
4353
end
4454
end
4555

46-
M.get_variable_name = function(ignore_treesitter)
56+
M.get_variable_name = function(ignore_treesitter, filetype_config)
4757
local variable_name = M.get_visual_selection()
4858

4959
if variable_name == false then
5060
return false
5161
end
5262

5363
if variable_name == nil and ignore_treesitter ~= true then
54-
variable_name = M.find_treesitter_variable()
64+
variable_name = M.find_treesitter_variable(filetype_config)
5565
end
5666

5767
if variable_name == nil then
@@ -163,44 +173,27 @@ M.get_effective_filetypes = function()
163173
end
164174
end
165175

166-
M.find_treesitter_variable = function()
167-
local node = get_node_at_cursor()
176+
M.find_treesitter_variable = function(filetype_config)
177+
local obj = {}
178+
179+
obj.node = get_node_at_cursor()
168180

169-
if node == nil then
181+
if obj.node == nil then
170182
return nil
171183
else
172-
local node_type = node:type()
173-
local parent_node_type
184+
obj.node_text = get_node_text(obj.node)
185+
obj.parent_node = obj.node:parent()
174186

175-
if node:parent() ~= nil then
176-
-- This check is necessary; it triggers for example in comments in
177-
-- lua code
178-
parent_node_type = node:parent():type()
179-
end
180-
181-
local variable_name
182-
183-
if vim.treesitter.get_node_text then
184-
-- vim.treesitter.query.get_node_text deprecated as of NeoVim
185-
-- 0.9
186-
variable_name = vim.treesitter.get_node_text(node, 0)
187+
if obj.parent_node then
188+
obj.parent_node_text = get_node_text(obj.parent_node)
187189
else
188-
variable_name = vim.treesitter.query.get_node_text(node, 0)
190+
obj.parent_node_text = nil
189191
end
190192

191-
-- lua, typescript -> identifier
192-
-- sh -> variable_name
193-
-- typescript -> shorthand_property_identifier_pattern (see issue #60)
194-
-- Makefile -> variable_reference
195-
if
196-
node_type == "identifier"
197-
or node_type == "variable_name"
198-
or node_type == "shorthand_property_identifier_pattern"
199-
or parent_node_type == "variable_reference"
200-
then
201-
return variable_name
193+
if vim.list_contains(vim.tbl_keys(filetype_config), "find_treesitter_variable") then
194+
return filetype_config.find_treesitter_variable(obj)
202195
else
203-
return nil
196+
return obj.node_text
204197
end
205198
end
206199
end

0 commit comments

Comments
 (0)