Skip to content

Commit 2549e36

Browse files
committed
fix: use correct query for function with 0 or more comments before
1 parent ecfabfb commit 2549e36

File tree

15 files changed

+146
-103
lines changed

15 files changed

+146
-103
lines changed

lua/refactoring/refactor/extract_func.lua

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -583,14 +583,13 @@ end
583583
---@param config refactor.Config
584584
M.extract_func = function(range_type, config)
585585
local get_extracted_range = require("refactoring.utils").get_extracted_range
586-
local get_extracted_lines = require("refactoring.utils").get_extracted_lines
587586
local input = require("refactoring.utils").input
588587

589588
local opts = config.refactor.extract_func
590589

591590
local buf = api.nvim_get_current_buf()
592591
local extracted_range = get_extracted_range(buf, range_type)
593-
local lines = get_extracted_lines(range_type)
592+
local lines = vim.fn.getregion(vim.fn.getpos "'[", vim.fn.getpos "']", { type = range_type })
594593

595594
local task = async.run(function()
596595
local fn_name = opts.input and table.remove(opts.input, 1) or input { prompt = "Function name: " }
@@ -612,14 +611,13 @@ end
612611
---@param config refactor.Config
613612
M.extract_func_to_file = function(range_type, config)
614613
local get_extracted_range = require("refactoring.utils").get_extracted_range
615-
local get_extracted_lines = require("refactoring.utils").get_extracted_lines
616614
local input = require("refactoring.utils").input
617615

618616
local opts = config.refactor.extract_func
619617

620618
local buf = api.nvim_get_current_buf()
621619
local extracted_range = get_extracted_range(buf, range_type)
622-
local lines = get_extracted_lines(range_type)
620+
local lines = vim.fn.getregion(vim.fn.getpos "'[", vim.fn.getpos "']", { type = range_type })
623621

624622
local task = async.run(function()
625623
local file_name = opts.input and table.remove(opts.input)

lua/refactoring/refactor/inline_func.lua

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,8 @@ function M.inline_func(_, config)
201201
local definitions = unpack(results[1]) ---@type refactor.QfItem[]
202202
local references = unpack(results[2]) ---@type refactor.QfItem[]
203203

204-
local match_info = get_processed_match_info(definitions, references, lang)
205-
if not match_info then return end
204+
local match_info_by_buf = get_processed_match_info(definitions, references, lang)
205+
if not match_info_by_buf then return end
206206

207207
---@class refactor.inline_func.DefinitionWithFunctionInfo
208208
---@field definition refactor.QfItem
@@ -215,7 +215,7 @@ function M.inline_func(_, config)
215215
function(d)
216216
local buf = vim.fn.bufadd(d.filename)
217217

218-
local match_info = match_info[buf]
218+
local match_info = match_info_by_buf[buf]
219219
-- TODO: add range.vimscript
220220
local d_range = range(d.lnum - 1, d.col - 1, d.end_lnum - 1, d.end_col - 1, { buf = buf })
221221
local function_info = iter(match_info.functions):find(
@@ -266,14 +266,18 @@ function M.inline_func(_, config)
266266
---@field reference refactor.QfItem
267267
---@field function_call_info refactor.FunctionCallInfo
268268

269+
-- TODO: some LSPs (like lua_ls) may give a reference to a symbol that is
270+
-- not a function_call (i.e. the variable declaration on `require`). Maybe
271+
-- give a warning and do nothing if there are no
272+
-- `references_with_function_call_info` (?
269273
---@type refactor.inline_func.ReferenceWithFunctionCallInfo[]
270274
local references_with_function_call_info = iter(references)
271275
:map(
272276
---@param r refactor.QfItem
273277
function(r)
274278
local buf = vim.fn.bufadd(r.filename)
275279

276-
local match_info = match_info[buf]
280+
local match_info = match_info_by_buf[buf]
277281
-- TODO: add range.vimscript
278282
local r_range = range(r.lnum - 1, r.col - 1, r.end_lnum - 1, r.end_col - 1, { buf = buf })
279283
local function_call_info = iter(match_info.function_calls):find(
@@ -303,9 +307,12 @@ function M.inline_func(_, config)
303307
else
304308
body_end_row, body_end_col = function_info.returns_info[1]["return"]:start()
305309
end
310+
local body_range = range(body_start_row, body_start_col, body_end_row, body_end_col, { buf = in_buf })
311+
local b_srow, b_scol, b_erow, b_ecol = body_range:to_extmark()
306312

307-
local body_lines_without_return =
308-
api.nvim_buf_get_text(in_buf, body_start_row, body_start_col, body_end_row, body_end_col, {})
313+
local body_lines_without_return = api.nvim_buf_get_text(in_buf, b_srow, b_scol, b_erow, b_ecol, {})
314+
-- NOTE: can't use `indent()` because first line may not have any indent because of treesitter
315+
-- TODO: use `indent()` without the first line and then add the first line
309316
local body_without_return = iter(body_lines_without_return)
310317
:map(
311318
---@param line string

lua/refactoring/refactor/inline_var.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,16 +173,16 @@ function M.inline_var(_, config)
173173
local definitions = unpack(results[1]) ---@type refactor.QfItem[]
174174
local references = unpack(results[2]) ---@type refactor.QfItem[]
175175

176-
local match_info = get_match_info(definitions, references, lang)
177-
if not match_info then return end
176+
local match_info_by_buf = get_match_info(definitions, references, lang)
177+
if not match_info_by_buf then return end
178178

179179
---@type {definition: refactor.QfItem, info: refactor.ProcessedVariableInfo}[]
180180
local definitions_with_info = iter(definitions)
181181
:map(
182182
---@param d refactor.QfItem
183183
function(d)
184184
local definition_buf = vim.fn.bufadd(d.filename)
185-
local variables_info = match_info[definition_buf].variables
185+
local variables_info = match_info_by_buf[definition_buf].variables
186186
local definition_info = get_definition_info(d, variables_info)
187187
return { definition = d, info = definition_info }
188188
end
@@ -242,7 +242,7 @@ function M.inline_var(_, config)
242242
-- TODO: add range.vimscript
243243
local reference_range = range(r.lnum - 1, r.col - 1, r.end_lnum - 1, r.end_col - 1, { buf = reference_buf })
244244

245-
local references_info = match_info[reference_buf].references
245+
local references_info = match_info_by_buf[reference_buf].references
246246
local reference_info = iter(references_info):find(
247247
---@param ri refactor.ReferenceInfo
248248
function(ri)

lua/refactoring/utils.lua

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -297,15 +297,6 @@ function M.get_extracted_range(buf, range_type)
297297
return range(range_start[1], range_start[2], range_end[1], range_end[2], { buf = buf })
298298
end
299299

300-
-- TODO: rename this everywhere (and the resulting var) to a more general name.
301-
-- `extracted_range` comes from the `extratc_func` refactor, but it's now used
302-
-- in multiple (all?) refactors
303-
-- TODO: inline this instead?
304-
---@param range_type 'v'|'V'|''
305-
function M.get_extracted_lines(range_type)
306-
return vim.fn.getregion(vim.fn.getpos "'[", vim.fn.getpos "']", { type = range_type })
307-
end
308-
309300
-- TODO: maybe use Info sufix for all of these types
310301
---@class refactor.TsInfo
311302
---@field debug_paths refactor.DebugPath[]

queries/c/extract_func.scm

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,7 @@
8484
(_) @scope)) @scope.outside
8585

8686
(translation_unit
87-
((comment)* @output.comment
88-
(function_definition) @output.function))
87+
_*
88+
(comment)* @output.comment
89+
.
90+
(function_definition) @output.function)

queries/c_sharp/extract_func.scm

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,20 @@
4646
(_) @scope.inside)) @scope
4747

4848
(compilation_unit
49-
((comment)* @output.comment
50-
(global_statement
51-
(local_function_statement) @output.function)))
49+
_*
50+
(comment)* @output.comment
51+
.
52+
(global_statement
53+
(local_function_statement) @output.function))
5254

5355
(compilation_unit
5456
(class_declaration
5557
(declaration_list
56-
((comment)* @output.comment
57-
[
58-
(method_declaration)
59-
(constructor_declaration)
60-
] @output.function)))
58+
_*
59+
(comment)* @output.comment
60+
.
61+
[
62+
(method_declaration)
63+
(constructor_declaration)
64+
] @output.function))
6165
(#set! method true))

queries/ecma/extract_func.scm

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -90,26 +90,34 @@
9090

9191
; function a() {}
9292
(program
93-
((comment)* @output.comment
94-
(function_declaration) @output.function))
93+
_*
94+
(comment)* @output.comment
95+
.
96+
(function_declaration) @output.function)
9597

9698
; const a = ()=>{}
9799
(program
98-
((comment)* @output.comment
99-
(lexical_declaration
100-
(variable_declarator
101-
(arrow_function))) @output.function))
100+
_*
101+
(comment)* @output.comment
102+
.
103+
(lexical_declaration
104+
(variable_declarator
105+
(arrow_function))) @output.function)
102106

103107
; a = ()=>{}
104108
(program
105-
((comment)* @output.comment
106-
(expression_statement
107-
(assignment_expression
108-
(arrow_function))) @output.function))
109+
_*
110+
(comment)* @output.comment
111+
.
112+
(expression_statement
113+
(assignment_expression
114+
(arrow_function))) @output.function)
109115

