Skip to content

Commit

Permalink
lib/neovim-plugin: Add lua configuration scoped to the plugin
Browse files Browse the repository at this point in the history
This commit adds a `plugins.<name>.config` section controlling the
plugin specific configuration.

The section contains the internal `init` option, containing the plugin's
initialization code.

It also contains the public `pre` and `post` options, that allow to add
code before & after the `init` section

Finally, it contains the `final` option, being the concatenation of the
three previous options.
  • Loading branch information
traxys committed Aug 20, 2024
1 parent cb41399 commit 7c6e818
Show file tree
Hide file tree
Showing 18 changed files with 82 additions and 27 deletions.
31 changes: 25 additions & 6 deletions lib/neovim-plugin.nix
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ with lib;
hasSettings ? true,
extraOptions ? { },
# config
configLocation ? [ (if isColorscheme then "extraConfigLuaPre" else "extraConfigLua") ],
# Required for plugins.flash, as it used to define config
skipConfigAttrs ? false,
luaName ? name,
setup ? ".setup",
extraConfig ? cfg: { },
Expand Down Expand Up @@ -79,6 +82,13 @@ with lib;

package = helpers.mkPluginPackageOption originalName defaultPackage;
}
// optionalAttrs (!skipConfigAttrs) {
config = lib.mkOption {
type = nixvim.nixvimTypes.pluginConfig;
default = { };
description = "The plugin's lua configuration";
};
}
// optionalAttrs hasSettings {
settings = helpers.mkSettingsOption {
description = settingsDescription;
Expand All @@ -91,18 +101,27 @@ with lib;
config =
let
cfg = config.${namespace}.${name};
extraConfigNamespace = if isColorscheme then "extraConfigLuaPre" else "extraConfigLua";
in
mkIf cfg.enable (mkMerge [
{
extraPlugins = (optional installPackage cfg.package) ++ extraPlugins;
inherit extraPackages;
}
(optionalAttrs callSetup {
${extraConfigNamespace} = ''
require('${luaName}')${setup}(${optionalString (cfg ? settings) (helpers.toLuaObject cfg.settings)})
'';
})
(
let
setupCode = ''
require('${luaName}')${setup}(${optionalString (cfg ? settings) (helpers.toLuaObject cfg.settings)})
'';
in
if !skipConfigAttrs then
(optionalAttrs callSetup { ${namespace}.${name}.config.init = setupCode; })
// (optionalAttrs (configLocation != null) (setAttrByPath (toList configLocation) cfg.config.final))
else
assert assertMsg (
callSetup -> configLocation != null
) "When a plugin has no config attrs and has a setup function it must have a config location";
optionalAttrs callSetup (setAttrByPath (toList configLocation) setupCode)
)
(optionalAttrs (isColorscheme && (colorscheme != null)) { colorscheme = mkDefault colorscheme; })
(extraConfig cfg)
]);
Expand Down
35 changes: 35 additions & 0 deletions lib/types.nix
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,41 @@ rec {
optionDescriptionPhrase (class: class == "noun" || class == "composite") elemType
}";
};

pluginConfig = types.submodule (
{ config, ... }:
{
options = {
pre = mkOption {
type = types.lines;
default = "";
description = "Configuration before the initialization of the plugin";
};
post = mkOption {
type = types.lines;
default = "";
description = "Configuration after the initialization of the plugin";
};
init = mkOption {
type = types.lines;
internal = true;
default = "";
description = "Initialization code (between pre & post)";
};
final = mkOption {
type = types.str;
description = "Complete plugin configuration";
readOnly = true;
};
};

config.final = nixvim.concatNonEmptyLines [
config.pre
config.init
config.post
];
}
);
}
# Allow to do `with nixvimTypes;` instead of `with types;`
// lib.types
2 changes: 1 addition & 1 deletion plugins/colorschemes/ayu.nix
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ helpers.neovim-plugin.mkNeovimPlugin config {
};

