From 2297d8b00d3c8aa388d39d3483d2d0b6f46a50c5 Mon Sep 17 00:00:00 2001 From: Bronley Plumb Date: Mon, 30 Sep 2024 15:41:27 -0400 Subject: [PATCH 1/2] Fix semantic tokens for component and interface instances. --- .../BrsFileSemanticTokensProcessor.spec.ts | 30 +++++++++++++++++++ .../BrsFileSemanticTokensProcessor.ts | 12 +++++--- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/bscPlugin/semanticTokens/BrsFileSemanticTokensProcessor.spec.ts b/src/bscPlugin/semanticTokens/BrsFileSemanticTokensProcessor.spec.ts index 8baacc690..00de49114 100644 --- a/src/bscPlugin/semanticTokens/BrsFileSemanticTokensProcessor.spec.ts +++ b/src/bscPlugin/semanticTokens/BrsFileSemanticTokensProcessor.spec.ts @@ -503,6 +503,36 @@ describe('BrsFileSemanticTokensProcessor', () => { ], false); }); + it('does not color variable of type roSGNode as a type', () => { + const file = program.setFile('source/main.bs', ` + sub main() + node = CreateObject("roSGNode", "ContentNode") + node.id = "content" + end sub + `); + program.validate(); + expectSemanticTokensIncludes(file, [ + // |node|.id = "content" + [SemanticTokenTypes.variable, 3, 16, 3, 20] + ], false); + }); + + it('does not color variable when it is an instance of an interface', () => { + const file = program.setFile('source/main.bs', ` + sub main(video as Movie) + video.url = "http://example.com" + end sub + interface Movie + url as string + end interface + `); + program.validate(); + expectSemanticTokensIncludes(file, [ + // |video|.url = "http://example.com" + [SemanticTokenTypes.variable, 2, 16, 2, 21] + ], false); + }); + it('works for `new` statement', () => { const file = program.setFile('source/main.bs', ` class Person diff --git a/src/bscPlugin/semanticTokens/BrsFileSemanticTokensProcessor.ts b/src/bscPlugin/semanticTokens/BrsFileSemanticTokensProcessor.ts index 81e853431..1b7c99b20 100644 --- a/src/bscPlugin/semanticTokens/BrsFileSemanticTokensProcessor.ts +++ b/src/bscPlugin/semanticTokens/BrsFileSemanticTokensProcessor.ts @@ -1,6 +1,6 @@ import { SemanticTokenModifiers } from 'vscode-languageserver-protocol'; import { SemanticTokenTypes } from 'vscode-languageserver-protocol'; -import { isCallableType, isClassType, isComponentType, isConstStatement, isDottedGetExpression, isEnumMemberType, isEnumType, isFunctionExpression, isFunctionStatement, isInterfaceType, isNamespaceType, isVariableExpression } from '../../astUtils/reflection'; +import { isCallableType, isClassType, isComponentType, isConstStatement, isDottedGetExpression, isEnumMemberType, isEnumType, isFunctionExpression, isFunctionStatement, isInterfaceType, isNamespaceType, isTypeExpression, isVariableExpression } from '../../astUtils/reflection'; import type { BrsFile } from '../../files/BrsFile'; import type { ExtraSymbolData, OnGetSemanticTokensEvent, SemanticToken, TypeChainEntry } from '../../interfaces'; import type { Locatable, Token } from '../../lexer/Token'; @@ -132,9 +132,9 @@ export class BrsFileSemanticTokensProcessor { } else { return { type: SemanticTokenTypes.method }; } - } else if (isInterfaceType(type)) { + } else if (isInterfaceType(type) && extraData.isInstance !== true) { return { type: SemanticTokenTypes.interface }; - } else if (isComponentType(type)) { + } else if (isComponentType(type) && extraData.isInstance !== true) { return { type: SemanticTokenTypes.class }; } else if (isEnumType(type)) { return { type: SemanticTokenTypes.enum }; @@ -146,7 +146,11 @@ export class BrsFileSemanticTokensProcessor { } else if (isConstStatement(node)) { return { type: SemanticTokenTypes.variable, modifiers: [SemanticTokenModifiers.readonly, SemanticTokenModifiers.static] }; } else if (isVariableExpression(node)) { - return { type: SemanticTokenTypes.variable }; + if (/m/i.test(node.tokens?.name?.text)) { + //don't color `m` variables + } else { + return { type: SemanticTokenTypes.variable }; + } } else { //we don't know what it is...return undefined to prevent creating a semantic token } From 427d78a3fbea421fb654116d9ad13f2eb136079d Mon Sep 17 00:00:00 2001 From: Bronley Plumb Date: Mon, 30 Sep 2024 16:08:11 -0400 Subject: [PATCH 2/2] Fix lint issue and failing test --- .../semanticTokens/BrsFileSemanticTokensProcessor.spec.ts | 2 -- src/bscPlugin/semanticTokens/BrsFileSemanticTokensProcessor.ts | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/bscPlugin/semanticTokens/BrsFileSemanticTokensProcessor.spec.ts b/src/bscPlugin/semanticTokens/BrsFileSemanticTokensProcessor.spec.ts index 00de49114..a932a2079 100644 --- a/src/bscPlugin/semanticTokens/BrsFileSemanticTokensProcessor.spec.ts +++ b/src/bscPlugin/semanticTokens/BrsFileSemanticTokensProcessor.spec.ts @@ -452,8 +452,6 @@ describe('BrsFileSemanticTokensProcessor', () => { expectSemanticTokens(file, [ // sub |init|() [SemanticTokenTypes.function, 1, 16, 1, 20], - // |m|.alien = new Humanoids.Aliens.Alien.NOT_A_CLASS() 'bs:disable-line - [SemanticTokenTypes.variable, 2, 16, 2, 17], // m.alien = new |Humanoids|.Aliens.Alien.NOT_A_CLASS() 'bs:disable-line [SemanticTokenTypes.namespace, 2, 30, 2, 39], // m.alien = new Humanoids.|Aliens|.Alien.NOT_A_CLASS() 'bs:disable-line diff --git a/src/bscPlugin/semanticTokens/BrsFileSemanticTokensProcessor.ts b/src/bscPlugin/semanticTokens/BrsFileSemanticTokensProcessor.ts index 1b7c99b20..0dbac2630 100644 --- a/src/bscPlugin/semanticTokens/BrsFileSemanticTokensProcessor.ts +++ b/src/bscPlugin/semanticTokens/BrsFileSemanticTokensProcessor.ts @@ -1,6 +1,6 @@ import { SemanticTokenModifiers } from 'vscode-languageserver-protocol'; import { SemanticTokenTypes } from 'vscode-languageserver-protocol'; -import { isCallableType, isClassType, isComponentType, isConstStatement, isDottedGetExpression, isEnumMemberType, isEnumType, isFunctionExpression, isFunctionStatement, isInterfaceType, isNamespaceType, isTypeExpression, isVariableExpression } from '../../astUtils/reflection'; +import { isCallableType, isClassType, isComponentType, isConstStatement, isDottedGetExpression, isEnumMemberType, isEnumType, isFunctionExpression, isFunctionStatement, isInterfaceType, isNamespaceType, isVariableExpression } from '../../astUtils/reflection'; import type { BrsFile } from '../../files/BrsFile'; import type { ExtraSymbolData, OnGetSemanticTokensEvent, SemanticToken, TypeChainEntry } from '../../interfaces'; import type { Locatable, Token } from '../../lexer/Token';