Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: preserve return statements inside nested functions when pasting (#8222)

Ahmad Yasser01771b6b37d1
diff --git a/frontend/src/core/codemirror/misc/__tests__/paste.test.ts b/frontend/src/core/codemirror/misc/__tests__/paste.test.ts
index 80f694ba05..f98656e129 100644
--- a/frontend/src/core/codemirror/misc/__tests__/paste.test.ts
+++ b/frontend/src/core/codemirror/misc/__tests__/paste.test.ts
@@ -140,6 +140,24 @@ def _(
expect(extractCells(input)).toEqual(["x = a + b + c"]);
});
+ it("preserves return statements inside nested functions", () => {
+ const input = `
+def _(mo, px):
+ def make_fig():
+ data = {'category': ['foo', 'bar'], 'value': [10, 20]}
+ fig = px.bar(data, x='category', y='value')
+ return fig
+
+ fig = make_fig()
+ mo.ui.plotly(fig)
+ return
+`;
+ expect(extractCells(input)).toEqual([
+ "def make_fig():\n data = {'category': ['foo', 'bar'], 'value': [10, 20]}\n fig = px.bar(data, x='category', y='value')\n return fig\n\nfig = make_fig()\nmo.ui.plotly(fig)",
+ ]);
+ });
+
it("handles cells with config", () => {
const input = `
@app.cell(hide_code=True, column=2)
diff --git a/frontend/src/core/codemirror/misc/paste.ts b/frontend/src/core/codemirror/misc/paste.ts
index f9ed0cd5b1..82428c65c0 100644
--- a/frontend/src/core/codemirror/misc/paste.ts
+++ b/frontend/src/core/codemirror/misc/paste.ts
@@ -42,6 +42,7 @@ export function extractCells(text: string): string[] {
let inMultilineArgs = false;
let inMultilineReturn = false;
let parenCount = 0;
+ let cellBaseIndent: number | null = null;
// Pre-compile regex patterns
const leadingParenRegex = /\(/g;
@@ -55,19 +56,16 @@ export function extractCells(text: string): string[] {
);
}
+ function getIndent(line: string): number {
+ const match = line.match(/^\s*/);
+ return match ? match[0].length : 0;
+ }
+
function finalizeCellIfNeeded() {
if (currentCell.length === 0) {
return;
}
- // Remove trailing returns
- while (
- currentCell.length > 0 &&
- currentCell[currentCell.length - 1].trim().startsWith("return")
- ) {
- currentCell.pop();
- }
-
// Only add non-empty cells
if (currentCell.some((l) => l.trim() !== "")) {
cells.push(dedent(currentCell.join("\n")));
@@ -88,6 +86,7 @@ export function extractCells(text: string): string[] {
finalizeCellIfNeeded();
inCell = true;
skipLines = 1; // Skip the def line
+ cellBaseIndent = null;
continue;
}
@@ -125,8 +124,13 @@ export function extractCells(text: string): string[] {
continue;
}
- // Handle return statements
- if (trimmed.startsWith("return")) {
+ // Detect base indentation of cell body from first content line
+ if (cellBaseIndent === null && trimmed) {
+ cellBaseIndent = getIndent(line);
+ }
+
+ // Handle return statements — only strip cell-level returns
+ if (trimmed.startsWith("return") && getIndent(line) === cellBaseIndent) {
if (trimmed.includes("(") && !trimmed.endsWith(")")) {
inMultilineReturn = true;
parenCount = countParens(trimmed);