From 7f18811668708e7d0505596c0cdb7a15ba3a5fcf Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Thu, 10 Jul 2025 20:30:39 +0800 Subject: [PATCH] vim-patch:32a1b26: runtime(filetype): improve asm heuristics and move into FTasmsyntax() (#34863) fixes: vim/vim#17474 closes: vim/vim#17683 https://github.com/vim/vim/commit/32a1b26ef3e821de9b5c518829b08002e933fa5a vim-patch:41ee98c: runtime(filetype): fix incorrect pattern and break early - Using `\n` is incorrect, as result of getline() does not contain line breaks and only uses `\n` for NUL bytes. - Return when b:asmsyntax is set, like many other filetypes. closes: vim/vim#17706 https://github.com/vim/vim/commit/41ee98c3c5342867cb99dfcddbe8d53caeda22db Co-authored-by: Wu Yongwei --- runtime/lua/vim/filetype/detect.lua | 55 ++++++++++++++++++----------- 1 file changed, 35 insertions(+), 20 deletions(-) diff --git a/runtime/lua/vim/filetype/detect.lua b/runtime/lua/vim/filetype/detect.lua index 07bb02029b..10ecefc7d2 100644 --- a/runtime/lua/vim/filetype/detect.lua +++ b/runtime/lua/vim/filetype/detect.lua @@ -31,15 +31,9 @@ local matchregex = vim.filetype._matchregex -- luacheck: push ignore 122 -- 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. +-- can be detected from the beginning of the file. --- @type vim.filetype.mapfn function M.asm(path, bufnr) - -- tiasm uses `* comment` - local lines = table.concat(getlines(bufnr, 1, 10), '\n') - if findany(lines, { '^%*', '\n%*', 'Texas Instruments Incorporated' }) then - return 'tiasm' - end - local syntax = vim.b[bufnr].asmsyntax if not syntax or syntax == '' then syntax = M.asm_syntax(path, bufnr) @@ -58,6 +52,40 @@ function M.asm(path, bufnr) end end +-- Checks the first lines for a asmsyntax=foo override. +-- Only whitespace characters can be present immediately before or after this statement. +--- @type vim.filetype.mapfn +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 + return match + end + local is_slash_star_encountered = false + for _, line in ipairs(getlines(bufnr, 1, 50)) do + if line:find('^/%*') then + is_slash_star_encountered = true + end + if + line:find('^; Listing generated by Microsoft') + or matchregex( + line, + [[\c^\%(\%(CONST\|_BSS\|_DATA\|_TEXT\)\s\+SEGMENT\>\)\|\s*\.[2-6]86P\?\>\|\s*\.XMM\>]] + ) + then + return 'masm' + elseif + line:find('Texas Instruments Incorporated') + -- tiasm uses `* commment`, but detection is unreliable if '/*' is seen + or (line:find('^%*') and not is_slash_star_encountered) + then + return 'tiasm' + elseif matchregex(line, [[\c\.title\>\|\.ident\>\|\.macro\>\|\.subtitle\>\|\.library\>]]) then + return 'vmasm' + end + end +end + --- Active Server Pages (with Perl or Visual Basic Script) --- @type vim.filetype.mapfn function M.asp(_, bufnr) @@ -69,19 +97,6 @@ function M.asp(_, bufnr) return 'aspvbs' end --- Checks the first 5 lines for a asmsyntax=foo override. --- Only whitespace characters can be present immediately before or after this statement. ---- @type vim.filetype.mapfn -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 - return match - elseif findany(lines, { '%.title', '%.ident', '%.macro', '%.subtitle', '%.library' }) then - return 'vmasm' - end -end - local visual_basic_content = [[\c^\s*\%(Attribute\s\+VB_Name\|Begin\s\+\%(VB\.\|{\%(\x\+-\)\+\x\+}\)\)]]