pub struct Selection {
pub anchor: Position,
pub active: bool,
pub mode: SelectionMode,
}Expand description
Selection state within a buffer.
Tracks whether a selection is active, where it started (anchor), and what mode it’s in. The selection extends from the anchor to the current cursor position.
§Selection Direction
The anchor is where selection started. The selection can extend in either direction from the anchor:
- Forward: cursor is after anchor
- Backward: cursor is before anchor
Use bounds() to get normalized (start, end) positions.
Fields§
§anchor: PositionThe position where selection started.
active: boolWhether selection is currently active.
mode: SelectionModeThe selection mode (character/block/line).
Implementations§
Source§impl Selection
impl Selection
Sourcepub const fn start(&mut self, pos: Position, mode: SelectionMode)
pub const fn start(&mut self, pos: Position, mode: SelectionMode)
Start a selection at the given position with the specified mode.
This sets the anchor and activates the selection.
§Arguments
pos- The anchor position (where selection starts)mode- The selection mode
Sourcepub const fn start_char(&mut self, pos: Position)
pub const fn start_char(&mut self, pos: Position)
Start a character-wise selection at the given position.
Convenience method equivalent to start(pos, SelectionMode::Character).
Sourcepub const fn start_line(&mut self, pos: Position)
pub const fn start_line(&mut self, pos: Position)
Start a line-wise selection at the given position.
Convenience method equivalent to start(pos, SelectionMode::Line).
Sourcepub const fn start_block(&mut self, pos: Position)
pub const fn start_block(&mut self, pos: Position)
Start a block selection at the given position.
Convenience method equivalent to start(pos, SelectionMode::Block).
Sourcepub const fn clear(&mut self)
pub const fn clear(&mut self)
Clear (deactivate) the selection.
The anchor position is preserved but the selection becomes inactive.
Sourcepub const fn mode(&self) -> SelectionMode
pub const fn mode(&self) -> SelectionMode
Get the selection mode.
Sourcepub const fn set_mode(&mut self, mode: SelectionMode)
pub const fn set_mode(&mut self, mode: SelectionMode)
Change the selection mode without changing anchor or active state.
Sourcepub fn bounds(&self, cursor_pos: Position) -> Option<(Position, Position)>
pub fn bounds(&self, cursor_pos: Position) -> Option<(Position, Position)>
Get selection bounds in document order (start <= end).
Returns None if selection is not active.
The returned positions are always ordered so that start comes
before or equals end in document order.
§Arguments
cursor_pos- The current cursor position (selection endpoint)
Sourcepub fn block_bounds(&self, cursor_pos: Position) -> Option<(Position, Position)>
pub fn block_bounds(&self, cursor_pos: Position) -> Option<(Position, Position)>
Get block selection bounds (top-left, bottom-right).
For block selections, this returns the rectangle corners:
- Top-left: (
min_line,min_column) - Bottom-right: (
max_line,max_column)
Returns None if selection is not active or not in block mode.
§Arguments
cursor_pos- The current cursor position
Sourcepub fn line_bounds(&self, cursor_pos: Position) -> Option<(usize, usize)>
pub fn line_bounds(&self, cursor_pos: Position) -> Option<(usize, usize)>
Get line-wise selection bounds (start_line, end_line).
For line selections, this returns the range of selected lines.
Returns None if selection is not active.
§Arguments
cursor_pos- The current cursor position
Sourcepub fn contains(&self, pos: Position, cursor_pos: Position) -> bool
pub fn contains(&self, pos: Position, cursor_pos: Position) -> bool
Check if a position is within the selection.
The behavior depends on the selection mode:
- Character: position is between start and end
- Block: position is within the rectangle
- Line: position’s line is within the line range
§Arguments
pos- The position to checkcursor_pos- The current cursor position (selection endpoint)
Sourcepub fn line_count(&self, cursor_pos: Position) -> usize
pub fn line_count(&self, cursor_pos: Position) -> usize
Get the number of lines in the selection.
Returns 0 if selection is not active.
§Arguments
cursor_pos- The current cursor position