nix-community.nixvim/modules/filetype.nix
Stanislav Asunkin 34aa3e00e7 modules/filetype: ensure that the module does not set empty settings
When setting any filetype suboption to null (or anything else guarded by
mkIf) it's value becomes:

    { extension = null; filename = null; pattern = null; }

Account for that case in mkIf condition so that the option would not
produce empty filetype definition.
2024-07-22 11:33:52 +03:00

62 lines
1.7 KiB
Nix

{
lib,
helpers,
config,
...
}:
with lib;
let
cfg = config.filetype;
filetypeDefinition = helpers.mkNullOrOption (
with types;
attrsOf (oneOf [
# Raw filetype
str
# Function to set the filetype
helpers.nixvimTypes.rawLua
# ["filetype" {priority = xx;}]
(listOf (
either str (submodule {
options = {
priority = mkOption {
type = ints.unsigned;
description = ''
Filename patterns can specify an optional priority to resolve cases when a file path
matches multiple patterns.
Higher priorities are matched first.
When omitted, the priority defaults to 0.
A pattern can contain environment variables of the form `"''${SOME_VAR}"` that will
be automatically expanded.
If the environment variable is not set, the pattern won't be matched.
'';
};
};
})
))
])
);
in
{
options.filetype =
helpers.mkCompositeOption
''
Define additional filetypes. The values can either be a literal filetype or a function
taking the filepath and the buffer number.
For more information check `:h vim.filetype.add()`
''
{
extension = filetypeDefinition "set filetypes matching the file extension";
filename = filetypeDefinition "set filetypes matching the file name (or path)";
pattern = filetypeDefinition "set filetypes matching the specified pattern";
};
config.extraConfigLua =
lib.mkIf (cfg != null && (builtins.any (v: v != null) (builtins.attrValues cfg)))
''
vim.filetype.add(${helpers.toLuaObject cfg})
'';
}