# clamp_selection

> **Function** in `deepagents_code`

📖 [View in docs](https://reference.langchain.com/python/deepagents-code/tui/widgets/diff/clamp_selection)

Return `selection` shifted past a diff row's gutter, if one is set.

Every form a selection can take over a single-line row covers the gutter
unless its start is moved past it:

- `Selection(None, None)` — the row sits mid-selection. Textual extracts
  the row's full text, so the start must move to the gutter's end even
  though no endpoint lands here.
- `Selection(None, end)` — entered from above; same move, plus an `end`
  still inside the gutter means nothing selectable is covered, reported
  as `None` so the row drops out of the screen's selection map.
- `Selection(start, None)` / `Selection(start, end)` — pull any endpoint
  inside the gutter forward to its end; a range that then collapses
  (wholly gutter) is `None`.

Endpoints arrive as offsets into the row's logical content, not as visual
screen coordinates, because every strip carries its own offset metadata.
A wrapped row's continuations therefore need no rule of their own: their
offsets — including the ones their synthetic gutter maps to — all land at
or past `prefix_len`, so the gutter tests below cannot fire on them.

## Signature

```python
clamp_selection(
    widget: Widget,
    selection: Selection,
) -> Selection | None
```

## Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `widget` | `Widget` | Yes | The row the selection applies to. Anything that is not a `_DiffRowStatic` is returned unchanged. |
| `selection` | `Selection` | Yes | The geometry Textual computed for this row. |

## Returns

`Selection | None`

The clamped selection, the original selection, or `None` when the

---

[View source on GitHub](https://github.com/langchain-ai/deepagents/blob/caaa7e7c12d214afa5cf0a1afed8eb6232aa6f7b/libs/code/deepagents_code/tui/widgets/diff.py#L635)