From ce68808185a151b529e95980bcc481b9ce8a01ca Mon Sep 17 00:00:00 2001 From: Vijay Janapa Reddi Date: Wed, 11 Feb 2026 18:46:18 -0500 Subject: [PATCH] Fix: make pipe table prettifier apply visible alignment changes Treat internal spacing changes as real formatting differences and normalize separator padding so table prettification is applied consistently. Save files before running pre-commit fixers from the extension so results match editor state. --- .../scripts/utilities/prettify_pipe_tables.py | 13 +++++++------ book/vscode-ext/src/commands/precommitCommands.ts | 15 +++++++++++---- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/book/tools/scripts/utilities/prettify_pipe_tables.py b/book/tools/scripts/utilities/prettify_pipe_tables.py index b4a20e2034..0e1ec2ac19 100644 --- a/book/tools/scripts/utilities/prettify_pipe_tables.py +++ b/book/tools/scripts/utilities/prettify_pipe_tables.py @@ -159,7 +159,9 @@ def prettify_table(table_lines: list[str]) -> list[str]: # Build separator row sep_cells = [make_separator_cell(alignments[j], col_widths[j]) for j in range(num_cols)] - result.append('|' + '|'.join(sep_cells) + '|') + # Keep separator visually consistent with data rows by adding + # single-space padding around each separator cell. + result.append('| ' + ' | '.join(sep_cells) + ' |') else: # Build data row with padding if row_idx < len(rows): @@ -236,14 +238,13 @@ def find_pipe_tables(content: str) -> list[tuple[int, int, list[str]]]: def tables_are_equal(original: list[str], prettified: list[str]) -> bool: - """Check if two tables are equivalent (ignoring whitespace differences).""" + """Check if two tables are textually identical for formatting purposes.""" if len(original) != len(prettified): return False for orig, pretty in zip(original, prettified): - # Normalize: strip and collapse multiple spaces - orig_norm = ' '.join(orig.split()) - pretty_norm = ' '.join(pretty.split()) - if orig_norm != pretty_norm: + # Ignore trailing whitespace-only differences, but keep internal + # spacing significant so alignment changes are detected. + if orig.rstrip() != pretty.rstrip(): return False return True diff --git a/book/vscode-ext/src/commands/precommitCommands.ts b/book/vscode-ext/src/commands/precommitCommands.ts index 5614c2d386..abd0fd8e32 100644 --- a/book/vscode-ext/src/commands/precommitCommands.ts +++ b/book/vscode-ext/src/commands/precommitCommands.ts @@ -14,15 +14,15 @@ export function registerPrecommitCommands(context: vscode.ExtensionContext): voi context.subscriptions.push( vscode.commands.registerCommand('mlsysbook.precommitRunAll', () => { - void runBookCommand('pre-commit run --all-files', root, { + void vscode.workspace.saveAll(false).then(() => runBookCommand('pre-commit run --all-files', root, { label: 'Pre-commit (all hooks)', - }); + })); }), vscode.commands.registerCommand('mlsysbook.precommitRunHook', (command: string) => { - void runBookCommand(command, root, { + void vscode.workspace.saveAll(false).then(() => runBookCommand(command, root, { label: 'Pre-commit (selected hook)', - }); + })); }), vscode.commands.registerCommand('mlsysbook.precommitRunFixersCurrentFile', async () => { @@ -37,6 +37,13 @@ export function registerPrecommitCommands(context: vscode.ExtensionContext): voi vscode.window.showWarningMessage('MLSysBook: current-file fixers only support .qmd files.'); return; } + if (editor.document.isDirty) { + const saved = await editor.document.save(); + if (!saved) { + vscode.window.showWarningMessage('MLSysBook: save the file before running current-file fixers.'); + return; + } + } const relativePath = path.relative(root, filePath); if (!relativePath || relativePath.startsWith('..') || path.isAbsolute(relativePath)) {