Skip to content

Commit 682c432

Browse files
author
Jordi Ramos
authored
Add grammar support for new directives: rendermode, preservewhitespace and typeparam (#6887)
* added grammar support for new directives - added grammar support for rendermode, preservewhitespace and typeparam in tmLanguage.yml - populated the corresponding json file with the npm run compile:razorTextMate command - added the corresponding tests in Microsoft.AspNetCore.Razor.VSCode.Grammar.Test. Also added the tests in the snapshot.
1 parent 1bf0848 commit 682c432

File tree

7 files changed

+377
-1
lines changed

7 files changed

+377
-1
lines changed

src/razor/syntaxes/aspnetcorerazor.tmLanguage.json

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,15 @@
525525
},
526526
{
527527
"include": "#using-directive"
528+
},
529+
{
530+
"include": "#rendermode-directive"
531+
},
532+
{
533+
"include": "#preservewhitespace-directive"
534+
},
535+
{
536+
"include": "#typeparam-directive"
528537
}
529538
]
530539
},
@@ -849,6 +858,75 @@
849858
}
850859
}
851860
},
861+
"rendermode-directive": {
862+
"name": "meta.directive",
863+
"match": "(@)(rendermode)\\s+([^$]+)?",
864+
"captures": {
865+
"1": {
866+
"patterns": [
867+
{
868+
"include": "#transition"
869+
}
870+
]
871+
},
872+
"2": {
873+
"name": "keyword.control.razor.directive.rendermode"
874+
},
875+
"3": {
876+
"patterns": [
877+
{
878+
"include": "source.cs#type"
879+
}
880+
]
881+
}
882+
}
883+
},
884+
"preservewhitespace-directive": {
885+
"name": "meta.directive",
886+
"match": "(@)(preservewhitespace)\\s+([^$]+)?",
887+
"captures": {
888+
"1": {
889+
"patterns": [
890+
{
891+
"include": "#transition"
892+
}
893+
]
894+
},
895+
"2": {
896+
"name": "keyword.control.razor.directive.preservewhitespace"
897+
},
898+
"3": {
899+
"patterns": [
900+
{
901+
"include": "source.cs#boolean-literal"
902+
}
903+
]
904+
}
905+
}
906+
},
907+
"typeparam-directive": {
908+
"name": "meta.directive",
909+
"match": "(@)(typeparam)\\s+([^$]+)?",
910+
"captures": {
911+
"1": {
912+
"patterns": [
913+
{
914+
"include": "#transition"
915+
}
916+
]
917+
},
918+
"2": {
919+
"name": "keyword.control.razor.directive.typeparam"
920+
},
921+
"3": {
922+
"patterns": [
923+
{
924+
"include": "source.cs#type"
925+
}
926+
]
927+
}
928+
}
929+
},
852930
"attribute-directive": {
853931
"name": "meta.directive",
854932
"begin": "(@)(attribute)\\b\\s+",

src/razor/syntaxes/aspnetcorerazor.tmLanguage.yml

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,9 @@ repository:
281281
- include: '#section-directive'
282282
- include: '#layout-directive'
283283
- include: '#using-directive'
284+
- include: '#rendermode-directive'
285+
- include: '#preservewhitespace-directive'
286+
- include: '#typeparam-directive'
284287

285288
#>>>>> @code and @functions <<<<<
286289

@@ -420,6 +423,36 @@ repository:
420423
3: { patterns: [ include: 'source.cs#type' ] }
421424
4: { name: 'entity.name.variable.property.cs' }
422425

426+
#>>>>> @rendermode <<<<<
427+
428+
rendermode-directive:
429+
name: 'meta.directive'
430+
match: '(@)(rendermode)\s+([^$]+)?'
431+
captures:
432+
1: { patterns: [ include: '#transition' ] }
433+
2: { name: 'keyword.control.razor.directive.rendermode'}
434+
3: { patterns: [ include: 'source.cs#type' ] }
435+
436+
#>>>>> @preservewhitespace <<<<<
437+
438+
preservewhitespace-directive:
439+
name: 'meta.directive'
440+
match: '(@)(preservewhitespace)\s+([^$]+)?'
441+
captures:
442+
1: { patterns: [ include: '#transition' ] }
443+
2: { name: 'keyword.control.razor.directive.preservewhitespace'}
444+
3: { patterns: [ include: 'source.cs#boolean-literal' ] }
445+
446+
#>>>>> @typeparam <<<<<
447+
448+
typeparam-directive:
449+
name: 'meta.directive'
450+
match: '(@)(typeparam)\s+([^$]+)?'
451+
captures:
452+
1: { patterns: [ include: '#transition' ] }
453+
2: { name: 'keyword.control.razor.directive.typeparam'}
454+
3: { patterns: [ include: 'source.cs#type' ] }
455+
423456
#>>>>> @attribute <<<<<
424457

425458
attribute-directive:
@@ -925,4 +958,4 @@ repository:
925958
- include: 'source.cs#punctuation-semicolon'
926959
end: '(\))'
927960
endCaptures:
928-
1: { name: 'punctuation.parenthesis.close.cs' }
961+
1: { name: 'punctuation.parenthesis.close.cs' }

test/razorTests/Microsoft.AspNetCore.Razor.VSCode.Grammar.Test/tests/__snapshots__/grammarTests.test.ts.snap

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2820,6 +2820,58 @@ exports[`Grammar tests @page directive Routed spaced 1`] = `
28202820
"
28212821
`;
28222822

2823+
exports[`Grammar tests @preservewhitespace directive Bool provided (false) 1`] = `
2824+
"Line: @preservewhitespace false
2825+
- token from 0 to 1 (@) with scopes text.aspnetcorerazor, meta.directive, keyword.control.cshtml.transition
2826+
- token from 1 to 19 (preservewhitespace) with scopes text.aspnetcorerazor, meta.directive, keyword.control.razor.directive.preservewhitespace
2827+
- token from 19 to 20 ( ) with scopes text.aspnetcorerazor, meta.directive
2828+
- token from 20 to 25 (false) with scopes text.aspnetcorerazor, meta.directive, constant.language.boolean.false.cs
2829+
"
2830+
`;
2831+
2832+
exports[`Grammar tests @preservewhitespace directive Bool provided (true) 1`] = `
2833+
"Line: @preservewhitespace true
2834+
- token from 0 to 1 (@) with scopes text.aspnetcorerazor, meta.directive, keyword.control.cshtml.transition
2835+
- token from 1 to 19 (preservewhitespace) with scopes text.aspnetcorerazor, meta.directive, keyword.control.razor.directive.preservewhitespace
2836+
- token from 19 to 20 ( ) with scopes text.aspnetcorerazor, meta.directive
2837+
- token from 20 to 24 (true) with scopes text.aspnetcorerazor, meta.directive, constant.language.boolean.true.cs
2838+
"
2839+
`;
2840+
2841+
exports[`Grammar tests @preservewhitespace directive Bool provided spaced 1`] = `
2842+
"Line: @preservewhitespace false
2843+
- token from 0 to 1 (@) with scopes text.aspnetcorerazor, meta.directive, keyword.control.cshtml.transition
2844+
- token from 1 to 19 (preservewhitespace) with scopes text.aspnetcorerazor, meta.directive, keyword.control.razor.directive.preservewhitespace
2845+
- token from 19 to 33 ( ) with scopes text.aspnetcorerazor, meta.directive
2846+
- token from 33 to 38 (false) with scopes text.aspnetcorerazor, meta.directive, constant.language.boolean.false.cs
2847+
- token from 38 to 48 ( ) with scopes text.aspnetcorerazor, meta.directive
2848+
"
2849+
`;
2850+
2851+
exports[`Grammar tests @preservewhitespace directive Incomplete bool 1`] = `
2852+
"Line: @preservewhitespace fal
2853+
- token from 0 to 1 (@) with scopes text.aspnetcorerazor, meta.directive, keyword.control.cshtml.transition
2854+
- token from 1 to 19 (preservewhitespace) with scopes text.aspnetcorerazor, meta.directive, keyword.control.razor.directive.preservewhitespace
2855+
- token from 19 to 20 ( ) with scopes text.aspnetcorerazor, meta.directive
2856+
- token from 20 to 24 (fal) with scopes text.aspnetcorerazor, meta.directive
2857+
"
2858+
`;
2859+
2860+
exports[`Grammar tests @preservewhitespace directive No bool 1`] = `
2861+
"Line: @preservewhitespace
2862+
- token from 0 to 1 (@) with scopes text.aspnetcorerazor, meta.directive, keyword.control.cshtml.transition
2863+
- token from 1 to 19 (preservewhitespace) with scopes text.aspnetcorerazor, meta.directive, keyword.control.razor.directive.preservewhitespace
2864+
"
2865+
`;
2866+
2867+
exports[`Grammar tests @preservewhitespace directive No bool spaced 1`] = `
2868+
"Line: @preservewhitespace
2869+
- token from 0 to 1 (@) with scopes text.aspnetcorerazor, meta.directive, keyword.control.cshtml.transition
2870+
- token from 1 to 19 (preservewhitespace) with scopes text.aspnetcorerazor, meta.directive, keyword.control.razor.directive.preservewhitespace
2871+
- token from 19 to 34 ( ) with scopes text.aspnetcorerazor, meta.directive
2872+
"
2873+
`;
2874+
28232875
exports[`Grammar tests @removeTagHelper directive Incomplete parameter 1`] = `
28242876
"Line: @removeTagHelper "
28252877
- token from 0 to 1 (@) with scopes text.aspnetcorerazor, meta.directive, keyword.control.cshtml.transition
@@ -2876,6 +2928,49 @@ exports[`Grammar tests @removeTagHelper directive Unquoted parameter 1`] = `
28762928
"
28772929
`;
28782930

2931+
exports[`Grammar tests @rendermode directive Incomplete mode 1`] = `
2932+
"Line: @rendermode InteractiveWebAssemb
2933+
- token from 0 to 1 (@) with scopes text.aspnetcorerazor, meta.directive, keyword.control.cshtml.transition
2934+
- token from 1 to 11 (rendermode) with scopes text.aspnetcorerazor, meta.directive, keyword.control.razor.directive.rendermode
2935+
- token from 11 to 12 ( ) with scopes text.aspnetcorerazor, meta.directive
2936+
- token from 12 to 32 (InteractiveWebAssemb) with scopes text.aspnetcorerazor, meta.directive, entity.name.type.cs
2937+
"
2938+
`;
2939+
2940+
exports[`Grammar tests @rendermode directive Mode provided 1`] = `
2941+
"Line: @rendermode InteractiveServer
2942+
- token from 0 to 1 (@) with scopes text.aspnetcorerazor, meta.directive, keyword.control.cshtml.transition
2943+
- token from 1 to 11 (rendermode) with scopes text.aspnetcorerazor, meta.directive, keyword.control.razor.directive.rendermode
2944+
- token from 11 to 12 ( ) with scopes text.aspnetcorerazor, meta.directive
2945+
- token from 12 to 29 (InteractiveServer) with scopes text.aspnetcorerazor, meta.directive, entity.name.type.cs
2946+
"
2947+
`;
2948+
2949+
exports[`Grammar tests @rendermode directive Mode provided spaced 1`] = `
2950+
"Line: @rendermode InteractiveAuto
2951+
- token from 0 to 1 (@) with scopes text.aspnetcorerazor, meta.directive, keyword.control.cshtml.transition
2952+
- token from 1 to 11 (rendermode) with scopes text.aspnetcorerazor, meta.directive, keyword.control.razor.directive.rendermode
2953+
- token from 11 to 25 ( ) with scopes text.aspnetcorerazor, meta.directive
2954+
- token from 25 to 40 (InteractiveAuto) with scopes text.aspnetcorerazor, meta.directive, entity.name.type.cs
2955+
- token from 40 to 50 ( ) with scopes text.aspnetcorerazor, meta.directive
2956+
"
2957+
`;
2958+
2959+
exports[`Grammar tests @rendermode directive No mode 1`] = `
2960+
"Line: @rendermode
2961+
- token from 0 to 1 (@) with scopes text.aspnetcorerazor, meta.directive, keyword.control.cshtml.transition
2962+
- token from 1 to 11 (rendermode) with scopes text.aspnetcorerazor, meta.directive, keyword.control.razor.directive.rendermode
2963+
"
2964+
`;
2965+
2966+
exports[`Grammar tests @rendermode directive No mode spaced 1`] = `
2967+
"Line: @rendermode
2968+
- token from 0 to 1 (@) with scopes text.aspnetcorerazor, meta.directive, keyword.control.cshtml.transition
2969+
- token from 1 to 11 (rendermode) with scopes text.aspnetcorerazor, meta.directive, keyword.control.razor.directive.rendermode
2970+
- token from 11 to 26 ( ) with scopes text.aspnetcorerazor, meta.directive
2971+
"
2972+
`;
2973+
28792974
exports[`Grammar tests @section directive As C# local 1`] = `
28802975
"Line: @section.method()
28812976
- token from 0 to 1 (@) with scopes text.aspnetcorerazor, meta.expression.implicit.cshtml, keyword.control.cshtml.transition
@@ -3661,6 +3756,63 @@ exports[`Grammar tests @try { ... } catch/finally { ... } Single line 1`] = `
36613756
"
36623757
`;
36633758

3759+
exports[`Grammar tests @typeparam directive Complete type, generic 1`] = `
3760+
"Line: @typeparam SomeViewBase<string>
3761+
- token from 0 to 1 (@) with scopes text.aspnetcorerazor, meta.directive, keyword.control.cshtml.transition
3762+
- token from 1 to 10 (typeparam) with scopes text.aspnetcorerazor, meta.directive, keyword.control.razor.directive.typeparam
3763+
- token from 10 to 11 ( ) with scopes text.aspnetcorerazor, meta.directive
3764+
- token from 11 to 23 (SomeViewBase) with scopes text.aspnetcorerazor, meta.directive, entity.name.type.cs
3765+
- token from 23 to 24 (<) with scopes text.aspnetcorerazor, meta.directive, punctuation.definition.typeparameters.begin.cs
3766+
- token from 24 to 30 (string) with scopes text.aspnetcorerazor, meta.directive, keyword.type.cs
3767+
- token from 30 to 31 (>) with scopes text.aspnetcorerazor, meta.directive, punctuation.definition.typeparameters.end.cs
3768+
"
3769+
`;
3770+
3771+
exports[`Grammar tests @typeparam directive Incomplete type, generic 1`] = `
3772+
"Line: @typeparam SomeViewBase<string
3773+
- token from 0 to 1 (@) with scopes text.aspnetcorerazor, meta.directive, keyword.control.cshtml.transition
3774+
- token from 1 to 10 (typeparam) with scopes text.aspnetcorerazor, meta.directive, keyword.control.razor.directive.typeparam
3775+
- token from 10 to 11 ( ) with scopes text.aspnetcorerazor, meta.directive
3776+
- token from 11 to 23 (SomeViewBase) with scopes text.aspnetcorerazor, meta.directive, entity.name.type.cs
3777+
- token from 23 to 24 (<) with scopes text.aspnetcorerazor, meta.directive, punctuation.definition.typeparameters.begin.cs
3778+
- token from 24 to 30 (string) with scopes text.aspnetcorerazor, meta.directive, keyword.type.cs
3779+
"
3780+
`;
3781+
3782+
exports[`Grammar tests @typeparam directive No type 1`] = `
3783+
"Line: @typeparam
3784+
- token from 0 to 1 (@) with scopes text.aspnetcorerazor, meta.directive, keyword.control.cshtml.transition
3785+
- token from 1 to 10 (typeparam) with scopes text.aspnetcorerazor, meta.directive, keyword.control.razor.directive.typeparam
3786+
"
3787+
`;
3788+
3789+
exports[`Grammar tests @typeparam directive No type spaced 1`] = `
3790+
"Line: @typeparam
3791+
- token from 0 to 1 (@) with scopes text.aspnetcorerazor, meta.directive, keyword.control.cshtml.transition
3792+
- token from 1 to 10 (typeparam) with scopes text.aspnetcorerazor, meta.directive, keyword.control.razor.directive.typeparam
3793+
- token from 10 to 25 ( ) with scopes text.aspnetcorerazor, meta.directive
3794+
"
3795+
`;
3796+
3797+
exports[`Grammar tests @typeparam directive Type provided 1`] = `
3798+
"Line: @typeparam Person
3799+
- token from 0 to 1 (@) with scopes text.aspnetcorerazor, meta.directive, keyword.control.cshtml.transition
3800+
- token from 1 to 10 (typeparam) with scopes text.aspnetcorerazor, meta.directive, keyword.control.razor.directive.typeparam
3801+
- token from 10 to 11 ( ) with scopes text.aspnetcorerazor, meta.directive
3802+
- token from 11 to 17 (Person) with scopes text.aspnetcorerazor, meta.directive, entity.name.type.cs
3803+
"
3804+
`;
3805+
3806+
exports[`Grammar tests @typeparam directive Type provided spaced 1`] = `
3807+
"Line: @typeparam Person
3808+
- token from 0 to 1 (@) with scopes text.aspnetcorerazor, meta.directive, keyword.control.cshtml.transition
3809+
- token from 1 to 10 (typeparam) with scopes text.aspnetcorerazor, meta.directive, keyword.control.razor.directive.typeparam
3810+
- token from 10 to 24 ( ) with scopes text.aspnetcorerazor, meta.directive
3811+
- token from 24 to 30 (Person) with scopes text.aspnetcorerazor, meta.directive, entity.name.type.cs
3812+
- token from 30 to 40 ( ) with scopes text.aspnetcorerazor, meta.directive
3813+
"
3814+
`;
3815+
36643816
exports[`Grammar tests @using ( ... ) { ... } Incomplete using statement, no condition 1`] = `
36653817
"Line: @using {}
36663818
- token from 0 to 1 (@) with scopes text.aspnetcorerazor, meta.directive, keyword.control.cshtml.transition

test/razorTests/Microsoft.AspNetCore.Razor.VSCode.Grammar.Test/tests/grammarTests.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ import { RunTryStatementSuite } from './tryStatement';
3939
import { RunUsingDirectiveSuite } from './usingDirective';
4040
import { RunUsingStatementSuite } from './usingStatement';
4141
import { RunWhileStatementSuite } from './whileStatement';
42+
import { RunRendermodeDirectiveSuite } from './rendermodeDirective';
43+
import { RunPreservewhitespaceDirectiveSuite } from './preservewhitespaceDirective';
44+
import { RunTypeparamDirectiveSuite } from './typeparamDirective';
4245

4346
// We bring together all test suites and wrap them in one here. The reason behind this is that
4447
// modules get reloaded per test suite and the vscode-textmate library doesn't support the way
@@ -71,6 +74,9 @@ describe('Grammar tests', () => {
7174
RunSectionDirectiveSuite();
7275
RunLayoutDirectiveSuite();
7376
RunUsingDirectiveSuite();
77+
RunRendermodeDirectiveSuite();
78+
RunPreservewhitespaceDirectiveSuite();
79+
RunTypeparamDirectiveSuite();
7480

7581
// Razor C# Control Structures
7682
RunUsingStatementSuite();
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
import { describe, it } from '@jest/globals';
7+
import { assertMatchesSnapshot } from './infrastructure/testUtilities';
8+
9+
// See GrammarTests.test.ts for details on exporting this test suite instead of running in place.
10+
11+
export function RunPreservewhitespaceDirectiveSuite() {
12+
describe('@preservewhitespace directive', () => {
13+
it('No bool', async () => {
14+
await assertMatchesSnapshot('@preservewhitespace');
15+
});
16+
17+
it('No bool spaced', async () => {
18+
await assertMatchesSnapshot('@preservewhitespace ');
19+
});
20+
21+
it('Incomplete bool', async () => {
22+
await assertMatchesSnapshot('@preservewhitespace fal');
23+
});
24+
25+
it('Bool provided (true)', async () => {
26+
await assertMatchesSnapshot('@preservewhitespace true');
27+
});
28+
29+
it('Bool provided (false)', async () => {
30+
await assertMatchesSnapshot('@preservewhitespace false');
31+
});
32+
33+
it('Bool provided spaced', async () => {
34+
await assertMatchesSnapshot('@preservewhitespace false ');
35+
});
36+
});
37+
}

0 commit comments

Comments
 (0)