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

Fixes CompletionItemKinds for typed variables #1145

Merged
merged 3 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions src/bscPlugin/completions/CompletionsProcessor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1898,6 +1898,86 @@ describe('CompletionsProcessor', () => {

});

describe('typed variables', () => {
it('shows variables of type interface as CompletionItemKind.Variable', () => {
program.setFile('source/main.bs', `
sub foo(thing as SomeInterface)
print thi|
end sub

interface SomeInterface
optional name as string
optional data
optional function doStuff()
end interface
`);
program.validate();
// print thi|
const completions = program.getCompletions('source/main.bs', util.createPosition(2, 33));
expectCompletionsIncludes(completions, [{
label: 'thing',
kind: CompletionItemKind.Variable
}]);
});

it('shows variables of type class as CompletionItemKind.Variable', () => {
program.setFile('source/main.bs', `
sub foo(thing as SomeKlass)
print thi|
end sub

class SomeKlass
name as string
data
function doStuff()
end function
end class
`);
program.validate();
// print thi|
const completions = program.getCompletions('source/main.bs', util.createPosition(2, 33));
expectCompletionsIncludes(completions, [{
label: 'thing',
kind: CompletionItemKind.Variable
}]);
});

it('shows variables of type enum as CompletionItemKind.Variable', () => {
program.setFile('source/main.bs', `
sub foo(thing as SomeEnum)
print thi|
end sub

enum SomeEnum
up
down
end end
`);
program.validate();
// print thi|
const completions = program.getCompletions('source/main.bs', util.createPosition(2, 33));
expectCompletionsIncludes(completions, [{
label: 'thing',
kind: CompletionItemKind.Variable
}]);
});

it('shows variables of type function as CompletionItemKind.Variable', () => {
program.setFile('source/main.bs', `
sub foo(thing as function)
print thi|
end sub
`);
program.validate();
// print thi|
const completions = program.getCompletions('source/main.bs', util.createPosition(2, 33));
expectCompletionsIncludes(completions, [{
label: 'thing',
kind: CompletionItemKind.Variable
}]);
});

});

describe('brighterscript vs brightscript', () => {
it('should not include transpiled versions of symbols in brighterscript code', () => {
Expand Down
23 changes: 17 additions & 6 deletions src/bscPlugin/completions/CompletionsProcessor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isBlock, isBrsFile, isCallableType, isClassStatement, isClassType, isComponentType, isConstStatement, isEnumMemberType, isEnumType, isFunctionExpression, isInterfaceType, isMethodStatement, isNamespaceStatement, isNamespaceType, isNativeType, isXmlFile, isXmlScope } from '../../astUtils/reflection';
import { isBlock, isBrsFile, isCallableType, isClassStatement, isClassType, isComponentType, isConstStatement, isEnumMemberType, isEnumType, isFunctionExpression, isInterfaceType, isMethodStatement, isNamespaceStatement, isNamespaceType, isNativeType, isTypedFunctionType, isXmlFile, isXmlScope } from '../../astUtils/reflection';
import type { FileReference, ProvideCompletionsEvent } from '../../interfaces';
import type { BscFile } from '../../files/BscFile';
import { AllowedTriviaTokens, DeclarableTypes, Keywords, TokenKind } from '../../lexer/TokenKind';
Expand Down Expand Up @@ -371,21 +371,32 @@ export class CompletionsProcessor {
private getCompletionKindFromSymbol(symbol: BscSymbol, areMembers = false) {
const type = symbol?.type;
const extraData = symbol?.data;
// eslint-disable-next-line no-bitwise
const finalTypeNameLower = type?.toString().split('.').pop().toLowerCase();
Copy link
Member

Choose a reason for hiding this comment

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

Where's the bitwise?

const symbolNameLower = symbol?.name.toLowerCase();
let nameMatchesType = symbolNameLower === finalTypeNameLower;
if (isConstStatement(extraData?.definingNode)) {
return CompletionItemKind.Constant;
} else if (isClassType(type)) {
} else if (isClassType(type) && nameMatchesType) {
return CompletionItemKind.Class;
} else if (isCallableType(type)) {
return areMembers ? CompletionItemKind.Method : CompletionItemKind.Function;
} else if (isInterfaceType(type)) {
if (isTypedFunctionType(type) && !nameMatchesType) {
if (symbolNameLower === type.name.replaceAll('.', '_').toLowerCase()) {
nameMatchesType = true;
}
}
if (nameMatchesType) {
return areMembers ? CompletionItemKind.Method : CompletionItemKind.Function;
}
} else if (isInterfaceType(type) && nameMatchesType) {
return CompletionItemKind.Interface;
} else if (isEnumType(type)) {
} else if (isEnumType(type) && nameMatchesType) {
return CompletionItemKind.Enum;
} else if (isEnumMemberType(type)) {
return CompletionItemKind.EnumMember;
} else if (isNamespaceType(type)) {
return CompletionItemKind.Module;
} else if (isComponentType(type)) {
} else if (isComponentType(type) && (nameMatchesType || symbolNameLower === 'rosgnode')) {
return CompletionItemKind.Interface;
}
if (areMembers) {
Expand Down
2 changes: 1 addition & 1 deletion src/parser/Statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ export class FunctionStatement extends Statement implements TypedefProvider {

getType(options: GetTypeOptions) {
const funcExprType = this.func.getType(options);
funcExprType.setName(this.tokens.name?.text);
funcExprType.setName(this.getName(ParseMode.BrighterScript));
return funcExprType;
}
}
Expand Down
Loading