runtime(filetype): improve asm heuristics and move into FTasmsyntax()
fixes: #17474
closes: #17683
Signed-off-by: Wu Yongwei <wuyongwei@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
diff --git a/runtime/autoload/dist/ft.vim b/runtime/autoload/dist/ft.vim
index 5014e75..ff1dc03 100644
--- a/runtime/autoload/dist/ft.vim
+++ b/runtime/autoload/dist/ft.vim
@@ -3,7 +3,7 @@
# Vim functions for file type detection
#
# Maintainer: The Vim Project <https://github.com/vim/vim>
-# Last Change: 2025 Jun 17
+# Last Change: 2025 Jul 08
# Former Maintainer: Bram Moolenaar <Bram@vim.org>
# These functions are moved here from runtime/filetype.vim to make startup
@@ -30,12 +30,8 @@
enddef
# 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.
export def FTasm()
- # tiasm uses `* commment`
- if join(getline(1, 10), "\n") =~ '\%(\%(^\|\n\)\*\|Texas Instruments Incorporated\)'
- setf tiasm
- endif
# make sure b:asmsyntax exists
if !exists("b:asmsyntax")
b:asmsyntax = ""
@@ -65,8 +61,26 @@
var match = matchstr(head, '\sasmsyntax=\zs[a-zA-Z0-9]\+\ze\s')
if match != ''
b:asmsyntax = match
- elseif ((head =~? '\.title') || (head =~? '\.ident') || (head =~? '\.macro') || (head =~? '\.subtitle') || (head =~? '\.library'))
- b:asmsyntax = "vmasm"
+ else
+ # Use heuristics
+ var is_slash_star_encountered = false
+ var i = 1
+ const n = min([50, line("$")])
+ while i <= n
+ const line = getline(i)
+ if line =~ '\%(^\|\n\)/\*'
+ is_slash_star_encountered = true
+ endif
+ if line =~# '^; Listing generated by Microsoft' || line =~? '\%(^\|\n\)\%(\%(CONST\|_BSS\|_DATA\|_TEXT\)\s\+SEGMENT\>\)\|\s*\.[2-6]86P\?\>\|\s*\.XMM\>'
+ b:asmsyntax = "masm"
+ elseif line =~ 'Texas Instruments Incorporated' || (line =~ '\%(^\|\n\)\*' && !is_slash_star_encountered)
+ # tiasm uses `* commment`, but detection is unreliable if '/*' is seen
+ b:asmsyntax = "tiasm"
+ elseif ((line =~? '\.title\>\|\.ident\>\|\.macro\>\|\.subtitle\>\|\.library\>'))
+ b:asmsyntax = "vmasm"
+ endif
+ i += 1
+ endwhile
endif
enddef