| 142 | |
| 143 | class RuffFormatter(Formatter): |
| 144 | async def format( |
| 145 | self, codes: CellCodes, stdin_filename: str | None = None |
| 146 | ) -> CellCodes: |
| 147 | # Wrap each cell in a dummy function so ruff applies function-scope |
| 148 | # blank-line rules (1 blank line around nested defs) instead of |
| 149 | # file-scope rules (2 blank lines). Cells with no executable |
| 150 | # statements (whitespace-only or comment-only) are left unwrapped |
| 151 | # since an empty or comment-only function body is invalid Python. |
| 152 | skip_wrap = { |
| 153 | key |
| 154 | for key, code in codes.items() |
| 155 | if all( |
| 156 | not line.strip() or line.strip().startswith("#") |
| 157 | for line in code.splitlines() |
| 158 | ) |
| 159 | } |
| 160 | wrapped: CellCodes = {} |
| 161 | for key, code in codes.items(): |
| 162 | if key not in skip_wrap: |
| 163 | wrapped[key] = "\n".join( |
| 164 | ["def _():", textwrap.indent(code, " ")] |
| 165 | ) |
| 166 | else: |
| 167 | wrapped[key] = code |
| 168 | |
| 169 | wrapped_result = await ruff( |
| 170 | wrapped, |
| 171 | "format", |
| 172 | "--line-length", |
| 173 | str(self.line_length), |
| 174 | stdin_filename=stdin_filename, |
| 175 | ) |
| 176 | |
| 177 | # Unwrap: use the AST to extract the function body, mirroring the |
| 178 | # parse mechanism in marimo/_ast/parse.py (Extractor + fixed_dedent). |
| 179 | result: CellCodes = {} |
| 180 | for key, code in codes.items(): |
| 181 | if key in skip_wrap: |
| 182 | result[key] = wrapped_result.get(key, code) |
| 183 | elif key in wrapped_result: |
| 184 | result[key] = unwrap_cell_body(wrapped_result[key]) |
| 185 | |
| 186 | return result |
| 187 | |
| 188 | |
| 189 | class BlackFormatter(Formatter): |