added formatter + reformat existing codebase

This commit is contained in:
Gaetan Lepage 2023-02-19 22:28:08 +01:00
parent 0bf4313f22
commit 1cab8e5167
96 changed files with 3727 additions and 3341 deletions

View file

@ -1,146 +1,185 @@
{ lib, ... }:
with lib;
rec {
{lib, ...}:
with lib; rec {
# vim dictionaries are, in theory, compatible with JSON
toVimDict = args: toJSON
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
if builtins.isAttrs args
then
if hasAttr "__raw" args
then args.__raw
else
"{" + (concatStringsSep ","
"{"
+ (concatStringsSep ","
(mapAttrsToList
(n: v:
if head (stringToCharacters n) == "@" then
toLuaObject 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!
(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 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
genMaps = mode: maps: let
normalized =
builtins.mapAttrs
(key: action:
{
action = action.action;
config = lib.filterAttrs (_: v: v) {
inherit (action) silent expr unique noremap script nowait;
};
key = key;
mode = mode;
})
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;
};
mkNullOrOption = type: desc:
lib.mkOption {
type = lib.types.nullOr type;
default = null;
description = desc;
};
mkIfNonNull = c: mkIf (!isNull c) c;
defaultNullOpts = rec {
mkNullable = type: default: desc: mkNullOrOption type (
let
defaultDesc = "default: `${default}`";
in
if desc == "" then defaultDesc else ''
${desc}
mkNullable = type: default: desc:
mkNullOrOption type (
let
defaultDesc = "default: `${default}`";
in
if desc == ""
then defaultDesc
else ''
${desc}
${defaultDesc}
''
);
${defaultDesc}
''
);
mkInt = default: mkNullable lib.types.int (toString default);
mkBool = default: mkNullable lib.types.bool (if default then "true" else "false");
mkBool = default:
mkNullable lib.types.bool (
if default
then "true"
else "false"
);
mkStr = default: mkNullable lib.types.str ''${builtins.toString default}'';
mkEnum = enum: default: mkNullable (lib.types.enum enum) ''"${default}"'';
mkEnumFirstDefault = enum: mkEnum enum (head enum);
};
mkPackageOption = name: default: mkOption {
type = types.package;
inherit default;
description = "Plugin to use for ${name}";
};
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 = mkPackageOption name package;
};
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;
};
mkPackageOption = name: default:
mkOption {
type = types.package;
inherit default;
description = "Plugin to use for ${name}";
};
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 = mkPackageOption name package;
};
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)
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), ... }: {
mkDefaultOpt = {
type,
global,
description ? null,
example ? null,
default ? null,
value ? v: (globalVal v),
...
}: {
option = mkOption {
type = types.nullOr type;
default = default;
@ -153,7 +192,7 @@ rec {
extraOptionsOptions = {
extraOptions = mkOption {
default = { };
default = {};
type = types.attrs;
description = ''
These attributes will be added to the table parameter for the setup function.
@ -162,7 +201,7 @@ rec {
};
};
mkRaw = r: { __raw = r; };
mkRaw = r: {__raw = r;};
wrapDo = string: ''
do

View file

@ -1,73 +1,74 @@
{ lib, ... }:
with lib;
rec {
{lib, ...}:
with lib; rec {
# This should be used instead of mkRemovedOptionModule, when the option still works,
# but is just deprecated and should be changed now and for the future
mkDeprecatedOption =
{ option # an array of the path to the option
, alternative ? null
, message ? null
, visible ? false
}:
{ options, config, ... }:
let
fromOpt = getAttrFromPath option options;
fromValue = getAttrFromPath option config;
fullMessage = "The option `${showOption option}` has been deprecated, but might still work." +
(optionalString (alternative != null) " You may want to use `${showOption alternative}` instead.") +
(optionalString (message != null) " Message: ${message}");
in
{
config = mkIf (fromOpt.isDefined && fromValue != fromOpt.default) {
warnings = [
("Nixvim: ${fullMessage}")
];
};
};
mkRenamedOption =
{ option
, newOption
, visible ? false
, warn ? true
, overrideDescription ? null
}:
{ options, ... }:
let
fromOpt = getAttrFromPath option options;
# toOf = attrByPath newOption
# (abort "Renaming error: option `${showOption newOption}` does not exist.");
toType = let opt = attrByPath newOption { } options; in
opt.type or (types.submodule { });
message = "`${showOption option}` has been renamed to `${showOption newOption}`, but can still be used for compatibility";
in
{
options = setAttrByPath option (mkOption
{
inherit visible;
description =
if overrideDescription == null
then message
else overrideDescription;
} // optionalAttrs (toType != null) {
type = toType;
});
config = mkMerge [
{
warnings = mkIf (warn && fromOpt.isDefined) [ "Nixvim: ${message}" ];
}
(mkAliasAndWrapDefinitions (setAttrByPath newOption) fromOpt)
mkDeprecatedOption = {
option, # an array of the path to the option
alternative ? null,
message ? null,
visible ? false,
}: {
options,
config,
...
}: let
fromOpt = getAttrFromPath option options;
fromValue = getAttrFromPath option config;
fullMessage =
"The option `${showOption option}` has been deprecated, but might still work."
+ (optionalString (alternative != null) " You may want to use `${showOption alternative}` instead.")
+ (optionalString (message != null) " Message: ${message}");
in {
config = mkIf (fromOpt.isDefined && fromValue != fromOpt.default) {
warnings = [
"Nixvim: ${fullMessage}"
];
};
mkAliasOption = option: newOption: mkRenamedOption {
inherit option newOption;
visible = true;
warn = false;
overrideDescription = "Alias of ${showOption newOption}";
};
mkRenamedOption = {
option,
newOption,
visible ? false,
warn ? true,
overrideDescription ? null,
}: {options, ...}: let
fromOpt = getAttrFromPath option options;
# toOf = attrByPath newOption
# (abort "Renaming error: option `${showOption newOption}` does not exist.");
toType = let
opt = attrByPath newOption {} options;
in
opt.type or (types.submodule {});
message = "`${showOption option}` has been renamed to `${showOption newOption}`, but can still be used for compatibility";
in {
options = setAttrByPath option (mkOption
{
inherit visible;
description =
if overrideDescription == null
then message
else overrideDescription;
}
// optionalAttrs (toType != null) {
type = toType;
});
config = mkMerge [
{
warnings = mkIf (warn && fromOpt.isDefined) ["Nixvim: ${message}"];
}
(mkAliasAndWrapDefinitions (setAttrByPath newOption) fromOpt)
];
};
mkAliasOption = option: newOption:
mkRenamedOption {
inherit option newOption;
visible = true;
warn = false;
overrideDescription = "Alias of ${showOption newOption}";
};
# TODO:
# mkRemovedOption =
# { option