From 8f2b447bf521eaa9e837409446a0a12f396cda29 Mon Sep 17 00:00:00 2001 From: Ralph Azucena Date: Thu, 17 Apr 2025 17:31:35 -0700 Subject: [PATCH] refactor test togglng in ruby --- lua/config/autocmds.lua | 17 +++++++++++ lua/config/keymaps.lua | 68 ++++++++++++++++++++++++++++------------- 2 files changed, 63 insertions(+), 22 deletions(-) diff --git a/lua/config/autocmds.lua b/lua/config/autocmds.lua index 1cb625a..bfc22e7 100644 --- a/lua/config/autocmds.lua +++ b/lua/config/autocmds.lua @@ -20,3 +20,20 @@ vim.api.nvim_create_autocmd("User", { -- end) end, }) + +local function set_rah_test_dir() + if vim.fn.expand("%:e") == "rb" then + local test_dirs = { "spec", "test" } + + if not vim.tbl_contains(test_dirs, vim.g.rah_test_dir) then + vim.g.rah_test_dir = test_dirs[1] -- Default to spec for Ruby files + end + else + vim.g.rah_test_dir = nil -- Default or unset + end +end + +-- Automatically set rah_test_dir when opening a buffer +vim.api.nvim_create_autocmd({ "BufEnter" }, { + callback = set_rah_test_dir, +}) diff --git a/lua/config/keymaps.lua b/lua/config/keymaps.lua index 120e6b1..e2f6842 100644 --- a/lua/config/keymaps.lua +++ b/lua/config/keymaps.lua @@ -5,29 +5,53 @@ -- Remove keymaps to move lines up/down: https://github.com/LazyVim/LazyVim/blob/main/lua/lazyvim/config/keymaps.lua#L25 vim.keymap.del({ "n", "i", "v" }, "") vim.keymap.del({ "n", "i", "v" }, "") - vim.keymap.set('v', 'y', '"+y') -- v-mode: yank to clipboard; -local function toggle_rb_spec() - -- rails - if vim.fn.filereadable("config/application.rb") == 1 then - if vim.fn.expand("%:t"):match("_spec%.rb$") then - vim.cmd("edit " .. vim.fn.expand("%:h"):gsub("spec", "app") .. "/" .. vim.fn.expand("%:t"):gsub("_spec%.rb$", ".rb")) - else - vim.cmd("edit " .. vim.fn.expand("%:h"):gsub("app", "spec") .. "/" .. vim.fn.expand("%:t"):gsub("%.rb$", "_spec.rb")) - end - end - -end -local function toggle_rspec() - vim.cmd("lcd " .. vim.fn.system("git rev-parse --show-toplevel"):gsub("\n", "")) - - if vim.bo.filetype == "ruby" and vim.fn.filereadable("Gemfile") == 1 then - toggle_rb_spec() - else - vim.notify("Unsupported file type and/or project", vim.log.levels.WARN) - end -end -vim.keymap.set('n', 'ti', toggle_rspec, { silent = true, desc = "Toggle Test F(i)le" }) vim.keymap.set("n", "ci", vim.diagnostic.open_float, { desc = "Code D(i)agnostics" }) vim.keymap.set({ "n", "v" }, "*", [[:let @/='\<'.expand('').'\>'|set hls]], { silent = true, desc = "No jump highlight"}) -- https://superuser.com/a/255120 vim.keymap.set({ "n", "v" }, "g*", [[:let @/=expand('')|set hls]], { silent = true, desc = "No jump highlight"}) -- https://stackoverflow.com/a/49944815 + +local function toggle_rb_test_dir() + if vim.g.rah_test_dir == "spec" then + vim.g.rah_test_dir = "test" + else + vim.g.rah_test_dir = "spec" + end +end + +local function toggle_test_dir() + local file_ext = vim.fn.expand("%:e") + + if not vim.g.rah_test_dir then + vim.notify("Toggline test file for '." .. file_ext .. "' is unsupported", vim.log.levels.INFO) + return + end + + if file_ext == "rb" then + toggle_rb_test_dir() + end + vim.notify("Test directory set to: " .. vim.g.rah_test_dir, vim.log.levels.INFO) +end +vim.keymap.set("n", "td", toggle_test_dir, { silent = true, desc = "Toggle Test Dir" }) + +local function toggle_rb_spec() + local test_dir = vim.g.rah_test_dir + -- rails + if vim.fn.filereadable("config/application.rb") == 1 then + if vim.fn.expand("%:t"):match("_spec%.rb$") then + vim.cmd("edit " .. vim.fn.expand("%:h"):gsub(test_dir, "app") .. "/" .. vim.fn.expand("%:t"):gsub("_" .. test_dir .. "%.rb$", ".rb")) + else + vim.cmd("edit " .. vim.fn.expand("%:h"):gsub("app", test_dir) .. "/" .. vim.fn.expand("%:t"):gsub("%.rb$", "_" .. test_dir .. ".rb")) + end + end +end + +local function toggle_rspec() + vim.cmd("lcd " .. vim.fn.system("git rev-parse --show-toplevel"):gsub("\n", "")) + + if vim.fn.expand("%:e") == "rb" and vim.fn.filereadable("Gemfile") == 1 then + toggle_rb_spec() + else + vim.notify("Unsupported filetype and/or project", vim.log.levels.WARN) + end +end +vim.keymap.set('n', 'ti', toggle_rspec, { silent = true, desc = "Toggle Test F(i)le" })