Skip to content

Commit

Permalink
Cache getCallableByName (#739)
Browse files Browse the repository at this point in the history
  • Loading branch information
TwitchBronBron committed Nov 3, 2022
1 parent 42a9c8c commit e59b470
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
28 changes: 19 additions & 9 deletions src/Scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { CompletionItemKind } from 'vscode-languageserver';
import chalk from 'chalk';
import type { DiagnosticInfo } from './DiagnosticMessages';
import { DiagnosticMessages } from './DiagnosticMessages';
import type { CallableContainer, BsDiagnostic, FileReference, BscFile, CallableContainerMap, FileLink } from './interfaces';
import type { CallableContainer, BsDiagnostic, FileReference, BscFile, CallableContainerMap, FileLink, Callable } from './interfaces';
import type { Program } from './Program';
import { BsClassValidator } from './validators/ClassValidator';
import type { NamespaceStatement, FunctionStatement, ClassStatement, EnumStatement, InterfaceStatement, EnumMemberStatement, ConstStatement } from './parser/Statement';
Expand Down Expand Up @@ -488,15 +488,25 @@ export class Scope {
* If there are overridden callables with the same name, the closest callable to this scope is returned
*/
public getCallableByName(name: string) {
let lowerName = name.toLowerCase();
let callables = this.getAllCallables();
for (let callable of callables) {
const callableName = callable.callable.getName(ParseMode.BrighterScript);
// Split by `.` and check the last term to consider namespaces.
if (callableName.toLowerCase() === lowerName || callableName.split('.').pop()?.toLowerCase() === lowerName) {
return callable.callable;
return this.getCallableMap().get(
name.toLowerCase()
);
}

public getCallableMap() {
return this.cache.getOrAdd('callableMap', () => {
const result = new Map<string, Callable>();
for (let callable of this.getAllCallables()) {
const callableName = callable.callable.getName(ParseMode.BrighterScript)?.toLowerCase();
result.set(callableName, callable.callable);
result.set(
// Split by `.` and check the last term to consider namespaces.
callableName.split('.').pop()?.toLowerCase(),
callable.callable
);
}
}
return result;
});
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/files/BrsFile.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2011,14 +2011,14 @@ describe('BrsFile', () => {
' The main function
'
sub main()
log("hello")
writeToLog("hello")
end sub
'
' Prints a message to the log.
' Works with *markdown* **content**
'
sub log(message as string)
sub writeToLog(message as string)
print message
end sub
`);
Expand All @@ -2028,7 +2028,7 @@ describe('BrsFile', () => {
program.getHover(file.srcPath, Position.create(5, 22))[0].contents
).to.equal([
'```brightscript',
'sub log(message as string) as void',
'sub writeToLog(message as string) as void',
'```',
'***',
'',
Expand Down

0 comments on commit e59b470

Please sign in to comment.