mirror of
https://github.com/neovim/neovim.git
synced 2024-09-17 20:58:20 -04:00
Compare commits
3 Commits
28a1c57e5c
...
4f15dbeb88
Author | SHA1 | Date | |
---|---|---|---|
|
4f15dbeb88 | ||
|
3cf2ad8dfa | ||
|
ea7ea9cb36 |
@ -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.
|
||||
|
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
@ -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',
|
||||
|
@ -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',
|
||||
|
Loading…
Reference in New Issue
Block a user