feat(python): add a Python Console tool window (#5164)

Adds a Python Console tool window: an interactive interpreter of the project's Python (the one Settings and the status bar show; in a workspace, the member the active file is in), started in the project root with the source roots on `PYTHONPATH`. It uses IPython when the interpreter has it and plain Python otherwise, in its own `__main__`. Enter runs the input, Shift+Enter adds a line, Up/Down recall earlier inputs, and while code runs a typed line answers its `input()`. A Variables pane lists the console's globals after each run. Execute Selection in Python Console (`python.console.executeSelection`, Alt+Shift+E, also in the editor context menu of Python files) sends the selection or the current line, starting the console if needed; a selection inside an indented block is dedented. The console is a small driver script run on the existing Run process host, so there are no backend changes.

Closes #5148

Co-authored-by: wildcatDownstairs <[email protected]>
Цей коміт міститься в:
LU XIANGRUIтаwildcatDownstairs authored and GitHub committed 2026-11-22 14:34:50 +08:00
1 джерело 88f494ca50
коміт c632dfa01c
17 files changed
+898

No files matched your search

+38
Переглянути файл
@@ -0,0 +1,38 @@
import { describe, expect, it } from "vitest";
import { request, stdoutParser } from "./protocol";
describe("stdoutParser", () => {
it("separates driver messages from output", () => {
const parse = stdoutParser();
expect(parse('Out[1]: 2\n\x1e{"done":{"variables":[]}}\nafter')).toEqual({
text: "Out[1]: 2\nafter",
messages: [{ done: { variables: [] } }],
});
});
it("waits for a message cut across chunks", () => {
const parse = stdoutParser();
expect(parse('hi\x1e{"ready":{"vers')).toEqual({ text: "hi", messages: [] });
expect(parse('ion":"3.12.1","ipython":null}}\n')).toEqual({
text: "",
messages: [{ ready: { version: "3.12.1", ipython: null } }],
});
});
it("keeps a line that only looks like a message as output", () => {
expect(stdoutParser()("a\x1enot json\nb")).toEqual({ text: "a\x1enot json\nb", messages: [] });
});
it("reads the message after output that printed \\x1e without ending its line", () => {
const parse = stdoutParser();
expect(parse("x\x1ey")).toEqual({ text: "x", messages: [] });
expect(parse('\x1e{"done":{"variables":[]}}\n')).toEqual({
text: "\x1ey",
messages: [{ done: { variables: [] } }],
});
});
});
it("encodes code as one stdin line", () => {
expect(request("x = 1\nprint(x)")).toBe('{"code":"x = 1\\nprint(x)"}\n');
});