1

Enable header cycling

This commit is contained in:
Alex Yatskov 2022-10-18 20:46:21 -07:00
parent b0c825af3a
commit 2d75690773

View File

@ -6,48 +6,35 @@ local function file_exists(path)
return vim.loop.fs_stat(path) ~= nil return vim.loop.fs_stat(path) ~= nil
end end
local function locate_flip_path(name, exts)
for key, _ in pairs(exts) do
local path = name .. key
if file_exists(path) then
return path
end
local path_upper = name .. key:upper()
if file_exists(path_upper) then
return path_upper
end
end
end
local function hflip() local function hflip()
local header_exts = { local exts = {'.h', '.hpp', '.c', '.cpp', '.inl'}
['.h'] = true, for i = 1, #exts do
['.hpp'] = true, table.insert(exts, exts[i]:upper())
['.lua'] = true,
}
local source_exts = {
['.c'] = true,
['.cpp'] = true
}
local index = vim.api.nvim_get_current_buf()
local path = vim.api.nvim_buf_get_name(index)
local name, ext = split_ext(path)
if ext ~= nil then
local ext_lower = ext:lower()
local flip_path
if header_exts[ext_lower] then
flip_path = locate_flip_path(name, source_exts)
elseif source_exts[ext_lower] then
flip_path = locate_flip_path(name, header_exts)
end end
if flip_path then local path = vim.api.nvim_buf_get_name(vim.api.nvim_get_current_buf())
vim.cmd(string.format('e %s', flip_path)) local name, ext = split_ext(path)
if not ext then
return
end
local ext_index = nil
for i, e in ipairs(exts) do
if e == ext:lower() then
ext_index = i
break
end
end
if not ext_index then
return
end
for i = 1, #exts do
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
end end
end end
end end