Skip to content

Commit 2352196

Browse files
authored
Put the new REPL normalization script behind an experiment (#14561)
* Put new normalization script behind experiment * Allow opt-in and opt-out * Restructure & add tests * normalizeSelection.py only uses \n * I should have read the documentation * Save the string -_- * Remove unnecessary check * derped * Fix tests after merge * Use different normalize helper for each script * .... * Remember that both scripts only use \n
1 parent 8fff701 commit 2352196

14 files changed

Lines changed: 265 additions & 97 deletions

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,7 @@
10421042
"tryPylance",
10431043
"jediLSP",
10441044
"debuggerDataViewer",
1045+
"pythonSendEntireLineToREPL",
10451046
"All"
10461047
]
10471048
},
@@ -1068,6 +1069,7 @@
10681069
"tryPylance",
10691070
"jediLSP",
10701071
"debuggerDataViewer",
1072+
"pythonSendEntireLineToREPL",
10711073
"All"
10721074
]
10731075
},

src/client/common/experiments/groups.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,8 @@ export enum JoinMailingListPromptVariants {
7575
variant2 = 'pythonJoinMailingListVar2',
7676
variant3 = 'pythonJoinMailingListVar3'
7777
}
78+
79+
// Experiment to use a different method for normalizing code to be sent to the REPL.
80+
export enum SendSelectionToREPL {
81+
experiment = 'pythonSendEntireLineToREPL'
82+
}

src/client/common/process/internal/scripts/index.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,21 @@ export function normalizeForInterpreter(): [string[], (out: string) => string] {
227227
return [args, parse];
228228
}
229229

230+
//============================
231+
// normalizeSelection.py
232+
233+
export function normalizeSelection(): [string[], (out: string) => string] {
234+
const script = path.join(SCRIPTS_DIR, 'normalizeSelection.py');
235+
const args = [ISOLATED, script];
236+
237+
function parse(out: string) {
238+
// The text will be used as-is.
239+
return out;
240+
}
241+
242+
return [args, parse];
243+
}
244+
230245
//============================
231246
// symbolProvider.py
232247

src/client/terminals/codeExecution/helper.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ import { Position, Range, TextEditor, Uri } from 'vscode';
77

88
import { IApplicationShell, IDocumentManager } from '../../common/application/types';
99
import { PYTHON_LANGUAGE } from '../../common/constants';
10+
import { SendSelectionToREPL } from '../../common/experiments/groups';
1011
import { traceError } from '../../common/logger';
1112
import * as internalScripts from '../../common/process/internal/scripts';
1213
import { IProcessServiceFactory } from '../../common/process/types';
14+
import { IExperimentService } from '../../common/types';
1315
import { createDeferred } from '../../common/utils/async';
1416
import { IInterpreterService } from '../../interpreter/contracts';
1517
import { IServiceContainer } from '../../ioc/types';
@@ -21,12 +23,14 @@ export class CodeExecutionHelper implements ICodeExecutionHelper {
2123
private readonly applicationShell: IApplicationShell;
2224
private readonly processServiceFactory: IProcessServiceFactory;
2325
private readonly interpreterService: IInterpreterService;
26+
private readonly experimentService: IExperimentService;
2427

2528
constructor(@inject(IServiceContainer) serviceContainer: IServiceContainer) {
2629
this.documentManager = serviceContainer.get<IDocumentManager>(IDocumentManager);
2730
this.applicationShell = serviceContainer.get<IApplicationShell>(IApplicationShell);
2831
this.processServiceFactory = serviceContainer.get<IProcessServiceFactory>(IProcessServiceFactory);
2932
this.interpreterService = serviceContainer.get<IInterpreterService>(IInterpreterService);
33+
this.experimentService = serviceContainer.get<IExperimentService>(IExperimentService);
3034
}
3135

3236
public async normalizeLines(code: string, resource?: Uri): Promise<string> {
@@ -40,7 +44,11 @@ export class CodeExecutionHelper implements ICodeExecutionHelper {
4044

4145
const interpreter = await this.interpreterService.getActiveInterpreter(resource);
4246
const processService = await this.processServiceFactory.create(resource);
43-
const [args, parse] = internalScripts.normalizeForInterpreter();
47+
48+
const inExperiment = await this.experimentService.inExperiment(SendSelectionToREPL.experiment);
49+
const [args, parse] = inExperiment
50+
? internalScripts.normalizeSelection()
51+
: internalScripts.normalizeForInterpreter();
4452
const observable = processService.execObservable(interpreter?.path || 'python', args, {
4553
throwOnStdErr: true
4654
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
def square(x):
2+
return x**2
3+
4+
print('hello')
5+
# Sample block 2
6+
7+
a = 2
8+
if a < 2:
9+
print('less than 2')
10+
else:
11+
print('more than 2')
12+
13+
print('hello')
14+
# Sample block 3
15+
16+
for i in range(5):
17+
print(i)
18+
print(i)
19+
print(i)
20+
print(i)
21+
22+
print('complete')
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def add(x, y):
2+
"""Adds x to y"""
3+
# Some comment
4+
return x + y
5+
6+
v = add(1, 7)
7+
print(v)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
if True:
2+
print(1)
3+
print(2)
4+
5+
print(3)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class pc(object):
2+
def __init__(self, pcname, model):
3+
self.pcname = pcname
4+
self.model = model
5+
def print_name(self):
6+
print('Workstation name is', self.pcname, 'model is', self.model)
7+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
for i in range(10):
2+
print('a')
3+
for j in range(5):
4+
print('b')
5+
print('b2')
6+
for k in range(2):
7+
print('c')
8+
print('done with first loop')
9+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
if True:
2+
print(1)
3+
else: print(2)
4+
5+
print('🔨')
6+
print(3)
7+
print(3)
8+
if True:
9+
print(1)
10+
else: print(2)
11+
12+
if True:
13+
print(1)
14+
else: print(2)
15+

0 commit comments

Comments
 (0)