Skip to content

Commit

Permalink
add bracketed paste mode for Python3.13 and above
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonykim1 committed Nov 6, 2024
1 parent b1cb5c2 commit eb51c96
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
7 changes: 4 additions & 3 deletions python_files/normalizeSelection.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import textwrap
from typing import Iterable

attach_bracket_paste = sys.version_info >= (3, 13)

def split_lines(source):
"""
Expand Down Expand Up @@ -279,14 +280,14 @@ def get_next_block_lineno(which_line_next):
normalized = result["normalized_smart_result"]
which_line_next = result["which_line_next"]
if normalized == "deprecated":
data = json.dumps({"normalized": normalized})
data = json.dumps({"normalized": normalized, "attach_bracket_paste": attach_bracket_paste})
else:
data = json.dumps(
{"normalized": normalized, "nextBlockLineno": result["which_line_next"]}
{"normalized": normalized, "nextBlockLineno": result["which_line_next"], "attach_bracket_paste": attach_bracket_paste}
)
else:
normalized = normalize_lines(contents["code"])
data = json.dumps({"normalized": normalized})
data = json.dumps({"normalized": normalized, "attach_bracket_paste": attach_bracket_paste })

stdout = sys.stdout if sys.version_info < (3,) else sys.stdout.buffer
stdout.write(data.encode("utf-8"))
Expand Down
10 changes: 10 additions & 0 deletions src/client/terminals/codeExecution/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,16 @@ export class CodeExecutionHelper implements ICodeExecutionHelper {
const lineOffset = object.nextBlockLineno - activeEditor!.selection.start.line - 1;
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) {
// return `\u001b[200~${object.normalized.trim()}\u001b[201~`;
let trimmedNormalized = object.normalized.replace(/\n$/, '');
if (trimmedNormalized.endsWith(':\n')) {
// In case where statement is unfinished via :, truncate so auto-indentation lands nicely.
trimmedNormalized = trimmedNormalized.replace(/\n$/, '');
}
return `\u001b[200~${trimmedNormalized}\u001b[201~`;
}

return parse(object.normalized);
} catch (ex) {
Expand Down

0 comments on commit eb51c96

Please sign in to comment.