diff --git a/plugins/default.nix b/plugins/default.nix index b4cbffd9..db32aae7 100644 --- a/plugins/default.nix +++ b/plugins/default.nix @@ -37,6 +37,7 @@ ./git/git-worktree.nix ./git/gitblame.nix ./git/gitgutter.nix + ./git/gitlinker.nix ./git/gitmessenger.nix ./git/gitsigns.nix ./git/neogit.nix diff --git a/plugins/git/gitlinker.nix b/plugins/git/gitlinker.nix new file mode 100644 index 00000000..c484b1c3 --- /dev/null +++ b/plugins/git/gitlinker.nix @@ -0,0 +1,119 @@ +{ + lib, + helpers, + config, + pkgs, + ... +}: +with lib; { + options.plugins.gitlinker = + helpers.extraOptionsOptions + // { + enable = mkEnableOption "gitlinker.nvim"; + + package = helpers.mkPackageOption "gitlinker.nvim" pkgs.vimPlugins.gitlinker-nvim; + + remote = helpers.mkNullOrOption types.str "Force the use of a specific remote."; + + addCurrentLineOnNormalMode = helpers.defaultNullOpts.mkBool true '' + Adds current line nr in the url for normal mode. + ''; + + actionCallback = + helpers.defaultNullOpts.mkNullable (with types; either str helpers.rawType) + "copy_to_clipboard" + '' + Callback for what to do with the url. + + Can be + - the name of a built-in callback. Example: `actionCallback = "copy_to_clipboard";` is + setting + ```lua + require('gitlinker.actions').copy_to_clipboard + ``` + in lua. + - Raw lua code `actionCallback.__raw = "function() ... end";`. + ''; + + printUrl = helpers.defaultNullOpts.mkBool true "Print the url after performing the action."; + + mappings = helpers.defaultNullOpts.mkStr "gy" "Mapping to call url generation."; + + callbacks = + helpers.defaultNullOpts.mkNullable + ( + with types; + attrsOf + ( + either + str + helpers.rawType + ) + ) + '' + { + "github.com" = "get_github_type_url"; + "gitlab.com" = "get_gitlab_type_url"; + "try.gitea.io" = "get_gitea_type_url"; + "codeberg.org" = "get_gitea_type_url"; + "bitbucket.org" = "get_bitbucket_type_url"; + "try.gogs.io" = "get_gogs_type_url"; + "git.sr.ht" = "get_srht_type_url"; + "git.launchpad.net" = "get_launchpad_type_url"; + "repo.or.cz" = "get_repoorcz_type_url"; + "git.kernel.org" = "get_cgit_type_url"; + "git.savannah.gnu.org" = "get_cgit_type_url"; + } + '' + '' + + Each key can be + - the name of a built-in callback. Example: `"get_gitlab_type_url";` is setting + ```lua + require('gitlinker.hosts').get_gitlab_type_url + ``` + in lua. + - Raw lua code `"github.com".__raw = "function(url_data) ... end";`. + + Learn more by reading `:h gitinker-callbacks`. + ''; + }; + + config = let + cfg = config.plugins.gitlinker; + in + mkIf cfg.enable { + extraPlugins = [cfg.package]; + + extraConfigLua = let + setupOptions = with cfg; + { + opts = { + inherit remote; + add_current_line_on_normal_mode = addCurrentLineOnNormalMode; + action_callback = + if isString actionCallback + then helpers.mkRaw "require('gitlinker.actions').${actionCallback}" + else actionCallback; + print_url = printUrl; + inherit mappings; + }; + callbacks = + helpers.ifNonNull' callbacks + ( + mapAttrs + ( + source: callback: + if isString callback + then helpers.mkRaw "require('gitlinker.hosts').${callback}" + else callback + ) + callbacks + ); + } + // cfg.extraOptions; + in '' + require('gitlinker').setup(${helpers.toLuaObject setupOptions}) + ''; + }; +} diff --git a/tests/test-sources/plugins/git/gitlinker.nix b/tests/test-sources/plugins/git/gitlinker.nix new file mode 100644 index 00000000..b089bffe --- /dev/null +++ b/tests/test-sources/plugins/git/gitlinker.nix @@ -0,0 +1,31 @@ +{ + empty = { + plugins.gitlinker.enable = true; + }; + + defaults = { + plugins.gitlinker = { + enable = true; + + remote = null; + addCurrentLineOnNormalMode = true; + actionCallback = "copy_to_clipboard"; + printUrl = true; + mappings = "gy"; + + callbacks = { + "github.com" = "get_github_type_url"; + "gitlab.com" = "get_gitlab_type_url"; + "try.gitea.io" = "get_gitea_type_url"; + "codeberg.org" = "get_gitea_type_url"; + "bitbucket.org" = "get_bitbucket_type_url"; + "try.gogs.io" = "get_gogs_type_url"; + "git.sr.ht" = "get_srht_type_url"; + "git.launchpad.net" = "get_launchpad_type_url"; + "repo.or.cz" = "get_repoorcz_type_url"; + "git.kernel.org" = "get_cgit_type_url"; + "git.savannah.gnu.org" = "get_cgit_type_url"; + }; + }; + }; +}