pub struct Selection {
pub anchor: usize,
pub head: usize,
}Expand description
Represents a text selection using an anchor and a head.
The anchor is the position where the selection started, while the head is the current cursor position. When they differ, the selection extends between these two positions.
Both positions are byte offsets into the document.
§Examples
A collapsed selection, where the cursor is at byte offset 10:
let selection = Selection::point(10);
assert!(selection.is_empty());A selection from byte offset 10 to 20:
let selection = Selection::range(10, 20);
assert_eq!(selection.byte_range(), 10..20);Fields§
§anchor: usizeThe byte offset where the selection started.
This remains fixed while the selection is extended or contracted by moving the head.
head: usizeThe current cursor position, represented as a byte offset.
Moving the head changes the extent and direction of the selection while the anchor remains fixed.
Implementations§
Source§impl Selection
impl Selection
Sourcepub const fn point(offset: usize) -> Self
pub const fn point(offset: usize) -> Self
Creates a collapsed selection with the cursor at offset.
Both the anchor and head are initialized to the same position.
Sourcepub const fn range(anchor: usize, head: usize) -> Self
pub const fn range(anchor: usize, head: usize) -> Self
Creates a selection from an anchor position to a head position.
The positions are not reordered, so anchor may be greater than
head. Use Self::byte_range to obtain the normalized range.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the selection is collapsed.
A collapsed selection has the anchor and head at the same position.
Sourcepub fn byte_range(&self) -> Range<usize> ⓘ
pub fn byte_range(&self) -> Range<usize> ⓘ
Returns the normalized byte range covered by the selection.
The returned range always starts at the smaller of the anchor and head and ends at the larger, regardless of the direction in which the selection was made.