From fccef69fa1e23b094d3f351bb6209ac94d844f38 Mon Sep 17 00:00:00 2001 From: Christopher Dwyer-Perkins Date: Wed, 20 Nov 2024 09:17:46 -0500 Subject: [PATCH] Syntax highlighting but for object functions (#604) * Unit tests * Fixed some scope names missing .brs * updated regex for function_declaration and method --- src/grammar/brightscript.tmLanguage.spec.ts | 240 ++++++++++++++++++++ syntaxes/brightscript.tmLanguage.json | 10 +- 2 files changed, 245 insertions(+), 5 deletions(-) diff --git a/src/grammar/brightscript.tmLanguage.spec.ts b/src/grammar/brightscript.tmLanguage.spec.ts index 5ac7572c..0620b1f0 100644 --- a/src/grammar/brightscript.tmLanguage.spec.ts +++ b/src/grammar/brightscript.tmLanguage.spec.ts @@ -535,6 +535,246 @@ describe('brightscript.tmlanguage.json', () => { `); }); + it('handles named function declarations', async () => { + await testGrammar(` + sub write() + ' ^^^^^ entity.name.function.brs + '^^^ keyword.declaration.function.brs + + end sub + '^^^^^^^ keyword.declaration.function.brs + `); + + await testGrammar(` + sub write () + ' ^^^^^ entity.name.function.brs + '^^^ keyword.declaration.function.brs + + end sub + '^^^^^^^ keyword.declaration.function.brs + `); + + await testGrammar(` + sub write() as string + ' ^^^^^^ storage.type.brs + ' ^^ keyword.control.brs + ' ^^^^^ entity.name.function.brs + '^^^ keyword.declaration.function.brs + + end sub + '^^^^^^^ keyword.declaration.function.brs + `); + + await testGrammar(` + sub write(param as function) as string + ' ^^^^^^ storage.type.brs + ' ^^ keyword.control.brs + ' ^^^^^^^^ storage.type.brs + ' ^^ keyword.control.brs + ' ^^^^^ entity.name.variable.local.brs + ' ^^^^^ entity.name.function.brs + '^^^ keyword.declaration.function.brs + + end sub + '^^^^^^^ keyword.declaration.function.brs + `); + }); + + it('handles named public/protected/private function declarations', async () => { + await testGrammar(` + public sub write() + ' ^^^^^ entity.name.function.brs + ' ^^^ keyword.declaration.function.brs + '^^^^^^ storage.modifier.brs + + end sub + '^^^^^^^ keyword.declaration.function.brs + `); + + await testGrammar(` + protected sub write() + ' ^^^^^ entity.name.function.brs + ' ^^^ keyword.declaration.function.brs + '^^^^^^^^^ storage.modifier.brs + + end sub + '^^^^^^^ keyword.declaration.function.brs + `); + + await testGrammar(` + private sub write() + ' ^^^^^ entity.name.function.brs + ' ^^^ keyword.declaration.function.brs + '^^^^^^^ storage.modifier.brs + + end sub + '^^^^^^^ keyword.declaration.function.brs + `); + + await testGrammar(` + public sub write() as string + ' ^^^^^^ storage.type.brs + ' ^^ keyword.control.brs + ' ^^^^^ entity.name.function.brs + ' ^^^ keyword.declaration.function.brs + '^^^^^^ storage.modifier.brs + + end sub + '^^^^^^^ keyword.declaration.function.brs + `); + + await testGrammar(` + public sub write(param as function) as string + ' ^^^^^^ storage.type.brs + ' ^^ keyword.control.brs + ' ^^^^^^^^ storage.type.brs + ' ^^ keyword.control.brs + ' ^^^^^ entity.name.variable.local.brs + ' ^^^^^ entity.name.function.brs + ' ^^^ keyword.declaration.function.brs + '^^^^^^ storage.modifier.brs + + end sub + '^^^^^^^ keyword.declaration.function.brs + `); + }); + + it('handles named public/protected/private with override function declarations', async () => { + await testGrammar(` + public override sub write() + ' ^^^^^ entity.name.function.brs + ' ^^^ keyword.declaration.function.brs + ' ^^^^^^^^ storage.modifier.brs + '^^^^^^ storage.modifier.brs + + end sub + '^^^^^^^ keyword.declaration.function.brs + `); + + await testGrammar(` + protected override sub write() + ' ^^^^^ entity.name.function.brs + ' ^^^ keyword.declaration.function.brs + ' ^^^^^^^^ storage.modifier.brs + '^^^^^^^^^ storage.modifier.brs + + end sub + '^^^^^^^ keyword.declaration.function.brs + `); + + await testGrammar(` + private override sub write() + ' ^^^^^ entity.name.function.brs + ' ^^^ keyword.declaration.function.brs + ' ^^^^^^^^ storage.modifier.brs + '^^^^^^^ storage.modifier.brs + + end sub + '^^^^^^^ keyword.declaration.function.brs + `); + + await testGrammar(` + public override sub write() as string + ' ^^^^^^ storage.type.brs + ' ^^ keyword.control.brs + ' ^^^^^ entity.name.function.brs + ' ^^^ keyword.declaration.function.brs + ' ^^^^^^^^ storage.modifier.brs + '^^^^^^ storage.modifier.brs + + end sub + '^^^^^^^ keyword.declaration.function.brs + `); + + await testGrammar(` + public override sub write(param as function) as string + ' ^^^^^^ storage.type.brs + ' ^^ keyword.control.brs + ' ^^^^^^^^ storage.type.brs + ' ^^ keyword.control.brs + ' ^^^^^ entity.name.variable.local.brs + ' ^^^^^ entity.name.function.brs + ' ^^^ keyword.declaration.function.brs + ' ^^^^^^^^ storage.modifier.brs + '^^^^^^ storage.modifier.brs + + end sub + '^^^^^^^ keyword.declaration.function.brs + `); + }); + + it('handles anon function declarations', async () => { + await testGrammar(` + var = function () + ' ^^^^^^^^ keyword.declaration.function.brs + '^^^ entity.name.variable.local.brs + + end function + '^^^^^^^ keyword.declaration.function.brs + `); + + await testGrammar(` + var = function() + ' ^^^^^^^^ keyword.declaration.function.brs + '^^^ entity.name.variable.local.brs + + end function + '^^^^^^^ keyword.declaration.function.brs + `); + + await testGrammar(` + var = function() as string + ' ^^^^^^ storage.type.brs + ' ^^ keyword.control.brs + ' ^^^^^^^^ keyword.declaration.function.brs + '^^^ entity.name.variable.local.brs + + end function + '^^^^^^^ keyword.declaration.function.brs + `); + + await testGrammar(` + var = function(param as function) as string + ' ^^^^^^ storage.type.brs + ' ^^ keyword.control.brs + ' ^^^^^^^^ storage.type.brs + ' ^^ keyword.control.brs + ' ^^^^^ entity.name.variable.local.brs + ' ^^^^^^^^ keyword.declaration.function.brs + '^^^ entity.name.variable.local.brs + + end function + '^^^^^^^ keyword.declaration.function.brs + `); + + await testGrammar(` + var = { + name: function() as string + ' ^^^^^^ storage.type.brs + ' ^^ keyword.control.brs + ' ^^^^^^^^ keyword.declaration.function.brs + + end function + '^^^^^^^ keyword.declaration.function.brs + } + `); + + await testGrammar(` + var = { + name: function(param as function) as string + ' ^^^^^^ storage.type.brs + ' ^^ keyword.control.brs + ' ^^^^^^^^ storage.type.brs + ' ^^ keyword.control.brs + ' ^^^^^ entity.name.variable.local.brs + ' ^^^^^^^^ keyword.declaration.function.brs + + end function + '^^^^^^^ keyword.declaration.function.brs + } + `); + }); + it.skip('colorizes class fields properly', async () => { //TODO the properties have the wrong scope...this should get fixed when we improve the class textmate scope flow await testGrammar(` diff --git a/syntaxes/brightscript.tmLanguage.json b/syntaxes/brightscript.tmLanguage.json index 86f5b6a2..cd28c268 100644 --- a/syntaxes/brightscript.tmLanguage.json +++ b/syntaxes/brightscript.tmLanguage.json @@ -510,7 +510,7 @@ "name": "entity.name.function.brs" } }, - "match": "(?i:(?:(public|protected|private)\\s+)?(?:(override)\\s+)?((?:sub|function)[^\\w])(?:\\s+([a-z_][a-z0-9_]*))?)" + "match": "(?i:(?:(public|protected|private)\\s+)?(?:(override)\\s+)?((?:sub|function))(?:\\s+(?:\\(|([a-z_][a-z0-9_]*))))" }, "field": { "match": "(?i:(public|protected|private)\\s+([a-z0-9_]+))", @@ -731,19 +731,19 @@ "name": "entity.name.function.brs" } }, - "match": "(?i:(?:(public|protected|private)\\s+)?(?:(override)\\s+)?((?:sub|function)[^\\w])(?:\\s+([a-z_][a-z0-9_]*))?)" + "match": "(?i:(?:(public|protected|private)\\s+)?(?:(override)\\s+)?((?:sub|function))(?:\\s+(?:\\(|([a-z_][a-z0-9_]*))))" }, "end_function": { - "name": "keyword.declaration.function", + "name": "keyword.declaration.function.brs", "match": "(?i)[ \\t]*end\\s*(sub|function)" }, "inline_function_declaration": { "captures": { "1": { - "name": "keyword.declaration.function" + "name": "keyword.declaration.function.brs" }, "2": { - "name": "keyword.declaration.function" + "name": "keyword.declaration.function.brs" } }, "match": "(?i)[^a-z0-9_\"](function|sub)\\s*\\("