Skip to content

Commit

Permalink
semantic token support for enums
Browse files Browse the repository at this point in the history
  • Loading branch information
TwitchBronBron committed Feb 10, 2022
1 parent f815312 commit a12d835
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('BrsFileSemanticTokensProcessor', () => {
program.dispose();
});

it('matches each namespace section', () => {
it('matches each namespace section for class', () => {
const file = program.setFile<BrsFile>('source/main.bs', `
namespace Earthlings.Humanoids
class Person
Expand All @@ -45,4 +45,35 @@ describe('BrsFileSemanticTokensProcessor', () => {
tokenType: SemanticTokenTypes.class
}]);
});

it.only('matches each namespace section', () => {
const file = program.setFile<BrsFile>('source/main.bs', `
sub main()
print Earthlings.Species.Human.Male
end sub
namespace Earthlings.Species
enum Human
Male
Female
end enum
end namespace
`);
program.validate();
expectZeroDiagnostics(program);
expect(
util.sortByRange(program.getSemanticTokens(file.pathAbsolute))
).to.eql([{
range: util.createRange(2, 22, 2, 32),
tokenType: SemanticTokenTypes.namespace
}, {
range: util.createRange(2, 33, 2, 40),
tokenType: SemanticTokenTypes.namespace
}, {
range: util.createRange(2, 41, 2, 46),
tokenType: SemanticTokenTypes.enum
}, {
range: util.createRange(2, 47, 2, 51),
tokenType: SemanticTokenTypes.enumMember
}]);
});
});
35 changes: 35 additions & 0 deletions src/bscPlugin/semanticTokens/BrsFileSemanticTokensProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export class BrsFileSemanticTokensProcessor {

public process() {
this.handleClasses();
this.handleEnums();
}

private handleClasses() {
Expand Down Expand Up @@ -64,6 +65,40 @@ export class BrsFileSemanticTokensProcessor {
});
}
}
}

private handleEnums() {
const enumLookup = this.event.file.program.getFirstScopeForFile(this.event.file)?.getEnumMap();
for (const expression of this.event.file.parser.references.expressions) {
const parts = util.getAllDottedGetParts(expression)?.map(x => x.toLowerCase());
if (parts) {
//discard the enum member name
const memberName = parts.pop();
//get the name of the enum (including leading namespace if applicable)
const enumName = parts.join('.');
const lowerEnumName = enumName.toLowerCase();
const theEnum = enumLookup.get(lowerEnumName)?.item;
if (theEnum) {
const tokens = util.splitGetRange('.', lowerEnumName + '.' + memberName, expression.range);
//enum member name
this.event.semanticTokens.push({
range: tokens.pop().range,
tokenType: SemanticTokenTypes.enumMember
});
//enum name
this.event.semanticTokens.push({
range: tokens.pop().range,
tokenType: SemanticTokenTypes.enum
});
//namespace parts
for (const token of tokens) {
this.event.semanticTokens.push({
range: token.range,
tokenType: SemanticTokenTypes.namespace
});
}
}
}
}
}
}
2 changes: 1 addition & 1 deletion src/bscPlugin/transpile/BrsFilePreTranspileProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class BrsFilePreTranspileProcessor {
//get the name of the enum (including leading namespace if applicable)
const enumName = parts.join('.');
const lowerEnumName = enumName.toLowerCase();
const theEnum = enumLookup.get(lowerEnumName).item;
const theEnum = enumLookup.get(lowerEnumName)?.item;
if (theEnum) {
const members = membersByEnum.getOrAdd(lowerEnumName, () => theEnum.getMemberValueMap());
const value = members?.get(memberName);
Expand Down

0 comments on commit a12d835

Please sign in to comment.