switch source/header

This commit is contained in:
antonl 2026-04-01 17:49:56 +02:00
parent 52a6757c21
commit 4462d24f42

View File

@ -337,6 +337,87 @@ vim.keymap.set("n", "<C-PageDown>", function() jump_comment_divider_line(vim.api
vim.keymap.set("n", "<C-PageUp>", function() jump_comment_divider_line(vim.api.nvim_get_current_buf(), false) end) vim.keymap.set("n", "<C-PageUp>", function() jump_comment_divider_line(vim.api.nvim_get_current_buf(), false) end)
vim.keymap.set("n", "<A-m>", "<cmd>make<CR>", { silent = true }) vim.keymap.set("n", "<A-m>", "<cmd>make<CR>", { silent = true })
------- Toggle between header/source
local function switch_header_source(open_in_other_pane)
local current = vim.api.nvim_buf_get_name(0)
if current == '' then return end
local filename = vim.fn.fnamemodify(current, ':t:r') -- foo
local ext = vim.fn.fnamemodify(current, ':e')
local patterns = {}
if ext == 'c' then
patterns = { filename .. '.h' }
elseif ext == 'h' then
patterns = { filename .. '.c' }
elseif ext == 'cpp' then
patterns = { filename .. '.hpp', filename .. '.h' }
elseif ext == 'hpp' then
patterns = { filename .. '.cpp', filename .. '.cc', filename .. '.c' }
else
print('Not a C/C++ file')
return
end
-- Run ripgrep to find matching files
local cmd = "rg --files"
local handle = io.popen(cmd)
if not handle then return end
local result = handle:read("*a")
handle:close()
local matches = {}
for line in result:gmatch("[^\n]+") do
for _, pat in ipairs(patterns) do
if line:match(pat .. "$") then
table.insert(matches, line)
end
end
end
if #matches == 0 then
print('No alternate file found')
return
end
-- Prefer closest match (same dir first)
table.sort(matches, function(a, b)
local curdir = vim.fn.fnamemodify(current, ':p:h')
local a_score = a:find(curdir, 1, true) and 0 or 1
local b_score = b:find(curdir, 1, true) and 0 or 1
return a_score < b_score
end)
local target = matches[1]
if open_in_other_pane then
if #vim.api.nvim_list_wins() == 1 then
vim.cmd('vsplit')
else
vim.cmd('wincmd w')
end
end
vim.cmd('edit ' .. target)
end
-- Existing behavior: open in opposite pane
vim.keymap.set('n', '<A-2>', function()
switch_header_source(true)
end, { desc = 'Switch header/source in other pane' })
-- New behavior: open in current pane
vim.keymap.set('n', '<A-@>', function()
switch_header_source(false)
end, { desc = 'Switch header/source in current pane' })
-----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------
-- Colortheme -- Colortheme
local function fleury_brown() local function fleury_brown()
vim.o.termguicolors = true vim.o.termguicolors = true