From 2f9f77cd722a9dad71df46f39c71dca88815c290 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Tue, 9 Dec 2025 09:08:09 +0800 Subject: [PATCH] vim-patch:9.1.1962: filetype: Erlang application resource files are not recognized (#36868) Problem: filetype: Erlang application resource files are not recognized Solution: Add content-based filetype detection for application resource files matching extension '*.app' (Doug Kearns) related: vim/vim#18835 closes: vim/vim#18842 https://github.com/vim/vim/commit/cf5c25526007a5cc39be317b023f55cb266d5ed2 Co-authored-by: Doug Kearns --- runtime/doc/filetype.txt | 1 + runtime/lua/vim/filetype.lua | 1 + runtime/lua/vim/filetype/detect.lua | 32 +++++++++++++++++++++++++++++ test/old/testdir/test_filetype.vim | 30 +++++++++++++++++++++++++++ 4 files changed, 64 insertions(+) diff --git a/runtime/doc/filetype.txt b/runtime/doc/filetype.txt index f625b8f84d..5f1471f21c 100644 --- a/runtime/doc/filetype.txt +++ b/runtime/doc/filetype.txt @@ -136,6 +136,7 @@ what kind of file it is. This doesn't always work. A number of global variables can be used to overrule the filetype used for certain extensions: file name variable ~ + `*.app` g:filetype_app `*.asa` g:filetype_asa |ft-aspperl-syntax| |ft-aspvbs-syntax| `*.asm` g:asmsyntax |ft-asm-syntax| diff --git a/runtime/lua/vim/filetype.lua b/runtime/lua/vim/filetype.lua index 9230ece5d3..0fbd3f4261 100644 --- a/runtime/lua/vim/filetype.lua +++ b/runtime/lua/vim/filetype.lua @@ -212,6 +212,7 @@ local extension = { aml = 'aml', run = 'ampl', g4 = 'antlr4', + app = detect.app, applescript = 'applescript', scpt = 'applescript', ino = 'arduino', diff --git a/runtime/lua/vim/filetype/detect.lua b/runtime/lua/vim/filetype/detect.lua index 2662b63843..aabb0361ad 100644 --- a/runtime/lua/vim/filetype/detect.lua +++ b/runtime/lua/vim/filetype/detect.lua @@ -30,6 +30,38 @@ local matchregex = vim.filetype._matchregex -- luacheck: push no unused args -- luacheck: push ignore 122 +-- Erlang Application Resource Files (*.app.src is matched by extension) +-- See: https://erlang.org/doc/system/applications +--- @type vim.filetype.mapfn +function M.app(path, bufnr) + if vim.g.filetype_app then + return vim.g.filetype_app + end + for lnum, line in ipairs(getlines(bufnr, 1, 100)) do + -- skip Erlang comments, might be something else + if not findany(line, { '^%s*%%', '^%s*$' }) then + if line:find('^%s*{') then + local name = fn.fnamemodify(path, ':t:r:r') + local lines = vim + .iter(getlines(bufnr, lnum, lnum + 9)) + :filter(function(v) + return not v:find('^%s*%%') + end) + :join(' ') + if + findany(lines, { + [[^%s*{%s*application%s*,%s*']] .. name .. [['%s*,]], + [[^%s*{%s*application%s*,%s*]] .. name .. [[%s*,]], + }) + then + return 'erlang' + end + end + return + end + end +end + -- This function checks for the kind of assembly that is wanted by the user, or -- can be detected from the beginning of the file. --- @type vim.filetype.mapfn diff --git a/test/old/testdir/test_filetype.vim b/test/old/testdir/test_filetype.vim index 4a1aaf854e..b014a9bcc4 100644 --- a/test/old/testdir/test_filetype.vim +++ b/test/old/testdir/test_filetype.vim @@ -3204,4 +3204,34 @@ func Test_m4_format() filetype off endfunc +" Erlang Application Resource File +func Test_app_file() + filetype on + + call writefile(['% line comment', '{application, xfile1,'], 'xfile1.app', 'D') + split xfile1.app + call assert_equal('erlang', &filetype) + bwipe! + + call writefile(['% line comment', "{application, 'Xfile2',"], 'Xfile2.app', 'D') + split Xfile2.app + call assert_equal('erlang', &filetype) + bwipe! + + call writefile([' % line comment', + \ ' ', + \ ' % line comment', + \ ' { ', + \ ' % line comment ', + \ ' application , ', + \ ' % line comment ', + \ ' xfile3 , '], + \ 'xfile3.app', 'D') + split xfile3.app + call assert_equal('erlang', &filetype) + bwipe! + + filetype off +endfunc + " vim: shiftwidth=2 sts=2 expandtab