Skip to content

Commit

Permalink
Merge pull request #236570 from microsoft/tyriar/236097
Browse files Browse the repository at this point in the history
Cache builtin commands and get all global commands for pwsh
  • Loading branch information
Tyriar authored Dec 19, 2024
2 parents 25b88b7 + cbfd8ab commit 8467007
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions extensions/terminal-suggest/src/terminalSuggestMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -38,32 +38,27 @@ 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);
} catch (e) {
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);
Expand Down

0 comments on commit 8467007

Please sign in to comment.