Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Semantic Tokes for Native Components and Type completion in Type Expressions #927

Merged
merged 3 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/astUtils/reflection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,17 +366,25 @@ export function isNumberType(value: any): value is IntegerType | LongIntegerType
}

const primitiveTypeKinds = [
BscTypeKind.IntegerType,
BscTypeKind.LongIntegerType,
BscTypeKind.FloatType,
BscTypeKind.DoubleType,
...numberTypeKinds,
BscTypeKind.BooleanType,
BscTypeKind.StringType
];
export function isPrimitiveType(value: any): value is IntegerType | LongIntegerType | FloatType | DoubleType | StringType | BooleanType {
return primitiveTypeKinds.includes(value?.kind);
}

const nativeTypeKinds = [
...primitiveTypeKinds,
BscTypeKind.DynamicType,
BscTypeKind.ObjectType,
BscTypeKind.VoidType,
BscTypeKind.FunctionType
];
export function isNativeType(value: any): value is IntegerType | LongIntegerType | FloatType | DoubleType | StringType | BooleanType | VoidType | DynamicType | ObjectType | FunctionType {
return nativeTypeKinds.includes(value?.kind);
}


// Literal reflection

Expand Down
62 changes: 62 additions & 0 deletions src/bscPlugin/completions/CompletionsProcessor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1595,4 +1595,66 @@ describe('CompletionsProcessor', () => {
});
});

describe('type expressions', () => {
it('finds built in types', () => {
program.setFile('source/main.bs', `
sub foo(thing as )
print thing
end sub
`);
program.validate();
// sub foo(thing as | )
const completions = program.getCompletions('source/main.bs', util.createPosition(1, 34));
expectCompletionsIncludes(completions, [{
label: 'integer',
kind: CompletionItemKind.Keyword
}]);
expectCompletionsIncludes(completions, [{
label: 'roSGNode',
kind: CompletionItemKind.Interface
}]);
});

it('finds custom types', () => {
program.setFile('source/main.bs', `
sub foo(thing as )
print thing
end sub

class SomeKlass
end class
`);
program.validate();
// sub foo(thing as | )
const completions = program.getCompletions('source/main.bs', util.createPosition(1, 34));
expectCompletionsIncludes(completions, [{
label: 'SomeKlass',
kind: CompletionItemKind.Class
}]);
});

it('only shows intrinsic/native types in brightscript', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice test!

program.setFile('source/main.brs', `
sub foo(thing as )
print thing
end sub
`);
program.validate();
// sub foo(thing as | )
const completions = program.getCompletions('source/main.brs', util.createPosition(1, 34));
expectCompletionsIncludes(completions, [{
label: 'integer',
kind: CompletionItemKind.Keyword
}]);
expectCompletionsIncludes(completions, [{
label: 'function',
kind: CompletionItemKind.Keyword
}]);
expectCompletionsExcludes(completions, [{
label: 'roSGNode'
}]);
});

});

});
31 changes: 27 additions & 4 deletions src/bscPlugin/completions/CompletionsProcessor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isBrsFile, isCallableType, isClassType, isComponentType, isConstStatement, isEnumMemberType, isEnumType, isInterfaceType, isMethodStatement, isNamespaceType, isXmlFile, isXmlScope } from '../../astUtils/reflection';
import { isBrsFile, isCallableType, isClassType, isComponentType, isConstStatement, isEnumMemberType, isEnumType, isInterfaceType, isMethodStatement, isNamespaceType, isNativeType, isXmlFile, isXmlScope } from '../../astUtils/reflection';
import type { BscFile, ExtraSymbolData, FileReference, ProvideCompletionsEvent } from '../../interfaces';
import { Keywords, TokenKind } from '../../lexer/TokenKind';
import { DeclarableTypes, Keywords, TokenKind } from '../../lexer/TokenKind';
import type { XmlScope } from '../../XmlScope';
import { util } from '../../util';
import type { Scope } from '../../Scope';
Expand Down Expand Up @@ -156,6 +156,7 @@ export class CompletionsProcessor {
let expression: AstNode;
let shouldLookForMembers = false;
let shouldLookForCallFuncMembers = false;
let symbolTableLookupFlag = SymbolTypeFlag.runtime;

if (file.tokenFollows(currentToken, TokenKind.Goto)) {
let functionScope = file.getFunctionScopeAtPosition(position);
Expand All @@ -173,6 +174,13 @@ export class CompletionsProcessor {
const beforeDotToken = file.getTokenBefore(dotToken);
expression = file.getClosestExpression(beforeDotToken?.range.end);
shouldLookForCallFuncMembers = true;
} else if (file.getPreviousToken(currentToken)?.kind === TokenKind.As || file.isTokenNextToTokenKind(currentToken, TokenKind.As)) {

if (file.parseMode === ParseMode.BrightScript) {
return NativeTypeCompletions;
}
expression = file.getClosestExpression(this.event.position);
symbolTableLookupFlag = SymbolTypeFlag.typetime;
} else {
expression = file.getClosestExpression(this.event.position);
}
Expand Down Expand Up @@ -212,7 +220,7 @@ export class CompletionsProcessor {

for (const scope of this.event.scopes) {
scope.linkSymbolTable();
let currentSymbols = getSymbolTableForLookups()?.getAllSymbols(SymbolTypeFlag.runtime) ?? [];
let currentSymbols = getSymbolTableForLookups()?.getAllSymbols(symbolTableLookupFlag) ?? [];
// eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check
switch (tokenBefore.kind) {
case TokenKind.New:
Expand Down Expand Up @@ -279,6 +287,8 @@ export class CompletionsProcessor {
return CompletionItemKind.EnumMember;
} else if (isNamespaceType(type)) {
return CompletionItemKind.Module;
} else if (isNativeType(type)) {
return CompletionItemKind.Keyword;
}
return areMembers ? CompletionItemKind.Field : CompletionItemKind.Variable;
}
Expand Down Expand Up @@ -409,7 +419,6 @@ export class CompletionsProcessor {
//no other result is possible in this case
return completionsArray;
}

}

/**
Expand All @@ -426,3 +435,17 @@ export const KeywordCompletions = Object.keys(Keywords)
kind: CompletionItemKind.Keyword
} as CompletionItem;
});


/**
* List of completions for all valid intrinsic types.
* Build this list once because it won't change for the lifetime of this process
*/
export const NativeTypeCompletions = DeclarableTypes
//create completions
.map(x => {
return {
label: x.toLowerCase(),
kind: CompletionItemKind.Keyword
} as CompletionItem;
});
Original file line number Diff line number Diff line change
Expand Up @@ -421,4 +421,29 @@ describe('BrsFileSemanticTokensProcessor', () => {
}
]);
});


