mirror of
https://github.com/nix-community/nixvim.git
synced 2025-06-21 00:25:42 +02:00
lib/neovim-plugin: Add lua configuration scoped to the plugin
This commit adds a `plugins.<name>.luaConfig` 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.
This commit is contained in:
parent
2bc6a94992
commit
d2f9e011d9
21 changed files with 129 additions and 35 deletions
|
@ -25,6 +25,11 @@
|
||||||
# colorscheme
|
# colorscheme
|
||||||
isColorscheme ? false,
|
isColorscheme ? false,
|
||||||
colorscheme ? name,
|
colorscheme ? name,
|
||||||
|
# luaConfig
|
||||||
|
configLocation ? if isColorscheme then "extraConfigLuaPre" else "extraConfigLua",
|
||||||
|
# For some plugins it may not make sense to have a configuration attribute, as they are
|
||||||
|
# configured through some other mean, like global variables
|
||||||
|
hasConfigAttrs ? true,
|
||||||
# options
|
# options
|
||||||
originalName ? name,
|
originalName ? name,
|
||||||
# Can be a string, a list of strings, or a module option:
|
# Can be a string, a list of strings, or a module option:
|
||||||
|
@ -60,7 +65,14 @@
|
||||||
let
|
let
|
||||||
cfg = config.${namespace}.${name};
|
cfg = config.${namespace}.${name};
|
||||||
opt = options.${namespace}.${name};
|
opt = options.${namespace}.${name};
|
||||||
extraConfigNamespace = if isColorscheme then "extraConfigLuaPre" else "extraConfigLua";
|
|
||||||
|
setupCode = ''
|
||||||
|
require('${luaName}')${setup}(${
|
||||||
|
lib.optionalString (cfg ? settings) (helpers.toLuaObject cfg.settings)
|
||||||
|
})
|
||||||
|
'';
|
||||||
|
|
||||||
|
setLuaConfig = lib.setAttrByPath (lib.toList configLocation);
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
meta = {
|
meta = {
|
||||||
|
@ -100,27 +112,40 @@
|
||||||
example = settingsExample;
|
example = settingsExample;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
// lib.optionalAttrs hasConfigAttrs {
|
||||||
|
luaConfig = lib.mkOption {
|
||||||
|
type = lib.nixvim.nixvimTypes.pluginLuaConfig;
|
||||||
|
default = { };
|
||||||
|
description = "The plugin's lua configuration";
|
||||||
|
};
|
||||||
|
}
|
||||||
// extraOptions;
|
// extraOptions;
|
||||||
|
|
||||||
config = lib.mkIf cfg.enable (
|
config =
|
||||||
lib.mkMerge [
|
assert lib.assertMsg (
|
||||||
{
|
callSetup -> configLocation != null
|
||||||
extraPlugins = (lib.optional installPackage cfg.package) ++ extraPlugins;
|
) "When a plugin has no config attrs and has a setup function it must have a config location";
|
||||||
inherit extraPackages;
|
lib.mkIf cfg.enable (
|
||||||
}
|
lib.mkMerge (
|
||||||
(lib.optionalAttrs callSetup {
|
[
|
||||||
${extraConfigNamespace} = ''
|
{
|
||||||
require('${luaName}')${setup}(${
|
extraPlugins = (lib.optional installPackage cfg.package) ++ extraPlugins;
|
||||||
lib.optionalString (cfg ? settings) (helpers.toLuaObject cfg.settings)
|
inherit extraPackages;
|
||||||
|
}
|
||||||
|
(lib.optionalAttrs (isColorscheme && (colorscheme != null)) {
|
||||||
|
colorscheme = lib.mkDefault colorscheme;
|
||||||
})
|
})
|
||||||
'';
|
(extraConfig cfg)
|
||||||
})
|
]
|
||||||
(lib.optionalAttrs (isColorscheme && (colorscheme != null)) {
|
++ (lib.optionals (!hasConfigAttrs) [
|
||||||
colorscheme = lib.mkDefault colorscheme;
|
(lib.optionalAttrs callSetup (setLuaConfig setupCode))
|
||||||
})
|
])
|
||||||
(extraConfig cfg)
|
++ (lib.optionals hasConfigAttrs [
|
||||||
]
|
(lib.optionalAttrs callSetup { ${namespace}.${name}.luaConfig.content = setupCode; })
|
||||||
);
|
(lib.optionalAttrs (configLocation != null) (setLuaConfig cfg.luaConfig.content))
|
||||||
|
])
|
||||||
|
)
|
||||||
|
);
|
||||||
};
|
};
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
|
|
|
@ -113,4 +113,38 @@ rec {
|
||||||
types.optionDescriptionPhrase (class: class == "noun" || class == "composite") elemType
|
types.optionDescriptionPhrase (class: class == "noun" || class == "composite") elemType
|
||||||
}";
|
}";
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pluginLuaConfig = types.submodule (
|
||||||
|
{ config, ... }:
|
||||||
|
{
|
||||||
|
options = {
|
||||||
|
pre = lib.mkOption {
|
||||||
|
type = types.lines;
|
||||||
|
default = "";
|
||||||
|
description = ''
|
||||||
|
Lua code inserted at the start of the plugin's configuration.
|
||||||
|
This is the same as using `lib.nixvim.utils.mkBeforeSection` when defining `content`.
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
post = lib.mkOption {
|
||||||
|
type = types.lines;
|
||||||
|
default = "";
|
||||||
|
description = ''
|
||||||
|
Lua code inserted at the end of the plugin's configuration.
|
||||||
|
This is the same as using `lib.nixvim.utils.mkAfterSection` when defining `content`.
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
content = lib.mkOption {
|
||||||
|
type = types.lines;
|
||||||
|
default = "";
|
||||||
|
description = "Configuration of the plugin";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
config.content = lib.mkMerge [
|
||||||
|
(lib.nixvim.utils.mkBeforeSection config.pre)
|
||||||
|
(lib.nixvim.utils.mkAfterSection config.post)
|
||||||
|
];
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -77,6 +77,12 @@ rec {
|
||||||
in
|
in
|
||||||
lib.concatStrings (map processWord words);
|
lib.concatStrings (map processWord words);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Those helpers control the lua sections split in `pre, content, post`
|
||||||
|
*/
|
||||||
|
mkBeforeSection = lib.mkOrder 300;
|
||||||
|
mkAfterSection = lib.mkOrder 2000;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Capitalize a string by making the first character uppercase.
|
Capitalize a string by making the first character uppercase.
|
||||||
|
|
||||||
|
|
|
@ -246,7 +246,7 @@ helpers.neovim-plugin.mkNeovimPlugin {
|
||||||
# ccc requires `termguicolors` to be enabled.
|
# ccc requires `termguicolors` to be enabled.
|
||||||
opts.termguicolors = lib.mkDefault true;
|
opts.termguicolors = lib.mkDefault true;
|
||||||
|
|
||||||
extraConfigLua = ''
|
plugins.ccc.luaConfig.content = ''
|
||||||
ccc = require('ccc')
|
ccc = require('ccc')
|
||||||
ccc.setup(${helpers.toLuaObject cfg.settings})
|
ccc.setup(${helpers.toLuaObject cfg.settings})
|
||||||
'';
|
'';
|
||||||
|
|
|
@ -82,7 +82,7 @@ helpers.neovim-plugin.mkNeovimPlugin {
|
||||||
coq_settings = cfg.settings;
|
coq_settings = cfg.settings;
|
||||||
};
|
};
|
||||||
|
|
||||||
extraConfigLua = "require('coq')";
|
plugins.coq-nvim.luaConfig.content = "require('coq')";
|
||||||
|
|
||||||
plugins.lsp = {
|
plugins.lsp = {
|
||||||
preConfig = ''
|
preConfig = ''
|
||||||
|
|
|
@ -36,7 +36,7 @@ helpers.neovim-plugin.mkNeovimPlugin {
|
||||||
|
|
||||||
callSetup = false;
|
callSetup = false;
|
||||||
extraConfig = cfg: {
|
extraConfig = cfg: {
|
||||||
extraConfigLua = ''
|
plugins.hydra.luaConfig.content = ''
|
||||||
hydra = require('hydra')
|
hydra = require('hydra')
|
||||||
|
|
||||||
hydra.setup(${helpers.toLuaObject cfg.settings})
|
hydra.setup(${helpers.toLuaObject cfg.settings})
|
||||||
|
|
|
@ -99,7 +99,7 @@ lib.nixvim.neovim-plugin.mkNeovimPlugin {
|
||||||
'';
|
'';
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
extraConfigLua =
|
plugins.mini.luaConfig.content =
|
||||||
lib.foldlAttrs (lines: name: config: ''
|
lib.foldlAttrs (lines: name: config: ''
|
||||||
${lines}
|
${lines}
|
||||||
require(${lib.nixvim.toLuaObject "mini.${name}"}).setup(${lib.nixvim.toLuaObject config})
|
require(${lib.nixvim.toLuaObject "mini.${name}"}).setup(${lib.nixvim.toLuaObject config})
|
||||||
|
|
|
@ -48,6 +48,8 @@ helpers.neovim-plugin.mkNeovimPlugin {
|
||||||
};
|
};
|
||||||
|
|
||||||
callSetup = false;
|
callSetup = false;
|
||||||
|
hasConfigAttrs = false;
|
||||||
|
configLocation = null;
|
||||||
extraConfig =
|
extraConfig =
|
||||||
cfg:
|
cfg:
|
||||||
mkMerge [
|
mkMerge [
|
||||||
|
|
|
@ -122,7 +122,7 @@ lib.nixvim.neovim-plugin.mkNeovimPlugin {
|
||||||
}
|
}
|
||||||
) cfg.keymaps;
|
) cfg.keymaps;
|
||||||
|
|
||||||
extraConfigLua = ''
|
plugins.telescope.luaConfig.content = ''
|
||||||
require('telescope').setup(${toLuaObject cfg.settings})
|
require('telescope').setup(${toLuaObject cfg.settings})
|
||||||
|
|
||||||
local __telescopeExtensions = ${toLuaObject cfg.enabledExtensions}
|
local __telescopeExtensions = ${toLuaObject cfg.enabledExtensions}
|
||||||
|
|
|
@ -431,7 +431,7 @@ helpers.neovim-plugin.mkNeovimPlugin {
|
||||||
installPackage = false;
|
installPackage = false;
|
||||||
|
|
||||||
extraConfig = cfg: {
|
extraConfig = cfg: {
|
||||||
extraConfigLua =
|
plugins.treesitter.luaConfig.content =
|
||||||
# NOTE: Upstream state that the parser MUST be at the beginning of runtimepath.
|
# 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.
|
# Otherwise the parsers from Neovim takes precedent, which may be incompatible with some queries.
|
||||||
(optionalString (cfg.settings.parser_install_dir != null) ''
|
(optionalString (cfg.settings.parser_install_dir != null) ''
|
||||||
|
|
|
@ -596,7 +596,7 @@ lib.nixvim.neovim-plugin.mkNeovimPlugin {
|
||||||
''
|
''
|
||||||
];
|
];
|
||||||
|
|
||||||
extraConfigLua = lib.optionalString opt.registrations.isDefined ''
|
plugins.which-key.luaConfig.content = lib.optionalString opt.registrations.isDefined ''
|
||||||
require("which-key").register(${toLuaObject cfg.registrations})
|
require("which-key").register(${toLuaObject cfg.registrations})
|
||||||
'';
|
'';
|
||||||
};
|
};
|
||||||
|
|
|
@ -373,7 +373,7 @@ helpers.neovim-plugin.mkNeovimPlugin {
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
extraConfigLua = ''
|
plugins.yanky.luaConfig.content = ''
|
||||||
do
|
do
|
||||||
local utils = require('yanky.utils')
|
local utils = require('yanky.utils')
|
||||||
${optionalString config.plugins.telescope.enable "local mapping = require('yanky.telescope.mapping')"}
|
${optionalString config.plugins.telescope.enable "local mapping = require('yanky.telescope.mapping')"}
|
||||||
|
|
|
@ -70,7 +70,7 @@ helpers.neovim-plugin.mkNeovimPlugin {
|
||||||
|
|
||||||
callSetup = false;
|
callSetup = false;
|
||||||
extraConfig = cfg: {
|
extraConfig = cfg: {
|
||||||
extraConfigLua =
|
plugins.cmp.luaConfig.content =
|
||||||
''
|
''
|
||||||
local cmp = require('cmp')
|
local cmp = require('cmp')
|
||||||
cmp.setup(${helpers.toLuaObject cfg.settings})
|
cmp.setup(${helpers.toLuaObject cfg.settings})
|
||||||
|
|
|
@ -39,7 +39,7 @@ lib.nixvim.neovim-plugin.mkNeovimPlugin {
|
||||||
};
|
};
|
||||||
|
|
||||||
extraConfig = cfg: {
|
extraConfig = cfg: {
|
||||||
extraConfigLuaPre = ''
|
colorschemes.ayu.luaConfig.content = ''
|
||||||
local ayu = require("ayu")
|
local ayu = require("ayu")
|
||||||
ayu.setup(${toLuaObject cfg.settings})
|
ayu.setup(${toLuaObject cfg.settings})
|
||||||
ayu.colorscheme()
|
ayu.colorscheme()
|
||||||
|
|
|
@ -179,7 +179,7 @@ lib.nixvim.neovim-plugin.mkNeovimPlugin {
|
||||||
# `settings` can either be passed to `with_config` before calling `setup`,
|
# `settings` can either be passed to `with_config` before calling `setup`,
|
||||||
# or it can be passed as `setup`'s 2nd argument.
|
# 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
|
# See https://github.com/RRethy/base16-nvim/blob/6ac181b5733518040a33017dde654059cd771b7c/lua/base16-colorscheme.lua#L107-L125
|
||||||
extraConfigLuaPre = ''
|
colorschemes.base16.luaConfig.content = ''
|
||||||
do
|
do
|
||||||
local base16 = require('${luaName}')
|
local base16 = require('${luaName}')
|
||||||
base16.with_config(${toLuaObject cfg.settings})
|
base16.with_config(${toLuaObject cfg.settings})
|
||||||
|
|
|
@ -34,7 +34,7 @@ lib.nixvim.neovim-plugin.mkNeovimPlugin {
|
||||||
callSetup = false;
|
callSetup = false;
|
||||||
colorscheme = null;
|
colorscheme = null;
|
||||||
extraConfig = cfg: {
|
extraConfig = cfg: {
|
||||||
extraConfigLuaPre = ''
|
colorschemes.onedark.luaConfig.content = ''
|
||||||
_onedark = require('onedark')
|
_onedark = require('onedark')
|
||||||
_onedark.setup(${lib.nixvim.toLuaObject cfg.settings})
|
_onedark.setup(${lib.nixvim.toLuaObject cfg.settings})
|
||||||
_onedark.load()
|
_onedark.load()
|
||||||
|
|
|
@ -31,7 +31,7 @@ lib.nixvim.neovim-plugin.mkNeovimPlugin {
|
||||||
};
|
};
|
||||||
|
|
||||||
extraConfig = cfg: {
|
extraConfig = cfg: {
|
||||||
extraConfigLuaPre = ''
|
colorschemes.vscode.luaConfig.content = ''
|
||||||
local _vscode = require("vscode")
|
local _vscode = require("vscode")
|
||||||
_vscode.setup(${toLuaObject cfg.settings})
|
_vscode.setup(${toLuaObject cfg.settings})
|
||||||
_vscode.load()
|
_vscode.load()
|
||||||
|
|
|
@ -147,7 +147,7 @@ helpers.neovim-plugin.mkNeovimPlugin {
|
||||||
];
|
];
|
||||||
|
|
||||||
# We only do this here because of enableLspFormat
|
# We only do this here because of enableLspFormat
|
||||||
extraConfigLua = ''
|
plugins.none-ls.luaConfig.content = ''
|
||||||
require("null-ls").setup(${helpers.toLuaObject setupOptions})
|
require("null-ls").setup(${helpers.toLuaObject setupOptions})
|
||||||
'';
|
'';
|
||||||
};
|
};
|
||||||
|
|
|
@ -190,7 +190,7 @@ nixvim.neovim-plugin.mkNeovimPlugin {
|
||||||
|
|
||||||
extraConfig = cfg: {
|
extraConfig = cfg: {
|
||||||
globals.lz_n = modules.mkAliasAndWrapDefsWithPriority id options.plugins.lz-n.settings;
|
globals.lz_n = modules.mkAliasAndWrapDefsWithPriority id options.plugins.lz-n.settings;
|
||||||
extraConfigLua = mkIf (cfg.plugins != [ ]) ''
|
plugins.lz-n.luaConfig.content = mkIf (cfg.plugins != [ ]) ''
|
||||||
require('lz.n').load( ${nixvim.toLuaObject cfg.plugins})
|
require('lz.n').load( ${nixvim.toLuaObject cfg.plugins})
|
||||||
'';
|
'';
|
||||||
};
|
};
|
||||||
|
|
|
@ -434,7 +434,8 @@ lib.nixvim.neovim-plugin.mkNeovimPlugin {
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
extraConfigLua = lib.mkIf cfg.enableTelescope ''require("telescope").load_extension("rest")'';
|
# TODO: There may be some interactions between this & telescope, maybe requiring #2292
|
||||||
|
plugins.rest.luaConfig.post = lib.mkIf cfg.enableTelescope ''require("telescope").load_extension("rest")'';
|
||||||
|
|
||||||
extraPackages = [ cfg.curlPackage ];
|
extraPackages = [ cfg.curlPackage ];
|
||||||
|
|
||||||
|
|
26
tests/test-sources/plugins/lua-config.nix
Normal file
26
tests/test-sources/plugins/lua-config.nix
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
{
|
||||||
|
lua-config-pre-post = {
|
||||||
|
extraConfigLuaPre = ''
|
||||||
|
list = {}
|
||||||
|
'';
|
||||||
|
plugins.cmp = {
|
||||||
|
enable = true;
|
||||||
|
luaConfig = {
|
||||||
|
pre = ''
|
||||||
|
table.insert(list, "pre")
|
||||||
|
'';
|
||||||
|
content = ''
|
||||||
|
table.insert(list, "init")
|
||||||
|
'';
|
||||||
|
post = ''
|
||||||
|
table.insert(list, "post")
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
};
|
||||||
|
extraConfigLuaPost = ''
|
||||||
|
if not vim.deep_equal(list, {"pre", "init", "post"}) then
|
||||||
|
vim.print("Unexpected list: " .. vim.inspect(list))
|
||||||
|
end
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue