Cache builtin commands and get all global commands for pwsh

This should make things quite a bit faster for expensive fetching of
commands. This also caches empty builtin commands so it's not attempted
every time.

Fixes #236097
Part of #235024
pull/236570/head
Daniel Imms 2024-12-19 02:50:00 -08:00
parent 7efdaa5e8e
commit cbfd8ab513
No known key found for this signature in database
GPG Key ID: E5CF412B63651C69
1 changed files with 7 additions and 12 deletions

View File

@ -12,14 +12,14 @@ import codeCompletionSpec from './completions/code';
import cdSpec from './completions/cd';
let cachedAvailableCommands: Set<string> | undefined;
let cachedBuiltinCommands: Map<string, string[]> | undefined;
const cachedBuiltinCommands: Map<string, string[] | undefined> = new Map();
export const availableSpecs = [codeCompletionSpec, codeInsidersCompletionSpec, cdSpec];
function getBuiltinCommands(shell: string): string[] | undefined {
try {
const shellType = path.basename(shell, path.extname(shell));
const cachedCommands = cachedBuiltinCommands?.get(shellType);
const cachedCommands = cachedBuiltinCommands.get(shellType);
if (cachedCommands) {
return cachedCommands;
}
@ -38,14 +38,14 @@ function getBuiltinCommands(shell: string): string[] | undefined {
break;
}
case 'fish': {
// TODO: ghost text in the command line prevents
// completions from working ATM for fish
// TODO: Ghost text in the command line prevents completions from working ATM for fish
const fishOutput = execSync('functions -n', options);
commands = fishOutput.split(', ').filter(filter);
break;
}
case 'pwsh': {
const output = execSync('Get-Command | Select-Object Name, CommandType, DisplayName | ConvertTo-Json', options);
// TODO: Select `CommandType, DisplayName` and map to a rich type with kind and detail
const output = execSync('Get-Command -All | Select-Object Name | ConvertTo-Json', options);
let json: any;
try {
json = JSON.parse(output);
@ -53,17 +53,12 @@ function getBuiltinCommands(shell: string): string[] | undefined {
console.error('Error parsing pwsh output:', e);
return [];
}
// TODO: Return a rich type with kind and detail
commands = (json as any[]).map(e => e.Name);
break;
}
}
// TODO: Cache failure results too
if (commands?.length) {
cachedBuiltinCommands?.set(shellType, commands);
return commands;
}
return;
cachedBuiltinCommands.set(shellType, commands);
return commands;
} catch (error) {
console.error('Error fetching builtin commands:', error);