Skip to content

Commit

Permalink
Syntax highlighting but for object functions (#604)
Browse files Browse the repository at this point in the history
* Unit tests

* Fixed some scope names missing .brs

* updated regex for function_declaration and method
  • Loading branch information
chrisdp authored Nov 20, 2024
1 parent ddbade2 commit fccef69
Show file tree
Hide file tree
Showing 2 changed files with 245 additions and 5 deletions.
240 changes: 240 additions & 0 deletions src/grammar/brightscript.tmLanguage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(`
Expand Down
10 changes: 5 additions & 5 deletions syntaxes/brightscript.tmLanguage.json
Original file line number Diff line number Diff line change
Expand Up @@ -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_]+))",
Expand Down Expand Up @@ -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*\\("
Expand Down

0 comments on commit fccef69

Please sign in to comment.