1

Compare commits

...

10 Commits

Author SHA1 Message Date
15336a718e Update README 2023-02-20 18:14:01 -08:00
6c2a9d9cc0 Add README 2023-01-26 21:03:12 -08:00
340cb2d8e7 Fixes 2023-01-26 17:13:44 -08:00
4e9385844f Remove globals 2023-01-25 19:56:57 -08:00
45618174b9 Update behavior 2023-01-24 22:33:01 -08:00
39080361ff Merge branch 'main' 2022-10-21 20:14:05 -07:00
1f280d9a52 Switch to lua 2022-10-21 20:13:40 -07:00
Alex Yatskov
caaab4c95e Fix for windows 2022-10-19 08:31:11 -07:00
7e6d996a9d Don't reload current file 2022-10-18 20:53:20 -07:00
2d75690773 Enable header cycling 2022-10-18 20:46:21 -07:00
5 changed files with 123 additions and 70 deletions

35
README.md Normal file
View File

@ -0,0 +1,35 @@
# revolver.nvim
This Neovim plugin makes it easy to rotate between related files in a common directory. This enables "header flipping"
in C/C++ and other languages where semantically related information is split between different files. revolver.nvim
alternates between files that share a common prefix after stripping away one of the predetermined suffixes. This suffix
is typically the file extension, but can include a part of the filename as well. Classic Vim is not supported.
## Configuration
This plugin can be configured by calling the `setup` function. A set of file suffixes is provided to override the
defaults shown below.
```lua
require('revolver').setup({
'.c',
'.cpp',
'.h',
'.hpp',
'.inl'
})
```
## Commands
For the following examples, imagine that you have the files `foo.cpp`, `foo.h`, and `foo.inl` in a directory and are
currently editing `foo.cpp`.
#### `Revolver`
Invoking the `Revolver` command will open `foo.h`. Subsequent invocation will cause `foo.h` and `foo.inl` to open before
wrapping back around to `foo.cpp`. The order that files are opened in corresponds to the ordering of suffixes provided
to the `setup` function.
#### `RevolverBack`
This command is similar to `Revolver` with the difference being that suffix rotation occurs backwards. The first file to
be opened would be `foo.inl`, followed by `foo.h`, and finally `foo.cpp` again.

View File

@ -1,57 +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 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 header_exts = {
['.h'] = true,
['.hpp'] = true,
['.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
if flip_path then
vim.cmd(string.format('e %s', flip_path))
end
end
end
return {
hflip = hflip
}

66
lua/revolver.lua Normal file
View File

@ -0,0 +1,66 @@
local RevolverSuffixes = {
'.c',
'.cpp',
'.h',
'.hpp',
'.inl',
}
local function setup(config)
if config then
RevolverSuffixes = config
end
end
local function escape(s)
return s:gsub('[%-%.%+%[%]%(%)%$%^%%%?%*]','%%%1')
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(escape(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(RevolverSuffixes) do
prefix = path_pivot(path, s)
if prefix then
suffix_index = i
break
end
end
if prefix then
local start = 1
local stop = #RevolverSuffixes - 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) % #RevolverSuffixes + 1
local next_path = prefix .. RevolverSuffixes[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,13 +0,0 @@
" in plugin/hflip.vim
if exists('g:loaded_hflip') | finish | endif " prevent loading file twice
let s:save_cpo = &cpo " save user coptions
set cpo&vim " reset them to defaults
" command to run our plugin
command! HFlip lua require'hflip'.hflip()
let &cpo = s:save_cpo " and restore after
unlet s:save_cpo
let g:loaded_hflip = 1

22
plugin/revolver.lua Normal file
View File

@ -0,0 +1,22 @@
if not vim.g.hflip then
local revolver = require('revolver')
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
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