extraConfig = cfg: {
extraConfigLuaPre = ''
colorschemes.ayu.config.init = ''
local ayu = require("ayu")
ayu.setup(${helpers.toLuaObject cfg.settings})
ayu.colorscheme()
Expand Down
2 changes: 1 addition & 1 deletion plugins/colorschemes/base16/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ helpers.neovim-plugin.mkNeovimPlugin config {
# `settings` can either be passed to `with_config` before calling `setup`,
# or it can be passed as `setup`'s 2nd argument.
# See https://github.com/RRethy/base16-nvim/blob/6ac181b5733518040a33017dde654059cd771b7c/lua/base16-colorscheme.lua#L107-L125
extraConfigLuaPre = ''
colorschemes.base16.config.init = ''
do
local base16 = require('${luaName}')
base16.with_config(${helpers.toLuaObject cfg.settings})
Expand Down
3 changes: 2 additions & 1 deletion plugins/colorschemes/onedark.nix
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ helpers.neovim-plugin.mkNeovimPlugin config {
callSetup = false;
colorscheme = null;
extraConfig = cfg: {
extraConfigLuaPre = ''
colorschemes.onedark.config.init = ''
_onedark = require('onedark')
_onedark.setup(${helpers.toLuaObject cfg.settings})
_onedark.load()
'';
extraConfigLuaPre = cfg.config.final;
};
}
2 changes: 1 addition & 1 deletion plugins/colorschemes/vscode.nix
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ helpers.neovim-plugin.mkNeovimPlugin config {
};

extraConfig = cfg: {
extraConfigLuaPre = ''
colorschemes.vscode.config.init = ''
local _vscode = require("vscode")
_vscode.setup(${helpers.toLuaObject cfg.settings})
_vscode.load()
Expand Down
2 changes: 1 addition & 1 deletion plugins/completion/cmp/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ helpers.neovim-plugin.mkNeovimPlugin config {

callSetup = false;
extraConfig = cfg: {
extraConfigLua =
plugins.cmp.config.init =
''
local cmp = require('cmp')
cmp.setup(${helpers.toLuaObject cfg.settings})
Expand Down
2 changes: 1 addition & 1 deletion plugins/completion/coq.nix
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ helpers.neovim-plugin.mkNeovimPlugin config {
coq_settings = cfg.settings;
};

extraConfigLua = "require('coq')";
plugins.coq-nvim.config.init = "require('coq')";

plugins.lsp = {
preConfig = ''
Expand Down
13 changes: 6 additions & 7 deletions plugins/languages/rust/rustaceanvim/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,9 @@ helpers.neovim-plugin.mkNeovimPlugin config {
};

callSetup = false;
configLocation = null;
extraConfig =
cfg:
let
configStr = ''
vim.g.rustaceanvim = ${helpers.toLuaObject cfg.settings}
'';
in
mkMerge [
{
extraPackages = [ cfg.rustAnalyzerPackage ];
Expand All @@ -74,6 +70,9 @@ helpers.neovim-plugin.mkNeovimPlugin config {
Note that if you supplied an attrset and not a function you need to set this attr set in:
`plugins.rustaceanvim.settings.server.default_settings.rust-analyzer'.
'';
plugins.rustaceanvim.config.init = ''
vim.g.rustaceanvim = ${helpers.toLuaObject cfg.settings}
'';
}
# If nvim-lspconfig is enabled:
(mkIf config.plugins.lsp.enable {
Expand All @@ -82,9 +81,9 @@ helpers.neovim-plugin.mkNeovimPlugin config {

# Ensure the plugin config is placed **after** the rest of the LSP configuration
# (and thus after the declaration of `__lspOnAttach`)
plugins.lsp.postConfig = configStr;
plugins.lsp.postConfig = cfg.config.final;
})
# Else, just put the plugin config anywhere
(mkIf (!config.plugins.lsp.enable) { extraConfigLua = configStr; })
(mkIf (!config.plugins.lsp.enable) { extraConfigLua = cfg.config.final; })
];
}
2 changes: 1 addition & 1 deletion plugins/languages/treesitter/treesitter.nix
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ helpers.neovim-plugin.mkNeovimPlugin config {
installPackage = false;

extraConfig = cfg: {
extraConfigLua =
plugins.treesitter.config.init =
# NOTE: Upstream state that the parser MUST be at the beginning of runtimepath.
# Otherwise the parsers from Neovim takes precedent, which may be incompatible with some queries.
(optionalString (cfg.settings.parser_install_dir != null) ''
Expand Down
2 changes: 1 addition & 1 deletion plugins/none-ls/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ helpers.neovim-plugin.mkNeovimPlugin config {
];

# We only do this here because of enableLspFormat
extraConfigLua = ''
plugins.none-ls.config.init = ''
require("null-ls").setup(${helpers.toLuaObject setupOptions})
'';
};
Expand Down
2 changes: 1 addition & 1 deletion plugins/pluginmanagers/lz-n.nix
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ nixvim.neovim-plugin.mkNeovimPlugin config {

extraConfig = cfg: {
globals.lz_n = modules.mkAliasAndWrapDefsWithPriority id options.plugins.lz-n.settings;
extraConfigLua = mkIf (cfg.plugins != [ ]) ''
plugins.lz-n.config.init = mkIf (cfg.plugins != [ ]) ''
require('lz.n').load( ${nixvim.toLuaObject cfg.plugins})
'';
};
Expand Down
2 changes: 1 addition & 1 deletion plugins/telescope/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ helpers.neovim-plugin.mkNeovimPlugin config {
}
) cfg.keymaps;

extraConfigLua = ''
plugins.telescope.config.init = ''
require('telescope').setup(${helpers.toLuaObject cfg.settings})
local __telescopeExtensions = ${helpers.toLuaObject cfg.enabledExtensions}
Expand Down
2 changes: 1 addition & 1 deletion plugins/utils/ccc.nix
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ helpers.neovim-plugin.mkNeovimPlugin config {
# ccc requires `termguicolors` to be enabled.
opts.termguicolors = lib.mkDefault true;

extraConfigLua = ''
plugins.ccc.config.init = ''
ccc = require('ccc')
ccc.setup(${helpers.toLuaObject cfg.settings})
'';
Expand Down
1 change: 1 addition & 0 deletions plugins/utils/flash.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ helpers.neovim-plugin.mkNeovimPlugin config {
originalName = "flash.nvim";
defaultPackage = pkgs.vimPlugins.flash-nvim;

skipConfigAttrs = true;
maintainers = with maintainers; [
traxys
MattSturgeon
Expand Down
2 changes: 1 addition & 1 deletion plugins/utils/hydra/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ helpers.neovim-plugin.mkNeovimPlugin config {

callSetup = false;
extraConfig = cfg: {
extraConfigLua = ''
plugins.hydra.config.init = ''
hydra = require('hydra')
hydra.setup(${helpers.toLuaObject cfg.settings})
Expand Down
2 changes: 1 addition & 1 deletion plugins/utils/which-key.nix
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ lib.nixvim.neovim-plugin.mkNeovimPlugin config {
''
];

extraConfigLua = lib.optionalString opt.registrations.isDefined ''
plugins.which-key.config.init = lib.optionalString opt.registrations.isDefined ''
require("which-key").register(${toLuaObject cfg.registrations})
'';
};
Expand Down
2 changes: 1 addition & 1 deletion plugins/utils/yanky.nix
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ helpers.neovim-plugin.mkNeovimPlugin config {
}
];

extraConfigLua = ''
plugins.yanky.config.init = ''
do
local utils = require('yanky.utils')
${optionalString config.plugins.telescope.enable "local mapping = require('yanky.telescope.mapping')"}
Expand Down

0 comments on commit 7c6e818

Please sign in to comment.