Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
TheLeoP committed May 26, 2023
1 parent 408c695 commit 4d4873c
Show file tree
Hide file tree
Showing 30 changed files with 127 additions and 71 deletions.
8 changes: 5 additions & 3 deletions lua/refactoring/debug/cleanup.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ local Region = require("refactoring.region")
local lsp_utils = require("refactoring.lsp_utils")
local post_refactor = require("refactoring.tasks.post_refactor")

local MAX_COL = 100000

local function cleanup(bufnr, config)
return Pipeline:from_task(refactor_setup(bufnr, config))
:add_task(
Expand All @@ -28,9 +30,9 @@ local function cleanup(bufnr, config)
region = Region:from_values(
bufnr,
row_num - 1,
100000,
MAX_COL,
row_num,
100000
MAX_COL
)
else
print(
Expand All @@ -41,7 +43,7 @@ local function cleanup(bufnr, config)
row_num,
1,
row_num,
100000
MAX_COL
)
end

Expand Down
4 changes: 3 additions & 1 deletion lua/refactoring/debug/print_var.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ local get_select_input = require("refactoring.get_select_input")
local utils = require("refactoring.utils")
local indent = require("refactoring.indent")

local MAX_COL = 100000

local function get_variable(opts, point)
if opts.normal then
local bufnr = 0
Expand Down Expand Up @@ -60,7 +62,7 @@ local function printDebug(bufnr, config)
opts.below = true
-- always go end for text
opts._end = true
point.col = opts.below and 100000 or 1
point.col = opts.below and MAX_COL or 1

if opts.normal == nil then
opts.normal = false
Expand Down
125 changes: 87 additions & 38 deletions lua/refactoring/debug/printf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ local ensure_code_gen = require("refactoring.tasks.ensure_code_gen")
local get_select_input = require("refactoring.get_select_input")
local indent = require("refactoring.indent")

local MAX_COL = 100000

local function printDebug(bufnr, config)
return Pipeline:from_task(refactor_setup(bufnr, config))
:add_task(
Expand All @@ -30,20 +32,7 @@ local function printDebug(bufnr, config)
-- set default `end` behavior
opts._end = opts.below

point.col = opts.below and 100000 or 1

local indentation
if refactor.ts:allows_indenting_task() then
local indent_amount = indent.buf_indent_amount(
refactor.cursor,
refactor,
opts.below,
refactor.bufnr
)
indentation = indent.indent(indent_amount, refactor.bufnr)
end

local debug_path = debug_utils.get_debug_path(refactor, point)
point.col = opts.below and MAX_COL or 1

local default_printf_statement =
refactor.code.default_printf_statement()
Expand All @@ -70,32 +59,92 @@ local function printDebug(bufnr, config)
printf_statement = default_printf_statement[1]
end

local printf_opts = {
statement = printf_statement,
content = debug_path,
}

local statement
if indentation ~= nil then
local temp = {}
temp[1] = indentation
temp[2] = refactor.code.print(printf_opts)
statement = table.concat(temp, "")
else
statement = refactor.code.print(printf_opts)
local empty_printf = refactor.code
.print({
statement = printf_statement,
content = "%d+",
})
:gsub("([%(%)])", "%%%1")
local debug_path = debug_utils.get_debug_path(refactor, point)

local text_to_count = debug_path ~= "" and debug_path
or empty_printf
text_to_count = text_to_count
.. ".*"
.. "__AUTO_GENERATED_PRINTF__"
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)

local current_lines_with_text = {}
for row_num, line in ipairs(lines) do
if string.find(line, text_to_count) ~= nil then
table.insert(current_lines_with_text, row_num)
end
end
local should_replace = vim.tbl_contains(
current_lines_with_text,
point.row
) and opts.below
table.insert(current_lines_with_text, point.row)
table.sort(current_lines_with_text)

refactor.text_edits = {}
for i, row_num in ipairs(current_lines_with_text) do
local content
if debug_path ~= "" then
content = table.concat({ debug_path, tostring(i) }, " ")
else
content = tostring(i)
end

local text = refactor.code.print({
statement = printf_statement,
content = content,
})
text = text
.. " "
.. refactor.code.comment("__AUTO_GENERATED_PRINTF__")

refactor.text_edits = {
lsp_utils.insert_new_line_text(
Region:from_point(point),
statement
.. " "
.. refactor.code.comment(
"__AUTO_GENERATED_PRINTF__"
),
opts
),
}
local indentation
if refactor.ts:allows_indenting_task() then
local indent_amount = indent.buf_indent_amount(
Point:from_values(row_num, MAX_COL),
refactor,
opts.below,
refactor.bufnr
)
indentation =
indent.indent(indent_amount, refactor.bufnr)
end
if indentation ~= nil then
text = table.concat({ indentation, text }, "")
end

if row_num == point.row and not should_replace then
should_replace = true

local range = Region:from_point(point, bufnr)
table.insert(
refactor.text_edits,
lsp_utils.insert_new_line_text(range, text, opts)
)
else
if row_num == point.row then
should_replace = false
end

local range = Region:from_values(
bufnr,
row_num,
1,
row_num,
MAX_COL
)
table.insert(
refactor.text_edits,
lsp_utils.replace_text(range, text)
)
end
end

return true, refactor
end
Expand Down
8 changes: 5 additions & 3 deletions lua/refactoring/lsp_utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ local Region = require("refactoring.region")

local M = {}

local MAX_COL = 10000

---@param pointOrRegion RefactorRegion|RefactorPoint
---@return RefactorRegion
local function to_region(pointOrRegion)
Expand Down Expand Up @@ -35,10 +37,10 @@ function M.insert_new_line_text(pointOrRegion, text, opts)
else
text = text .. code.new_line()
end
-- what is after? I just assume 10000 is equivalent

if opts._end then
region.start_col = 10000
region.end_col = 10000
region.start_col = MAX_COL
region.end_col = MAX_COL
else
region.start_col = 1
region.end_col = 1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
int main (int argc, char *argv[])
{
printf("debug path main"); // __AUTO_GENERATED_PRINTF__
printf("debug path main 1"); // __AUTO_GENERATED_PRINTF__
return 0;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
int main() {
return 0;
printf("main(%d): \n", __LINE__); // __AUTO_GENERATED_PRINTF__
printf("main 1(%d): \n", __LINE__); // __AUTO_GENERATED_PRINTF__
}

Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ class Test {

void Test::foo() {

printf("Test::foo(%d): \n", __LINE__); // __AUTO_GENERATED_PRINTF__
printf("Test::foo 1(%d): \n", __LINE__); // __AUTO_GENERATED_PRINTF__
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Test {
public:
~Test() {

printf("Test#~Test(%d): \n", __LINE__); // __AUTO_GENERATED_PRINTF__
printf("Test#~Test 1(%d): \n", __LINE__); // __AUTO_GENERATED_PRINTF__
}
};

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Test {
~Test() { }
void foo() {

printf("Test#foo(%d): \n", __LINE__); // __AUTO_GENERATED_PRINTF__
printf("Test#foo 1(%d): \n", __LINE__); // __AUTO_GENERATED_PRINTF__
}
};

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
int main (int argc, char *argv[])
{
printf("debug path main"); // __AUTO_GENERATED_PRINTF__
printf("debug path main 1"); // __AUTO_GENERATED_PRINTF__
return 0;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
int main() {
return 0;
printf("main(%d): \n", __LINE__); // __AUTO_GENERATED_PRINTF__
printf("main 1(%d): \n", __LINE__); // __AUTO_GENERATED_PRINTF__
}

Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import "fmt"

func main() {
fmt.Println("test")
fmt.Println("debug path main") // __AUTO_GENERATED_PRINTF__
fmt.Println("debug path main 1") // __AUTO_GENERATED_PRINTF__
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ func simple_function(a int) {
test_other := 1
for idx := test - 1; idx < test_other; idx++ {
fmt.Println(idx, a)
fmt.Println("simple_function") // __AUTO_GENERATED_PRINTF__
fmt.Println("simple_function 1") // __AUTO_GENERATED_PRINTF__
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class Extract {
public static void simpleFunction(int a) {
int test = 1, test_other = 11;
for (int x = 0; x < test_other + test; x++) {
System.out.println("Extract#simpleFunction#for"); // __AUTO_GENERATED_PRINTF__
System.out.println("Extract#simpleFunction#for 1"); // __AUTO_GENERATED_PRINTF__
System.out.println(x + " " + a);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Printf {
public static void main(String[] args) {
System.out.println("test");
System.out.println("debug path Printf#main"); // __AUTO_GENERATED_PRINTF__
System.out.println("debug path Printf#main 1"); // __AUTO_GENERATED_PRINTF__
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function main() {
console.log("debug path main"); // __AUTO_GENERATED_PRINTF__
console.log("debug path main 1"); // __AUTO_GENERATED_PRINTF__
return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class foo {

test() {
"that seems wrong"
console.log("foo#test"); // __AUTO_GENERATED_PRINTF__
console.log("foo#test 1"); // __AUTO_GENERATED_PRINTF__
return 5
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local hello = function ()
print("test")
print("debug path function"); -- __AUTO_GENERATED_PRINTF__
print("debug path function 1"); -- __AUTO_GENERATED_PRINTF__
end
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local hello = function ()
local hello = function()
print("test")
print("function 1") -- __AUTO_GENERATED_PRINTF__
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local function poggers()
print("this function is quite simple indeed")
print("poggers") -- __AUTO_GENERATED_PRINTF__
print("poggers 1") -- __AUTO_GENERATED_PRINTF__
end
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
function testFunction() {
printf("debug path testFunction"); // __AUTO_GENERATED_PRINTF__
printf("debug path testFunction 1"); // __AUTO_GENERATED_PRINTF__
}
?>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
function testFunction() {
printf("testFunction\n"); // __AUTO_GENERATED_PRINTF__
printf("testFunction 1\n"); // __AUTO_GENERATED_PRINTF__
}
?>
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
def main():
print("debug path main") # __AUTO_GENERATED_PRINTF__
print("debug path main 1") # __AUTO_GENERATED_PRINTF__
pass
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class Poggers:

def simple_function(self, a):
test = 1
print(f"Poggers#simple_function") # __AUTO_GENERATED_PRINTF__
print(f"Poggers#simple_function 1") # __AUTO_GENERATED_PRINTF__
test_other = 11
for x in range(test_other + test):
print(x, a)
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module SimpleModule
class SimpleClass
def simple_function(a)
puts "SimpleModule#SimpleClass#simple_function" # __AUTO_GENERATED_PRINTF__
puts "SimpleModule#SimpleClass#simple_function 1" # __AUTO_GENERATED_PRINTF__
test = 1
test_other = 11
for x in test..test_other do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def main
puts "debug path main" # __AUTO_GENERATED_PRINTF__
puts "debug path main 1" # __AUTO_GENERATED_PRINTF__
pass
end
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class SimpleClass
def simple_function(a)
test = 1
puts "SimpleClass#simple_function" # __AUTO_GENERATED_PRINTF__
puts "SimpleClass#simple_function 1" # __AUTO_GENERATED_PRINTF__
test_other = 11
for x in test..test_other do
puts x, a
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function main() {
console.log("debug path main"); // __AUTO_GENERATED_PRINTF__
console.log("debug path main 1"); // __AUTO_GENERATED_PRINTF__
return 0;
}
Loading

0 comments on commit 4d4873c

Please sign in to comment.