1
revolver.nvim/lua/hflip.lua

45 lines
954 B
Lua
Raw Normal View History

2022-10-02 21:50:43 +00:00
local function split_ext(path)
return path:match('^(.+)(%..+)$')
end
local function file_exists(path)
return vim.loop.fs_stat(path) ~= nil
end
local function hflip()
2022-10-19 03:46:21 +00:00
local exts = {'.h', '.hpp', '.c', '.cpp', '.inl'}
for i = 1, #exts do
table.insert(exts, exts[i]:upper())
end
2022-10-02 21:50:43 +00:00
2022-10-19 03:46:21 +00:00
local path = vim.api.nvim_buf_get_name(vim.api.nvim_get_current_buf())
2022-10-02 21:50:43 +00:00
local name, ext = split_ext(path)
2022-10-19 03:46:21 +00:00
if not ext then
return
end
2022-10-02 21:50:43 +00:00
2022-10-19 03:46:21 +00:00
local ext_index = nil
for i, e in ipairs(exts) do
if e == ext:lower() then
ext_index = i
break
2022-10-04 03:22:04 +00:00
end
2022-10-19 03:46:21 +00:00
end
if not ext_index then
return
end
2022-10-02 21:50:43 +00:00
2022-10-19 03:53:20 +00:00
for i = 1, #exts - 1 do
2022-10-19 03:46:21 +00:00
local j = (i + ext_index - 1) % #exts + 1
local next_path = name .. exts[j]
if file_exists(next_path) then
vim.cmd(string.format('e %s', next_path))
break
2022-10-04 03:22:04 +00:00
end
2022-10-02 21:50:43 +00:00
end
end
return {
hflip = hflip
}