Skip to content

Commit f5f00dd

Browse files
committed
feat: Split print_var and print_var_norm behaviors so we can iterate on print_var_norm
1 parent 802667c commit f5f00dd

25 files changed

+141
-36
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ supported (with individual support for each function may vary):
8686

8787
- Also comes with various useful features for debugging
8888
- **Printf:** Automated insertion of print statement to mark the calling of a function
89-
- **Print var:** Automated insertion of print statement to print a variable at a given point in the code
89+
- **Print var:** Automated insertion of print statement to print a variable at a given point in the code from highlight text in visual mode
90+
- This function would be `print_var`
91+
- **Print var norm:** Automated insertion of print statement to print a variable under cursor at a given point in the code in normal mode
92+
- This function would be `print_var_norm`
9093
- **Cleanup:** Automated cleanup of all print statements generated by the plugin
9194

9295
## Configuration<a name="configuration"></a>

lua/refactoring/debug/init.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
local Config = require("refactoring.config")
22
local printf = require("refactoring.debug.printf")
33
local print_var = require("refactoring.debug.print_var")
4+
local print_var_norm = require("refactoring.debug.print_var_norm")
45
local get_path = require("refactoring.debug.get_path")
56
local cleanup = require("refactoring.debug.cleanup")
67

@@ -16,6 +17,11 @@ function M.print_var(opts)
1617
return print_var(vim.api.nvim_get_current_buf(), config)
1718
end
1819

20+
function M.print_var_norm(opts)
21+
local config = Config.get():merge(opts)
22+
return print_var_norm(vim.api.nvim_get_current_buf(), config)
23+
end
24+
1925
function M.cleanup(opts)
2026
local config = Config.get():merge(opts)
2127
return cleanup(vim.api.nvim_get_current_buf(), config)

lua/refactoring/debug/print_var.lua

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,12 @@ local Region = require("refactoring.region")
44
local refactor_setup = require("refactoring.tasks.refactor_setup")
55
local post_refactor = require("refactoring.tasks.post_refactor")
66
local lsp_utils = require("refactoring.lsp_utils")
7-
local ts_utils = require("nvim-treesitter.ts_utils")
8-
local parsers = require("nvim-treesitter.parsers")
97
local debug_utils = require("refactoring.debug.debug_utils")
108
local ensure_code_gen = require("refactoring.tasks.ensure_code_gen")
119
local get_select_input = require("refactoring.get_select_input")
1210

1311
local function get_variable()
1412
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
21-
local bufnr = 0
22-
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
27-
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,
32-
})
33-
for _, tree in ipairs(lang_tree:trees()) do
34-
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)
39-
end
40-
end
41-
local node = ts_utils.get_node_at_cursor()
42-
local filetype = vim.bo[bufnr].filetype
43-
if filetype == "php" then
44-
return "$" .. ts_utils.get_node_text(node)[1]
45-
end
46-
return ts_utils.get_node_text(node)[1]
47-
end
4813
return variable_region:get_text()[1]
4914
end
5015

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
local Pipeline = require("refactoring.pipeline")
2+
local Point = require("refactoring.point")
3+
local Region = require("refactoring.region")
4+
local refactor_setup = require("refactoring.tasks.refactor_setup")
5+
local post_refactor = require("refactoring.tasks.post_refactor")
6+
local lsp_utils = require("refactoring.lsp_utils")
7+
local ts_utils = require("nvim-treesitter.ts_utils")
8+
local parsers = require("nvim-treesitter.parsers")
9+
local debug_utils = require("refactoring.debug.debug_utils")
10+
local ensure_code_gen = require("refactoring.tasks.ensure_code_gen")
11+
local get_select_input = require("refactoring.get_select_input")
12+
13+
local function get_variable()
14+
local variable_region = Region:from_current_selection()
15+
local bufnr = 0
16+
local root_lang_tree = parsers.get_parser(bufnr)
17+
local current_pos = Point:from_cursor()
18+
local line = current_pos.row
19+
local col = current_pos.col
20+
variable_region.start_row = current_pos.row
21+
local lang_tree = root_lang_tree:language_for_range({
22+
current_pos.row,
23+
current_pos.col,
24+
current_pos.row,
25+
current_pos.col,
26+
})
27+
for _, tree in ipairs(lang_tree:trees()) do
28+
local root = tree:root()
29+
if root and ts_utils.is_in_node_range(root, current_pos.row, col) then
30+
root:named_descendant_for_range(line, col, line, col)
31+
end
32+
end
33+
local node = ts_utils.get_node_at_cursor()
34+
local filetype = vim.bo[bufnr].filetype
35+
if filetype == "php" then
36+
return "$" .. ts_utils.get_node_text(node)[1]
37+
end
38+
return ts_utils.get_node_text(node)[1]
39+
end
40+
41+
local function get_indent_amount(refactor)
42+
return refactor.whitespace.cursor / refactor.whitespace.tabstop
43+
end
44+
45+
local function printDebug(bufnr, config)
46+
return Pipeline
47+
:from_task(refactor_setup(bufnr, config))
48+
:add_task(function(refactor)
49+
return ensure_code_gen(refactor, { "print_var", "comment" })
50+
end)
51+
:add_task(function(refactor)
52+
local opts = refactor.config:get()
53+
local point = Point:from_cursor()
54+
55+
-- always go below for text
56+
opts.below = true
57+
point.col = opts.below and 100000 or 1
58+
59+
-- Get variable text
60+
local variable = get_variable()
61+
local indent
62+
if refactor.ts.allows_indenting_task then
63+
local indent_amount = get_indent_amount(refactor)
64+
indent = refactor.code.indent({
65+
indent_width = refactor.whitespace.tabstop,
66+
indent_amount = indent_amount,
67+
})
68+
end
69+
70+
local debug_path = debug_utils.get_debug_path(refactor, point)
71+
local prefix = string.format("%s %s:", debug_path, variable)
72+
73+
local default_print_var_statement =
74+
refactor.code.default_print_var_statement()
75+
76+
local custom_print_var_statements =
77+
opts.print_var_statements[refactor.filetype]
78+
79+
local print_var_statement
80+
81+
if custom_print_var_statements then
82+
if #custom_print_var_statements > 1 then
83+
print_var_statement = get_select_input(
84+
custom_print_var_statements,
85+
"print_var: Select a statement to insert:",
86+
function(item)
87+
return item
88+
end
89+
)
90+
else
91+
print_var_statement = custom_print_var_statements[1]
92+
end
93+
else
94+
print_var_statement = default_print_var_statement[1]
95+
end
96+
97+
local print_var_opts = {
98+
statement = print_var_statement,
99+
prefix = prefix,
100+
var = variable,
101+
}
102+
103+
local print_statement = refactor.code.print_var(print_var_opts)
104+
105+
local statement
106+
if indent ~= nil then
107+
local temp = {}
108+
temp[1] = indent
109+
temp[2] = print_statement
110+
statement = table.concat(temp, "")
111+
else
112+
statement = print_statement
113+
end
114+
115+
refactor.text_edits = {
116+
lsp_utils.insert_new_line_text(
117+
Region:from_point(point),
118+
statement
119+
.. " "
120+
.. refactor.code.comment("__AUTO_GENERATED_PRINT_VAR__"),
121+
opts
122+
),
123+
}
124+
125+
return true, refactor
126+
end)
127+
:after(post_refactor.post_refactor)
128+
:run()
129+
end
130+
131+
return printDebug

0 commit comments

Comments
 (0)