110116
(program
111117
(class_declaration
112118
(class_body
113-
((comment)* @output.comment
114-
(method_definition) @output.function)))
119+
_*
120+
(comment)* @output.comment
121+
.
122+
(method_definition) @output.function))
115123
(#set! method true))

queries/go/extract_func.scm

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,20 @@
119119
(_) @scope.inside)) @scope
120120

121121
(source_file
122-
((comment)* @output.comment
123-
(function_declaration) @output.function))
122+
_*
123+
(comment)* @output.comment
124+
.
125+
(function_declaration) @output.function)
124126

125127
(source_file
126-
((comment)* @output.comment
127-
(method_declaration
128-
receiver: (parameter_list
129-
(parameter_declaration
130-
name: (identifier) @_struct_var_name
131-
type: (pointer_type
132-
(type_identifier) @_struct_name)))) @output.function)
128+
_*
129+
(comment)* @output.comment
130+
.
131+
(method_declaration
132+
receiver: (parameter_list
133+
(parameter_declaration
134+
name: (identifier) @_struct_var_name
135+
type: (pointer_type
136+
(type_identifier) @_struct_name)))) @output.function
133137
(#set! struct_name @_struct_name)
134138
(#set! struct_var_name @_struct_var_name))

queries/lua/extract_func.scm

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
; local a = function() end
22
(chunk
3-
((comment)* @output.comment
4-
(variable_declaration
5-
(assignment_statement
6-
(expression_list
7-
(function_definition)))) @output.function))
3+
_*
4+
(comment)* @output.comment
5+
.
6+
(variable_declaration
7+
(assignment_statement
8+
(expression_list
9+
(function_definition)))) @output.function)
810

911
; a = function() end
1012
(chunk
11-
((comment)* @output.comment
12-
(assignment_statement
13-
(expression_list
14-
(function_definition))) @output.function))
13+
_*
14+
(comment)* @output.comment
15+
(assignment_statement
16+
(expression_list
17+
(function_definition))) @output.function)
1518

1619
; function a() end
1720
(chunk
18-
((comment)* @output.comment
19-
(function_declaration) @output.function))
21+
_*
22+
(comment)* @output.comment
23+
.
24+
(function_declaration) @output.function)
2025

