nix-community.nixvim/plugins/helpers.nix

140 lines
3.6 KiB
Nix
Raw Normal View History

{ lib, ... }:
2021-01-02 16:53:38 +00:00
with lib;
rec {
# vim dictionaries are, in theory, compatible with JSON
toVimDict = args: toJSON
2021-01-02 16:53:38 +00:00
(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
2021-03-18 14:03:17 +00:00
if hasAttr "__raw" args then
args.__raw
else
"{" + (concatStringsSep ","
(mapAttrsToList
(n: v: if head (stringToCharacters n) == "@" then
toLuaObject v
else "[${toLuaObject n}] = " + (toLuaObject v))
2021-03-24 13:57:33 +00:00
(filterAttrs (n: v: !isNull v && toLuaObject 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!
builtins.toJSON args
else if builtins.isPath args then
builtins.toJSON (toString args)
2021-01-02 16:53:38 +00:00
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);
# Creates an option with a nullable type that defaults to null.
2021-02-11 15:24:00 +00:00
mkNullOrOption = type: desc: lib.mkOption {
type = lib.types.nullOr type;
default = null;
description = desc;
};
2021-02-11 15:24:00 +00:00
mkPlugin = { config, lib, ... }: {
name,
description,
extraPlugins ? [],
extraPackages ? [],
2021-02-11 15:24:00 +00:00
options ? {},
...
}: let
cfg = config.plugins.${name};
2021-02-11 15:24:00 +00:00
# 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.plugins.${name} = {
2021-02-11 15:24:00 +00:00
enable = mkEnableOption description;
} // pluginOptions;
config = mkIf cfg.enable {
inherit extraPlugins extraPackages globals;
2021-02-11 15:24:00 +00:00
};
};
globalVal = val: if builtins.isBool val then
(if val == false then 0 else 1)
else val;
2021-03-19 15:09:39 +00:00
mkDefaultOpt = { type, global, description ? null, example ? null, default ? null, value ? v: (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;
};
2021-03-18 14:03:17 +00:00
extraOptionsOptions = {
extraOptions = mkOption {
default = { };
type = types.attrs;
description = ''
These attributes will be added to the table parameter for the setup function.
(Can override other attributes set by nixvim)
'';
};
};
2021-03-18 14:03:17 +00:00
mkRaw = r: { __raw = r; };
2022-10-25 01:15:09 +02:00
wrapDo = string: ''
do
${string}
end
'';
2022-10-25 01:15:09 +02:00
rawType = types.submodule {
options = {
__raw = mkOption {
type = types.str;
description = "raw lua code";
default = "";
};
};
};
isRawType = v: lib.isAttrs v && lib.hasAttr "__raw" v && lib.isString v.__raw;
}