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