|
| 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