1

Update behavior

This commit is contained in:
Alex Yatskov 2023-01-24 22:33:01 -08:00
parent 39080361ff
commit 45618174b9
4 changed files with 88 additions and 49 deletions

View File

@ -1,44 +0,0 @@
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()
local exts = {'.h', '.hpp', '.c', '.cpp', '.inl'}
for i = 1, #exts do
table.insert(exts, exts[i]:upper())
end
local path = vim.api.nvim_buf_get_name(vim.api.nvim_get_current_buf())
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 - 1 do
local j = (i + ext_index - 1) % #exts + 1
local next_path = name .. exts[j]
if path ~= next_path and file_exists(next_path) then
vim.cmd(string.format('e %s', next_path))
break
end
end
end
return {
hflip = hflip
}

66
lua/revolver.lua Normal file
View File

@ -0,0 +1,66 @@
Config = {
Suffixes = {
'.c',
'.cpp',
'.h',
'.hpp',
'.inl',
}
}
local function setup(config)
if config then
for key, value in pairs(config) do
Config[key] = config[key] or value
end
end
end
local function file_exists(path)
return vim.loop.fs_stat(path) ~= nil
end
local function path_pivot(path, suffix)
local start, stop = path:find(suffix)
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
for i, s in ipairs(Config.Suffixes) do
prefix = path_pivot(path, s)
if prefix then
suffix_index = i
break
end
end
if prefix then
local start = 1
local stop = #Config.Suffixes - 1
local step = 1
if back then
start, stop = stop, start
step = -1
end
for i = start, stop, step do
local j = (i + suffix_index - 1) % #Config.Suffixes + 1
local next_path = prefix .. Config.Suffixes[j]
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,
}

View File

@ -1,5 +0,0 @@
if not vim.g.hflip then
local hflip = require('hflip')
vim.api.nvim_create_user_command('HFlip', hflip.hflip, {})
vim.g.hflip = true
end

22
plugin/revolver.lua Normal file
View File

@ -0,0 +1,22 @@
if not vim.g.hflip then
local function reload()
package.loaded.revolver = nil
revolver = require('revolver')
end
local function revolve()
revolver.revolve(false)
end
local function revolve_back()
revolver.revolve(true)
end
reload()
vim.api.nvim_create_user_command('Revolver', revolve, {})
vim.api.nvim_create_user_command('RevolverBack', revolve_back, {})
vim.api.nvim_create_user_command('RevolverReload', reload, {})
vim.g.revolver = true
end