Skip to content

Commit 7328413

Browse files
authored
Merge pull request #322 from teddylear/print-var-norm-mode-split-cmd
feat: add option for print_var for normal mode attempt to find var under cursor
2 parents b220562 + 2cc1ffe commit 7328413

File tree

10 files changed

+75
-41
lines changed

10 files changed

+75
-41
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,10 @@ are supported (with individual support for each function may vary):
101101
- **Print var:** Automated insertion of print statement to print a variable
102102
at a given point in the code. This map can be made with either visual or
103103
normal mode:
104-
- Using this function in normal mode will automatically find the variable
105-
under the cursor and print it.
106104
- Using this function in visual mode will print out whatever is in the
107105
visual selection.
106+
- Setting `{ normal = true }` to the function will automatically find the variable
107+
under the cursor and print it from normal mode without needing visual mode at all
108108
- **Cleanup:** Automated cleanup of all print statements generated by the
109109
plugin
110110

@@ -294,5 +294,9 @@ require('refactoring').setup({
294294
c = true,
295295
java = true,
296296
},
297+
-- custom print_var_statements by language
298+
print_var_statements = {}
299+
-- custom printf_statements by language
300+
printf_statements = {}
297301
})
298302
```

lua/refactoring/debug/print_var.lua

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,33 @@ local debug_utils = require("refactoring.debug.debug_utils")
1010
local ensure_code_gen = require("refactoring.tasks.ensure_code_gen")
1111
local get_select_input = require("refactoring.get_select_input")
1212

13-
local function get_variable()
14-
local variable_region = Region:from_current_selection()
15-
if
16-
variable_region.start_col == 0
17-
and variable_region.end_col == 0
18-
and variable_region.start_row == 0
19-
and variable_region.end_row == 0
20-
then
13+
local function get_variable(opts, point)
14+
if opts.normal then
2115
local bufnr = 0
2216
local root_lang_tree = parsers.get_parser(bufnr)
23-
local current_pos = Point:from_cursor()
24-
local line = current_pos.row
25-
local col = current_pos.col
26-
variable_region.start_row = current_pos.row
17+
local row = point.row
18+
local col = point.col
2719
local lang_tree = root_lang_tree:language_for_range({
28-
current_pos.row,
29-
current_pos.col,
30-
current_pos.row,
31-
current_pos.col,
20+
point.row,
21+
point.col,
22+
point.row,
23+
point.col,
3224
})
3325
for _, tree in ipairs(lang_tree:trees()) do
3426
local root = tree:root()
35-
if
36-
root and ts_utils.is_in_node_range(root, current_pos.row, col)
37-
then
38-
root:named_descendant_for_range(line, col, line, col)
27+
if root and ts_utils.is_in_node_range(root, row, col) then
28+
root:named_descendant_for_range(row, col, row, col)
3929
end
4030
end
4131
local node = ts_utils.get_node_at_cursor()
4232
local filetype = vim.bo[bufnr].filetype
33+
-- TODO: Can we do something with treesitter files here?
4334
if filetype == "php" then
4435
return "$" .. ts_utils.get_node_text(node)[1]
4536
end
4637
return ts_utils.get_node_text(node)[1]
4738
end
39+
local variable_region = Region:from_current_selection()
4840
return variable_region:get_text()[1]
4941
end
5042

@@ -66,8 +58,12 @@ local function printDebug(bufnr, config)
6658
opts.below = true
6759
point.col = opts.below and 100000 or 1
6860

61+
if opts.normal == nil then
62+
opts.normal = false
63+
end
64+
6965
-- Get variable text
70-
local variable = get_variable()
66+
local variable = get_variable(opts, point)
7167
local indent
7268
if refactor.ts.allows_indenting_task then
7369
local indent_amount = get_indent_amount(refactor)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
true
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
true
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
true
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
true
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
true
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
true
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
true

lua/refactoring/tests/debug_spec.lua

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,25 @@ local function set_config_options(filename_prefix, filename_extension)
3434
string.format("%s.config", filename_prefix)
3535
)
3636

37-
local filetypes = {
38-
["ts"] = "typescript",
39-
["js"] = "javascript",
40-
["py"] = "python",
41-
}
42-
43-
-- get the real filetype from the above table if possible
44-
local real_filetype = filetypes[filename_extension]
45-
or filename_extension
46-
47-
local printf_statements = {}
48-
printf_statements[real_filetype] = { config_values[1] }
49-
Config:get():set_printf_statements(printf_statements)
50-
51-
local print_var_statements = {}
52-
print_var_statements[real_filetype] = { config_values[1] }
53-
Config:get():set_print_var_statements(print_var_statements)
37+
if config_values[1] ~= "" then
38+
local filetypes = {
39+
["ts"] = "typescript",
40+
["js"] = "javascript",
41+
["py"] = "python",
42+
}
43+
44+
-- get the real filetype from the above table if possible
45+
local real_filetype = filetypes[filename_extension]
46+
or filename_extension
47+
48+
local printf_statements = {}
49+
printf_statements[real_filetype] = { config_values[1] }
50+
Config:get():set_printf_statements(printf_statements)
51+
52+
local print_var_statements = {}
53+
print_var_statements[real_filetype] = { config_values[1] }
54+
Config:get():set_print_var_statements(print_var_statements)
55+
end
5456
end
5557
end
5658

@@ -77,6 +79,29 @@ local function get_debug_operation(path)
7779
return temp[#temp]
7880
end
7981

82+
local function get_func_opts(filename_prefix)
83+
local opts_file_name = string.format("%s.opts", filename_prefix)
84+
85+
local opts_file = Path:new(
86+
cwd,
87+
"lua",
88+
"refactoring",
89+
"tests",
90+
opts_file_name
91+
)
92+
93+
local opts = {}
94+
if opts_file:exists() then
95+
local opts_values = test_utils.get_contents(opts_file_name)
96+
97+
if opts_values[1] ~= nil then
98+
opts["normal"] = true
99+
end
100+
end
101+
102+
return opts
103+
end
104+
80105
describe("Debug", function()
81106
for_each_file(function(file)
82107
a.it(string.format("printf: %s", file), function()
@@ -102,7 +127,9 @@ describe("Debug", function()
102127
test_utils.run_commands(filename_prefix)
103128
Config.get():set_test_bufnr(bufnr)
104129

105-
debug[debug_operation]({})
130+
local func_opts = get_func_opts(filename_prefix)
131+
132+
debug[debug_operation](func_opts)
106133
async.util.scheduler()
107134
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
108135
eq(expected, lines)

0 commit comments

Comments
 (0)