Skip to content

Commit e59b470

Browse files
Cache getCallableByName (#739)
1 parent 42a9c8c commit e59b470

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

src/Scope.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { CompletionItemKind } from 'vscode-languageserver';
44
import chalk from 'chalk';
55
import type { DiagnosticInfo } from './DiagnosticMessages';
66
import { DiagnosticMessages } from './DiagnosticMessages';
7-
import type { CallableContainer, BsDiagnostic, FileReference, BscFile, CallableContainerMap, FileLink } from './interfaces';
7+
import type { CallableContainer, BsDiagnostic, FileReference, BscFile, CallableContainerMap, FileLink, Callable } from './interfaces';
88
import type { Program } from './Program';
99
import { BsClassValidator } from './validators/ClassValidator';
1010
import type { NamespaceStatement, FunctionStatement, ClassStatement, EnumStatement, InterfaceStatement, EnumMemberStatement, ConstStatement } from './parser/Statement';
@@ -488,15 +488,25 @@ export class Scope {
488488
* If there are overridden callables with the same name, the closest callable to this scope is returned
489489
*/
490490
public getCallableByName(name: string) {
491-
let lowerName = name.toLowerCase();
492-
let callables = this.getAllCallables();
493-
for (let callable of callables) {
494-
const callableName = callable.callable.getName(ParseMode.BrighterScript);
495-
// Split by `.` and check the last term to consider namespaces.
496-
if (callableName.toLowerCase() === lowerName || callableName.split('.').pop()?.toLowerCase() === lowerName) {
497-
return callable.callable;
491+
return this.getCallableMap().get(
492+
name.toLowerCase()
493+
);
494+
}
495+
496+
public getCallableMap() {
497+
return this.cache.getOrAdd('callableMap', () => {
498+
const result = new Map<string, Callable>();
499+
for (let callable of this.getAllCallables()) {
500+
const callableName = callable.callable.getName(ParseMode.BrighterScript)?.toLowerCase();
501+
result.set(callableName, callable.callable);
502+
result.set(
503+
// Split by `.` and check the last term to consider namespaces.
504+
callableName.split('.').pop()?.toLowerCase(),
505+
callable.callable
506+
);
498507
}
499-
}
508+
return result;
509+
});
500510
}
501511

502512
/**

src/files/BrsFile.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2011,14 +2011,14 @@ describe('BrsFile', () => {
20112011
' The main function
20122012
'
20132013
sub main()
2014-
log("hello")
2014+
writeToLog("hello")
20152015
end sub
20162016
20172017
'
20182018
' Prints a message to the log.
20192019
' Works with *markdown* **content**
20202020
'
2021-
sub log(message as string)
2021+
sub writeToLog(message as string)
20222022
print message
20232023
end sub
20242024
`);
@@ -2028,7 +2028,7 @@ describe('BrsFile', () => {
20282028
program.getHover(file.srcPath, Position.create(5, 22))[0].contents
20292029
).to.equal([
20302030
'```brightscript',
2031-
'sub log(message as string) as void',
2031+
'sub writeToLog(message as string) as void',
20322032
'```',
20332033
'***',
20342034
'',

0 commit comments

Comments
 (0)