From 124691d0d94b25e284ea3cec55ced1ec5fa2c5b7 Mon Sep 17 00:00:00 2001 From: Rob Watson Date: Sun, 17 Sep 2023 19:43:16 +0200 Subject: [PATCH] refactor(nvim): extract more lua --- nvimrc | 162 +--------------------------------------- vim/lua/commands.lua | 52 +++++++++++++ vim/lua/main.lua | 2 + vim/lua/test_runner.lua | 104 ++++++++++++++++++++++++++ 4 files changed, 159 insertions(+), 161 deletions(-) create mode 100644 vim/lua/commands.lua create mode 100644 vim/lua/test_runner.lua diff --git a/nvimrc b/nvimrc index 4804e75..b76a9af 100644 --- a/nvimrc +++ b/nvimrc @@ -146,169 +146,9 @@ nnoremap dq ggVG"_d:wq iabbrev esdebug debugger; // eslint-disable-line no-debugger iabbrev bashstrict set -euo pipefailIFS=$'\n\t' -" lua includes +" include all lua lua require("main") -lua <pu", [[:lua _G.insert_uuid()]], {noremap = true, silent = true}) -vim.api.nvim_set_keymap("i", "", [[:lua _G.insert_uuid()a]], {noremap = true, silent = true}) - -function _G.insert_jira_url() - local url = vim.call("system", "jira") - local errcode = vim.v.shell_error - if errcode ~= 0 then - vim.api.nvim_err_writeln("jira returned error code: " .. errcode) - return - end - - vim.call("setreg", "u", url) - vim.api.nvim_command([[normal! "up]]) -end - -vim.api.nvim_set_keymap("n", "pj", [[:lua _G.insert_jira_url()]], {noremap = true, silent = true}) -vim.api.nvim_set_keymap("i", "", [[:lua _G.insert_jira_url()a]], {noremap = true, silent = true}) - -function _G.insert_iso8601_timestamp() - local ts = vim.call("system", "echo -n $(date --iso-8601=seconds)") - - local errcode = vim.v.shell_error - if errcode ~= 0 then - vim.api.nvim_err_writeln("date returned error code: " .. errcode) - return - end - - vim.call("setreg", "u", ts) - vim.api.nvim_command([[normal! "up]]) -end - - -vim.api.nvim_set_keymap("n", "pt", [[:lua _G.insert_iso8601_timestamp()]], {noremap = true, silent = true}) -vim.api.nvim_set_keymap("i", "", [[:lua _G.insert_iso8601_timestamp()a]], {noremap = true, silent = true}) - -_G._test_cmd_to_wins = {} - -_G._build_test_cmd = function() - local bufnr = vim.call("bufnr", "%") - local filetype = vim.call("getbufvar", bufnr, "&filetype") - if filetype == "" then - vim.api.nvim_err_writeln("cannot build test command for filetype:" .. filetype) - return nil - end - - local path = vim.call("expand", "%:p") - - -- TODO: allow line number to be passed - -- TODO: check file exists before running command - if filetype == "ruby" then - if not path:find("_spec.rb") then - path = path:gsub("/app/", "/spec/"):gsub([[.rb$]], "_spec.rb") - end - - return "bundle exec rspec --format=progress --no-profile " .. path - elseif filetype == "go" then - return "go test -v " .. path:gsub("^(.*)/(.*go)$", "%1/...") - else - vim.api.nvim_err_writeln("filetype not supported: " .. filetype) - return nil - end -end - -_G.run_tests = function() - local cmd = _G._build_test_cmd() - if cmd == nil then - return - end - - vim.api.nvim_command([[silent up]]) - - local winid = _G._test_cmd_to_wins[cmd] - local current_winid = vim.call("win_getid") - if winid == nil or not vim.api.nvim_win_is_valid(winid) or winid == current_winid or not vim.call("win_gotoid", winid) == -1 then - vim.api.nvim_command([[10split]]) - winid = vim.call("win_getid") - _G._test_cmd_to_wins[cmd] = winid - end - - local buf = vim.api.nvim_create_buf(false, true) - vim.api.nvim_buf_set_option(buf, "bufhidden", "delete") - vim.api.nvim_win_set_buf(winid, buf) - - vim.call("termopen", cmd) - vim.api.nvim_command([[normal! G]]) - vim.api.nvim_command([[wincmd p]]) -end - -_G.focus_tests = function() - local cmd = _G._build_test_cmd() - if cmd == nil then - return - end - - local winid = _G._test_cmd_to_wins[cmd] - if winid == nil or not vim.api.nvim_win_is_valid(winid) then - return - end - - vim.call("win_gotoid", winid) - vim.api.nvim_command([[wincmd =]]) -end - -_G.close_tests = function() - local current_winid = vim.api.nvim_get_current_win() - for _, winid in pairs(_G._test_cmd_to_wins) do - if current_winid == winid and vim.api.nvim_win_is_valid(winid) then - vim.api.nvim_win_close(winid, false) - return - end - end - - local cmd = _G._build_test_cmd() - if cmd == nil then - return - end - - local winid = _G._test_cmd_to_wins[cmd] - if winid == nil or not vim.api.nvim_win_is_valid(winid) then - return - end - - vim.api.nvim_win_close(winid, false) -end - -_G.copy_test_cmd = function() - local cmd = _G._build_test_cmd() - if cmd == nil then - return - end - - vim.call("setreg", "+", cmd) - print("Copied: " .. cmd) -end - -vim.api.nvim_set_keymap("n", "cr", [[:lua _G.run_tests()]], {noremap = true, silent = true}) -vim.api.nvim_set_keymap("n", "cf", [[:lua _G.focus_tests()]], {noremap = true, silent = true}) -vim.api.nvim_set_keymap("n", "cq", [[:lua _G.close_tests()]], {noremap = true, silent = true}) -vim.api.nvim_set_keymap("n", "cc", [[:lua _G.copy_test_cmd()]], {noremap = true, silent = true}) -EOF - " Function to eat trailing character when applying iabbrevs. " https://stackoverflow.com/questions/11858927/preventing-trailing-whitespace-when-using-vim-abbreviations func! Eatchar(pat) diff --git a/vim/lua/commands.lua b/vim/lua/commands.lua new file mode 100644 index 0000000..64f8416 --- /dev/null +++ b/vim/lua/commands.lua @@ -0,0 +1,52 @@ +function _G.dump(...) + local objects = vim.tbl_map(vim.inspect, {...}) + print(unpack(objects)) + return ... +end + +function _G.insert_uuid() + local uuid = vim.fn.system("uuidprint") + local errcode = vim.v.shell_error + if errcode ~= 0 then + vim.api.nvim_err_writeln("uuidprint returned error code: " .. errcode) + return + end + + vim.fn.setreg("u", uuid) + vim.api.nvim_command([[normal! "up]]) +end + +vim.api.nvim_set_keymap("n", "pu", [[:lua _G.insert_uuid()]], {noremap = true, silent = true}) +vim.api.nvim_set_keymap("i", "", [[:lua _G.insert_uuid()a]], {noremap = true, silent = true}) + +function _G.insert_jira_url() + local url = vim.fn.system("jira") + local errcode = vim.v.shell_error + if errcode ~= 0 then + vim.api.nvim_err_writeln("jira returned error code: " .. errcode) + return + end + + vim.fn.setreg("u", url) + vim.api.nvim_command([[normal! "up]]) +end + +vim.api.nvim_set_keymap("n", "pj", [[:lua _G.insert_jira_url()]], {noremap = true, silent = true}) +vim.api.nvim_set_keymap("i", "", [[:lua _G.insert_jira_url()a]], {noremap = true, silent = true}) + +function _G.insert_iso8601_timestamp() + local ts = vim.fn.system("echo -n $(date --iso-8601=seconds)") + + local errcode = vim.v.shell_error + if errcode ~= 0 then + vim.api.nvim_err_writeln("date returned error code: " .. errcode) + return + end + + vim.fn.setreg("u", ts) + vim.api.nvim_command([[normal! "up]]) +end + + +vim.api.nvim_set_keymap("n", "pt", [[:lua _G.insert_iso8601_timestamp()]], {noremap = true, silent = true}) +vim.api.nvim_set_keymap("i", "", [[:lua _G.insert_iso8601_timestamp()a]], {noremap = true, silent = true}) diff --git a/vim/lua/main.lua b/vim/lua/main.lua index b131820..882109f 100644 --- a/vim/lua/main.lua +++ b/vim/lua/main.lua @@ -1 +1,3 @@ require "helpers" +require "commands" +require "test_runner" diff --git a/vim/lua/test_runner.lua b/vim/lua/test_runner.lua new file mode 100644 index 0000000..a836744 --- /dev/null +++ b/vim/lua/test_runner.lua @@ -0,0 +1,104 @@ +_G._test_cmd_to_wins = {} + +_G._build_test_cmd = function() + local bufnr = vim.fn.bufnr("%") + local filetype = vim.fn.getbufvar(bufnr, "&filetype") + if filetype == "" then + vim.api.nvim_err_writeln("cannot build test command for filetype:" .. filetype) + return nil + end + + local path = vim.fn.expand("%:p") + + -- TODO: allow line number to be passed + -- TODO: check file exists before running command + if filetype == "ruby" then + if not path:find("_spec.rb") then + path = path:gsub("/app/", "/spec/"):gsub([[.rb$]], "_spec.rb") + end + + return "bundle exec rspec --format=progress --no-profile " .. path + elseif filetype == "go" then + return "go test -v " .. path:gsub("^(.*)/(.*go)$", "%1/...") + else + vim.api.nvim_err_writeln("filetype not supported: " .. filetype) + return nil + end +end + +_G.run_tests = function() + local cmd = _G._build_test_cmd() + if cmd == nil then + return + end + + vim.api.nvim_command([[silent up]]) + + local winid = _G._test_cmd_to_wins[cmd] + local current_winid = vim.fn.win_getid() + if winid == nil or not vim.api.nvim_win_is_valid(winid) or winid == current_winid or not vim.fn.win_gotoid(winid) == -1 then + vim.api.nvim_command([[10split]]) + winid = vim.fn.win_getid() + _G._test_cmd_to_wins[cmd] = winid + end + + local buf = vim.api.nvim_create_buf(false, true) + vim.api.nvim_buf_set_option(buf, "bufhidden", "delete") + vim.api.nvim_win_set_buf(winid, buf) + + vim.fn.termopen(cmd) + vim.api.nvim_command([[normal! G]]) + vim.api.nvim_command([[wincmd p]]) +end + +_G.focus_tests = function() + local cmd = _G._build_test_cmd() + if cmd == nil then + return + end + + local winid = _G._test_cmd_to_wins[cmd] + if winid == nil or not vim.api.nvim_win_is_valid(winid) then + return + end + + vim.fn.win_gotoid(winid) + vim.api.nvim_command([[wincmd =]]) +end + +_G.close_tests = function() + local current_winid = vim.api.nvim_get_current_win() + for _, winid in pairs(_G._test_cmd_to_wins) do + if current_winid == winid and vim.api.nvim_win_is_valid(winid) then + vim.api.nvim_win_close(winid, false) + return + end + end + + local cmd = _G._build_test_cmd() + if cmd == nil then + return + end + + local winid = _G._test_cmd_to_wins[cmd] + if winid == nil or not vim.api.nvim_win_is_valid(winid) then + return + end + + vim.api.nvim_win_close(winid, false) +end + +_G.copy_test_cmd = function() + local cmd = _G._build_test_cmd() + if cmd == nil then + return + end + + vim.fn.setreg("+", cmd) + print("Copied: " .. cmd) +end + +vim.api.nvim_set_keymap("n", "cr", [[:lua _G.run_tests()]], {noremap = true, silent = true}) +vim.api.nvim_set_keymap("n", "cf", [[:lua _G.focus_tests()]], {noremap = true, silent = true}) +vim.api.nvim_set_keymap("n", "cq", [[:lua _G.close_tests()]], {noremap = true, silent = true}) +vim.api.nvim_set_keymap("n", "cc", [[:lua _G.copy_test_cmd()]], {noremap = true, silent = true})