Updates the comment tokenization for handlebars syntax.

This updates the tokenization to support mustaches in
block comments per the handlebars documentation [1].

[1] https://handlebarsjs.com/guide/#template-comments
This commit is contained in:
Justin Mancusi 2020-05-06 16:40:36 -04:00
parent 618f2cff2d
commit 3875ac87b3
2 changed files with 44 additions and 2 deletions

View file

@ -281,5 +281,35 @@ testTokenization(['handlebars', 'css'], [
{ startIndex: 30, type: 'delimiter.handlebars' }, { startIndex: 30, type: 'delimiter.handlebars' },
{ startIndex: 32, type: '' } { startIndex: 32, type: '' }
] ]
}] }],
// Block comment
[{
line: '{{!-- block comment --}}',
tokens: [
{ startIndex: 0, type: 'comment.block.start.handlebars' },
{ startIndex: 5, type: 'comment.content.handlebars' },
{ startIndex: 20, type: 'comment.block.end.handlebars' }
]
}],
// Block comment with mustache
[{
line: '{{!-- block comment }} with mustache --}}',
tokens: [
{ startIndex: 0, type: 'comment.block.start.handlebars' },
{ startIndex: 5, type: 'comment.content.handlebars' },
{ startIndex: 37, type: 'comment.block.end.handlebars' }
]
}],
// Handlebars comment
[{
line: '{{! comment }}',
tokens: [
{ startIndex: 0, type: 'comment.start.handlebars' },
{ startIndex: 3, type: 'comment.content.handlebars' },
{ startIndex: 12, type: 'comment.end.handlebars' }
]
}],
]); ]);

View file

@ -63,9 +63,11 @@ export const language = <ILanguage>{
// The main tokenizer for our languages // The main tokenizer for our languages
tokenizer: { tokenizer: {
root: [ root: [
[/\{\{!--/, 'comment.block.start.handlebars', '@commentBlock'],
[/\{\{!/, 'comment.start.handlebars', '@comment'],
[/\{\{/, { token: '@rematch', switchTo: '@handlebarsInSimpleState.root' }], [/\{\{/, { token: '@rematch', switchTo: '@handlebarsInSimpleState.root' }],
[/<!DOCTYPE/, 'metatag.html', '@doctype'], [/<!DOCTYPE/, 'metatag.html', '@doctype'],
[/<!--/, 'comment.html', '@comment'], [/<!--/, 'comment.html', '@commentHtml'],
[/(<)(\w+)(\/>)/, ['delimiter.html', 'tag.html', 'delimiter.html']], [/(<)(\w+)(\/>)/, ['delimiter.html', 'tag.html', 'delimiter.html']],
[/(<)(script)/, ['delimiter.html', { token: 'tag.html', next: '@script' }]], [/(<)(script)/, ['delimiter.html', { token: 'tag.html', next: '@script' }]],
[/(<)(style)/, ['delimiter.html', { token: 'tag.html', next: '@style' }]], [/(<)(style)/, ['delimiter.html', { token: 'tag.html', next: '@style' }]],
@ -83,6 +85,16 @@ export const language = <ILanguage>{
], ],
comment: [ comment: [
[/\}\}/, 'comment.end.handlebars', '@pop'],
[/./, 'comment.content.handlebars']
],
commentBlock: [
[/--\}\}/, 'comment.block.end.handlebars', '@pop'],
[/./, 'comment.content.handlebars']
],
commentHtml: [
[/\{\{/, { token: '@rematch', switchTo: '@handlebarsInSimpleState.comment' }], [/\{\{/, { token: '@rematch', switchTo: '@handlebarsInSimpleState.comment' }],
[/-->/, 'comment.html', '@pop'], [/-->/, 'comment.html', '@pop'],
[/[^-]+/, 'comment.content.html'], [/[^-]+/, 'comment.content.html'],