1
revolver.nvim/lua/revolver.lua

67 lines
1.4 KiB
Lua
Raw Permalink Normal View History

2023-01-26 03:56:57 +00:00
local RevolverSuffixes = {
'.c',
'.cpp',
'.h',
'.hpp',
'.inl',
2023-01-25 06:33:01 +00:00
}
local function setup(config)
if config then
2023-01-26 03:56:57 +00:00
RevolverSuffixes = config
2023-01-25 06:33:01 +00:00
end
end
2023-01-27 01:13:44 +00:00
local function escape(s)
return s:gsub('[%-%.%+%[%]%(%)%$%^%%%?%*]','%%%1')
end
2023-01-25 06:33:01 +00:00
local function file_exists(path)
return vim.loop.fs_stat(path) ~= nil
end
local function path_pivot(path, suffix)
2023-01-27 01:13:44 +00:00
local start, stop = path:find(escape(suffix) .. '$')
2023-01-25 06:33:01 +00:00
if start and stop == #path then
return path:sub(0, start - 1)
end
end
local function revolve(back)
local path = vim.api.nvim_buf_get_name(vim.api.nvim_get_current_buf())
local suffix_index = nil
local prefix = nil
2023-01-26 03:56:57 +00:00
for i, s in ipairs(RevolverSuffixes) do
2023-01-25 06:33:01 +00:00
prefix = path_pivot(path, s)
if prefix then
suffix_index = i
break
end
end
if prefix then
local start = 1
2023-01-26 03:56:57 +00:00
local stop = #RevolverSuffixes - 1
2023-01-25 06:33:01 +00:00
local step = 1
if back then
start, stop = stop, start
step = -1
end
for i = start, stop, step do
2023-01-26 03:56:57 +00:00
local j = (i + suffix_index - 1) % #RevolverSuffixes + 1
local next_path = prefix .. RevolverSuffixes[j]
2023-01-25 06:33:01 +00:00
if path ~= next_path and file_exists(next_path) then
vim.cmd(string.format('e %s', next_path))
break
end
end
end
end
return {
revolve = revolve,
setup = setup,
}