2126
; foo = bar
2227
(assignment_statement

queries/lua/inline_func.scm

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
((comment)* @function.comment
2+
.
23
(function_declaration
34
parameters: (parameters
45
(identifier) @function.arg
@@ -7,6 +8,7 @@
78
body: (block) @function.body) @function)
89

910
((comment)* @function.comment
11+
.
1012
(variable_declaration
1113
(assignment_statement
1214
(expression_list
@@ -19,13 +21,13 @@
1921

2022
(return_statement
2123
(expression_list
22-
(identifier) @return.value
24+
(_) @return.value
2325
(","
24-
(identifier) @return.value)*)?) @return
26+
(_) @return.value)*)?) @return
2527

2628
; b()
2729
((function_call
28-
name: (identifier) @function_call.name
30+
name: (_) @function_call.name
2931
arguments: (arguments
3032
.
3133
(_) @function_call.arg
@@ -37,12 +39,12 @@
3739
; a = b()
3840
((assignment_statement
3941
(variable_list
40-
name: (identifier) @function_call.return_value
42+
name: (_) @function_call.return_value
4143
(","
42-
name: (identifier) @function_call.return_value)*)
44+
name: (_) @function_call.return_value)*)
4345
(expression_list
4446
value: (function_call
45-
name: (identifier) @function_call.name
47+
name: (_) @function_call.name
4648
arguments: (arguments
4749
.
4850
(_) @function_call.arg
@@ -55,12 +57,12 @@
5557
(variable_declaration
5658
(assignment_statement
5759
(variable_list
58-
name: (identifier) @function_call.return_value
60+
name: (_) @function_call.return_value
5961
(","
60-
name: (identifier) @function_call.return_value)*)
62+
name: (_) @function_call.return_value)*)
6163
(expression_list
6264
value: (function_call
63-
name: (identifier) @function_call.name
65+
name: (_) @function_call.name
6466
arguments: (arguments
6567
.
6668
(_) @function_call.arg

0 commit comments

Comments
 (0)