Skip to content

fix(datagrid): route Cmd+V to row paste when clipboard holds copied rows#935

Merged
datlechin merged 2 commits intomainfrom
fix/datagrid-paste-routing
Apr 29, 2026
Merged

fix(datagrid): route Cmd+V to row paste when clipboard holds copied rows#935
datlechin merged 2 commits intomainfrom
fix/datagrid-paste-routing

Conversation

@datlechin
Copy link
Copy Markdown
Member

Summary

Fixes a Cmd+C / Cmd+V asymmetry: Cmd+C copies the selected rows (row-level), but Cmd+V was always cell-pasting whenever a cell had focus — so copying a row then pasting silently overwrote the row's tail columns with the source row's leading values instead of inserting a new row.

Root cause

KeyHandlingTableView.paste(_:) chose between cell paste and row paste based purely on whether a cell was focused:

if focusedRow >= 0, focusedColumn >= 1 {
    pasteCellsFromClipboard(...)
} else {
    dataGridPasteRows()
}

Clicking any cell sets focus, but copy(_:) always uses selectedRowIndexes. Net effect: copy works at row level, paste at cell level, and the round trip mangles cells.

Fix

pasteCellsFromClipboard now defers to row paste on two signals:

  1. Pasteboard tag: ClipboardProvider gains writeRows(tsv:html:?) which writes the TSV (and optional HTML) plus a com.TablePro.gridRows pasteboard type. hasGridRows reads it. Migrates RowOperationsManager.copySelectedRowsToClipboard and TableViewCoordinator.copyRows / copyRowsWithHeaders to use it. Drops the now-unused writeTabular.
  2. Shape heuristic: if every line of the clipboard TSV has exactly numberOfDataColumns values, treat it as full rows. Catches external row-shaped data (e.g., from another DB tool) that the user clearly meant as an insert.

Cell paste continues to work for shape-mismatched TSV (external spreadsheet selections, single-column copies, etc.).

Audit of other paste paths (no changes needed)

  • TableRowViewWithMenu right-click row > Paste → already calls dataGridPasteRows directly
  • MainContentCommandActions.pasteRows (menu / .pasteRows notification) → already calls coordinator?.pasteRows() directly

Only the Cmd+V keyboard path needed routing fix.

Test plan

  • Build clean
  • RowOperationsManagerCopyTests pass (mock updated to new protocol)
  • Manual: Cmd+C on row, Cmd+V → inserts new row ✓
  • Manual: Cmd+C on multiple rows, Cmd+V → inserts N rows ✓
  • Manual: Cmd+C on single cell, Cmd+V into another cell → cell paste ✓
  • Manual: paste TSV from external (Excel) into cell selection → cell paste ✓
  • Manual: right-click row > Paste → inserts (unchanged) ✓

Cmd+C on a row selection writes TSV via ClipboardService and Cmd+V then routed through pasteCellsFromClipboard, which overwrote cells starting at the focused cell with the source row's values, instead of inserting a new row. The asymmetry was: Cmd+C always operated on selectedRowIndexes (row level), Cmd+V chose cell paste whenever any cell was focused, so copying then pasting in the same selection looked like the row's tail columns silently mutated.

Two signals now defer cell paste to dataGridPasteRows (which inserts):
  1. The clipboard was written by this app's row-copy path. ClipboardProvider gains a writeRows(tsv:html:?) method that tags the pasteboard with com.TablePro.gridRows. pasteCellsFromClipboard checks hasGridRows first and bails when set.
  2. The clipboard's TSV shape is full-row (every line has exactly numberOfDataColumns values). Catches external row-shaped data the user clearly intended as a row insert.

Migrates RowOperationsManager.copySelectedRowsToClipboard and TableViewCoordinator.copyRows / copyRowsWithHeaders from writeText / writeTabular to writeRows. Drops the now-unused writeTabular method from the protocol.

Cell paste continues to work for external single-column or shape-mismatched TSV (clearly cell-shaped data from spreadsheets etc.). Right-click row > Paste and Cmd+V with no cell focus already went straight to row paste; unchanged.
Locks the contract that pasteCellsFromClipboard returns false (deferring to dataGridPasteRows) on the two new signals: clipboard carries the gridRows tag, or every TSV line matches the table's column count. Without these tests, the bug from this same PR could regress silently if someone changes the pasteboard tag string or removes the shape check.

Also covers the cell-paste happy path (shape-mismatched TSV) and the read-only short-circuit.
@datlechin datlechin merged commit 7c1d844 into main Apr 29, 2026
2 checks passed
@datlechin datlechin deleted the fix/datagrid-paste-routing branch April 29, 2026 04:44
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 35c89c8fe5

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread TablePro/Views/Results/Extensions/DataGridView+CellPaste.swift
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant