Skip to content

Commit 9156eb1

Browse files
authored
Fixes CompletionItemKinds for typed variables (#1145)
* Fixes CompletionItemKinds for typed variables * removed comment * No replaceAll() on github test runner
1 parent 1b7000b commit 9156eb1

File tree

3 files changed

+97
-7
lines changed

3 files changed

+97
-7
lines changed

src/bscPlugin/completions/CompletionsProcessor.spec.ts

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1898,6 +1898,86 @@ describe('CompletionsProcessor', () => {
18981898

18991899
});
19001900

1901+
describe('typed variables', () => {
1902+
it('shows variables of type interface as CompletionItemKind.Variable', () => {
1903+
program.setFile('source/main.bs', `
1904+
sub foo(thing as SomeInterface)
1905+
print thi|
1906+
end sub
1907+
1908+
interface SomeInterface
1909+
optional name as string
1910+
optional data
1911+
optional function doStuff()
1912+
end interface
1913+
`);
1914+
program.validate();
1915+
// print thi|
1916+
const completions = program.getCompletions('source/main.bs', util.createPosition(2, 33));
1917+
expectCompletionsIncludes(completions, [{
1918+
label: 'thing',
1919+
kind: CompletionItemKind.Variable
1920+
}]);
1921+
});
1922+
1923+
it('shows variables of type class as CompletionItemKind.Variable', () => {
1924+
program.setFile('source/main.bs', `
1925+
sub foo(thing as SomeKlass)
1926+
print thi|
1927+
end sub
1928+
1929+
class SomeKlass
1930+
name as string
1931+
data
1932+
function doStuff()
1933+
end function
1934+
end class
1935+
`);
1936+
program.validate();
1937+
// print thi|
1938+
const completions = program.getCompletions('source/main.bs', util.createPosition(2, 33));
1939+
expectCompletionsIncludes(completions, [{
1940+
label: 'thing',
1941+
kind: CompletionItemKind.Variable
1942+
}]);
1943+
});
1944+
1945+
it('shows variables of type enum as CompletionItemKind.Variable', () => {
1946+
program.setFile('source/main.bs', `
1947+
sub foo(thing as SomeEnum)
1948+
print thi|
1949+
end sub
1950+
1951+
enum SomeEnum
1952+
up
1953+
down
1954+
end end
1955+
`);
1956+
program.validate();
1957+
// print thi|
1958+
const completions = program.getCompletions('source/main.bs', util.createPosition(2, 33));
1959+
expectCompletionsIncludes(completions, [{
1960+
label: 'thing',
1961+
kind: CompletionItemKind.Variable
1962+
}]);
1963+
});
1964+
1965+
it('shows variables of type function as CompletionItemKind.Variable', () => {
1966+
program.setFile('source/main.bs', `
1967+
sub foo(thing as function)
1968+
print thi|
1969+
end sub
1970+
`);
1971+
program.validate();
1972+
// print thi|
1973+
const completions = program.getCompletions('source/main.bs', util.createPosition(2, 33));
1974+
expectCompletionsIncludes(completions, [{
1975+
label: 'thing',
1976+
kind: CompletionItemKind.Variable
1977+
}]);
1978+
});
1979+
1980+
});
19011981

19021982
describe('brighterscript vs brightscript', () => {
19031983
it('should not include transpiled versions of symbols in brighterscript code', () => {

src/bscPlugin/completions/CompletionsProcessor.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isBlock, isBrsFile, isCallableType, isClassStatement, isClassType, isComponentType, isConstStatement, isEnumMemberType, isEnumType, isFunctionExpression, isInterfaceType, isMethodStatement, isNamespaceStatement, isNamespaceType, isNativeType, isXmlFile, isXmlScope } from '../../astUtils/reflection';
1+
import { isBlock, isBrsFile, isCallableType, isClassStatement, isClassType, isComponentType, isConstStatement, isEnumMemberType, isEnumType, isFunctionExpression, isInterfaceType, isMethodStatement, isNamespaceStatement, isNamespaceType, isNativeType, isTypedFunctionType, isXmlFile, isXmlScope } from '../../astUtils/reflection';
22
import type { FileReference, ProvideCompletionsEvent } from '../../interfaces';
33
import type { BscFile } from '../../files/BscFile';
44
import { AllowedTriviaTokens, DeclarableTypes, Keywords, TokenKind } from '../../lexer/TokenKind';
@@ -371,21 +371,31 @@ export class CompletionsProcessor {
371371
private getCompletionKindFromSymbol(symbol: BscSymbol, areMembers = false) {
372372
const type = symbol?.type;
373373
const extraData = symbol?.data;
374+
const finalTypeNameLower = type?.toString().split('.').pop().toLowerCase();
375+
const symbolNameLower = symbol?.name.toLowerCase();
376+
let nameMatchesType = symbolNameLower === finalTypeNameLower;
374377
if (isConstStatement(extraData?.definingNode)) {
375378
return CompletionItemKind.Constant;
376-
} else if (isClassType(type)) {
379+
} else if (isClassType(type) && nameMatchesType) {
377380
return CompletionItemKind.Class;
378381
} else if (isCallableType(type)) {
379-
return areMembers ? CompletionItemKind.Method : CompletionItemKind.Function;
380-
} else if (isInterfaceType(type)) {
382+
if (isTypedFunctionType(type) && !nameMatchesType) {
383+
if (symbolNameLower === type.name.replace(/\./gi, '_').toLowerCase()) {
384+
nameMatchesType = true;
385+
}
386+
}
387+
if (nameMatchesType) {
388+
return areMembers ? CompletionItemKind.Method : CompletionItemKind.Function;
389+
}
390+
} else if (isInterfaceType(type) && nameMatchesType) {
381391
return CompletionItemKind.Interface;
382-
} else if (isEnumType(type)) {
392+
} else if (isEnumType(type) && nameMatchesType) {
383393
return CompletionItemKind.Enum;
384394
} else if (isEnumMemberType(type)) {
385395
return CompletionItemKind.EnumMember;
386396
} else if (isNamespaceType(type)) {
387397
return CompletionItemKind.Module;
388-
} else if (isComponentType(type)) {
398+
} else if (isComponentType(type) && (nameMatchesType || symbolNameLower === 'rosgnode')) {
389399
return CompletionItemKind.Interface;
390400
}
391401
if (areMembers) {

src/parser/Statement.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ export class FunctionStatement extends Statement implements TypedefProvider {
434434

435435
getType(options: GetTypeOptions) {
436436
const funcExprType = this.func.getType(options);
437-
funcExprType.setName(this.tokens.name?.text);
437+
funcExprType.setName(this.getName(ParseMode.BrighterScript));
438438
return funcExprType;
439439
}
440440
}

0 commit comments

Comments
 (0)