From 45618174b9dd393e80297541fa4985ec17431125 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Tue, 24 Jan 2023 22:33:01 -0800 Subject: [PATCH] Update behavior --- lua/hflip.lua | 44 ------------------------------ lua/revolver.lua | 66 +++++++++++++++++++++++++++++++++++++++++++++ plugin/hflip.lua | 5 ---- plugin/revolver.lua | 22 +++++++++++++++ 4 files changed, 88 insertions(+), 49 deletions(-) delete mode 100644 lua/hflip.lua create mode 100644 lua/revolver.lua delete mode 100644 plugin/hflip.lua create mode 100644 plugin/revolver.lua diff --git a/lua/hflip.lua b/lua/hflip.lua deleted file mode 100644 index 5e1f9f0..0000000 --- a/lua/hflip.lua +++ /dev/null @@ -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 -} diff --git a/lua/revolver.lua b/lua/revolver.lua new file mode 100644 index 0000000..b73e0d9 --- /dev/null +++ b/lua/revolver.lua @@ -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, +} diff --git a/plugin/hflip.lua b/plugin/hflip.lua deleted file mode 100644 index 54c4501..0000000 --- a/plugin/hflip.lua +++ /dev/null @@ -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 diff --git a/plugin/revolver.lua b/plugin/revolver.lua new file mode 100644 index 0000000..19cf7f7 --- /dev/null +++ b/plugin/revolver.lua @@ -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