Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prompt Native REPL in Terminal #24477

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/client/common/utils/localize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ export namespace AttachProcess {

export namespace Repl {
export const disableSmartSend = l10n.t('Disable Smart Send');
// TODO: get feedback on text message below:
export const terminalSuggestNativeReplPrompt = l10n.t(
'The Python extension now includes an editor based native Python REPL with Intellisense, syntax highlighting. Would you like to try this out?',
);
}
export namespace Pylance {
export const remindMeLater = l10n.t('Remind me later');
Expand Down
2 changes: 1 addition & 1 deletion src/client/extensionActivation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export function activateFeatures(ext: ExtensionState, _components: Components):
);
const executionHelper = ext.legacyIOC.serviceContainer.get<ICodeExecutionHelper>(ICodeExecutionHelper);
const commandManager = ext.legacyIOC.serviceContainer.get<ICommandManager>(ICommandManager);
registerTriggerForTerminalREPL(ext.disposables);
registerTriggerForTerminalREPL(commandManager, ext.disposables);
registerStartNativeReplCommand(ext.disposables, interpreterService);
registerReplCommands(ext.disposables, interpreterService, executionHelper, commandManager);
registerReplExecuteOnEnter(ext.disposables, interpreterService, commandManager);
Expand Down
41 changes: 38 additions & 3 deletions src/client/terminals/codeExecution/terminalCodeExecution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@

import { inject, injectable } from 'inversify';
import * as path from 'path';
import { Disposable, Uri } from 'vscode';
import { Disposable, TerminalShellExecutionStartEvent, Uri } from 'vscode';
import { IApplicationShell, ICommandManager, IWorkspaceService } from '../../common/application/types';
import '../../common/extensions';
import { IPlatformService } from '../../common/platform/types';
import { ITerminalService, ITerminalServiceFactory } from '../../common/terminal/types';
import { IConfigurationService, IDisposable, IDisposableRegistry, Resource } from '../../common/types';
import { Diagnostics, Repl } from '../../common/utils/localize';
import { showWarningMessage } from '../../common/vscodeApis/windowApis';
import { Common, Diagnostics, Repl } from '../../common/utils/localize';
import { onDidStartTerminalShellExecution, showWarningMessage } from '../../common/vscodeApis/windowApis';
import { IInterpreterService } from '../../interpreter/contracts';
import { traceInfo } from '../../logging';
import { buildPythonExecInfo, PythonExecInfo } from '../../pythonEnvironments/exec';
import { ICodeExecutionService } from '../../terminals/types';
import { EventName } from '../../telemetry/constants';
import { sendTelemetryEvent } from '../../telemetry';
import { getActiveInterpreter } from '../../repl/replUtils';
import { getNativeRepl } from '../../repl/nativeRepl';
import { checkREPLCommand } from './terminalReplWatcher';

@injectable()
export class TerminalCodeExecutionProvider implements ICodeExecutionService {
Expand Down Expand Up @@ -63,11 +66,43 @@ export class TerminalCodeExecutionProvider implements ICodeExecutionService {
}
}

public suggestNativeRepl(resource: Resource) {
this.disposables.push(
onDidStartTerminalShellExecution(async (e: TerminalShellExecutionStartEvent) => {
if (e.execution.commandLine.isTrusted && checkREPLCommand(e.execution.commandLine.value)) {
sendTelemetryEvent(EventName.REPL, undefined, { replType: 'manualTerminal' });
const selection = await showWarningMessage(
Repl.terminalSuggestNativeReplPrompt,
Common.doNotShowAgain,
);
if (selection === Repl.terminalSuggestNativeReplPrompt) {
sendTelemetryEvent(EventName.REPL, undefined, { replType: 'Native' });
const interpreter = await getActiveInterpreter(resource as Uri, this.interpreterService);
if (interpreter) {
const nativeRepl = await getNativeRepl(interpreter, this.disposables);
await nativeRepl.sendToNativeRepl(undefined, false);
}
}
}
}),
);
}

public async initializeRepl(resource: Resource) {
const terminalService = this.getTerminalService(resource);
if (!this.replActive) {
this.suggestNativeRepl(resource);
}
if (this.replActive && (await this.replActive)) {
await terminalService.show();
return;
} else {
// Suggest launch of Native REPL
const interpreter = await getActiveInterpreter(resource!, this.interpreterService);
if (interpreter) {
const nativeRepl = await getNativeRepl(interpreter, this.disposables);
await nativeRepl.sendToNativeRepl(undefined, false);
}
}
sendTelemetryEvent(EventName.REPL, undefined, { replType: 'Terminal' });
this.replActive = new Promise<boolean>(async (resolve) => {
Expand Down
16 changes: 14 additions & 2 deletions src/client/terminals/codeExecution/terminalReplWatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,29 @@ import { Disposable, TerminalShellExecutionStartEvent } from 'vscode';
import { onDidStartTerminalShellExecution } from '../../common/vscodeApis/windowApis';
import { sendTelemetryEvent } from '../../telemetry';
import { EventName } from '../../telemetry/constants';
import { ICommandManager } from '../../common/application/types';
import { Commands } from '../../common/constants';

function checkREPLCommand(command: string): boolean {
export function checkREPLCommand(command: string): boolean {
const lower = command.toLowerCase().trimStart();
return lower.startsWith('python') || lower.startsWith('py ');
}

export function registerTriggerForTerminalREPL(disposables: Disposable[]): void {
export async function registerTriggerForTerminalREPL(
commandManager: ICommandManager,
disposables: Disposable[],
): Promise<void> {
disposables.push(
onDidStartTerminalShellExecution(async (e: TerminalShellExecutionStartEvent) => {
if (e.execution.commandLine.isTrusted && checkREPLCommand(e.execution.commandLine.value)) {
sendTelemetryEvent(EventName.REPL, undefined, { replType: 'manualTerminal' });
// TODO: Prompt user to start Native REPL

// If yes, then launch native REPL
await commandManager.executeCommand(Commands.Start_Native_REPL, undefined);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question is, do I really need the URI and pass it in? It seems like its used for await getActiveInterpreter(resource!, this.interpreterService); but works fine in extension host.
Perhaps this is because interpreter has already been pre-selected.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use the executeCommand API from src\client\common\vscodeApis\commandApis.ts

I was speaking to Daniel. Could we just put the use native REPL in the Shell welcome message? The REPL part can be turned into a link that launches the REPL. We can start with a not so in your face message.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This API, in terminal shell options:
image

Copy link
Author

@anthonykim1 anthonykim1 Nov 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is interesting, challenge would be supporting the opening of a link to open the Native REPL though.
I made it so it prompt only shows once per non-reloaded session of VS Code and also pressing do not show will never show the suggestion again across VS Code instances (even after reloading).

Maybe we can start with the message and later add the link as well? I think the more visibility we can offer, the better it will be. /cc @cwebster-99


// TODO: Decide whether we want everytime, or once per workspace, or once per terminal
// How do I even track all terminal instances.
}
}),
);
Expand Down
Loading