diff --git a/runtime/filetype.lua b/runtime/filetype.lua index 47d55d8465e3c7..2bb63743356bb8 100644 --- a/runtime/filetype.lua +++ b/runtime/filetype.lua @@ -11,9 +11,9 @@ vim.api.nvim_create_augroup("filetypedetect", {clear = false}) vim.api.nvim_create_autocmd({"BufRead", "BufNewFile"}, { group = "filetypedetect", - callback = function() - vim.filetype.match(vim.fn.expand("")) - end, + callback = function(args) + vim.api.nvim_buf_set_option(args.buf, "filetype", vim.filetype.match(args.file)) + end }) -- These *must* be sourced after the autocommand above is created diff --git a/runtime/lua/vim/filetype.lua b/runtime/lua/vim/filetype.lua index 420d343a8a89ed..4a1460e346e017 100644 --- a/runtime/lua/vim/filetype.lua +++ b/runtime/lua/vim/filetype.lua @@ -851,7 +851,7 @@ local extension = { stm = function() vim.fn["dist#ft#FThtml"]() end, tcsh = function() vim.fn["dist#ft#SetFileTypeShell"]("tcsh") end, tex = function() vim.fn["dist#ft#FTtex"]() end, - tf = function() vim.fn["dist#ft#FTtf"]() end, + tf = function(path, bufnr) return require("vim.filetype.detect").tf(path, bufnr) end, w = function() vim.fn["dist#ft#FTprogress_cweb"]() end, xml = function() vim.fn["dist#ft#FTxml"]() end, y = function() vim.fn["dist#ft#FTy"]() end, @@ -1562,11 +1562,6 @@ local function dispatch(ft, path, bufnr, ...) ft = ft(path, bufnr, ...) end - if type(ft) == "string" then - api.nvim_buf_set_option(bufnr, "filetype", ft) - return true - end - -- Any non-falsey value (that is, anything other than 'nil' or 'false') will -- end filetype matching. This is useful for e.g. the dist#ft functions that -- return 0, but set the buffer's filetype themselves @@ -1603,14 +1598,16 @@ function M.match(name, bufnr) -- First check for the simple case where the full path exists as a key local path = vim.fn.resolve(vim.fn.fnamemodify(name, ":p")) - if dispatch(filename[path], path, bufnr) then - return + local ft = dispatch(filename[path], path, bufnr) + if ft then + return ft end -- Next check against just the file name local tail = vim.fn.fnamemodify(name, ":t") - if dispatch(filename[tail], path, bufnr) then - return + ft = dispatch(filename[tail], path, bufnr) + if ft then + return ft end -- Next, check the file path against available patterns with non-negative priority @@ -1626,16 +1623,18 @@ function M.match(name, bufnr) local ft = v[k][1] local matches = match_pattern(name, path, tail, k) if matches then - if dispatch(ft, path, bufnr, matches) then - return + ft = dispatch(ft, path, bufnr, matches) + if ft then + return ft end end end -- Next, check file extension local ext = vim.fn.fnamemodify(name, ":e") - if dispatch(extension[ext], path, bufnr) then - return + ft = dispatch(extension[ext], path, bufnr) + if ft then + return ft end -- Finally, check patterns with negative priority @@ -1646,8 +1645,9 @@ function M.match(name, bufnr) local ft = v[k][1] local matches = match_pattern(name, path, tail, k) if matches then - if dispatch(ft, path, bufnr, matches) then - return + ft = dispatch(ft, path, bufnr, matches) + if ft then + return ft end end end diff --git a/runtime/lua/vim/filetype/detect.lua b/runtime/lua/vim/filetype/detect.lua new file mode 100644 index 00000000000000..dddd3679d2f13e --- /dev/null +++ b/runtime/lua/vim/filetype/detect.lua @@ -0,0 +1,15 @@ +local M = {} + +-- Determine if a *.tf file is TF mud client or terraform +function M.tf(_, bufnr) + number_of_lines = vim.api.nvim_buf_line_count(bufnr) + for _,line in ipairs(vim.api.nvim_buf_get_lines(bufnr, 0, number_of_lines, true)) do + if not line:find("^[;/ ]") then + return "terraform" + end + end + + return "tf" +end + +return M