diff --git a/LICENSE b/LICENSE index cbb42dc..81b14b2 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2024 Developlend (Vladyslav Topyekha) info@dropsway.net +Copyright (c) 2024 Developland (Vladyslav Topyekha) info@dropsway.net Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 3b5f66f..f13aa82 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ If you need to test a function that you are constantly changing, you might find - Add a test to favorites and run it without opening the list. - Delete tests from the list. - Choose the split option to run the tests. +- Jump to next and previous test The tests you add to the list are saved to a configuration file, so when you come back, you can easily run them. @@ -76,6 +77,7 @@ vim.keymap.set("n", "tf", function() print("Running favorite test") gtestler.execute_favorite_test() end, { desc = "Run favorite test" }) + ``` ## Help @@ -91,6 +93,6 @@ For more information, run the command: **Version:** 0.1.0 -**Author:** Developlend (Vladyslav Topyekha) +**Author:** Developland (Vladyslav Topyekha) **License:** MIT **Description:** Easy to use golang run tests solution. diff --git a/TODO.md b/TODO.md index 6ed514c..5438bf1 100644 --- a/TODO.md +++ b/TODO.md @@ -8,14 +8,16 @@ - [x] delete visually selected tests - [x] make a already added test in the list favorite - [x] run test in the list window -- [x] restore favorite - [x] jump to test under the cursor +- [x] delete test in normal mode +- [ ] restore favorite +- [x] jump to first test in buffer even if not added - [ ] highlight tests already in list -- [ ] consider test name change, so delete test if not found - [ ] run tests by numbers - [ ] on favorite toggle with multiple tests jump to correct line - [ ] add test file location, and search test location in that file - [ ] add line to the floating buffer with fields to explain - [ ] add all tests in buffer to gtester list - [ ] consider telescope integration +- [ ] consider test name change, so delete test if not found diff --git a/doc/gtestler.txt b/doc/gtestler.txt index 91117d2..705cf6a 100644 --- a/doc/gtestler.txt +++ b/doc/gtestler.txt @@ -6,14 +6,17 @@ gtestler provides a simple list of APIs. Below is the documentation for usage. ------------------------------------------------------------------------------ Available Commands: - :h gtestler.open_tests_list - :h gtestler.execute_test :h gtestler.add_test - :h gtestler.delete_test :h gtestler.add_favorite_test + :h gtestler.open_tests_list + :h gtestler.execute_test :h gtestler.execute_favorite_test + :h gtestler.delete_test :h gtestler.visual_delete :h gtestler.toggle_favorite + :h gtestler.go_to_test_function + :h gtestler.jump_to_next + :h gtestler.jump_to_previous ------------------------------------------------------------------------------ @@ -25,7 +28,7 @@ directory is used to locate the valid tests: < The last directory in the path provided by the `getcwd()` command is used as the project directory. -If before test labal you see * (ex: * TestMyTestSample) it meens this test is +If before test name you see * (ex: * TestMyTestSample) it means this test is a favorite test at the moment. ------------------------------------------------------------------------------ @@ -75,10 +78,28 @@ Pressing d will delete all the selected tests. It will automatically refresh the buffer list. ------------------------------------------------------------------------------ -toggle_favorite() *gtestler.favorite()* +toggle_favorite() *gtestler.toggle_favorite()* -In the list of tests press f to toggle betwin current test +In the list of tests press f to toggle between current test being favorite or not +------------------------------------------------------------------------------ +go_to_test_function() *gtestler.go_to_test_function()* + +Pressing g on the test in the list, it will take you directly to that +function. Be aware renaming the file or the test function, will broke +the jumping system. +------------------------------------------------------------------------------ +jump_to_next() *jump_to_next()* + +Jumps to next test in the current buffer, from your current cursor location. +If no test is founded below the cursor it will wrap around to check +tests available before the cursor. + +------------------------------------------------------------------------------ +jump_to_previous() *jump_to_previous()* +Jumps to previous test in the current buffer, from your current cursor location. +If no test is founded above the cursor it will wrap around to check +tests available below the cursor. -vim:tw=80:ts=8:ft=help:norl: +vim:tw=80:ts=8:ft=help diff --git a/lua/gtestler/init.lua b/lua/gtestler/init.lua index 31521ed..d22a419 100644 --- a/lua/gtestler/init.lua +++ b/lua/gtestler/init.lua @@ -32,6 +32,15 @@ vim.api.nvim_create_autocmd({ "BufLeave" }, { group = gtestler_autogroup, }) +vim.api.nvim_create_autocmd({ "BufEnter" }, { + pattern = "*_test.go", + + callback = function() + gtestler_utils.find_all_tests_lines_in_buffer() + end, + group = gtestler_autogroup, +}) + vim.api.nvim_create_autocmd("VimEnter", { callback = function() @@ -39,7 +48,6 @@ vim.api.nvim_create_autocmd("VimEnter", { if retore_tabel ~= nil then tests_commands = retore_tabel - gtestler_log.log_message("INFO", tests_commands) end end, @@ -57,6 +65,7 @@ vim.api.nvim_create_autocmd("FileType", { desc = "jump to test file and func line", noremap = true, }) + vim.keymap.set("n", "f", function() M.toggle_favorite() end, { @@ -64,11 +73,12 @@ vim.api.nvim_create_autocmd("FileType", { desc = "test is now favorite", noremap = true, }) + vim.keymap.set("n", "tr", function() M.run_selected_test() end, { buffer = true, - desc = "run selected test", + desc = "run current test in the list", noremap = true, }) @@ -78,7 +88,11 @@ vim.api.nvim_create_autocmd("FileType", { vim.keymap.set("v", "d", function() M.delete_selected_tests() - end, { buffer = true, desc = "run selected test" }) + end, { buffer = true, desc = "delete selected tests" }) + + vim.keymap.set("n", "d", function() + M.delete_test() + end, { buffer = true, desc = "delete test" }) vim.keymap.set( "n", @@ -126,7 +140,6 @@ end, { function M.go_to_test_file() local command_alias = gtestler_utils.get_command_alias() command_alias = gtestler_utils.remove_star_if_exists(command_alias) - gtestler_log.log_message("BIG", command_alias) local file_path = tests_commands[wd][command_alias].file_path vim.api.nvim_command("wincmd w") @@ -138,7 +151,70 @@ function M.go_to_test_file() end, 1) end +function M.jump_to_next_test() + local row, _ = unpack(vim.api.nvim_win_get_cursor(0)) + + local test_lines = gtestler_utils.find_all_tests_lines_in_buffer() + -- local test_lines = { 5, 7 } + + if test_lines ~= nil then + -- search test forward + for _, line_value in pairs(test_lines) do + if line_value > row then + -- gtestler_log.log_message("jump to line: ", line_value) + vim.api.nvim_win_set_cursor(0, { line_value, 0 }) + return + end + end + + -- search tests backwards + + for i = #test_lines, 1, -1 do + gtestler_log.log_message("lines backwards: ", test_lines[i]) + + if row > test_lines[i] then + -- gtestler_log.log_message("jump to line: ", line_value) + vim.api.nvim_win_set_cursor(0, { test_lines[i], 0 }) + return + end + end + else + print("No test found") + end +end +function M.jump_to_previous_test() + local row, _ = unpack(vim.api.nvim_win_get_cursor(0)) + + local test_lines = gtestler_utils.find_all_tests_lines_in_buffer() + -- local test_lines = { 5, 7 } + + if test_lines ~= nil then + -- search tests backwards + for i = #test_lines, 1, -1 do + gtestler_log.log_message("lines backwards: ", test_lines[i]) + + if row > test_lines[i] then + -- gtestler_log.log_message("jump to line: ", line_value) + vim.api.nvim_win_set_cursor(0, { test_lines[i], 0 }) + return + end + end + + -- search test forward + for _, line_value in pairs(test_lines) do + if line_value > row then + -- gtestler_log.log_message("jump to line: ", line_value) + vim.api.nvim_win_set_cursor(0, { line_value, 0 }) + return + end + end + else + print("No test found") + end +end + --- opens the list with all the tests to run +--- function M.open_tests_list() local floating_buffer, win_id = gtestler_ui.create_buffer() gtestler_win_id = win_id @@ -255,6 +331,7 @@ function M.add_favorite_test() return current_favorite_test end +--- delete test under the cursor function M.delete_test() local command_alias = gtestler_utils.get_command_alias() diff --git a/lua/gtestler/utils.lua b/lua/gtestler/utils.lua index 7f7a4bb..caa8e4b 100644 --- a/lua/gtestler/utils.lua +++ b/lua/gtestler/utils.lua @@ -41,8 +41,6 @@ function M.find_specific_function(func_label) local regex = "func%s+" .. func_label .. "%s*%b()" - -- gtestler_log.log_message("lines", lines) - for i, line in ipairs(lines) do -- print("the line:", line) if startsWith(line, "func") then @@ -57,6 +55,24 @@ function M.find_specific_function(func_label) return -1 end +--- scans all buffer, and returns lines with tests +--- return {1..3} +function M.find_all_tests_lines_in_buffer() + local line_indexes = {} + local buffer_lines = vim.api.nvim_buf_get_lines(0, 0, -1, false) + + local regex = "func%s+Test" + + for line_index, text_line in ipairs(buffer_lines) do + if string.match(text_line, regex) then + table.insert(line_indexes, line_index) + end + end + gtestler_log.log_message("INFO find: ", line_indexes) + + return line_indexes +end + --- @param method string --- @return string -- default: "split" function M.validate_split_method(method) @@ -99,7 +115,6 @@ function M.get_command_and_test_name() return new_cmd, test_name end --- Scrivere la stringa JSON nel file function M.save_json_to_file(commands_table) local current_commands = vim.fn.json_encode(commands_table) local state_dir = vim.fn.stdpath("state") @@ -112,10 +127,7 @@ function M.save_json_to_file(commands_table) end end --- Funzione per caricare il file JSON function M.load_commands_table() - -- Apri il file in modalità lettura - local state_dir = vim.fn.stdpath("state") local file = io.open(state_dir .. "/gtestler.json", "r") if not file then @@ -123,7 +135,6 @@ function M.load_commands_table() return nil end - -- Leggi il contenuto del file local content = file:read("*all") file:close() @@ -159,15 +170,4 @@ function M.get_command_alias() return line_text end ---- @param file_path string -local function jump_to_file_dev(file_path) - -- code - -- - -- local buf = vim.api.nvim_get_current_buf() - local cwd = vim.fn.getcwd() - -- need to save full path to file - local filename = cwd .. "/lua/gtestler/test/simple_test.go" -- replace with desired filename - vim.api.nvim_command("wincmd w") - vim.cmd("e " .. filename) -end return M