it('matches native interfaces', () => {
const file = program.setFile<BrsFile>('source/main.bs', `
sub init()
m.alien = new Humanoids.Aliens.Alien.NOT_A_CLASS() 'bs:disable-line
end sub

namespace Humanoids.Aliens
class Alien
end class
end namespace
`);
expectSemanticTokens(file, [{
range: util.createRange(2, 30, 2, 39),
tokenType: SemanticTokenTypes.namespace
}, {
range: util.createRange(2, 40, 2, 46),
tokenType: SemanticTokenTypes.namespace
}, {
range: util.createRange(2, 47, 2, 52),
tokenType: SemanticTokenTypes.class
}]);
});

});
40 changes: 37 additions & 3 deletions src/bscPlugin/semanticTokens/BrsFileSemanticTokensProcessor.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import type { Range } from 'vscode-languageserver-protocol';
import { SemanticTokenModifiers } from 'vscode-languageserver-protocol';
import { SemanticTokenTypes } from 'vscode-languageserver-protocol';
import { isCallExpression, isClassType, isNamespaceStatement, isNewExpression } from '../../astUtils/reflection';
import { isCallExpression, isCallableType, isClassType, isComponentType, isConstStatement, isEnumMemberType, isEnumType, isInterfaceType, isNamespaceStatement, isNamespaceType, isNativeType, isNewExpression } from '../../astUtils/reflection';
import type { BrsFile } from '../../files/BrsFile';
import type { OnGetSemanticTokensEvent } from '../../interfaces';
import type { ExtraSymbolData, OnGetSemanticTokensEvent } from '../../interfaces';
import type { Locatable } from '../../lexer/Token';
import { ParseMode } from '../../parser/Parser';
import type { NamespaceStatement } from '../../parser/Statement';
import util from '../../util';
import { SymbolTypeFlag } from '../../SymbolTable';
import type { BscType } from '../../types/BscType';

