diff options
| author | Gregory Anders <8965202+gpanders@users.noreply.github.com> | 2022-06-09 13:12:36 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-06-09 13:12:36 -0600 |
| commit | 58323b1fe2e494cf6a75f108780e21c08996c08e (patch) | |
| tree | a301a6d047003efa3835460a793a87d4f205f785 /runtime/lua/vim/filetype | |
| parent | 28e43881b74b800fa37957e77b5e56994e1120cd (diff) | |
| download | rneovim-58323b1fe2e494cf6a75f108780e21c08996c08e.tar.gz rneovim-58323b1fe2e494cf6a75f108780e21c08996c08e.tar.bz2 rneovim-58323b1fe2e494cf6a75f108780e21c08996c08e.zip | |
feat(filetype): remove side effects from vim.filetype.match (#18894)
Many filetypes from filetype.vim set buffer-local variables, meaning
vim.filetype.match cannot be used without side effects. Instead of
setting these buffer-local variables in the filetype detection functions
themselves, have vim.filetype.match return an optional function value
that, when called, sets these variables. This allows vim.filetype.match
to work without side effects.
Diffstat (limited to 'runtime/lua/vim/filetype')
| -rw-r--r-- | runtime/lua/vim/filetype/detect.lua | 83 |
1 files changed, 48 insertions, 35 deletions
diff --git a/runtime/lua/vim/filetype/detect.lua b/runtime/lua/vim/filetype/detect.lua index 2cc75b026e..7e5ed0f4d1 100644 --- a/runtime/lua/vim/filetype/detect.lua +++ b/runtime/lua/vim/filetype/detect.lua @@ -27,24 +27,22 @@ local matchregex = vim.filetype.matchregex -- This function checks for the kind of assembly that is wanted by the user, or -- can be detected from the first five lines of the file. function M.asm(bufnr) - -- Make sure b:asmsyntax exists - if not vim.b[bufnr].asmsyntax then - vim.b[bufnr].asmsyntax = '' - end - - if vim.b[bufnr].asmsyntax == '' then - M.asm_syntax(bufnr) + local syntax = vim.b[bufnr].asmsyntax + if not syntax or syntax == '' then + syntax = M.asm_syntax(bufnr) end -- If b:asmsyntax still isn't set, default to asmsyntax or GNU - if vim.b[bufnr].asmsyntax == '' then + if not syntax or syntax == '' then if vim.g.asmsyntax and vim.g.asmsyntax ~= 0 then - vim.b[bufnr].asmsyntax = vim.g.asmsyntax + syntax = vim.g.asmsyntax else - vim.b[bufnr].asmsyntax = 'asm' + syntax = 'asm' end end - return vim.fn.fnameescape(vim.b[bufnr].asmsyntax) + return syntax, function(b) + vim.b[b].asmsyntax = syntax + end end -- Checks the first 5 lines for a asmsyntax=foo override. @@ -53,9 +51,9 @@ function M.asm_syntax(bufnr) local lines = table.concat(getlines(bufnr, 1, 5), ' '):lower() local match = lines:match('%sasmsyntax=([a-zA-Z0-9]+)%s') if match then - vim.b['asmsyntax'] = match + return match elseif findany(lines, { '%.title', '%.ident', '%.macro', '%.subtitle', '%.library' }) then - vim.b['asmsyntax'] = 'vmasm' + return 'vmasm' end end @@ -376,12 +374,13 @@ function M.inc(bufnr) elseif findany(lines, { '^%s{', '^%s%(%*' }) or matchregex(lines, pascal_keywords) then return 'pascal' else - M.asm_syntax(bufnr) - if vim.b[bufnr].asm_syntax then - return vim.fn.fnameescape(vim.b[bufnr].asm_syntax) - else + local syntax = M.asm_syntax(bufnr) + if not syntax or syntax == '' then return 'pov' end + return syntax, function(b) + vim.b[b].asmsyntax = syntax + end end end @@ -778,6 +777,8 @@ function M.sh(path, bufnr, name) return end + local on_detect + if matchregex(name, [[\<csh\>]]) then -- Some .sh scripts contain #!/bin/csh. return M.shell(path, bufnr, 'csh') @@ -788,19 +789,25 @@ function M.sh(path, bufnr, name) elseif matchregex(name, [[\<zsh\>]]) then return M.shell(path, bufnr, 'zsh') elseif matchregex(name, [[\<ksh\>]]) then - vim.b[bufnr].is_kornshell = 1 - vim.b[bufnr].is_bash = nil - vim.b[bufnr].is_sh = nil + on_detect = function(b) + vim.b[b].is_kornshell = 1 + vim.b[b].is_bash = nil + vim.b[b].is_sh = nil + end elseif vim.g.bash_is_sh or matchregex(name, [[\<bash\>]]) or matchregex(name, [[\<bash2\>]]) then - vim.b[bufnr].is_bash = 1 - vim.b[bufnr].is_kornshell = nil - vim.b[bufnr].is_sh = nil + on_detect = function(b) + vim.b[b].is_bash = 1 + vim.b[b].is_kornshell = nil + vim.b[b].is_sh = nil + end elseif matchregex(name, [[\<sh\>]]) then - vim.b[bufnr].is_sh = 1 - vim.b[bufnr].is_kornshell = nil - vim.b[bufnr].is_bash = nil + on_detect = function(b) + vim.b[b].is_sh = 1 + vim.b[b].is_kornshell = nil + vim.b[b].is_bash = nil + end end - return M.shell(path, bufnr, 'sh') + return M.shell(path, bufnr, 'sh'), on_detect end -- For shell-like file types, check for an "exec" command hidden in a comment, as used for Tcl. @@ -918,9 +925,11 @@ function M.xml(bufnr) line = line:lower() local is_docbook5 = line:find([[ xmlns="http://docbook.org/ns/docbook"]]) if is_docbook4 or is_docbook5 then - vim.b[bufnr].docbk_type = 'xml' - vim.b[bufnr].docbk_ver = is_docbook4 and 4 or 5 - return 'docbk' + return 'docbk', + function(b) + vim.b[b].docbk_type = 'xml' + vim.b[b].docbk_ver = is_docbook4 and 4 or 5 + end end if line:find([[xmlns:xbl="http://www.mozilla.org/xbl"]]) then return 'xbl' @@ -954,9 +963,10 @@ function M.sgml(bufnr) if lines:find('linuxdoc') then return 'smgllnx' elseif lines:find('<!DOCTYPE.*DocBook') then - vim.b[bufnr].docbk_type = 'sgml' - vim.b[bufnr].docbk_ver = 4 - return 'docbk' + return 'docbk', function(b) + vim.b[b].docbk_type = 'sgml' + vim.b[b].docbk_ver = 4 + end else return 'sgml' end @@ -1049,10 +1059,13 @@ end -- XFree86 config function M.xfree86(bufnr) local line = getlines(bufnr, 1) + local on_detect if matchregex(line, [[\<XConfigurator\>]]) then - vim.b[bufnr].xf86conf_xfree86_version = 3 - return 'xf86conf' + on_detect = function(b) + vim.b[b].xf86conf_xfree86_version = 3 + end end + return 'xf86conf', on_detect end -- luacheck: pop |