chore: refactor the input component (#165)
* refactor the input component * fix clippy * clean up
This commit is contained in:
46
crates/ui/src/input/cursor.rs
Normal file
46
crates/ui/src/input/cursor.rs
Normal file
@@ -0,0 +1,46 @@
|
||||
use std::ops::Range;
|
||||
|
||||
/// A selection in the text, represented by start and end byte indices.
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq, Default)]
|
||||
pub struct Selection {
|
||||
pub start: usize,
|
||||
pub end: usize,
|
||||
}
|
||||
|
||||
impl Selection {
|
||||
pub fn new(start: usize, end: usize) -> Self {
|
||||
Self { start, end }
|
||||
}
|
||||
|
||||
pub fn len(&self) -> usize {
|
||||
self.end.saturating_sub(self.start)
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.start == self.end
|
||||
}
|
||||
|
||||
/// Clears the selection, setting start and end to 0.
|
||||
pub fn clear(&mut self) {
|
||||
self.start = 0;
|
||||
self.end = 0;
|
||||
}
|
||||
|
||||
/// Checks if the given offset is within the selection range.
|
||||
pub fn contains(&self, offset: usize) -> bool {
|
||||
offset >= self.start && offset < self.end
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Range<usize>> for Selection {
|
||||
fn from(value: Range<usize>) -> Self {
|
||||
Self::new(value.start, value.end)
|
||||
}
|
||||
}
|
||||
impl From<Selection> for Range<usize> {
|
||||
fn from(value: Selection) -> Self {
|
||||
value.start..value.end
|
||||
}
|
||||
}
|
||||
|
||||
pub type Position = lsp_types::Position;
|
||||
Reference in New Issue
Block a user