# HG changeset patch # User Christian Brabandt # Date 1720548006 -7200 # Node ID 1650d9b72febdcb27635527468669e7278d11597 # Parent c5efad4d560b6de21b10bae88171cb34bdfb7bd8 patch 9.1.0551: filetype: htmlangular files are not properly detected Commit: https://github.com/vim/vim/commit/1ad194c0dfd82ca1e7a1b6f2fca89a487794158d Author: Dennis van den Berg Date: Tue Jul 9 19:25:33 2024 +0200 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: #15190 Signed-off-by: Dennis van den Berg Signed-off-by: Christian Brabandt diff --git a/runtime/autoload/dist/ft.vim b/runtime/autoload/dist/ft.vim --- a/runtime/autoload/dist/ft.vim +++ b/runtime/autoload/dist/ft.vim @@ -402,14 +402,29 @@ export def FTharedoc() endif enddef -# Distinguish between HTML, XHTML and Django +# Distinguish between HTML, XHTML, Django and Angular export def FThtml() var n = 1 + + # Test if the filename follows the Angular component template convention + if expand('%:t') =~ '^.*\.component\.html$' + setf htmlangular + return + endif + while n < 40 && n <= line("$") + # Check for Angular + if getline(n) =~ '@\(if\|for\|defer\|switch\)\|\*\(ngIf\|ngFor\|ngSwitch\|ngTemplateOutlet\)\|ng-template\|ng-content\|{{.*}}' + setf htmlangular + return + endif + + # Check for XHTML if getline(n) =~ '\\|{#\s\+' setf htmldjango return diff --git a/runtime/ftplugin/htmlangular.vim b/runtime/ftplugin/htmlangular.vim new file mode 100644 --- /dev/null +++ b/runtime/ftplugin/htmlangular.vim @@ -0,0 +1,12 @@ +" Vim filetype plugin file +" Language: Angular HTML Template +" Maintainer: Dennis van den Berg +" 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/src/testdir/test_filetype.vim b/src/testdir/test_filetype.vim --- a/src/testdir/test_filetype.vim +++ b/src/testdir/test_filetype.vim @@ -334,7 +334,8 @@ def s:GetFilenameChecks(): dict {{ item.name }}', '} @empty {', '
  • There are no items.
  • ', '}'] + call writefile(content, 'Xfile.html', 'D') + split Xfile.html + call assert_equal('htmlangular', &filetype) + bwipe! + + " Django Template + let content = ['{% if foobar %}', + \ ' ', + \ '{% else %}', + \ '

    No polls are available.

    ', + \ '{% endif %}'] + call writefile(content, 'Xfile.html', 'D') + split Xfile.html + call assert_equal('htmldjango', &filetype) + bwipe! + + " regular HTML + let content = ['', '', ' Foobar', ' Content', ' ', ''] + call writefile(content, 'Xfile.html', 'D') + split Xfile.html + call assert_equal('html', &filetype) + bwipe! + + filetype off +endfunc + func Test_m_file() filetype on diff --git a/src/version.c b/src/version.c --- a/src/version.c +++ b/src/version.c @@ -705,6 +705,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ /**/ + 551, +/**/ 550, /**/ 549,