diff options
| author | Lewis Russell <lewis6991@gmail.com> | 2023-08-08 16:36:06 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-08-08 16:36:06 +0100 |
| commit | c6c21db82b31ea43ce878ab3725dcd901db1e7a1 (patch) | |
| tree | 6002ddf7e9ef9d2ca40ef0029f501864ff43cbd9 /runtime/lua/vim | |
| parent | 61ed45486da01dca788f8a444fc2021002f0c947 (diff) | |
| download | rneovim-c6c21db82b31ea43ce878ab3725dcd901db1e7a1.tar.gz rneovim-c6c21db82b31ea43ce878ab3725dcd901db1e7a1.tar.bz2 rneovim-c6c21db82b31ea43ce878ab3725dcd901db1e7a1.zip | |
fix(filetype): add typing and dry (#24573)
Diffstat (limited to 'runtime/lua/vim')
| -rw-r--r-- | runtime/lua/vim/_editor.lua | 7 | ||||
| -rw-r--r-- | runtime/lua/vim/filetype.lua | 1205 | ||||
| -rw-r--r-- | runtime/lua/vim/filetype/detect.lua | 546 |
3 files changed, 815 insertions, 943 deletions
diff --git a/runtime/lua/vim/_editor.lua b/runtime/lua/vim/_editor.lua index 6182270708..d81464a3ca 100644 --- a/runtime/lua/vim/_editor.lua +++ b/runtime/lua/vim/_editor.lua @@ -428,10 +428,17 @@ vim.cmd = setmetatable({}, { end, }) +--- @class vim.var_accessor +--- @field [string] any +--- @field [integer] vim.var_accessor + -- These are the vim.env/v/g/o/bo/wo variable magic accessors. do local validate = vim.validate + --- @param scope string + --- @param handle? false|integer + --- @return vim.var_accessor local function make_dict_accessor(scope, handle) validate({ scope = { scope, 's' }, diff --git a/runtime/lua/vim/filetype.lua b/runtime/lua/vim/filetype.lua index 66e224ce69..a53216f8ea 100644 --- a/runtime/lua/vim/filetype.lua +++ b/runtime/lua/vim/filetype.lua @@ -1,7 +1,16 @@ local api = vim.api +local fn = vim.fn local M = {} +--- @alias vim.filetype.mapfn fun(path:string,bufnr:integer, ...):string?, fun(b:integer)? +--- @alias vim.filetype.maptbl {[1]:string|vim.filetype.mapfn, [2]:{priority:integer}} +--- @alias vim.filetype.mapping.value string|vim.filetype.mapfn|vim.filetype.maptbl +--- @alias vim.filetype.mapping table<string,vim.filetype.mapping.value> + +--- @param ft string|vim.filetype.mapfn +--- @param opts? {priority:integer} +--- @return vim.filetype.maptbl local function starsetf(ft, opts) return { function(path, bufnr) @@ -23,36 +32,38 @@ local function starsetf(ft, opts) end ---@private ---- Get a single line or line range from the buffer. ---- If only start_lnum is specified, return a single line as a string. ---- If both start_lnum and end_lnum are omitted, return all lines from the buffer. ---- +--- Get a line range from the buffer. ---@param bufnr integer The buffer to get the lines from ---@param start_lnum integer|nil The line number of the first line (inclusive, 1-based) ---@param end_lnum integer|nil The line number of the last line (inclusive, 1-based) ----@return table<string>|string Array of lines, or string when end_lnum is omitted -function M.getlines(bufnr, start_lnum, end_lnum) - if end_lnum then - -- Return a line range - return api.nvim_buf_get_lines(bufnr, start_lnum - 1, end_lnum, false) - end +---@return string[] # Array of lines +function M._getlines(bufnr, start_lnum, end_lnum) if start_lnum then - -- Return a single line - return api.nvim_buf_get_lines(bufnr, start_lnum - 1, start_lnum, false)[1] or '' - else - -- Return all lines - return api.nvim_buf_get_lines(bufnr, 0, -1, false) + return api.nvim_buf_get_lines(bufnr, start_lnum - 1, end_lnum or start_lnum, false) end + + -- Return all lines + return api.nvim_buf_get_lines(bufnr, 0, -1, false) +end + +---@private +--- Get a single line from the buffer. +---@param bufnr integer The buffer to get the lines from +---@param start_lnum integer The line number of the first line (inclusive, 1-based) +---@return string +function M._getline(bufnr, start_lnum) + -- Return a single line + return api.nvim_buf_get_lines(bufnr, start_lnum - 1, start_lnum, false)[1] or '' end ---@private --- Check whether a string matches any of the given Lua patterns. --- ----@param s string The string to check ----@param patterns table<string> A list of Lua patterns +---@param s string? The string to check +---@param patterns string[] A list of Lua patterns ---@return boolean `true` if s matched a pattern, else `false` -function M.findany(s, patterns) - if s == nil then +function M._findany(s, patterns) + if not s then return false end for _, v in ipairs(patterns) do @@ -69,8 +80,8 @@ end ---@param bufnr integer The buffer to get the line from ---@param start_lnum integer The line number of the first line to start from (inclusive, 1-based) ---@return string|nil The first non-blank line if found or `nil` otherwise -function M.nextnonblank(bufnr, start_lnum) - for _, line in ipairs(M.getlines(bufnr, start_lnum, -1)) do +function M._nextnonblank(bufnr, start_lnum) + for _, line in ipairs(M._getlines(bufnr, start_lnum, -1)) do if not line:find('^%s*$') then return line end @@ -78,30 +89,93 @@ function M.nextnonblank(bufnr, start_lnum) return nil end ----@private ---- Check whether the given string matches the Vim regex pattern. -M.matchregex = (function() - local cache = {} - return function(s, pattern) - if s == nil then - return nil +do + --- @type table<string,vim.regex> + local regex_cache = {} + + ---@private + --- Check whether the given string matches the Vim regex pattern. + --- @param s string? + --- @param pattern string + --- @return boolean + function M._matchregex(s, pattern) + if not s then + return false + end + if not regex_cache[pattern] then + regex_cache[pattern] = vim.regex(pattern) + end + return regex_cache[pattern]:match_str(s) ~= nil + end +end + +--- @module 'vim.filetype.detect' +local detect = setmetatable({}, { + --- @param k string + --- @param t table<string,function> + --- @return function + __index = function(t, k) + t[k] = function(...) + return require('vim.filetype.detect')[k](...) + end + return t[k] + end, +}) + +--- @param ... string|vim.filetype.mapfn +--- @return vim.filetype.mapfn +local function detect_seq(...) + local candidates = { ... } + return function(...) + for _, c in ipairs(candidates) do + if type(c) == 'string' then + return c + end + if type(c) == 'function' then + local r = c(...) + if r then + return r + end + end end - if not cache[pattern] then - cache[pattern] = vim.regex(pattern) + end +end + +local function detect_noext(path, bufnr) + local root = fn.fnamemodify(path, ':r') + return M.match({ buf = bufnr, filename = root }) +end + +--- @param pat string +--- @param a string? +--- @param b string? +--- @return vim.filetype.mapfn +local function detect_line1(pat, a, b) + return function(_path, bufnr) + if M._getline(bufnr, 1):find(pat) then + return a end - return cache[pattern]:match_str(s) + return b end -end)() +end + +--- @type vim.filetype.mapfn +local detect_rc = function(path, _bufnr) + if not path:find('/etc/Muttrc%.d/') then + return 'rc' + end +end -- luacheck: push no unused args -- luacheck: push ignore 122 -- Filetypes based on file extension ---@diagnostic disable: unused-local +--- @type vim.filetype.mapping local extension = { -- BEGIN EXTENSION ['8th'] = '8th', - ['a65'] = 'a65', + a65 = 'a65', aap = 'aap', abap = 'abap', abc = 'abc', @@ -127,51 +201,33 @@ local extension = { end return 'aspvbs' end, - asm = function(path, bufnr) - return require('vim.filetype.detect').asm(bufnr) - end, - lst = function(path, bufnr) - return require('vim.filetype.detect').asm(bufnr) - end, - mac = function(path, bufnr) - return require('vim.filetype.detect').asm(bufnr) - end, - ['asn1'] = 'asn', + asm = detect.asm, + lst = detect.asm, + mac = detect.asm, + asn1 = 'asn', asn = 'asn', - asp = function(path, bufnr) - return require('vim.filetype.detect').asp(bufnr) - end, + asp = detect.asp, astro = 'astro', atl = 'atlas', as = 'atlas', ahk = 'autohotkey', - ['au3'] = 'autoit', + au3 = 'autoit', ave = 'ave', gawk = 'awk', awk = 'awk', ref = 'b', imp = 'b', mch = 'b', - bas = function(path, bufnr) - return require('vim.filetype.detect').bas(bufnr) - end, + bas = detect.bas, bass = 'bass', - bi = function(path, bufnr) - return require('vim.filetype.detect').bas(bufnr) - end, - bm = function(path, bufnr) - return require('vim.filetype.detect').bas(bufnr) - end, + bi = detect.bas, + bm = detect.bas, bc = 'bc', bdf = 'bdf', beancount = 'beancount', bib = 'bib', - com = function(path, bufnr) - return require('vim.filetype.detect').bindzone(bufnr, 'dcl') - end, - db = function(path, bufnr) - return require('vim.filetype.detect').bindzone(bufnr) - end, + com = detect_seq(detect.bindzone, 'dcl'), + db = detect.bindzone, bicep = 'bicep', bb = 'bitbake', bbappend = 'bitbake', @@ -201,9 +257,7 @@ local extension = { hgrc = 'cfg', chf = 'ch', chai = 'chaiscript', - ch = function(path, bufnr) - return require('vim.filetype.detect').change(bufnr) - end, + ch = detect.change, chs = 'chaskell', chatito = 'chatito', chopro = 'chordpro', @@ -226,7 +280,7 @@ local extension = { atg = 'coco', recipe = 'conaryrecipe', hook = function(path, bufnr) - return M.getlines(bufnr, 1) == '[Trigger]' and 'conf' + return M._getline(bufnr, 1) == '[Trigger]' and 'conf' or nil end, nmconnection = 'confini', mklx = 'context', @@ -234,16 +288,10 @@ local extension = { mkii = 'context', mkxl = 'context', mkvi = 'context', - control = function(path, bufnr) - return require('vim.filetype.detect').control(bufnr) - end, - copyright = function(path, bufnr) - return require('vim.filetype.detect').copyright(bufnr) - end, + control = detect.control, + copyright = detect.copyright, corn = 'corn', - csh = function(path, bufnr) - return require('vim.filetype.detect').csh(path, bufnr) - end, + csh = detect.csh, cpon = 'cpon', moc = 'cpp', hh = 'cpp', @@ -261,12 +309,8 @@ local extension = { cppm = 'cpp', cxxm = 'cpp', ['c++m'] = 'cpp', - cpp = function(path, bufnr) - return vim.g.cynlib_syntax_for_cpp and 'cynlib' or 'cpp' - end, - cc = function(path, bufnr) - return vim.g.cynlib_syntax_for_cc and 'cynlib' or 'cpp' - end, + cpp = detect.cpp, + cc = detect.cpp, cql = 'cqlang', crm = 'crm', cr = 'crystal', @@ -291,15 +335,9 @@ local extension = { drt = 'dart', ds = 'datascript', dcd = 'dcd', - decl = function(path, bufnr) - return require('vim.filetype.detect').decl(bufnr) - end, - dec = function(path, bufnr) - return require('vim.filetype.detect').decl(bufnr) - end, - dcl = function(path, bufnr) - return require('vim.filetype.detect').decl(bufnr) or 'clean' - end, + decl = detect.decl, + dec = detect.decl, + dcl = detect_seq(detect.decl, 'clean'), def = 'def', desc = 'desc', directory = 'desktop', @@ -317,27 +355,19 @@ local extension = { drac = 'dracula', drc = 'dracula', dtd = 'dtd', - d = function(path, bufnr) - return require('vim.filetype.detect').dtrace(bufnr) - end, + d = detect.dtrace, dts = 'dts', dtsi = 'dts', dylan = 'dylan', intr = 'dylanintr', lid = 'dylanlid', - e = function(path, bufnr) - return require('vim.filetype.detect').e(bufnr) - end, - E = function(path, bufnr) - return require('vim.filetype.detect').e(bufnr) - end, + e = detect.e, + E = detect.e, ecd = 'ecd', edf = 'edif', edif = 'edif', edo = 'edif', - edn = function(path, bufnr) - return require('vim.filetype.detect').edn(bufnr) - end, + edn = detect.edn, eex = 'eelixir', leex = 'eelixir', am = 'elf', @@ -345,9 +375,7 @@ local extension = { elm = 'elm', lc = 'elsa', elv = 'elvish', - ent = function(path, bufnr) - return require('vim.filetype.detect').ent(bufnr) - end, + ent = detect.ent, epp = 'epuppet', erl = 'erlang', hrl = 'erlang', @@ -358,43 +386,23 @@ local extension = { ec = 'esqlc', EC = 'esqlc', strl = 'esterel', - eu = function(path, bufnr) - return vim.g.filetype_euphoria or 'euphoria3' - end, - EU = function(path, bufnr) - return vim.g.filetype_euphoria or 'euphoria3' - end, - ew = function(path, bufnr) - return vim.g.filetype_euphoria or 'euphoria3' - end, - EW = function(path, bufnr) - return vim.g.filetype_euphoria or 'euphoria3' - end, - EX = function(path, bufnr) - return vim.g.filetype_euphoria or 'euphoria3' - end, - exu = function(path, bufnr) - return vim.g.filetype_euphoria or 'euphoria3' - end, - EXU = function(path, bufnr) - return vim.g.filetype_euphoria or 'euphoria3' - end, - exw = function(path, bufnr) - return vim.g.filetype_euphoria or 'euphoria3' - end, - EXW = function(path, bufnr) - return vim.g.filetype_euphoria or 'euphoria3' - end, - ex = function(path, bufnr) - return require('vim.filetype.detect').ex(bufnr) - end, + eu = detect.euphoria, + EU = detect.euphoria, + ew = detect.euphoria, + EW = detect.euphoria, + EX = detect.euphoria, + exu = detect.euphoria, + EXU = detect.euphoria, + exw = detect.euphoria, + EXW = detect.euphoria, + ex = detect.ex, exp = 'expect', factor = 'factor', fal = 'falcon', fan = 'fan', fwt = 'fan', fnl = 'fennel', - ['m4gl'] = 'fgl', + m4gl = 'fgl', ['4gl'] = 'fgl', ['4gh'] = 'fgl', fir = 'firrtl', @@ -404,33 +412,29 @@ local extension = { fth = 'forth', ft = 'forth', FOR = 'fortran', - ['f77'] = 'fortran', - ['f03'] = 'fortran', + f77 = 'fortran', + f03 = 'fortran', fortran = 'fortran', - ['F95'] = 'fortran', - ['f90'] = 'fortran', - ['F03'] = 'fortran', + F95 = 'fortran', + f90 = 'fortran', + F03 = 'fortran', fpp = 'fortran', FTN = 'fortran', ftn = 'fortran', ['for'] = 'fortran', - ['F90'] = 'fortran', - ['F77'] = 'fortran', - ['f95'] = 'fortran', + F90 = 'fortran', + F77 = 'fortran', + f95 = 'fortran', FPP = 'fortran', f = 'fortran', F = 'fortran', - ['F08'] = 'fortran', - ['f08'] = 'fortran', + F08 = 'fortran', + f08 = 'fortran', fpc = 'fpcmake', fsl = 'framescript', - frm = function(path, bufnr) - return require('vim.filetype.detect').frm(bufnr) - end, + frm = detect.frm, fb = 'freebasic', - fs = function(path, bufnr) - return require('vim.filetype.detect').fs(bufnr) - end, + fs = detect.fs, fsh = 'fsh', fsi = 'fsharp', fsx = 'fsharp', @@ -478,9 +482,7 @@ local extension = { ht = 'haste', htpp = 'hastepreproc', hb = 'hb', - h = function(path, bufnr) - return require('vim.filetype.detect').header(bufnr) - end, + h = detect.header, sum = 'hercules', errsum = 'hercules', ev = 'hercules', @@ -495,56 +497,28 @@ local extension = { hog = 'hog', hws = 'hollywood', hoon = 'hoon', - cpt = function(path, bufnr) - return require('vim.filetype.detect').html(bufnr) - end, - dtml = function(path, bufnr) - return require('vim.filetype.detect').html(bufnr) - end, - htm = function(path, bufnr) - return require('vim.filetype.detect').html(bufnr) - end, - html = function(path, bufnr) - return require('vim.filetype.detect').html(bufnr) - end, - pt = function(path, bufnr) - return require('vim.filetype.detect').html(bufnr) - end, - shtml = function(path, bufnr) - return require('vim.filetype.detect').html(bufnr) - end, - stm = function(path, bufnr) - return require('vim.filetype.detect').html(bufnr) - end, + cpt = detect.html, + dtml = detect.html, + htm = detect.html, + html = detect.html, + pt = detect.html, + shtml = detect.html, + stm = detect.html, htt = 'httest', htb = 'httest', - hw = function(path, bufnr) - return require('vim.filetype.detect').hw(bufnr) - end, - module = function(path, bufnr) - return require('vim.filetype.detect').hw(bufnr) - end, - pkg = function(path, bufnr) - return require('vim.filetype.detect').hw(bufnr) - end, + hw = detect.hw, + module = detect.hw, + pkg = detect.hw, iba = 'ibasic', ibi = 'ibasic', icn = 'icon', - idl = function(path, bufnr) - return require('vim.filetype.detect').idl(bufnr) - end, - inc = function(path, bufnr) - return require('vim.filetype.detect').inc(bufnr) - end, + idl = detect.idl, + inc = detect.inc, inf = 'inform', INF = 'inform', ii = 'initng', - inp = function(path, bufnr) - return require('vim.filetype.detect').inp(bufnr) - end, - ms = function(path, bufnr) - return require('vim.filetype.detect').nroff(bufnr) or 'xmath' - end, + inp = detect.inp, + ms = detect_seq(detect.nroff, 'xmath'), iss = 'iss', mst = 'ist', ist = 'ist', @@ -566,7 +540,7 @@ local extension = { jsx = 'javascriptreact', clp = 'jess', jgr = 'jgraph', - ['j73'] = 'jovial', + j73 = 'jovial', jov = 'jovial', jovial = 'jovial', properties = 'jproperties', @@ -628,29 +602,21 @@ local extension = { lou = 'lout', ulpc = 'lpc', lpc = 'lpc', - c = function(path, bufnr) - return require('vim.filetype.detect').lpc(bufnr) - end, - lsl = function(path, bufnr) - return require('vim.filetype.detect').lsl(bufnr) - end, + c = detect.lpc, + lsl = detect.lsl, lss = 'lss', nse = 'lua', rockspec = 'lua', lua = 'lua', luau = 'luau', lrc = 'lyrics', - m = function(path, bufnr) - return require('vim.filetype.detect').m(bufnr) - end, + m = detect.m, at = 'm4', - mc = function(path, bufnr) - return require('vim.filetype.detect').mc(bufnr) - end, + mc = detect.mc, quake = 'm3quake', - ['m4'] = function(path, bufnr) + m4 = function(path, bufnr) path = path:lower() - return not (path:find('html%.m4$') or path:find('fvwm2rc')) and 'm4' + return not (path:find('html%.m4$') or path:find('fvwm2rc')) and 'm4' or nil end, eml = 'mail', mk = 'make', @@ -689,16 +655,12 @@ local extension = { mib = 'mib', mix = 'mix', mixal = 'mix', - mm = function(path, bufnr) - return require('vim.filetype.detect').mm(bufnr) - end, + mm = detect.mm, nb = 'mma', mmp = 'mmp', - mms = function(path, bufnr) - return require('vim.filetype.detect').mms(bufnr) - end, + mms = detect.mms, DEF = 'modula2', - ['m2'] = 'modula2', + m2 = 'modula2', mi = 'modula2', lm3 = 'modula3', ssc = 'monk', @@ -709,28 +671,16 @@ local extension = { moon = 'moonscript', move = 'move', mp = 'mp', - mpiv = function(path, bufnr) - return 'mp', function(b) - vim.b[b].mp_metafun = 1 - end - end, - mpvi = function(path, bufnr) - return 'mp', function(b) - vim.b[b].mp_metafun = 1 - end - end, - mpxl = function(path, bufnr) - return 'mp', function(b) - vim.b[b].mp_metafun = 1 - end - end, + mpiv = detect.mp, + mpvi = detect.mp, + mpxl = detect.mp, mof = 'msidl', odl = 'msidl', msql = 'msql', mu = 'mupad', mush = 'mush', mysql = 'mysql', - ['n1ql'] = 'n1ql', + n1ql = 'n1ql', nql = 'n1ql', nanorc = 'nanorc', ncf = 'ncf', @@ -798,12 +748,10 @@ local extension = { pike = 'pike', pmod = 'pike', rcp = 'pilrc', - PL = function(path, bufnr) - return require('vim.filetype.detect').pl(bufnr) - end, + PL = detect.pl, pli = 'pli', - ['pl1'] = 'pli', - ['p36'] = 'plm', + pl1 = 'pli', + p36 = 'plm', plm = 'plm', pac = 'plm', plp = 'plp', @@ -832,11 +780,11 @@ local extension = { pml = 'promela', proto = 'proto', prql = 'prql', - ['psd1'] = 'ps1', - ['psm1'] = 'ps1', - ['ps1'] = 'ps1', + psd1 = 'ps1', + psm1 = 'ps1', + ps1 = 'ps1', pssc = 'ps1', - ['ps1xml'] = 'ps1xml', + ps1xml = 'ps1xml', psf = 'psf', psl = 'psl', pug = 'pug', @@ -850,22 +798,20 @@ local extension = { ql = 'ql', qll = 'ql', qmd = 'quarto', - R = function(path, bufnr) - return require('vim.filetype.detect').r(bufnr) - end, + R = detect.r, rkt = 'racket', rktd = 'racket', rktl = 'racket', rad = 'radiance', mat = 'radiance', - ['pod6'] = 'raku', + pod6 = 'raku', rakudoc = 'raku', rakutest = 'raku', rakumod = 'raku', - ['pm6'] = 'raku', + pm6 = 'raku', raku = 'raku', - ['t6'] = 'raku', - ['p6'] = 'raku', + t6 = 'raku', + p6 = 'raku', raml = 'raml', rbs = 'rbs', rego = 'rego', @@ -938,34 +884,18 @@ local extension = { sdl = 'sdl', sed = 'sed', sexp = 'sexplib', - bash = function(path, bufnr) - return require('vim.filetype.detect').sh(path, M.getlines(bufnr), 'bash') - end, - ebuild = function(path, bufnr) - return require('vim.filetype.detect').sh(path, M.getlines(bufnr), 'bash') - end, - eclass = function(path, bufnr) - return require('vim.filetype.detect').sh(path, M.getlines(bufnr), 'bash') - end, - env = function(path, bufnr) - return require('vim.filetype.detect').sh(path, M.getlines(bufnr)) - end, - ksh = function(path, bufnr) - return require('vim.filetype.detect').sh(path, M.getlines(bufnr), 'ksh') - end, - sh = function(path, bufnr) - return require('vim.filetype.detect').sh(path, M.getlines(bufnr)) - end, + bash = detect.bash, + ebuild = detect.bash, + eclass = detect.bash, + env = detect.sh, + ksh = detect.ksh, + sh = detect.sh, sieve = 'sieve', siv = 'sieve', - sig = function(path, bufnr) - return require('vim.filetype.detect').sig(bufnr) - end, - sil = function(path, bufnr) - return require('vim.filetype.detect').sil(bufnr) - end, + sig = detect.sig, + sil = detect.sil, sim = 'simula', - ['s85'] = 'sinda', + s85 = 'sinda', sin = 'sinda', ssm = 'sisu', sst = 'sisu', @@ -1002,9 +932,7 @@ local extension = { spi = 'spyce', spy = 'spyce', tyc = 'sql', - typ = function(path, bufnr) - return require('vim.filetype.detect').typ(bufnr) - end, + typ = detect.typ, pkb = 'sql', tyb = 'sql', pks = 'sql', @@ -1012,11 +940,11 @@ local extension = { sqi = 'sqr', sqr = 'sqr', nut = 'squirrel', - ['s28'] = 'srec', - ['s37'] = 'srec', + s28 = 'srec', + s37 = 'srec', srec = 'srec', mot = 'srec', - ['s19'] = 'srec', + s19 = 'srec', srt = 'srt', ssa = 'ssa', ass = 'ssa', @@ -1056,9 +984,7 @@ local extension = { bbl = 'tex', latex = 'tex', sty = 'tex', - cls = function(path, bufnr) - return require('vim.filetype.detect').cls(bufnr) - end, + cls = detect.cls, texi = 'texinfo', txi = 'texinfo', texinfo = 'texinfo', @@ -1077,9 +1003,7 @@ local extension = { tsv = 'tsv', tutor = 'tutor', twig = 'twig', - ts = function(path, bufnr) - return M.getlines(bufnr, 1):find('<%?xml') and 'xml' or 'typescript' - end, + ts = detect_line1('<%?xml', 'xml', 'typescript'), mts = 'typescript', cts = 'typescript', tsx = 'typescriptreact', @@ -1104,9 +1028,7 @@ local extension = { vr = 'vera', vri = 'vera', vrh = 'vera', - v = function(path, bufnr) - return require('vim.filetype.detect').v(bufnr) - end, + v = detect.v, va = 'verilogams', vams = 'verilogams', vhdl = 'vhdl', @@ -1136,7 +1058,7 @@ local extension = { xht = 'xhtml', msc = 'xmath', msf = 'xmath', - ['psc1'] = 'xml', + psc1 = 'xml', tpm = 'xml', xliff = 'xml', atom = 'xml', @@ -1152,10 +1074,8 @@ local extension = { csproj = 'xml', wpl = 'xml', xmi = 'xml', - xpm = function(path, bufnr) - return M.getlines(bufnr, 1):find('XPM2') and 'xpm2' or 'xpm' - end, - ['xpm2'] = 'xpm2', + xpm = detect_line1('XPM2', 'xpm2', 'xpm'), + xpm2 = 'xpm2', xqy = 'xquery', xqm = 'xquery', xquery = 'xquery', @@ -1172,7 +1092,7 @@ local extension = { yaml = 'yaml', yang = 'yang', yuck = 'yuck', - ['z8a'] = 'z8a', + z8a = 'z8a', zig = 'zig', zir = 'zir', zu = 'zimbu', @@ -1180,183 +1100,69 @@ local extension = { zs = 'zserio', zsh = 'zsh', vala = 'vala', - web = function(path, bufnr) - return require('vim.filetype.detect').web(bufnr) - end, - pl = function(path, bufnr) - return require('vim.filetype.detect').pl(bufnr) - end, - pp = function(path, bufnr) - return require('vim.filetype.detect').pp(bufnr) - end, - i = function(path, bufnr) - return require('vim.filetype.detect').progress_asm(bufnr) - end, - w = function(path, bufnr) - return require('vim.filetype.detect').progress_cweb(bufnr) - end, - p = function(path, bufnr) - return require('vim.filetype.detect').progress_pascal(bufnr) - end, - pro = function(path, bufnr) - return require('vim.filetype.detect').proto(bufnr, 'idlang') - end, - patch = function(path, bufnr) - return require('vim.filetype.detect').patch(bufnr) - end, - r = function(path, bufnr) - return require('vim.filetype.detect').r(bufnr) - end, - rdf = function(path, bufnr) - return require('vim.filetype.detect').redif(bufnr) - end, - rules = function(path, bufnr) - return require('vim.filetype.detect').rules(path) - end, - sc = function(path, bufnr) - return require('vim.filetype.detect').sc(bufnr) - end, - scd = function(path, bufnr) - return require('vim.filetype.detect').scd(bufnr) - end, + web = detect.web, + pl = detect.pl, + pp = detect.pp, + i = detect.progress_asm, + w = detect.progress_cweb, + p = detect.progress_pascal, + pro = detect_seq(detect.proto, 'idlang'), + patch = detect.patch, + r = detect.r, + rdf = detect.redif, + rules = detect.rules, + sc = detect.sc, + scd = detect.scd, tcsh = function(path, bufnr) - return require('vim.filetype.detect').shell(path, M.getlines(bufnr), 'tcsh') - end, - sql = function(path, bufnr) - return vim.g.filetype_sql and vim.g.filetype_sql or 'sql' - end, - zsql = function(path, bufnr) - return vim.g.filetype_sql and vim.g.filetype_sql or 'sql' - end, - tex = function(path, bufnr) - return require('vim.filetype.detect').tex(path, bufnr) - end, - tf = function(path, bufnr) - return require('vim.filetype.detect').tf(bufnr) - end, - txt = function(path, bufnr) - return require('vim.filetype.detect').txt(bufnr) - end, - xml = function(path, bufnr) - return require('vim.filetype.detect').xml(bufnr) - end, - y = function(path, bufnr) - return require('vim.filetype.detect').y(bufnr) - end, - cmd = function(path, bufnr) - return M.getlines(bufnr, 1):find('^/%*') and 'rexx' or 'dosbatch' - end, - rul = function(path, bufnr) - return require('vim.filetype.detect').rul(bufnr) - end, - cpy = function(path, bufnr) - return M.getlines(bufnr, 1):find('^##') and 'python' or 'cobol' - end, - dsl = function(path, bufnr) - return M.getlines(bufnr, 1):find('^%s*<!') and 'dsl' or 'structurizr' - end, - smil = function(path, bufnr) - return M.getlines(bufnr, 1):find('<%?%s*xml.*%?>') and 'xml' or 'smil' - end, - smi = function(path, bufnr) - return require('vim.filetype.detect').smi(bufnr) - end, - install = function(path, bufnr) - return require('vim.filetype.detect').install(path, bufnr) - end, - pm = function(path, bufnr) - return require('vim.filetype.detect').pm(bufnr) - end, - me = function(path, bufnr) - return require('vim.filetype.detect').me(path) - end, - reg = function(path, bufnr) - return require('vim.filetype.detect').reg(bufnr) - end, - ttl = function(path, bufnr) - return require('vim.filetype.detect').ttl(bufnr) - end, - rc = function(path, bufnr) - if not path:find('/etc/Muttrc%.d/') then - return 'rc' - end - end, - rch = function(path, bufnr) - if not path:find('/etc/Muttrc%.d/') then - return 'rc' - end - end, - class = function(path, bufnr) - require('vim.filetype.detect').class(bufnr) - end, - sgml = function(path, bufnr) - return require('vim.filetype.detect').sgml(bufnr) - end, - sgm = function(path, bufnr) - return require('vim.filetype.detect').sgml(bufnr) - end, - t = function(path, bufnr) - local nroff = require('vim.filetype.detect').nroff(bufnr) - return nroff or require('vim.filetype.detect').perl(path, bufnr) or 'tads' - end, + return require('vim.filetype.detect').shell(path, M._getlines(bufnr), 'tcsh') + end, + sql = detect.sql, + zsql = detect.sql, + tex = detect.tex, + tf = detect.tf, + txt = detect.txt, + xml = detect.xml, + y = detect.y, + cmd = detect_line1('^/%*', 'rexx', 'dosbatch'), + rul = detect.rul, + cpy = detect_line1('^##', 'python', 'cobol'), + dsl = detect_line1('^%s*<!', 'dsl', 'structurizr'), + smil = detect_line1('<%?%s*xml.*%?>', 'xml', 'smil'), + smi = detect.smi, + install = detect.install, + pm = detect.pm, + me = detect.me, + reg = detect.reg, + ttl = detect.ttl, + rc = detect_rc, + rch = detect_rc, + class = detect.class, + sgml = detect.sgml, + sgm = detect.sgml, + t = detect_seq(detect.nroff, detect.perl, 'tads'), -- Ignored extensions - bak = function(path, bufnr) - local root = vim.fn.fnamemodify(path, ':r') - return M.match({ buf = bufnr, filename = root }) - end, - ['dpkg-bak'] = function(path, bufnr) - local root = vim.fn.fnamemodify(path, ':r') - return M.match({ buf = bufnr, filename = root }) - end, - ['dpkg-dist'] = function(path, bufnr) - local root = vim.fn.fnamemodify(path, ':r') - return M.match({ buf = bufnr, filename = root }) - end, - ['dpkg-old'] = function(path, bufnr) - local root = vim.fn.fnamemodify(path, ':r') - return M.match({ buf = bufnr, filename = root }) - end, - ['dpkg-new'] = function(path, bufnr) - local root = vim.fn.fnamemodify(path, ':r') - return M.match({ buf = bufnr, filename = root }) - end, + bak = detect_noext, + ['dpkg-bak'] = detect_noext, + ['dpkg-dist'] = detect_noext, + ['dpkg-old'] = detect_noext, + ['dpkg-new'] = detect_noext, ['in'] = function(path, bufnr) if vim.fs.basename(path) ~= 'configure.in' then - local root = vim.fn.fnamemodify(path, ':r') + local root = fn.fnamemodify(path, ':r') return M.match({ buf = bufnr, filename = root }) end end, - new = function(path, bufnr) - local root = vim.fn.fnamemodify(path, ':r') - return M.match({ buf = bufnr, filename = root }) - end, - old = function(path, bufnr) - local root = vim.fn.fnamemodify(path, ':r') - return M.match({ buf = bufnr, filename = root }) - end, - orig = function(path, bufnr) - local root = vim.fn.fnamemodify(path, ':r') - return M.match({ buf = bufnr, filename = root }) - end, - pacsave = function(path, bufnr) - local root = vim.fn.fnamemodify(path, ':r') - return M.match({ buf = bufnr, filename = root }) - end, - pacnew = function(path, bufnr) - local root = vim.fn.fnamemodify(path, ':r') - return M.match({ buf = bufnr, filename = root }) - end, - rpmsave = function(path, bufnr) - local root = vim.fn.fnamemodify(path, ':r') - return M.match({ buf = bufnr, filename = root }) - end, - rmpnew = function(path, bufnr) - local root = vim.fn.fnamemodify(path, ':r') - return M.match({ buf = bufnr, filename = root }) - end, + new = detect_noext, + old = detect_noext, + orig = detect_noext, + pacsave = detect_noext, + pacnew = detect_noext, + rpmsave = detect_noext, + rmpnew = detect_noext, -- END EXTENSION } +--- @type vim.filetype.mapping local filename = { -- BEGIN FILENAME ['a2psrc'] = 'a2ps', @@ -1387,24 +1193,12 @@ local filename = { ['/etc/defaults/cdrdao'] = 'cdrdaoconf', ['cfengine.conf'] = 'cfengine', ['CMakeLists.txt'] = 'cmake', - ['.alias'] = function(path, bufnr) - return require('vim.filetype.detect').csh(path, bufnr) - end, - ['.cshrc'] = function(path, bufnr) - return require('vim.filetype.detect').csh(path, bufnr) - end, - ['.login'] = function(path, bufnr) - return require('vim.filetype.detect').csh(path, bufnr) - end, - ['csh.cshrc'] = function(path, bufnr) - return require('vim.filetype.detect').csh(path, bufnr) - end, - ['csh.login'] = function(path, bufnr) - return require('vim.filetype.detect').csh(path, bufnr) - end, - ['csh.logout'] = function(path, bufnr) - return require('vim.filetype.detect').csh(path, bufnr) - end, + ['.alias'] = detect.csh, + ['.cshrc'] = detect.csh, + ['.login'] = detect.csh, + ['csh.cshrc'] = detect.csh, + [' |