|
59 | 59 | ---@return {method: boolean?, singleton: boolean?, struct_name: string?, struct_var_name: string?} |
60 | 60 | local function get_output_node(nested_lang_tree, query, buf, extracted_range) |
61 | 61 | local is_first_closer = require("refactoring.utils").is_first_closer |
| 62 | + local get_ts_info = require("refactoring.utils").get_ts_info |
62 | 63 |
|
63 | | - local outputs = {} ---@type refactor.Output[] |
64 | | - for _, tree in ipairs(nested_lang_tree:trees()) do |
65 | | - for _, match, metadata in query:iter_matches(tree:root(), buf) do |
66 | | - local output ---@type table|refactor.Output|nil |
67 | | - for capture_id, nodes in pairs(match) do |
68 | | - local name = query.captures[capture_id] |
69 | | - |
70 | | - -- TODO: split input.info and output location |
71 | | - if name == "output.comment" then |
72 | | - output = output or {} |
73 | | - output.comment = nodes |
74 | | - elseif name == "output.function" then |
75 | | - output = output or {} |
76 | | - output.fn = nodes[1] |
77 | | - output.method = metadata.method ~= nil |
78 | | - output.singleton = metadata.singleton ~= nil |
79 | | - |
80 | | - local struct_name = metadata.struct_name |
81 | | - if struct_name then output.struct_name = ts.get_node_text(match[struct_name][1], buf) end |
82 | | - local struct_var_name = metadata.struct_var_name |
83 | | - if struct_var_name then output.struct_var_name = ts.get_node_text(match[struct_var_name][1], buf) end |
84 | | - end |
85 | | - end |
86 | | - if output then table.insert(outputs, output) end |
87 | | - end |
88 | | - end |
| 64 | + local ts_info = get_ts_info(buf, nested_lang_tree, query) |
| 65 | + local outputs = ts_info.outputs |
89 | 66 |
|
90 | 67 | local extracted_start_pos = pos(extracted_range.start_row, extracted_range.start_col, { buf = extracted_range.buf }) |
91 | 68 | ---@type refactor.Output|nil |
@@ -180,6 +157,7 @@ local function extract_func(opts) |
180 | 157 | local get_declarations_by_scope = require("refactoring.utils").get_declarations_by_scope |
181 | 158 | local scopes_for_range = require("refactoring.utils").scopes_for_range |
182 | 159 | local get_declaration_scope = require("refactoring.utils").get_declaration_scope |
| 160 | + local get_ts_info = require("refactoring.utils").get_ts_info |
183 | 161 |
|
184 | 162 | local code_generation = opts.config_opts.code_generation |
185 | 163 |
|
@@ -220,30 +198,19 @@ local function extract_func(opts) |
220 | 198 | output_range = range.extmark(0, 0, 0, 0, { buf = out_buf }) |
221 | 199 | end |
222 | 200 |
|
223 | | - local references_info = {} ---@type refactor.ReferenceInfo[] |
224 | | - local scopes = {} ---@type TSNode[] |
225 | | - for _, tree in ipairs(nested_lang_tree:trees()) do |
226 | | - for _, match, metadata in in_query:iter_matches(tree:root(), in_buf) do |
227 | | - for capture_id, nodes in pairs(match) do |
228 | | - local name = in_query.captures[capture_id] |
229 | | - if name == "reference.identifier" then |
230 | | - for i, node in ipairs(nodes) do |
231 | | - table.insert(references_info, { |
232 | | - identifier = node, |
233 | | - reference_type = metadata.reference_type, |
234 | | - type = metadata.types and metadata.types[i], |
235 | | - declaration = metadata.declaration ~= nil, |
236 | | - }) |
237 | | - end |
238 | | - elseif name == "scope" then |
239 | | - for _, node in ipairs(nodes) do |
240 | | - table.insert(scopes, node) |
241 | | - end |
242 | | - end |
| 201 | + local in_ts_info = get_ts_info(in_buf, nested_lang_tree, in_query) |
| 202 | + local references_info = in_ts_info.references |
| 203 | + -- TODO: modify the util functions that use `scopes` as TSNode[] to use |
| 204 | + -- refactor.Scope[] instead? |
| 205 | + ---@type TSNode[] |
| 206 | + local scopes = iter(in_ts_info.scopes) |
| 207 | + :map( |
| 208 | + ---@param scope refactor.Scope |
| 209 | + function(scope) |
| 210 | + return scope.scope |
243 | 211 | end |
244 | | - end |
245 | | - end |
246 | | - -- TODO: maybe check that all the treesitter captures are not empty(? |
| 212 | + ) |
| 213 | + :totable() |
247 | 214 |
|
248 | 215 | local scopes_for_extracted_range = scopes_for_range(in_buf, scopes, extracted_range) |
249 | 216 |
|
@@ -272,7 +239,6 @@ local function extract_func(opts) |
272 | 239 | ---@param a refactor.ReferenceInfo |
273 | 240 | ---@param b refactor.ReferenceInfo |
274 | 241 | function(a, b) |
275 | | - -- TODO: don't I already have a function to sort nodes in utils? |
276 | 242 | local a_srow, a_scol, a_erow, a_ecol = a.identifier:range() |
277 | 243 | local a_range = range(a_srow, a_scol, a_erow, a_ecol, { buf = in_buf }) |
278 | 244 | local b_srow, b_scol, b_erow, b_ecol = b.identifier:range() |
|
0 commit comments