diff --git a/modules/filetype.nix b/modules/filetype.nix new file mode 100644 index 00000000..28e4b061 --- /dev/null +++ b/modules/filetype.nix @@ -0,0 +1,40 @@ +{ + config, + lib, + ... +} @ args: +with lib; let + helpers = import ../lib/helpers.nix args; + filetypeDefinition = helpers.mkNullOrOption (types.attrsOf ( + types.oneOf [ + # Raw filetype + types.str + # Function to set the filetype + helpers.rawType + # ["filetype" {priority = xx;}] + (types.listOf (types.either types.str (types.submodule { + options = { + priority = mkOption { + type = types.int; + }; + }; + }))) + ] + )); +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 = helpers.mkIfNonNull' config.filetype '' + vim.filetype.add(${helpers.toLuaObject config.filetype}) + ''; +} diff --git a/tests/test-sources/modules/filetypes.nix b/tests/test-sources/modules/filetypes.nix new file mode 100644 index 00000000..b91d93e5 --- /dev/null +++ b/tests/test-sources/modules/filetypes.nix @@ -0,0 +1,41 @@ +{ + example = { + filetype = { + extension = { + foo = "fooscript"; + bar.__raw = '' + function(path, bufnr) + if some_condition() then + return 'barscript', function(bufnr) + -- Set a buffer variable + vim.b[bufnr].barscript_version = 2 + end + end + return 'bar' + end + ''; + }; + filename = { + ".foorc" = "toml"; + "/etc/foo/config" = "toml"; + }; + pattern = { + ".*/etc/foo.*" = "fooscript"; + ".*/etc/foo.*%.conf" = [ + "dosini" + {priority = 10;} + ]; + "\${XDG_CONFIG_HOME}/foo/git" = "git"; + "README.(a+)$".__raw = '' + function(path, bufnr, ext) + if ext == 'md' then + return 'markdown' + elseif ext == 'rst' then + return 'rst' + end + end + ''; + }; + }; + }; +}