This repository has been archived on 2025-01-07. You can view files and clone it, but cannot push or open issues or pull requests.
opdavies.nvim/plugin/edit_alternate.lua
Oliver Davies 9a7863e5e0 Add edit_alternate.vim
https://github.com/tjdevries/edit_alternate.vim

This works for switching between classes and tests in Drupal modules,
which is something I was struggling to do with Projectionist.

Different to Projectionist, though, this plugin doesn't create an
alternate file if it doesn't exist, and doesn't prompt to select from
multiple options and returns the first matching one.
2024-08-11 10:47:49 +01:00

41 lines
1.3 KiB
Lua

vim.fn["edit_alternate#rule#add"]("php", function(filename)
if filename:find "Test.php$" then
filename = filename:gsub("Test.php$", ".php")
if filename:find "tests/src/" then
-- Drupal tests. Remove the `src/{type}` from the path.
return filename:gsub("tests/src/(.-)/", "src/")
else
return filename:gsub("tests/", "src/")
end
else
filename = filename:gsub(".php$", "Test.php")
if filename:find "modules/custom" then
-- Drupal test types.
local test_types = { "Functional", "FunctionalJavaScript", "Kernel", "Unit" }
for _, test_type in ipairs(test_types) do
local filename_with_test_type = filename:gsub("src/", string.format("tests/src/%s/", test_type))
-- Return the first matching test file that exists.
if vim.fn.filereadable(filename_with_test_type) == 1 then
return filename_with_test_type
end
end
end
end
end)
if vim.fn.filereadable "fractal.config.js" == 1 then
vim.fn["edit_alternate#rule#add"]("twig", function(filename)
return (filename:gsub("%.twig$", ".config.yml"))
end)
vim.fn["edit_alternate#rule#add"]("yml", function(filename)
return (filename:gsub("%.config.yml$", ".twig"))
end)
end
vim.keymap.set("n", "<leader>ea", "<CMD>EditAlternate<CR>", { silent = true })