Extract just the body code from a cell source string. Strips decorator lines, def/class/with header, and trailing return. Used when a cell fails ast.parse() to get the body for UnparsableCell.
(cell_source: str, kind: str)
| 395 | |
| 396 | |
| 397 | def _extract_body_code(cell_source: str, kind: str) -> str: |
| 398 | """Extract just the body code from a cell source string. |
| 399 | |
| 400 | Strips decorator lines, def/class/with header, and trailing return. |
| 401 | Used when a cell fails ast.parse() to get the body for UnparsableCell. |
| 402 | """ |
| 403 | from marimo._ast.parse import fixed_dedent |
| 404 | |
| 405 | if kind == "unparsable": |
| 406 | # Already app._unparsable_cell(...) — return as-is |
| 407 | return cell_source |
| 408 | |
| 409 | lines = cell_source.splitlines(keepends=True) |
| 410 | body_start = _find_body_start(lines, kind) |
| 411 | |
| 412 | if body_start >= len(lines): |
| 413 | return "" |
| 414 | |
| 415 | body_lines = list(lines[body_start:]) |
| 416 | |
| 417 | # Strip trailing return at body indentation level |
| 418 | _strip_trailing_return(body_lines) |
| 419 | |
| 420 | body = "".join(body_lines) |
| 421 | return fixed_dedent(body).strip() |
| 422 | |
| 423 | |
| 424 | def _find_body_start(lines: list[str], kind: str) -> int: |
no test coverage detected
searching dependent graphs…