forked from AckslD/nvim-pytrize.lua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusages.lua
More file actions
221 lines (187 loc) · 6.4 KB
/
usages.lua
File metadata and controls
221 lines (187 loc) · 6.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
local M = {}
local ts = vim.treesitter
local warn = require('pytrize.warn').warn
local paths = require('pytrize.paths')
local ts_utils = require('pytrize.ts')
local function find_python_files(root_dir, name)
return vim.fn.systemlist(string.format(
'grep -rl --include="*.py" "%s" "%s"',
vim.fn.escape(name, '"\\'),
root_dir
))
end
local function get_param_name_node(param_node)
local t = param_node:type()
if t == 'identifier' then
return param_node
elseif t == 'default_parameter' or t == 'typed_default_parameter' then
return param_node:field('name')[1]
elseif t == 'typed_parameter' then
local first = param_node:named_child(0)
if first and first:type() == 'identifier' then
return first
end
end
return nil
end
local function find_body_references(body_node, fixture_name, bufnr)
local positions = {}
local function walk_body(node, shadowed)
if shadowed then return end
local node_type = node:type()
if node_type == 'function_definition' then
local params_node = node:field('parameters')[1]
if params_node then
local re_declares = false
for child in params_node:iter_children() do
local name_node = get_param_name_node(child)
if name_node and ts.get_node_text(name_node, bufnr) == fixture_name then
re_declares = true
break
end
end
for child in node:iter_children() do
walk_body(child, re_declares)
end
return
end
end
if node_type == 'identifier' then
if ts.get_node_text(node, bufnr) == fixture_name then
local parent = node:parent()
if parent then
local parent_type = parent:type()
if parent_type == 'attribute' then
local attr_field = parent:field('attribute')[1]
if attr_field and attr_field:id() == node:id() then
goto continue
end
end
if parent_type == 'keyword_argument' then
local name_field = parent:field('name')[1]
if name_field and name_field:id() == node:id() then
goto continue
end
end
end
local row, col_start, _, col_end = node:range()
table.insert(positions, { row = row, col_start = col_start, col_end = col_end })
end
::continue::
end
for child in node:iter_children() do
walk_body(child, false)
end
end
walk_body(body_node, false)
return positions
end
-- Find usage positions (parameters, body references, usefixtures strings)
-- Does NOT include fixture definitions.
local find_usage_positions = function(bufnr, fixture_name)
local ok = pcall(function() vim.treesitter.language.inspect('python') end)
if not ok then return {} end
local parser = ts.get_parser(bufnr, 'python')
local tree = parser:parse()[1]
local root = tree:root()
local positions = {}
ts_utils.walk(root, function(node)
local node_type = node:type()
-- Case A: fixture consumers (parameter + body refs)
if node_type == 'function_definition' then
local params_node = node:field('parameters')[1]
if params_node then
local found_param_node = nil
for child in params_node:iter_children() do
local name_node = get_param_name_node(child)
if name_node and ts.get_node_text(name_node, bufnr) == fixture_name then
found_param_node = name_node
break
end
end
if found_param_node then
local row, col_start, _, col_end = found_param_node:range()
table.insert(positions, { row = row, col_start = col_start, col_end = col_end })
local body_node = node:field('body')[1]
if body_node then
for _, pos in ipairs(find_body_references(body_node, fixture_name, bufnr)) do
table.insert(positions, pos)
end
end
end
end
end
-- Case B: @pytest.mark.usefixtures("fixture_name") strings
if node_type == 'call' then
local func = node:field('function')[1]
if func and func:type() == 'attribute' then
if ts.get_node_text(func, bufnr) == 'pytest.mark.usefixtures' then
local args = node:field('arguments')[1]
if args then
for child in args:iter_children() do
if child:type() == 'string' then
for schild in child:iter_children() do
if schild:type() == 'string_content' then
if ts.get_node_text(schild, bufnr) == fixture_name then
local row, col_start, _, col_end = schild:range()
table.insert(positions, { row = row, col_start = col_start, col_end = col_end })
end
end
end
end
end
end
end
end
end
end)
return positions
end
local find_all_usages = function(fixture_name, root_dir)
local py_files = find_python_files(root_dir, fixture_name)
local items = {}
for _, filepath in ipairs(py_files) do
local existing_bufnr = vim.fn.bufnr(filepath)
local was_loaded = existing_bufnr ~= -1 and vim.fn.bufloaded(existing_bufnr) == 1
local bufnr = vim.fn.bufadd(filepath)
if not was_loaded then
vim.fn.bufload(bufnr)
end
vim.api.nvim_set_option_value('filetype', 'python', { buf = bufnr })
local positions = find_usage_positions(bufnr, fixture_name)
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
for _, pos in ipairs(positions) do
table.insert(items, {
filename = filepath,
lnum = pos.row + 1,
col = pos.col_start + 1,
text = lines[pos.row + 1] or '',
})
end
if not was_loaded then
vim.api.nvim_buf_delete(bufnr, { force = false })
end
end
return items
end
M.show_usages = function()
local fixture_name = vim.fn.expand('<cword>')
if fixture_name == '' then
warn('no word under cursor')
return
end
local filepath = vim.api.nvim_buf_get_name(0)
local root_dir = paths.split_at_root(filepath)
if root_dir == nil then return end
local items = find_all_usages(fixture_name, root_dir)
if #items == 0 then
warn(string.format('no usages found for fixture "%s"', fixture_name))
return
end
vim.fn.setqflist(items, 'r')
vim.cmd('copen')
end
-- Internal exports for testing
M._find_usage_positions = find_usage_positions
M._find_all_usages = find_all_usages
return M