Compare commits

...

3 Commits

Author SHA1 Message Date
Evgeni Chasnovski
4f15dbeb88
Merge 3cf2ad8dfa into deac7df80a 2024-09-13 00:03:19 +01:00
Evgeni Chasnovski
3cf2ad8dfa
feat(filetype): add fast option for faster filetype detection 2024-08-18 19:44:40 +03:00
Evgeni Chasnovski
ea7ea9cb36
fix(filetype): fix typo in 'vim' filetype content matching 2024-08-18 19:30:39 +03:00
5 changed files with 59 additions and 8 deletions

View File

@ -2781,6 +2781,9 @@ vim.filetype.match({args}) *vim.filetype.match()*
• {contents}? (`string[]`) An array of lines representing file
contents to use for matching. Can be used with {filename}.
Mutually exclusive with {buf}.
• {fast}? (`boolean`) Whether to skip time consuming detection
steps, like pattern and content matching. Makes detection
significantly faster at the cost of less filetypes detected.
Return (multiple): ~
(`string?`) If a match was found, the matched filetype.

View File

@ -148,7 +148,7 @@ LSP
LUA
TODO
|vim.filetype.match()| now takes an option `fast` to perform fast detection.
OPTIONS
@ -157,7 +157,7 @@ OPTIONS
PERFORMANCE
TODO
|vim.filetype.match()| overall performance is improved.
PLUGINS

View File

@ -2710,6 +2710,11 @@ end
--- matching. Can be used with {filename}. Mutually exclusive
--- with {buf}.
--- @field contents? string[]
---
--- Whether to skip time consuming detection steps, like
--- pattern and content matching. Makes detection significantly
--- faster at the cost of less filetypes detected.
--- @field fast? boolean
--- Perform filetype detection.
---
@ -2758,6 +2763,7 @@ function M.match(args)
local bufnr = args.buf
local name = args.filename
local contents = args.contents
local not_fast = not args.fast
if bufnr and not name then
name = api.nvim_buf_get_name(bufnr)
@ -2786,8 +2792,10 @@ function M.match(args)
-- Next, check the file path against available patterns with non-negative priority
-- Cache match results of all parent patterns to improve performance
local parent_matches = {}
ft, on_detect =
match_pattern_sorted(name, path, tail, pattern_sorted_pos, parent_matches, bufnr)
if not_fast then
ft, on_detect =
match_pattern_sorted(name, path, tail, pattern_sorted_pos, parent_matches, bufnr)
end
if ft then
return ft, on_detect
end
@ -2802,15 +2810,17 @@ function M.match(args)
end
-- Next, check patterns with negative priority
ft, on_detect =
match_pattern_sorted(name, path, tail, pattern_sorted_neg, parent_matches, bufnr)
if not_fast then
ft, on_detect =
match_pattern_sorted(name, path, tail, pattern_sorted_neg, parent_matches, bufnr)
end
if ft then
return ft, on_detect
end
end
-- Finally, check file contents
if contents or bufnr then
if not_fast and (contents or bufnr) then
if contents == nil then
assert(bufnr)
if api.nvim_buf_line_count(bufnr) > 101 then

View File

@ -1932,7 +1932,7 @@ local patterns_text = {
-- Mason
['^<[%%&].*>'] = 'mason',
-- Vim scripts (must have '" vim' as the first line to trigger this)
['^" *[vV]im$['] = 'vim',
['^" *[vV]im$'] = 'vim',
-- libcxx and libstdc++ standard library headers like ["iostream["] do not have
-- an extension, recognize the Emacs file mode.
['%-%*%-.*[cC]%+%+.*%-%*%-'] = 'cpp',

View File

@ -128,6 +128,44 @@ describe('vim.filetype', function()
)
end)
it('can be fast', function()
eq(
exec_lua(function()
return vim.filetype.match({ filename = 'init.lua', fast = true })
end),
'lua'
)
eq(
exec_lua(function()
return vim.filetype.match({ filename = 'Dockerfile', fast = true })
end),
'dockerfile'
)
-- Pattern and content matching should be skipped
eq(
exec_lua(function()
return {
no_fast = vim.filetype.match({ filename = '/etc/a2ps/top-secret.cfg' }),
yes_fast = vim.filetype.match({ filename = '/etc/a2ps/top-secret.cfg', fast = true }),
}
end),
{ no_fast = 'a2ps', yes_fast = nil }
)
eq(
exec_lua(function()
local buf = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_lines(buf, 0, -1, false, { '" vim' })
return {
no_fast = vim.filetype.match({ buf = buf }),
yes_fast = vim.filetype.match({ buf = buf, fast = true }),
}
end),
{ no_fast = 'vim', yes_fast = nil }
)
end)
it('considers extension mappings when matching from hashbang', function()
eq(
'fooscript',