Skip to content

Commit

Permalink
add a setting to determine which terminal suggest providers are activ…
Browse files Browse the repository at this point in the history
…ated (#237826)
  • Loading branch information
meganrogge authored Jan 13, 2025
1 parent 2a91911 commit c594d55
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class PwshCompletionProviderAddon extends Disposable implements ITerminal
id: string = PwshCompletionProviderAddon.ID;
triggerCharacters?: string[] | undefined;
isBuiltin?: boolean = true;
static readonly ID = 'terminal.pwshCompletionProvider';
static readonly ID = 'pwsh-shell-integration';
static cachedPwshCommands: Set<ITerminalCompletion>;
readonly shellTypes = [GeneralShellType.PowerShell];
private _codeCompletionsRequested: boolean = false;
Expand Down Expand Up @@ -348,7 +348,7 @@ function rawCompletionToITerminalCompletion(rawCompletion: PwshCompletion, repla

return {
label,
provider: 'pwsh-script',
provider: PwshCompletionProviderAddon.ID,
icon,
detail,
isFile: rawCompletion.ResultType === 3,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class TerminalSuggestContribution extends DisposableStore implements ITerminalCo
this._ctx.instance.focus();
this._ctx.instance.sendText(text, false);
}));
this.add(this._terminalCompletionService.registerTerminalCompletionProvider('builtinPwsh', 'pwsh', pwshCompletionProviderAddon));
this.add(this._terminalCompletionService.registerTerminalCompletionProvider('builtinPwsh', pwshCompletionProviderAddon.id, pwshCompletionProviderAddon));
// If completions are requested, pause and queue input events until completions are
// received. This fixing some problems in PowerShell, particularly enter not executing
// when typing quickly and some characters being printed twice. On Windows this isn't
Expand Down Expand Up @@ -178,6 +178,11 @@ class TerminalSuggestContribution extends DisposableStore implements ITerminalCo
return;
}
addon.shellType = this._ctx.instance.shellType;
if (!this._ctx.instance.xterm?.raw) {
return;
}
// Relies on shell type being set
this._loadPwshCompletionAddon(this._ctx.instance.xterm.raw);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { IFileService } from '../../../../../platform/files/common/files.js';
import { createDecorator } from '../../../../../platform/instantiation/common/instantiation.js';
import { TerminalShellType } from '../../../../../platform/terminal/common/terminal.js';
import { ISimpleCompletion } from '../../../../services/suggest/browser/simpleCompletionItem.js';
import { ITerminalSuggestConfiguration, terminalSuggestConfigSection } from '../common/terminalSuggestConfiguration.js';
import { ITerminalSuggestConfiguration, terminalSuggestConfigSection, TerminalSuggestSettingId } from '../common/terminalSuggestConfiguration.js';

export const ITerminalCompletionService = createDecorator<ITerminalCompletionService>('terminalCompletionService');

Expand Down Expand Up @@ -149,6 +149,17 @@ export class TerminalCompletionService extends Disposable implements ITerminalCo

if (!extensionCompletionsEnabled || skipExtensionCompletions) {
providers = providers.filter(p => p.isBuiltin);
return this._collectCompletions(providers, shellType, promptValue, cursorPosition, token);
}

const providerConfig: { [key: string]: boolean } = this._configurationService.getValue(TerminalSuggestSettingId.Providers);
providers = providers.filter(p => {
const providerId = p.id;
return providerId && providerId in providerConfig && providerConfig[providerId];
});

if (!providers.length) {
return;
}

return this._collectCompletions(providers, shellType, promptValue, cursorPosition, token);
Expand All @@ -164,7 +175,12 @@ export class TerminalCompletionService extends Disposable implements ITerminalCo
return undefined;
}
const completionItems = Array.isArray(completions) ? completions : completions.items ?? [];

if (provider.isBuiltin) {
//TODO: why is this needed?
for (const item of completionItems) {
item.provider = provider.id;
}
}
if (Array.isArray(completions)) {
return completionItems;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const enum TerminalSuggestSettingId {
RunOnEnter = 'terminal.integrated.suggest.runOnEnter',
BuiltinCompletions = 'terminal.integrated.suggest.builtinCompletions',
EnableExtensionCompletions = 'terminal.integrated.suggest.enableExtensionCompletions',
Providers = 'terminal.integrated.suggest.providers',
}

export const terminalSuggestConfigSection = 'terminal.integrated.suggest';
Expand All @@ -28,6 +29,10 @@ export interface ITerminalSuggestConfiguration {
'pwshCode': boolean;
'pwshGit': boolean;
};
providers: {
'terminal-suggest': boolean;
'pwsh-shell-integration': boolean;
};
enableExtensionCompletions: boolean;
}

Expand All @@ -39,6 +44,17 @@ export const terminalSuggestConfiguration: IStringDictionary<IConfigurationPrope
default: false,
tags: ['experimental'],
},
[TerminalSuggestSettingId.Providers]: {
restricted: true,
markdownDescription: localize('suggest.providers', "Controls which providers are enabled for terminal suggestions. Also be aware of the {0}-setting which controls if extensions are able to provide suggestions.", `\`#${TerminalSuggestSettingId.EnableExtensionCompletions}#\``),
type: 'object',
properties: {},
default: {
'terminal-suggest': true,
'pwsh-shell-integration': false,
},
tags: ['experimental'],
},
[TerminalSuggestSettingId.QuickSuggestions]: {
restricted: true,
markdownDescription: localize('suggest.quickSuggestions', "Controls whether suggestions should automatically show up while typing. Also be aware of the {0}-setting which controls if suggestions are triggered by special characters.", `\`#${TerminalSuggestSettingId.SuggestOnTriggerCharacters}#\``),
Expand Down Expand Up @@ -85,7 +101,7 @@ export const terminalSuggestConfiguration: IStringDictionary<IConfigurationPrope
},
[TerminalSuggestSettingId.EnableExtensionCompletions]: {
restricted: true,
markdownDescription: localize('suggest.enableExtensionCompletions', "Controls whether extension completions are enabled."),
markdownDescription: localize('suggest.enableExtensionCompletions', "Controls whether extension completions are enabled. Also be aware of the {0}-setting which controls which providers are enabled.", `\`#${TerminalSuggestSettingId.Providers}#\``),
type: 'boolean',
default: false,
tags: ['experimental'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ suite('Terminal Contrib Suggest Recordings', () => {
pwshCode: true,
pwshGit: true
},
providers: {
'terminal-suggest': true,
'pwsh-shell-integration': true,
},
enableExtensionCompletions: false
} satisfies ITerminalSuggestConfiguration
}
Expand Down

0 comments on commit c594d55

Please sign in to comment.