Skip to content

Commit eb51c96

Browse files
committed
add bracketed paste mode for Python3.13 and above
1 parent b1cb5c2 commit eb51c96

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

python_files/normalizeSelection.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import textwrap
99
from typing import Iterable
1010

11+
attach_bracket_paste = sys.version_info >= (3, 13)
1112

1213
def split_lines(source):
1314
"""
@@ -279,14 +280,14 @@ def get_next_block_lineno(which_line_next):
279280
normalized = result["normalized_smart_result"]
280281
which_line_next = result["which_line_next"]
281282
if normalized == "deprecated":
282-
data = json.dumps({"normalized": normalized})
283+
data = json.dumps({"normalized": normalized, "attach_bracket_paste": attach_bracket_paste})
283284
else:
284285
data = json.dumps(
285-
{"normalized": normalized, "nextBlockLineno": result["which_line_next"]}
286+
{"normalized": normalized, "nextBlockLineno": result["which_line_next"], "attach_bracket_paste": attach_bracket_paste}
286287
)
287288
else:
288289
normalized = normalize_lines(contents["code"])
289-
data = json.dumps({"normalized": normalized})
290+
data = json.dumps({"normalized": normalized, "attach_bracket_paste": attach_bracket_paste })
290291

291292
stdout = sys.stdout if sys.version_info < (3,) else sys.stdout.buffer
292293
stdout.write(data.encode("utf-8"))

src/client/terminals/codeExecution/helper.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,16 @@ export class CodeExecutionHelper implements ICodeExecutionHelper {
118118
const lineOffset = object.nextBlockLineno - activeEditor!.selection.start.line - 1;
119119
await this.moveToNextBlock(lineOffset, activeEditor);
120120
}
121+
// For new _pyrepl for Python3.13 and above, we need to send code via bracketed paste mode.
122+
if (object.attach_bracket_paste) {
123+
// return `\u001b[200~${object.normalized.trim()}\u001b[201~`;
124+
let trimmedNormalized = object.normalized.replace(/\n$/, '');
125+
if (trimmedNormalized.endsWith(':\n')) {
126+
// In case where statement is unfinished via :, truncate so auto-indentation lands nicely.
127+
trimmedNormalized = trimmedNormalized.replace(/\n$/, '');
128+
}
129+
return `\u001b[200~${trimmedNormalized}\u001b[201~`;
130+
}
121131

122132
return parse(object.normalized);
123133
} catch (ex) {

0 commit comments

Comments
 (0)