Skip to content

Commit

Permalink
Do not use bracketed paste mode for native repl (#24433)
Browse files Browse the repository at this point in the history
Resolves: #24417
  • Loading branch information
anthonykim1 authored Nov 13, 2024
1 parent 3e30f9d commit 63cbb30
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 13 deletions.
7 changes: 6 additions & 1 deletion src/client/repl/replCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
import { registerCommand } from '../common/vscodeApis/commandApis';
import { sendTelemetryEvent } from '../telemetry';
import { EventName } from '../telemetry/constants';
import { ReplType } from './types';

/**
* Register Start Native REPL command in the command palette
Expand Down Expand Up @@ -69,7 +70,11 @@ export async function registerReplCommands(
if (activeEditor && activeEditor.document) {
wholeFileContent = activeEditor.document.getText();
}
const normalizedCode = await executionHelper.normalizeLines(code!, wholeFileContent);
const normalizedCode = await executionHelper.normalizeLines(
code!,
ReplType.native,
wholeFileContent,
);
await nativeRepl.sendToNativeRepl(normalizedCode);
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/client/repl/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

'use strict';

export enum ReplType {
terminal = 'terminal',
native = 'native',
}
7 changes: 6 additions & 1 deletion src/client/terminals/codeExecution/codeExecutionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
CreateEnvironmentCheckKind,
triggerCreateEnvironmentCheckNonBlocking,
} from '../../pythonEnvironments/creation/createEnvironmentTrigger';
import { ReplType } from '../../repl/types';

@injectable()
export class CodeExecutionManager implements ICodeExecutionManager {
Expand Down Expand Up @@ -149,7 +150,11 @@ export class CodeExecutionManager implements ICodeExecutionManager {
if (activeEditor && activeEditor.document) {
wholeFileContent = activeEditor.document.getText();
}
const normalizedCode = await codeExecutionHelper.normalizeLines(codeToExecute!, wholeFileContent);
const normalizedCode = await codeExecutionHelper.normalizeLines(
codeToExecute!,
ReplType.terminal,
wholeFileContent,
);
if (!normalizedCode || normalizedCode.trim().length === 0) {
return;
}
Expand Down
10 changes: 8 additions & 2 deletions src/client/terminals/codeExecution/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { traceError } from '../../logging';
import { IConfigurationService, Resource } from '../../common/types';
import { sendTelemetryEvent } from '../../telemetry';
import { EventName } from '../../telemetry/constants';
import { ReplType } from '../../repl/types';

@injectable()
export class CodeExecutionHelper implements ICodeExecutionHelper {
Expand Down Expand Up @@ -52,7 +53,12 @@ export class CodeExecutionHelper implements ICodeExecutionHelper {
this.activeResourceService = this.serviceContainer.get<IActiveResourceService>(IActiveResourceService);
}

public async normalizeLines(code: string, wholeFileContent?: string, resource?: Uri): Promise<string> {
public async normalizeLines(
code: string,
replType: ReplType,
wholeFileContent?: string,
resource?: Uri,
): Promise<string> {
try {
if (code.trim().length === 0) {
return '';
Expand Down Expand Up @@ -119,7 +125,7 @@ export class CodeExecutionHelper implements ICodeExecutionHelper {
await this.moveToNextBlock(lineOffset, activeEditor);
}
// For new _pyrepl for Python3.13 and above, we need to send code via bracketed paste mode.
if (object.attach_bracket_paste) {
if (object.attach_bracket_paste && replType === ReplType.terminal) {
let trimmedNormalized = object.normalized.replace(/\n$/, '');
if (trimmedNormalized.endsWith(':\n')) {
// In case where statement is unfinished via :, truncate so auto-indentation lands nicely.
Expand Down
3 changes: 2 additions & 1 deletion src/client/terminals/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import { Event, Terminal, TextEditor, Uri } from 'vscode';
import { IDisposable, Resource } from '../common/types';
import { ReplType } from '../repl/types';

export const ICodeExecutionService = Symbol('ICodeExecutionService');

Expand All @@ -15,7 +16,7 @@ export interface ICodeExecutionService {
export const ICodeExecutionHelper = Symbol('ICodeExecutionHelper');

export interface ICodeExecutionHelper {
normalizeLines(code: string, wholeFileContent?: string): Promise<string>;
normalizeLines(code: string, replType: ReplType, wholeFileContent?: string, resource?: Uri): Promise<string>;
getFileToExecute(): Promise<Uri | undefined>;
saveFileIfDirty(file: Uri): Promise<Resource>;
getSelectedTextToExecute(textEditor: TextEditor): Promise<string | undefined>;
Expand Down
9 changes: 5 additions & 4 deletions src/test/terminals/codeExecution/helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { EnvironmentType, PythonEnvironment } from '../../../client/pythonEnviro
import { CodeExecutionHelper } from '../../../client/terminals/codeExecution/helper';
import { ICodeExecutionHelper } from '../../../client/terminals/types';
import { PYTHON_PATH } from '../../common';
import { ReplType } from '../../../client/repl/types';

const TEST_FILES_PATH = path.join(EXTENSION_ROOT_DIR, 'src', 'test', 'python_files', 'terminalExec');

Expand Down Expand Up @@ -160,7 +161,7 @@ suite('Terminal - Code Execution Helper', () => {
};
jsonParseStub.returns(mockResult);

const result = await helper.normalizeLines('print("Looks like you are on 3.13")');
const result = await helper.normalizeLines('print("Looks like you are on 3.13")', ReplType.terminal);

expect(result).to.equal(`\u001b[200~print("Looks like you are on 3.13")\u001b[201~`);
jsonParseStub.restore();
Expand Down Expand Up @@ -190,7 +191,7 @@ suite('Terminal - Code Execution Helper', () => {
actualProcessService.execObservable.apply(actualProcessService, [file, args, options]),
);

const result = await helper.normalizeLines('print("Looks like you are not on 3.13")');
const result = await helper.normalizeLines('print("Looks like you are not on 3.13")', ReplType.terminal);

expect(result).to.equal('print("Looks like you are not on 3.13")');
jsonParseStub.restore();
Expand All @@ -207,7 +208,7 @@ suite('Terminal - Code Execution Helper', () => {
return ({} as unknown) as ObservableExecutionResult<string>;
});

await helper.normalizeLines('print("hello")');
await helper.normalizeLines('print("hello")', ReplType.terminal);

expect(execArgs).to.contain('normalizeSelection.py');
});
Expand All @@ -228,7 +229,7 @@ suite('Terminal - Code Execution Helper', () => {
.returns((file, args, options) =>
actualProcessService.execObservable.apply(actualProcessService, [file, args, options]),
);
const normalizedCode = await helper.normalizeLines(source);
const normalizedCode = await helper.normalizeLines(source, ReplType.terminal);
const normalizedExpected = expectedSource.replace(/\r\n/g, '\n');
expect(normalizedCode).to.be.equal(normalizedExpected);
}
Expand Down
17 changes: 13 additions & 4 deletions src/test/terminals/codeExecution/smartSend.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { PYTHON_PATH } from '../../common';
import { Architecture } from '../../../client/common/utils/platform';
import { ProcessService } from '../../../client/common/process/proc';
import { l10n } from '../../mocks/vsc';
import { ReplType } from '../../../client/repl/types';

const TEST_FILES_PATH = path.join(EXTENSION_ROOT_DIR, 'src', 'test', 'python_files', 'terminalExec');

Expand Down Expand Up @@ -145,7 +146,7 @@ suite('REPL - Smart Send', () => {
.setup((p) => p.execObservable(TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny()))
.returns((file, args, options) => execObservable.apply(actualProcessService, [file, args, options]));

await codeExecutionHelper.normalizeLines('my_dict = {', wholeFileContent);
await codeExecutionHelper.normalizeLines('my_dict = {', ReplType.terminal, wholeFileContent);

commandManager
.setup((c) => c.executeCommand('cursorMove', TypeMoq.It.isAny()))
Expand Down Expand Up @@ -197,7 +198,11 @@ suite('REPL - Smart Send', () => {
.setup((p) => p.execObservable(TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny()))
.returns((file, args, options) => execObservable.apply(actualProcessService, [file, args, options]));

const actualSmartOutput = await codeExecutionHelper.normalizeLines('my_dict = {', wholeFileContent);
const actualSmartOutput = await codeExecutionHelper.normalizeLines(
'my_dict = {',
ReplType.terminal,
wholeFileContent,
);

// my_dict = { <----- smart shift+enter here
// "key1": "value1",
Expand Down Expand Up @@ -247,7 +252,11 @@ suite('REPL - Smart Send', () => {
.setup((p) => p.execObservable(TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny()))
.returns((file, args, options) => execObservable.apply(actualProcessService, [file, args, options]));

const actualNonSmartResult = await codeExecutionHelper.normalizeLines('my_dict = {', wholeFileContent);
const actualNonSmartResult = await codeExecutionHelper.normalizeLines(
'my_dict = {',
ReplType.terminal,
wholeFileContent,
);
const expectedNonSmartResult = 'my_dict = {\n\n'; // Standard for previous normalization logic
expect(actualNonSmartResult).to.be.equal(expectedNonSmartResult);
});
Expand Down Expand Up @@ -285,7 +294,7 @@ suite('REPL - Smart Send', () => {
.setup((p) => p.execObservable(TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny()))
.returns((file, args, options) => execObservable.apply(actualProcessService, [file, args, options]));

await codeExecutionHelper.normalizeLines('my_dict = {', wholeFileContent);
await codeExecutionHelper.normalizeLines('my_dict = {', ReplType.terminal, wholeFileContent);

applicationShell
.setup((a) =>
Expand Down

0 comments on commit 63cbb30

Please sign in to comment.