zed-industries/zed · error

ensured that there's only one selection

Error message

ensured that there's only one selection

What it means

A precondition guard in select_previous: the implementation computes the previous match relative to a single selection's word range and later expects the selection set to contain exactly one selection. It fires when select_previous is invoked on an editor with zero or multiple selections — the surrounding word/match logic only defines a result for the single-selection case, so the invariant 'ensured that there's only one selection' was violated.

Source

Thrown at crates/editor/src/selection.rs:539

                for selection in &mut selections {
                    let (word_range, _) = buffer.surrounding_word(selection.start, None);
                    selection.start = word_range.start;
                    selection.end = word_range.end;
                    selection.goal = SelectionGoal::None;
                    selection.reversed = false;
                    self.select_match_ranges(
                        selection.start..selection.end,
                        selection.reversed,
                        action.replace_newest,
                        Some(Autoscroll::newest()),
                        window,
                        cx,
                    );
                }
                if selections.len() == 1 {
                    let selection = selections
                        .last()
                        .expect("ensured that there's only one selection");
                    let query = buffer
                        .text_for_range(selection.start..selection.end)
                        .collect::<String>();
                    let is_empty = query.is_empty();
                    let select_state = SelectNextState {
                        query: self.build_query(&[query.chars().rev().collect::<String>()], cx)?,
                        wordwise: true,
                        done: is_empty,
                    };
                    self.select_prev_state = Some(select_state);
                } else {
                    self.select_prev_state = None;
                }
            } else if let Some(selected_text) = selected_text {
                self.select_prev_state = Some(SelectNextState {
                    query: self
                        .build_query(&[selected_text.chars().rev().collect::<String>()], cx)?,
                    wordwise: false,

View on GitHub (pinned to 9d272b0363)

Solutions

  1. Branch on the selection count: no-op for zero, operate on the newest/last selection for multiple instead of panicking
  2. Call the single-selection API only after checking editor.selection_count() or selecting the newest selection explicitly
  3. Add a debug_assert! to catch multi-selection callers during development while degrading gracefully in release
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/editor/src/selection.rs:539 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of zed-industries/zed@9d272b0363 (2026-08-20). Data as JSON: /api/errors/45532d06306a5f48. Report an issue: GitHub.