From 9a7863e5e0083a69f055795984819bced211cde5 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Sun, 11 Aug 2024 01:52:07 +0100 Subject: [PATCH] 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. --- lib/default.nix | 4 ++++ overlays/vim-plugins.nix | 30 +++++++++++++++++++++++++++++ plugin/edit_alternate.lua | 40 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 plugin/edit_alternate.lua diff --git a/lib/default.nix b/lib/default.nix index a5809c9..d9c64df 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -187,6 +187,10 @@ rec { # Themes vimPlugins.catppuccin-nvim + vimPlugins.conf-vim + vimPlugins.edit-alternate-vim + vimPlugins.standard-vim + # Configuration. opdavies-nvim ]; diff --git a/overlays/vim-plugins.nix b/overlays/vim-plugins.nix index ff9c6f7..8130ba2 100644 --- a/overlays/vim-plugins.nix +++ b/overlays/vim-plugins.nix @@ -1,5 +1,35 @@ final: prev: { vimPlugins = prev.vimPlugins // { + conf-vim = final.vimUtils.buildVimPlugin { + name = "conf-vim"; + src = final.fetchFromGitHub { + owner = "tjdevries"; + repo = "conf.vim"; + rev = "master"; + sha256 = "AjiTJsoim0BAnyfqk1IQzNsa6jhFM2+A66E7q9sJqz0="; + }; + }; + + edit-alternate-vim = final.vimUtils.buildVimPlugin { + name = "edit-alternate-vim"; + src = final.fetchFromGitHub { + owner = "tjdevries"; + repo = "edit_alternate.vim"; + rev = "master"; + sha256 = "mEKnqYAhgrdxPRoKf4S4yYecdFIHGg8bDxpqPuC1+S4="; + }; + }; + + standard-vim = final.vimUtils.buildVimPlugin { + name = "standard-vim"; + src = final.fetchFromGitHub { + owner = "tjdevries"; + repo = "standard.vim"; + rev = "master"; + sha256 = "9VwkvV1Dv6cE4uDkPp36DozjWJOclDR883yDMYw000E="; + }; + }; + tabline-vim = final.vimUtils.buildVimPlugin { name = "tabline-vim"; src = final.fetchFromGitHub { diff --git a/plugin/edit_alternate.lua b/plugin/edit_alternate.lua new file mode 100644 index 0000000..293a28b --- /dev/null +++ b/plugin/edit_alternate.lua @@ -0,0 +1,40 @@ +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", "ea", "EditAlternate", { silent = true })