Skip to content

Commit 34aa3e0

Browse files
committed
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.
1 parent d2f733e commit 34aa3e0

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

modules/filetype.nix

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
}:
77
with lib;
88
let
9+
cfg = config.filetype;
10+
911
filetypeDefinition = helpers.mkNullOrOption (
1012
with types;
1113
attrsOf (oneOf [
@@ -52,7 +54,9 @@ in
5254
pattern = filetypeDefinition "set filetypes matching the specified pattern";
5355
};
5456

55-
config.extraConfigLua = helpers.mkIfNonNull' config.filetype ''
56-
vim.filetype.add(${helpers.toLuaObject config.filetype})
57-
'';
57+
config.extraConfigLua =
58+
lib.mkIf (cfg != null && (builtins.any (v: v != null) (builtins.attrValues cfg)))
59+
''
60+
vim.filetype.add(${helpers.toLuaObject cfg})
61+
'';
5862
}

tests/test-sources/modules/filetypes.nix

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,21 @@
3838
};
3939
};
4040
};
41+
42+
default-empty.module =
43+
{ config, ... }:
44+
{
45+
files.test = { };
46+
47+
assertions = [
48+
{
49+
assertion = builtins.match ".*vim\.filetype\..*" config.content == null;
50+
message = "No vim.filetype definitions should be present in init.lua by default.";
51+
}
52+
{
53+
assertion = builtins.match ".*vim\.filetype\..*" config.files.test.content == null;
54+
message = "No vim.filetype definitions should be present in files submodules by default.";
55+
}
56+
];
57+
};
4158
}

0 commit comments

Comments
 (0)