Skip to content

Commit

Permalink
refactor(runtime): modify vim.filetype.match to only return filetype
Browse files Browse the repository at this point in the history
Also write a lua version of dist#ft#FTtf and use it in filetype.lua

Work on neovim#18241
  • Loading branch information
dundargoc committed Apr 25, 2022
1 parent 440b65c commit 1ebeb61
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 19 deletions.
6 changes: 3 additions & 3 deletions runtime/filetype.lua
Expand Up @@ -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("<afile>"))
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
Expand Down
32 changes: 16 additions & 16 deletions runtime/lua/vim/filetype.lua
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
15 changes: 15 additions & 0 deletions 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

0 comments on commit 1ebeb61

Please sign in to comment.