2020-12-31 18:15:19 +00:00
|
|
|
{ lib, ... }:
|
2021-01-02 16:53:38 +00:00
|
|
|
with lib;
|
|
|
|
rec {
|
2020-12-31 18:15:19 +00:00
|
|
|
# vim dictionaries are, in theory, compatible with JSON
|
2021-01-02 16:53:38 +00:00
|
|
|
toVimDict = args: toJSON
|
|
|
|
(lib.filterAttrs (n: v: !isNull v) args);
|
|
|
|
|
|
|
|
# Black functional magic that converts a bunch of different Nix types to their
|
|
|
|
# lua equivalents!
|
|
|
|
toLuaObject = args:
|
|
|
|
if builtins.isAttrs args then
|
|
|
|
"{" + (concatStringsSep ","
|
|
|
|
(mapAttrsToList
|
|
|
|
(n: v: "[${toLuaObject n}] = " + (toLuaObject v))
|
2021-01-05 11:26:49 +00:00
|
|
|
(filterAttrs (n: v: !isNull v || v == {}) args))) + "}"
|
2021-01-02 16:53:38 +00:00
|
|
|
else if builtins.isList args then
|
|
|
|
"{" + concatMapStringsSep "," toLuaObject args + "}"
|
|
|
|
else if builtins.isString args then
|
|
|
|
# This should be enough!
|
|
|
|
escapeShellArg args
|
|
|
|
else if builtins.isBool args then
|
|
|
|
"${ boolToString args }"
|
|
|
|
else if builtins.isFloat args then
|
|
|
|
"${ toString args }"
|
|
|
|
else if builtins.isInt args then
|
|
|
|
"${ toString args }"
|
|
|
|
else if isNull args then
|
|
|
|
"nil"
|
|
|
|
else "";
|
2021-01-05 16:11:56 +00:00
|
|
|
|
|
|
|
# Generates maps for a lua config
|
|
|
|
genMaps = mode: maps: let
|
|
|
|
normalized = builtins.mapAttrs (key: action:
|
|
|
|
if builtins.isString action then
|
|
|
|
{
|
|
|
|
silent = false;
|
|
|
|
expr = false;
|
|
|
|
unique = false;
|
|
|
|
noremap = true;
|
|
|
|
script = false;
|
|
|
|
nowait = false;
|
|
|
|
action = action;
|
|
|
|
}
|
|
|
|
else action) maps;
|
|
|
|
in builtins.attrValues (builtins.mapAttrs (key: action:
|
|
|
|
{
|
|
|
|
action = action.action;
|
|
|
|
config = lib.filterAttrs (_: v: v) {
|
|
|
|
inherit (action) silent expr unique noremap script nowait;
|
|
|
|
};
|
|
|
|
key = key;
|
|
|
|
mode = mode;
|
|
|
|
}) normalized);
|
2021-02-10 21:03:30 +00:00
|
|
|
|
|
|
|
# Creates an option with a nullable type that defaults to null.
|
2021-02-11 15:24:00 +00:00
|
|
|
mkNullOrOption = type: desc: lib.mkOption {
|
2021-02-10 21:03:30 +00:00
|
|
|
type = lib.types.nullOr type;
|
|
|
|
default = null;
|
|
|
|
description = desc;
|
|
|
|
};
|
2021-02-11 15:24:00 +00:00
|
|
|
|
|
|
|
mkPlugin = { config, lib, ... }: {
|
|
|
|
name,
|
|
|
|
description,
|
|
|
|
extraPlugins ? [],
|
|
|
|
options ? {},
|
|
|
|
...
|
|
|
|
}: let
|
|
|
|
cfg = config.programs.nixvim.plugins.${name};
|
|
|
|
# TODO support nested options!
|
|
|
|
pluginOptions = mapAttrs (k: v: v.option) options;
|
|
|
|
globals = mapAttrs' (name: opt: {
|
|
|
|
name = opt.global;
|
|
|
|
value = if cfg.${name} != null then opt.value cfg.${name} else null;
|
|
|
|
}) options;
|
|
|
|
in {
|
|
|
|
options.programs.nixvim.plugins.${name} = {
|
|
|
|
enable = mkEnableOption description;
|
|
|
|
} // pluginOptions;
|
|
|
|
|
|
|
|
config.programs.nixvim = mkIf cfg.enable {
|
|
|
|
inherit extraPlugins globals;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
globalVal = val: if builtins.isBool val then
|
|
|
|
(if val == false then 0 else 1)
|
|
|
|
else val;
|
|
|
|
|
2021-03-18 10:03:55 +00:00
|
|
|
mkDefaultOpt = { type, global, description ? null, example ? null, default ? null, value ? v: toLuaObject (globalVal v), ... }: {
|
2021-02-11 15:24:00 +00:00
|
|
|
option = mkOption {
|
|
|
|
type = types.nullOr type;
|
2021-03-18 10:03:55 +00:00
|
|
|
default = default;
|
2021-02-11 15:24:00 +00:00
|
|
|
description = description;
|
|
|
|
example = example;
|
|
|
|
};
|
|
|
|
|
|
|
|
inherit value global;
|
|
|
|
};
|
2020-12-31 18:15:19 +00:00
|
|
|
}
|