export class BrsFileSemanticTokensProcessor {
public constructor(
Expand Down Expand Up @@ -92,7 +93,7 @@ export class BrsFileSemanticTokensProcessor {
if (!scope) {
return;
}

scope.linkSymbolTable();
const nodes = [
...this.event.file.parser.references.expressions,
//make a new VariableExpression to wrap the name. This is a hack, we could probably do it better
Expand Down Expand Up @@ -120,14 +121,47 @@ export class BrsFileSemanticTokensProcessor {
this.addToken(token, SemanticTokenTypes.enum);
} else if (scope.getClass(entityName, containingNamespaceNameLower)) {
this.addToken(token, SemanticTokenTypes.class);
} else if (scope.getInterface(entityName, containingNamespaceNameLower)) {
this.addToken(token, SemanticTokenTypes.interface);
} else if (scope.getCallableByName(entityName)) {
this.addToken(token, SemanticTokenTypes.function);
} else if (scope.getNamespace(entityName, containingNamespaceNameLower)) {
this.addToken(token, SemanticTokenTypes.namespace);
} else if (scope.getConstFileLink(entityName, containingNamespaceNameLower)) {
this.addToken(token, SemanticTokenTypes.variable, [SemanticTokenModifiers.readonly, SemanticTokenModifiers.static]);
} else {
const extraData = {};
const symbolType = scope.symbolTable.getSymbolType(token.text, { flags: SymbolTypeFlag.typetime, data: extraData });
if (symbolType?.isResolvable()) {
this.addToken(token, this.getSemanticTokenTypeFromType(symbolType, extraData, !!containingNamespaceNameLower));
}
}
}
}
scope.unlinkSymbolTable();
}

// TODO: We can use the actual symbol tables to find methods and member fields.
private getSemanticTokenTypeFromType(type: BscType, extraData: ExtraSymbolData, areMembers = false) {
if (isConstStatement(extraData?.definingNode)) {
return SemanticTokenTypes.variable;
} else if (isClassType(type)) {
return SemanticTokenTypes.class;
} else if (isCallableType(type)) {
return areMembers ? SemanticTokenTypes.method : SemanticTokenTypes.function;
} else if (isInterfaceType(type)) {
return SemanticTokenTypes.interface;
} else if (isComponentType(type)) {
return SemanticTokenTypes.class;
} else if (isEnumType(type)) {
return SemanticTokenTypes.enum;
} else if (isEnumMemberType(type)) {
return SemanticTokenTypes.enumMember;
} else if (isNamespaceType(type)) {
return SemanticTokenTypes.namespace;
} else if (isNativeType(type)) {
return SemanticTokenTypes.type;
}
return areMembers ? SemanticTokenTypes.property : SemanticTokenTypes.variable;
}
}
1 change: 0 additions & 1 deletion src/lexer/TokenKind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,6 @@ export const DeclarableTypes = [
TokenKind.Double,
TokenKind.String,
TokenKind.Object,
TokenKind.Interface,
TokenKind.Dynamic,
TokenKind.Void,
TokenKind.Function
Expand Down
10 changes: 10 additions & 0 deletions src/parser/Parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,16 @@ describe('parser', () => {
expect(stmt.expression.obj.name.text).to.equal('a');
expect(stmt.expression.name.text).to.equal('b');
});

it('adds function statement with missing type after as', () => {
let parser = parse(`
sub foo(thing as )
print thing
end sub
`, ParseMode.BrighterScript);
expect(parser.diagnostics[0]?.message).to.exist;
expect(parser.ast.statements[0]).to.be.instanceof(FunctionStatement);
});
});

describe('comments', () => {
Expand Down
5 changes: 5 additions & 0 deletions src/parser/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,11 @@ export class Parser {
});
//consume the statement separator
this.consumeStatementSeparators();
} else if (this.peek().kind !== TokenKind.Identifier && !this.checkAny(...DeclarableTypes, ...AllowedTypeIdentifiers)) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only try to find a type if the next token can be a type identifier -- basically, don't throw an exception if there isn't the correct kind of token next.
This is important so that we can do completions inside this expression

this.diagnostics.push({
...DiagnosticMessages.expectedIdentifierAfterKeyword(asToken.text),
range: asToken.range
});
} else {
typeExpression = this.typeExpression();
}
Expand Down