@@ -29,6 +29,16 @@ local get_node_at_cursor = function()
29
29
end
30
30
end
31
31
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
+
32
42
M .is_modifiable = function ()
33
43
if
34
44
not vim .api .nvim_get_option_value (
@@ -43,15 +53,15 @@ M.is_modifiable = function()
43
53
end
44
54
end
45
55
46
- M .get_variable_name = function (ignore_treesitter )
56
+ M .get_variable_name = function (ignore_treesitter , filetype_config )
47
57
local variable_name = M .get_visual_selection ()
48
58
49
59
if variable_name == false then
50
60
return false
51
61
end
52
62
53
63
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 )
55
65
end
56
66
57
67
if variable_name == nil then
@@ -163,44 +173,27 @@ M.get_effective_filetypes = function()
163
173
end
164
174
end
165
175
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 ()
168
180
169
- if node == nil then
181
+ if obj . node == nil then
170
182
return nil
171
183
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 ()
174
186
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 )
187
189
else
188
- variable_name = vim . treesitter . query . get_node_text ( node , 0 )
190
+ obj . parent_node_text = nil
189
191
end
190
192
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 )
202
195
else
203
- return nil
196
+ return obj . node_text
204
197
end
205
198
end
206
199
end
0 commit comments