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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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.context, 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
42 changes: 39 additions & 3 deletions src/client/terminals/codeExecution/terminalReplWatcher.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,54 @@
import { Disposable, TerminalShellExecutionStartEvent } from 'vscode';
import { onDidStartTerminalShellExecution } from '../../common/vscodeApis/windowApis';
import { onDidStartTerminalShellExecution, showWarningMessage } from '../../common/vscodeApis/windowApis';
import { sendTelemetryEvent } from '../../telemetry';
import { EventName } from '../../telemetry/constants';
import { ICommandManager } from '../../common/application/types';
import { Commands } from '../../common/constants';
import { Common, Repl } from '../../common/utils/localize';
import { IExtensionContext } from '../../common/types';
import { getGlobalStorage, getWorkspaceStateValue, updateWorkspaceStateValue } from '../../common/persistentState';

function checkREPLCommand(command: string): boolean {
export const SUGGEST_NATIVE_REPL = 'suggestNativeRepl';

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,
context: IExtensionContext,
disposables: Disposable[],
): Promise<void> {
// When extension reloads via user triggering reloading of VS Code, reset to suggest Native REPL on workspace level.
await updateWorkspaceStateValue(SUGGEST_NATIVE_REPL, true);
disposables.push(
onDidStartTerminalShellExecution(async (e: TerminalShellExecutionStartEvent) => {
if (e.execution.commandLine.isTrusted && checkREPLCommand(e.execution.commandLine.value)) {
sendTelemetryEvent(EventName.REPL, undefined, { replType: 'manualTerminal' });

// Plan:
// Global memento to disable show of prompt entirely - global memento
// workspace memento to track and only show suggest of native REPL once.
const globalSuggestNativeRepl = getGlobalStorage<boolean>(context, SUGGEST_NATIVE_REPL, true);
const workspaceSuggestNativeRepl = getWorkspaceStateValue<boolean>(SUGGEST_NATIVE_REPL, true);
if (globalSuggestNativeRepl.get() && workspaceSuggestNativeRepl) {
// Prompt user to start Native REPL
const selection = await showWarningMessage(
Repl.terminalSuggestNativeReplPrompt,
'Launch Native REPL',
Common.doNotShowAgain,
);

if (selection === 'Launch Native REPL') {
await commandManager.executeCommand(Commands.Start_Native_REPL, undefined);
} else {
// Update global suggest to disable Native REPL suggestion in future even after reload.
await globalSuggestNativeRepl.set(false);
}
}
// Update workspace native repl suggestion value after the first 'python' in terminal.
await updateWorkspaceStateValue(SUGGEST_NATIVE_REPL, false);
}
}),
);
Expand Down
Loading