Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ are supported (with individual support for each function may vary):
- **Print var:** Automated insertion of print statement to print a variable
at a given point in the code. This map can be made with either visual or
normal mode:
- Using this function in normal mode will automatically find the variable
under the cursor and print it.
- Using this function in visual mode will print out whatever is in the
visual selection.
- Setting `{ normal = true }` to the function will automatically find the variable
under the cursor and print it from normal mode without needing visual mode at all
- **Cleanup:** Automated cleanup of all print statements generated by the
plugin

Expand Down Expand Up @@ -294,5 +294,9 @@ require('refactoring').setup({
c = true,
java = true,
},
-- custom print_var_statements by language
print_var_statements = {}
-- custom printf_statements by language
printf_statements = {}
})
```
38 changes: 17 additions & 21 deletions lua/refactoring/debug/print_var.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,33 @@ local debug_utils = require("refactoring.debug.debug_utils")
local ensure_code_gen = require("refactoring.tasks.ensure_code_gen")
local get_select_input = require("refactoring.get_select_input")

local function get_variable()
local variable_region = Region:from_current_selection()
if
variable_region.start_col == 0
and variable_region.end_col == 0
and variable_region.start_row == 0
and variable_region.end_row == 0
then
local function get_variable(opts, point)
if opts.normal then
local bufnr = 0
local root_lang_tree = parsers.get_parser(bufnr)
local current_pos = Point:from_cursor()
local line = current_pos.row
local col = current_pos.col
variable_region.start_row = current_pos.row
local row = point.row
local col = point.col
local lang_tree = root_lang_tree:language_for_range({
current_pos.row,
current_pos.col,
current_pos.row,
current_pos.col,
point.row,
point.col,
point.row,
point.col,
})
for _, tree in ipairs(lang_tree:trees()) do
local root = tree:root()
if
root and ts_utils.is_in_node_range(root, current_pos.row, col)
then
root:named_descendant_for_range(line, col, line, col)
if root and ts_utils.is_in_node_range(root, row, col) then
root:named_descendant_for_range(row, col, row, col)
end
end
local node = ts_utils.get_node_at_cursor()
local filetype = vim.bo[bufnr].filetype
-- TODO: Can we do something with treesitter files here?
if filetype == "php" then
return "$" .. ts_utils.get_node_text(node)[1]
end
return ts_utils.get_node_text(node)[1]
end
local variable_region = Region:from_current_selection()
return variable_region:get_text()[1]
end

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

if opts.normal == nil then
opts.normal = false
end

-- Get variable text
local variable = get_variable()
local variable = get_variable(opts, point)
local indent
if refactor.ts.allows_indenting_task then
local indent_amount = get_indent_amount(refactor)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
true
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
true
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
true
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
true
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
true
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
true
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
true
63 changes: 45 additions & 18 deletions lua/refactoring/tests/debug_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,25 @@ local function set_config_options(filename_prefix, filename_extension)
string.format("%s.config", filename_prefix)
)

local filetypes = {
["ts"] = "typescript",
["js"] = "javascript",
["py"] = "python",
}

-- get the real filetype from the above table if possible
local real_filetype = filetypes[filename_extension]
or filename_extension

local printf_statements = {}
printf_statements[real_filetype] = { config_values[1] }
Config:get():set_printf_statements(printf_statements)

local print_var_statements = {}
print_var_statements[real_filetype] = { config_values[1] }
Config:get():set_print_var_statements(print_var_statements)
if config_values[1] ~= "" then
local filetypes = {
["ts"] = "typescript",
["js"] = "javascript",
["py"] = "python",
}

-- get the real filetype from the above table if possible
local real_filetype = filetypes[filename_extension]
or filename_extension

local printf_statements = {}
printf_statements[real_filetype] = { config_values[1] }
Config:get():set_printf_statements(printf_statements)

local print_var_statements = {}
print_var_statements[real_filetype] = { config_values[1] }
Config:get():set_print_var_statements(print_var_statements)
end
end
end

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

local function get_func_opts(filename_prefix)
local opts_file_name = string.format("%s.opts", filename_prefix)

local opts_file = Path:new(
cwd,
"lua",
"refactoring",
"tests",
opts_file_name
)

local opts = {}
if opts_file:exists() then
local opts_values = test_utils.get_contents(opts_file_name)

if opts_values[1] ~= nil then
opts["normal"] = true
end
end

return opts
end

describe("Debug", function()
for_each_file(function(file)
a.it(string.format("printf: %s", file), function()
Expand All @@ -102,7 +127,9 @@ describe("Debug", function()
test_utils.run_commands(filename_prefix)
Config.get():set_test_bufnr(bufnr)

debug[debug_operation]({})
local func_opts = get_func_opts(filename_prefix)

debug[debug_operation](func_opts)
async.util.scheduler()
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
eq(expected, lines)
Expand Down