nix-community.nixvim/plugins/helpers.nix
Alexander Nortung 3f9effc575
general: add package options (#127)
* barbar: package option

* Base16: package option

* gruvbox: package option

* nord: package option

* one: package option

* onedark: package option

* tokyonight: package option

* nvim-cmp: package option

* coq: package option

* lspkind: package option

* helpers: added package option to mkPlugin

* fugitive: package option

* gitgutter: package option

* gitsigns: package option

* neogit: package option

* ledger: package option

* nix: package option

* plantuml-syntax: package option

* treesitter-context: package option + formatting

* treesitter-refactor: package option + formatting

* treesitter: package option

* zig: package option

* null-ls: package option

* null-ls/servers: package option

* lsp-lines: package option

* lspsaga: package option

* trouble: package option

* luasnip: added description for package option

* airline: package option

* lightline: package option

* lualine: package option

* telescope: package option

* telescope/frecency: package option

* telescope/fzf-native: package option

* telescope/media-files: package option

* comment-nvim: package option

* vim-commentary: package option

* dashboard: package option

* easyescape: package option

* emmet: package option

* endwise: package option

* floaterm: package option

* goyo: package option

* intellitab: package option

* mark-radar: package option

* notify: package option

* nvim-autopairs: package option

* nvim-tree: package option

* project-nvim: package option

* specs: package option

* startify: package option

* surround: package option

* undotree: package option
2023-01-19 10:45:15 +00:00

150 lines
4 KiB
Nix

{ lib, ... }:
with lib;
rec {
# vim dictionaries are, in theory, compatible with JSON
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
if hasAttr "__raw" args then
args.__raw
else
"{" + (concatStringsSep ","
(mapAttrsToList
(n: v: if head (stringToCharacters n) == "@" then
toLuaObject v
else "[${toLuaObject n}] = " + (toLuaObject v))
(filterAttrs (n: v: !isNull v && toLuaObject v != "{}") args))) + "}"
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)
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 "";
# 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.
mkNullOrOption = type: desc: lib.mkOption {
type = lib.types.nullOr type;
default = null;
description = desc;
};
mkPlugin = { config, lib, ... }: {
name,
description,
package ? null,
extraPlugins ? [],
extraPackages ? [],
options ? {},
...
}: let
cfg = config.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;
# does this evaluate package?
packageOption = if package == null then { } else {
package = mkOption {
type = types.package;
default = package;
description = "Plugin to use for ${name}";
};
};
in {
options.plugins.${name} = {
enable = mkEnableOption description;
} // packageOption // pluginOptions;
config = mkIf cfg.enable {
inherit extraPackages globals;
# does this evaluate package? it would not be desired to evaluate pacakge if we use another package.
extraPlugins = extraPlugins ++ optional (package != null) cfg.package;
};
};
globalVal = val: if builtins.isBool val then
(if val == false then 0 else 1)
else val;
mkDefaultOpt = { type, global, description ? null, example ? null, default ? null, value ? v: (globalVal v), ... }: {
option = mkOption {
type = types.nullOr type;
default = default;
description = description;
example = example;
};
inherit value global;
};
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)
'';
};
};
mkRaw = r: { __raw = r; };
wrapDo = string: ''
do
${string}
end
'';
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;
}