|
1 | 1 | local M = {} |
2 | 2 |
|
3 | 3 | local ts = vim.treesitter |
4 | | -local ts_utils = require('nvim-treesitter.ts_utils') |
5 | 4 | local ts_query = ts.query |
6 | | -local reverse = require('pytrize.tables').reverse |
| 5 | + |
7 | 6 | local warn = require('pytrize.warn').warn |
| 7 | +local tbls = require('pytrize.tables') |
8 | 8 |
|
9 | | -local function get_root() |
10 | | - local parser = ts.get_parser() |
| 9 | +local get_root = function(bufnr) |
| 10 | + local parser = ts.get_parser(bufnr) |
11 | 11 | local tstree = parser:parse()[1] |
12 | 12 | return tstree:root() |
13 | 13 | end |
14 | 14 |
|
15 | | -local function get_param_call_nodes() |
16 | | - local tsroot = get_root() |
| 15 | +local get_param_call_nodes = function(bufnr) |
| 16 | + local tsroot = get_root(bufnr) |
17 | 17 | local query = ts_query.parse_query( |
18 | 18 | 'python', |
19 | | - '(call function: (attribute) @param) (#eq? @param "pytest.mark.parametrize")' |
| 19 | + -- TODO not sure why eg (#eq? @param "pytest.mark.parametrize") does not work |
| 20 | + [[ |
| 21 | + (decorated_definition ( |
| 22 | + decorator ( |
| 23 | + call |
| 24 | + function: ((attribute) @param) |
| 25 | + ) |
| 26 | + )) |
| 27 | + ]] |
20 | 28 | ) |
21 | 29 | local nodes = {} |
22 | 30 | for _, node, _ in query:iter_captures(tsroot) do |
| 31 | + if ts.get_node_text(node, bufnr) == 'pytest.mark.parametrize' then |
23 | 32 | table.insert(nodes, node:parent()) |
| 33 | + end |
24 | 34 | end |
25 | 35 | return nodes |
26 | 36 | end |
27 | 37 |
|
28 | | -M.get = function() |
29 | | - local calls = get_param_call_nodes() |
30 | | - local call_specs = {} |
31 | | - local param_order = {} |
32 | | - for _, call in ipairs(calls) do |
33 | | - -- Move to separate func, better way? |
34 | | - local decorated_definition = call:parent():parent() |
35 | | - if decorated_definition:type() ~= 'decorated_definition' then |
36 | | - warn("couldn't parse params") |
37 | | - return |
38 | | - end |
39 | | - local func = decorated_definition:field('definition')[1] |
40 | | - local func_name = ts_utils.get_node_text(func:field('name')[1])[1] |
| 38 | +local get_second_arg_node = function(call_node) |
| 39 | + local arguments = call_node:field('arguments')[1] |
| 40 | + return arguments:child(3) |
| 41 | +end |
41 | 42 |
|
42 | | - local arguments = call:field('arguments')[1] |
43 | | - local params_node = arguments:child(1) |
44 | | - if params_node:type() ~= 'string' then |
45 | | - warn("couldn't parse params") |
46 | | - return |
47 | | - end |
48 | | - local params_str = ts_utils.get_node_text(params_node)[1] -- TODO multiline strings? |
49 | | - params_str = params_str:sub(2, -2) |
50 | | - local params = vim.fn.split(params_str, [[,\s*]]) -- TODO avoid vim script? |
51 | | - table.insert(call_specs, { |
52 | | - call_node = call, |
53 | | - params = params, |
54 | | - func_name = func_name, |
55 | | - }) |
56 | | - for _, param in ipairs(reverse(params)) do |
57 | | - table.insert(param_order, param) |
58 | | - end |
| 43 | +local get_named_children = function(node) |
| 44 | + local children = {} |
| 45 | + for child in node:iter_children() do |
| 46 | + if child:named() and child:type() ~= 'comment' then |
| 47 | + table.insert(children, child) |
59 | 48 | end |
60 | | - return reverse(param_order), call_specs |
| 49 | + end |
| 50 | + return children |
61 | 51 | end |
62 | 52 |
|
63 | | -M.has_param = function(call_spec, param) |
64 | | - for _, p in ipairs(call_spec.params) do |
65 | | - if p == param then |
66 | | - return true |
67 | | - end |
| 53 | +local list_entries = function(call_node) |
| 54 | + local list = get_second_arg_node(call_node) |
| 55 | + if list:type() ~= 'list' then |
| 56 | + return {} |
68 | 57 | end |
69 | | - return false |
| 58 | + return get_named_children(list) |
70 | 59 | end |
71 | 60 |
|
72 | | -local non_entry_types = { |
73 | | - ['comment'] = true, |
74 | | - -- TODO why are these even entry types? |
75 | | - ['['] = true, |
76 | | - [','] = true, |
77 | | - [']'] = true, |
| 61 | +local LITERALS = { |
| 62 | + integer = true, |
| 63 | + float = true, |
| 64 | + ['true'] = true, |
| 65 | + ['false'] = true, |
78 | 66 | } |
79 | 67 |
|
80 | | -M.get_second_arg_node = function(call_node) |
81 | | - local arguments = call_node:field('arguments')[1] |
82 | | - return arguments:child(3) |
| 68 | +local is_simple_literal = function(node) |
| 69 | + return LITERALS[node:type()] ~= nil |
83 | 70 | end |
84 | 71 |
|
85 | | -M.list_entries = function(call_node) |
86 | | - local entry_nodes = {} |
87 | | - local list = M.get_second_arg_node(call_node) |
88 | | - if list:type() ~= 'list' then |
89 | | - return entry_nodes |
| 72 | +local get_item_id = function(entry_idx, item_node, param, bufnr) |
| 73 | + if item_node:type() == 'string' then |
| 74 | + local str = ts.get_node_text(item_node, bufnr) |
| 75 | + local quote = str:sub(1, 1) |
| 76 | + str = vim.fn.trim(str, quote):gsub('\n', '\\n') |
| 77 | + return str |
| 78 | + elseif is_simple_literal(item_node) then |
| 79 | + return ts.get_node_text(item_node, bufnr) |
| 80 | + end |
| 81 | + return string.format('%s%d', param, entry_idx) |
| 82 | +end |
| 83 | + |
| 84 | +local get_entry = function(entry_idx, entry_node, params, bufnr) |
| 85 | + if entry_node:type() ~= 'tuple' then |
| 86 | + if #params == 1 then |
| 87 | + return {{ |
| 88 | + id = get_item_id(entry_idx, entry_node, params[1], bufnr), |
| 89 | + node = entry_node, |
| 90 | + param = params[1], |
| 91 | + idx = entry_idx, |
| 92 | + }} |
| 93 | + else |
| 94 | + return {{ |
| 95 | + id = string.format('unknown (%d)', entry_idx), |
| 96 | + node = entry_node, |
| 97 | + }} |
| 98 | + end |
| 99 | + end |
| 100 | + local items = {} |
| 101 | + local item_nodes = get_named_children(entry_node) |
| 102 | + if #params ~= #item_nodes then |
| 103 | + -- TODO warn here? |
| 104 | + -- warn(string.format( |
| 105 | + -- 'number of items in entry tuple differ from number of params, %d items and %d params (line %d in %s)', |
| 106 | + -- #item_nodes, |
| 107 | + -- #params, |
| 108 | + -- entry_node:start() + 1, |
| 109 | + -- vim.fn.bufname(bufnr) |
| 110 | + -- )) |
| 111 | + return nil |
| 112 | + end |
| 113 | + for i, param in ipairs(params) do |
| 114 | + table.insert(items, { |
| 115 | + id = get_item_id(entry_idx, item_nodes[i], param, bufnr), |
| 116 | + node = item_nodes[i], |
| 117 | + param = param, |
| 118 | + idx = entry_idx, |
| 119 | + }) |
| 120 | + end |
| 121 | + return items |
| 122 | +end |
| 123 | + |
| 124 | +local get_entries = function(call_node, params, bufnr) |
| 125 | + local entries = {} |
| 126 | + for entry_idx, entry_node in ipairs(list_entries(call_node)) do |
| 127 | + local items = get_entry(entry_idx - 1, entry_node, params, bufnr) |
| 128 | + if items ~= nil then |
| 129 | + table.insert(entries, { |
| 130 | + id = table.concat(tbls.list_map(function(item) return item.id end, items), '-'), |
| 131 | + items = items, |
| 132 | + node = entry_node, |
| 133 | + }) |
90 | 134 | end |
91 | | - for child in list:iter_children() do |
92 | | - if non_entry_types[child:type()] == nil then |
93 | | - table.insert(entry_nodes, child) |
94 | | - end |
| 135 | + end |
| 136 | + return entries |
| 137 | +end |
| 138 | + |
| 139 | +M.get_calls = function(bufnr) |
| 140 | + local calls = get_param_call_nodes(bufnr or 0) |
| 141 | + local call_specs = {} |
| 142 | + for _, call in ipairs(calls) do |
| 143 | + -- Move to separate func, better way? |
| 144 | + local decorated_definition = call:parent():parent() |
| 145 | + if decorated_definition:type() ~= 'decorated_definition' then |
| 146 | + local row = call:start() |
| 147 | + warn(string.format( |
| 148 | + "couldn't parse params (line %d)\n expected `decorated_definition`\n got `%s`", |
| 149 | + row, |
| 150 | + decorated_definition:type() |
| 151 | + )) |
| 152 | + return |
| 153 | + end |
| 154 | + local func = decorated_definition:field('definition')[1] |
| 155 | + local func_name = ts.get_node_text(func:field('name')[1], bufnr) |
| 156 | + |
| 157 | + local arguments = call:field('arguments')[1] |
| 158 | + local params_node = arguments:child(1) |
| 159 | + if params_node:type() ~= 'string' then |
| 160 | + local row = call:start() |
| 161 | + warn(string.format( |
| 162 | + "couldn't parse params (line %d)\n expected `string`\n got `%s`", |
| 163 | + row, |
| 164 | + params_node:type() |
| 165 | + )) |
| 166 | + return |
95 | 167 | end |
96 | | - return entry_nodes |
| 168 | + local params_str = ts.get_node_text(params_node, bufnr) |
| 169 | + params_str = params_str:sub(2, -2) |
| 170 | + local params = vim.fn.split(params_str, [[,\s*]]) -- TODO avoid vim script? |
| 171 | + local entries = get_entries(call, params, bufnr) |
| 172 | + if call_specs[func_name] == nil then call_specs[func_name] = {} end |
| 173 | + table.insert(call_specs[func_name], { |
| 174 | + node = call, |
| 175 | + entries = entries, |
| 176 | + params = params, |
| 177 | + func_name = func_name, |
| 178 | + }) |
| 179 | + end |
| 180 | + return call_specs |
97 | 181 | end |
98 | 182 |
|
99 | 183 | return M |
0 commit comments