refactor(nvim): extract more lua
This commit is contained in:
parent
b0ff3024b3
commit
124691d0d9
162
nvimrc
162
nvimrc
|
@ -146,169 +146,9 @@ nnoremap <leader>dq ggVG"_d:wq<cr>
|
|||
iabbrev esdebug debugger; // eslint-disable-line no-debugger
|
||||
iabbrev bashstrict set -euo pipefail<cr>IFS=$'\n\t'<cr>
|
||||
|
||||
" lua includes
|
||||
" include all lua
|
||||
lua require("main")
|
||||
|
||||
lua <<EOF
|
||||
function _G.dump(...)
|
||||
local objects = vim.tbl_map(vim.inspect, {...})
|
||||
print(unpack(objects))
|
||||
return ...
|
||||
end
|
||||
|
||||
function _G.insert_uuid()
|
||||
local uuid = vim.call("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.call("setreg", "u", uuid)
|
||||
vim.api.nvim_command([[normal! "up]])
|
||||
end
|
||||
|
||||
vim.api.nvim_set_keymap("n", "<leader>pu", [[:lua _G.insert_uuid()<cr>]], {noremap = true, silent = true})
|
||||
vim.api.nvim_set_keymap("i", "<c-d><c-u>", [[<esc>:lua _G.insert_uuid()<cr>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", "<leader>pj", [[:lua _G.insert_jira_url()<cr>]], {noremap = true, silent = true})
|
||||
vim.api.nvim_set_keymap("i", "<c-d><c-j>", [[<esc>:lua _G.insert_jira_url()<cr>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", "<leader>pt", [[:lua _G.insert_iso8601_timestamp()<cr>]], {noremap = true, silent = true})
|
||||
vim.api.nvim_set_keymap("i", "<c-d><c-t>", [[<esc>:lua _G.insert_iso8601_timestamp()<cr>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", "<leader>cr", [[:lua _G.run_tests()<cr>]], {noremap = true, silent = true})
|
||||
vim.api.nvim_set_keymap("n", "<leader>cf", [[:lua _G.focus_tests()<cr>]], {noremap = true, silent = true})
|
||||
vim.api.nvim_set_keymap("n", "<leader>cq", [[:lua _G.close_tests()<cr>]], {noremap = true, silent = true})
|
||||
vim.api.nvim_set_keymap("n", "<leader>cc", [[:lua _G.copy_test_cmd()<cr>]], {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)
|
||||
|
|
|
@ -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", "<leader>pu", [[:lua _G.insert_uuid()<cr>]], {noremap = true, silent = true})
|
||||
vim.api.nvim_set_keymap("i", "<c-d><c-u>", [[<esc>:lua _G.insert_uuid()<cr>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", "<leader>pj", [[:lua _G.insert_jira_url()<cr>]], {noremap = true, silent = true})
|
||||
vim.api.nvim_set_keymap("i", "<c-d><c-j>", [[<esc>:lua _G.insert_jira_url()<cr>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", "<leader>pt", [[:lua _G.insert_iso8601_timestamp()<cr>]], {noremap = true, silent = true})
|
||||
vim.api.nvim_set_keymap("i", "<c-d><c-t>", [[<esc>:lua _G.insert_iso8601_timestamp()<cr>a]], {noremap = true, silent = true})
|
|
@ -1 +1,3 @@
|
|||
require "helpers"
|
||||
require "commands"
|
||||
require "test_runner"
|
||||
|
|
|
@ -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", "<leader>cr", [[:lua _G.run_tests()<cr>]], {noremap = true, silent = true})
|
||||
vim.api.nvim_set_keymap("n", "<leader>cf", [[:lua _G.focus_tests()<cr>]], {noremap = true, silent = true})
|
||||
vim.api.nvim_set_keymap("n", "<leader>cq", [[:lua _G.close_tests()<cr>]], {noremap = true, silent = true})
|
||||
vim.api.nvim_set_keymap("n", "<leader>cc", [[:lua _G.copy_test_cmd()<cr>]], {noremap = true, silent = true})
|
Loading…
Reference in New Issue