From 3875ac87b30b4461e77f81bf0e8cc1255cd4d100 Mon Sep 17 00:00:00 2001 From: Justin Mancusi Date: Wed, 6 May 2020 16:40:36 -0400 Subject: [PATCH 1/2] 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 --- src/handlebars/handlebars.test.ts | 32 ++++++++++++++++++++++++++++++- src/handlebars/handlebars.ts | 14 +++++++++++++- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/handlebars/handlebars.test.ts b/src/handlebars/handlebars.test.ts index 4e5aa2bd..16032c81 100644 --- a/src/handlebars/handlebars.test.ts +++ b/src/handlebars/handlebars.test.ts @@ -281,5 +281,35 @@ testTokenization(['handlebars', 'css'], [ { startIndex: 30, type: 'delimiter.handlebars' }, { 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' } + ] + }], ]); diff --git a/src/handlebars/handlebars.ts b/src/handlebars/handlebars.ts index dc1eee51..8be0e6b8 100644 --- a/src/handlebars/handlebars.ts +++ b/src/handlebars/handlebars.ts @@ -63,9 +63,11 @@ export const language = { // The main tokenizer for our languages tokenizer: { root: [ + [/\{\{!--/, 'comment.block.start.handlebars', '@commentBlock'], + [/\{\{!/, 'comment.start.handlebars', '@comment'], [/\{\{/, { token: '@rematch', switchTo: '@handlebarsInSimpleState.root' }], [/)/, ['delimiter.html', 'tag.html', 'delimiter.html']], [/(<)(script)/, ['delimiter.html', { token: 'tag.html', next: '@script' }]], [/(<)(style)/, ['delimiter.html', { token: 'tag.html', next: '@style' }]], @@ -83,6 +85,16 @@ export const language = { ], comment: [ + [/\}\}/, 'comment.end.handlebars', '@pop'], + [/./, 'comment.content.handlebars'] + ], + + commentBlock: [ + [/--\}\}/, 'comment.block.end.handlebars', '@pop'], + [/./, 'comment.content.handlebars'] + ], + + commentHtml: [ [/\{\{/, { token: '@rematch', switchTo: '@handlebarsInSimpleState.comment' }], [/-->/, 'comment.html', '@pop'], [/[^-]+/, 'comment.content.html'], From b557a57c71c7de1cf4866e7582fd930e76b0ec9e Mon Sep 17 00:00:00 2001 From: Alex Dima Date: Wed, 26 Aug 2020 22:35:47 +0200 Subject: [PATCH 2/2] Fix whitespace --- src/handlebars/handlebars.test.ts | 8 ++++---- src/handlebars/handlebars.ts | 16 ++++++++-------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/handlebars/handlebars.test.ts b/src/handlebars/handlebars.test.ts index 16032c81..71738d77 100644 --- a/src/handlebars/handlebars.test.ts +++ b/src/handlebars/handlebars.test.ts @@ -283,7 +283,7 @@ testTokenization(['handlebars', 'css'], [ ] }], - // Block comment + // Block comment [{ line: '{{!-- block comment --}}', tokens: [ @@ -292,8 +292,8 @@ testTokenization(['handlebars', 'css'], [ { startIndex: 20, type: 'comment.block.end.handlebars' } ] }], - - // Block comment with mustache + + // Block comment with mustache [{ line: '{{!-- block comment }} with mustache --}}', tokens: [ @@ -303,7 +303,7 @@ testTokenization(['handlebars', 'css'], [ ] }], - // Handlebars comment + // Handlebars comment [{ line: '{{! comment }}', tokens: [ diff --git a/src/handlebars/handlebars.ts b/src/handlebars/handlebars.ts index 8be0e6b8..17db3fc3 100644 --- a/src/handlebars/handlebars.ts +++ b/src/handlebars/handlebars.ts @@ -85,14 +85,14 @@ export const language = { ], comment: [ - [/\}\}/, 'comment.end.handlebars', '@pop'], - [/./, 'comment.content.handlebars'] - ], - - commentBlock: [ - [/--\}\}/, 'comment.block.end.handlebars', '@pop'], - [/./, 'comment.content.handlebars'] - ], + [/\}\}/, 'comment.end.handlebars', '@pop'], + [/./, 'comment.content.handlebars'] + ], + + commentBlock: [ + [/--\}\}/, 'comment.block.end.handlebars', '@pop'], + [/./, 'comment.content.handlebars'] + ], commentHtml: [ [/\{\{/, { token: '@rematch', switchTo: '@handlebarsInSimpleState.comment' }],