diff options
author | Christian Clason <c.clason@uni-graz.at> | 2024-07-09 19:58:02 +0200 |
---|---|---|
committer | Christian Clason <c.clason@uni-graz.at> | 2024-07-10 09:31:48 +0200 |
commit | 7fa089f463dac83f256a8336ddb0adc9eae483a2 (patch) | |
tree | 2734fce32c870ec7940807e3ccc70196c5e6b1a8 | |
parent | 80530d07e75a98cdf65f1c731b031d05bc2f5087 (diff) | |
download | rneovim-7fa089f463dac83f256a8336ddb0adc9eae483a2.tar.gz rneovim-7fa089f463dac83f256a8336ddb0adc9eae483a2.tar.bz2 rneovim-7fa089f463dac83f256a8336ddb0adc9eae483a2.zip |
vim-patch:9.1.0551: filetype: htmlangular files are not properly detected
Problem: filetype: htmlangular files are not properly detected
Solution: Use the new htmlangular filetype for angular files, because
since angular v17, those are no longer valid HTML files.
(Dennis van den Berg)
Since Angular 17, the new Control Flow Syntax is not valid HTML. This PR
adds a new filetype detection for the HTML templates of Angular.
It first checks the filename. The Angular convention is to use
*.component.html for the template. However, this is not mandatory.
If the filename does not match, it will check the contents of the file
if it contains:
- One of the Control-Flow blocks: @if, @for, @switch, @defer
- A structural directive: *ngIf, *ngFor, *ngSwitch, *ngTemplateOutlet
- Builtin Angular elements: ng-template or ng-content
- String interpolation: {{ something }}
This enables the Angular LSP to attach only to htmlangular filetypes, as
well as language parsers, such as tree-sitter.
closes: vim/vim#15190
https://github.com/vim/vim/commit/1ad194c0dfd82ca1e7a1b6f2fca89a487794158d
Co-authored-by: Dennis van den Berg <dennis.vandenberg@nedap.com>
-rw-r--r-- | runtime/ftplugin/htmlangular.vim | 12 | ||||
-rw-r--r-- | runtime/lua/vim/filetype/detect.lua | 17 | ||||
-rw-r--r-- | test/old/testdir/test_filetype.vim | 41 |
3 files changed, 66 insertions, 4 deletions
diff --git a/runtime/ftplugin/htmlangular.vim b/runtime/ftplugin/htmlangular.vim new file mode 100644 index 0000000000..b181b203d0 --- /dev/null +++ b/runtime/ftplugin/htmlangular.vim @@ -0,0 +1,12 @@ +" Vim filetype plugin file +" Language: Angular HTML Template +" Maintainer: Dennis van den Berg <dennis@vdberg.dev> +" Last Change: 2024 Jul 8 + +" Only use this filetype plugin when no other was loaded. +if exists("b:did_ftplugin") + finish +endif + +" Use HTML and Angular template ftplugins +runtime! ftplugin/html.vim diff --git a/runtime/lua/vim/filetype/detect.lua b/runtime/lua/vim/filetype/detect.lua index 85c8d4c9dc..8a217a4ac2 100644 --- a/runtime/lua/vim/filetype/detect.lua +++ b/runtime/lua/vim/filetype/detect.lua @@ -710,9 +710,22 @@ function M.haredoc(path, _) end --- @type vim.filetype.mapfn -function M.html(_, bufnr) +function M.html(path, bufnr) + -- Test if the filename follows the Angular component template convention + local filename = fn.fnamemodify(path, ':t') + if filename:find('%.component%.html$') then + return 'htmlangular' + end + for _, line in ipairs(getlines(bufnr, 1, 40)) do - if matchregex(line, [[\<DTD\s\+XHTML\s]]) then + if + matchregex( + line, + [[@\(if\|for\|defer\|switch\)\|\*\(ngIf\|ngFor\|ngSwitch\|ngTemplateOutlet\)\|ng-template\|ng-content\|{{.*}}]] + ) + then + return 'htmlangular' + elseif matchregex(line, [[\<DTD\s\+XHTML\s]]) then return 'xhtml' elseif matchregex( diff --git a/test/old/testdir/test_filetype.vim b/test/old/testdir/test_filetype.vim index a421c83340..00756ad68a 100644 --- a/test/old/testdir/test_filetype.vim +++ b/test/old/testdir/test_filetype.vim @@ -336,7 +336,8 @@ func s:GetFilenameChecks() abort \ 'hoon': ['file.hoon'], \ 'hostconf': ['/etc/host.conf', 'any/etc/host.conf'], \ 'hostsaccess': ['/etc/hosts.allow', '/etc/hosts.deny', 'any/etc/hosts.allow', 'any/etc/hosts.deny'], - \ 'html': ['file.html', 'file.htm', 'file.cshtml', 'file.component.html'], + \ 'html': ['file.html', 'file.htm', 'file.cshtml'], + \ 'htmlangular': ['file.component.html'], \ 'htmlm4': ['file.html.m4'], \ 'httest': ['file.htt', 'file.htb'], \ 'hurl': ['file.hurl'], @@ -1045,7 +1046,8 @@ func Test_emptybuf_ftdetect() call assert_equal('', &filetype) filetype detect call assert_equal('sh', &filetype) - close! + " close the swapfile + bw! endfunc " Test for ':filetype indent on' and ':filetype indent off' commands @@ -1569,6 +1571,41 @@ func Test_hook_file() filetype off endfunc +func Test_html_file() + filetype on + + " HTML Angular + let content = ['@for (item of items; track item.name) {', ' <li> {{ item.name }}</li>', '} @empty {', ' <li> There are no items.</li>', '}'] + call writefile(content, 'Xfile.html', 'D') + split Xfile.html + call assert_equal('htmlangular', &filetype) + bwipe! + + " Django Template + let content = ['{% if foobar %}', + \ ' <ul>', + \ ' {% for question in list %}', + \ ' <li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>', + \ ' {% endfor %}', + \ ' </ul>', + \ '{% else %}', + \ ' <p>No polls are available.</p>', + \ '{% endif %}'] + call writefile(content, 'Xfile.html', 'D') + split Xfile.html + call assert_equal('htmldjango', &filetype) + bwipe! + + " regular HTML + let content = ['<!DOCTYPE html>', '<html>', ' <head>Foobar</head>', ' <body>Content', ' </body>', '</html>'] + call writefile(content, 'Xfile.html', 'D') + split Xfile.html + call assert_equal('html', &filetype) + bwipe! + + filetype off +endfunc + func Test_m_file() filetype on |