mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
perf(filetype): skip contents check in match()
if there is no contents (#29596)
Problem: `vim.filetype.match()` tries to match on contents even if there is no contents (empty buffer or `{''}` explicit contents). This results in extra avoidable execution duration for cases. It matters, for example, when trying to match filetype based solely on file name (which still needs `contents` or `buf` to properly match earlier in the code path). Solution: skip matching based solely on contents if it is `{''}`. This works because: - Matching solely on content is done after any user-configured `vim.filetype.add()` hooks. - All default matching on content might depend on supplied path *only* if there is non-empty content (like in `require('vim.filetype.detect').match_from_hashbang()`).
This commit is contained in:
parent
b3d94b1087
commit
dc04ef2a20
@ -2598,20 +2598,24 @@ function M.match(args)
|
|||||||
contents = M._getlines(bufnr)
|
contents = M._getlines(bufnr)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
-- If name is nil, catch any errors from the contents filetype detection function.
|
|
||||||
-- If the function tries to use the filename that is nil then it will fail,
|
-- Match based solely on content only if there is any content (for performance)
|
||||||
-- but this enables checks which do not need a filename to still work.
|
if not (#contents == 1 and contents[1] == '') then
|
||||||
local ok
|
-- If name is nil, catch any errors from the contents filetype detection function.
|
||||||
ok, ft, on_detect = pcall(
|
-- If the function tries to use the filename that is nil then it will fail,
|
||||||
require('vim.filetype.detect').match_contents,
|
-- but this enables checks which do not need a filename to still work.
|
||||||
contents,
|
local ok
|
||||||
name,
|
ok, ft, on_detect = pcall(
|
||||||
function(ext)
|
require('vim.filetype.detect').match_contents,
|
||||||
return dispatch(extension[ext], name, bufnr)
|
contents,
|
||||||
|
name,
|
||||||
|
function(ext)
|
||||||
|
return dispatch(extension[ext], name, bufnr)
|
||||||
|
end
|
||||||
|
)
|
||||||
|
if ok then
|
||||||
|
return ft, on_detect
|
||||||
end
|
end
|
||||||
)
|
|
||||||
if ok then
|
|
||||||
return ft, on_detect
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user