Skip to content

Commit

Permalink
[tui] Clean up confusing caret types - raw and scroll adjusted
Browse files Browse the repository at this point in the history
Caret position can be of two kinds:
- Raw
- Scroll adjusted

Make this explicity using the type system and provide
conversions between the two.
  • Loading branch information
nazmulidris committed Jan 30, 2025
1 parent 09ccbe1 commit db8ee45
Show file tree
Hide file tree
Showing 13 changed files with 426 additions and 123 deletions.
24 changes: 0 additions & 24 deletions .vscode/bookmarks.json

This file was deleted.

1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
"mworld",
"nanos",
"neovim",
"nextest",
"Nodesource",
"notcurses",
"nushell",
Expand Down
5 changes: 1 addition & 4 deletions core/src/tui_core/dimens/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

use std::{fmt::{self, Debug},
ops::{Add, AddAssign, Mul}};
ops::{Add, AddAssign, Mul, Sub}};

use crate::{ChUnit, Size, ch};

Expand Down Expand Up @@ -144,8 +143,6 @@ impl Position {
}

pub mod position_math_ops {
use std::ops::Sub;

use super::*;

impl AddAssign<ChUnit> for Position {
Expand Down
5 changes: 3 additions & 2 deletions core/src/tui_core/graphemes/access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,10 @@ impl UnicodeString {
/// Uses [SelectionRange] to calculate width and simply calls
/// [clip_to_width](Self::clip_to_width).
pub fn clip_to_range(&self, range: SelectionRange) -> &str {
// BUG: [ ] introduce scroll adjusted type
let SelectionRange {
start_display_col_index,
end_display_col_index,
start_display_col_index_scroll_adjusted: start_display_col_index,
end_display_col_index_scroll_adjusted: end_display_col_index,
} = range;
let max_display_col_count = end_display_col_index - start_display_col_index;
self.clip_to_width(start_display_col_index, max_display_col_count)
Expand Down
29 changes: 16 additions & 13 deletions core/src/tui_core/graphemes/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@ use crate::{ChUnit, Position};
/// - `▓▓` = `😃`
/// - [clip_to_range](crate::UnicodeString::clip_to_range): "e😃"
#[derive(Default, Clone, PartialEq, Copy, size_of::SizeOf)]
// BUG: [ ] introduce scroll adjusted type
pub struct SelectionRange {
pub start_display_col_index: ChUnit,
pub end_display_col_index: ChUnit,
/// This is not "raw", this is "scroll adjusted".
pub start_display_col_index_scroll_adjusted: ChUnit,
/// This is not "raw", this is "scroll adjusted".
pub end_display_col_index_scroll_adjusted: ChUnit,
}

#[derive(Clone, PartialEq, Copy, Debug)]
Expand All @@ -54,7 +57,7 @@ impl SelectionRange {
&self,
scroll_offset: Position,
) -> ScrollOffsetColLocationInRange {
if self.start_display_col_index >= scroll_offset.col_index {
if self.start_display_col_index_scroll_adjusted >= scroll_offset.col_index {
ScrollOffsetColLocationInRange::Underflow
} else {
ScrollOffsetColLocationInRange::Overflow
Expand Down Expand Up @@ -163,9 +166,9 @@ impl SelectionRange {
/// ```
/// - [UnicodeString::clip_to_range](crate::UnicodeString::clip_to_range): "ell"
pub fn locate_column(&self, caret_display_col_index: ChUnit) -> CaretLocationInRange {
if caret_display_col_index < self.start_display_col_index {
if caret_display_col_index < self.start_display_col_index_scroll_adjusted {
CaretLocationInRange::Underflow
} else if caret_display_col_index >= self.end_display_col_index {
} else if caret_display_col_index >= self.end_display_col_index_scroll_adjusted {
CaretLocationInRange::Overflow
} else {
CaretLocationInRange::Contained
Expand All @@ -174,8 +177,8 @@ impl SelectionRange {

pub fn new(start_display_col_index: ChUnit, end_display_col_index: ChUnit) -> Self {
Self {
start_display_col_index,
end_display_col_index,
start_display_col_index_scroll_adjusted: start_display_col_index,
end_display_col_index_scroll_adjusted: end_display_col_index,
}
}

Expand All @@ -189,7 +192,7 @@ impl SelectionRange {
/// ```
pub fn grow_end_by(&self, amount: ChUnit) -> Self {
let mut copy = *self;
copy.end_display_col_index += amount;
copy.end_display_col_index_scroll_adjusted += amount;
copy
}

Expand All @@ -203,7 +206,7 @@ impl SelectionRange {
/// ```
pub fn shrink_end_by(&self, amount: ChUnit) -> Self {
let mut copy = *self;
copy.end_display_col_index -= amount;
copy.end_display_col_index_scroll_adjusted -= amount;
copy
}

Expand All @@ -217,7 +220,7 @@ impl SelectionRange {
/// ```
pub fn grow_start_by(&self, amount: ChUnit) -> Self {
let mut copy = *self;
copy.start_display_col_index -= amount;
copy.start_display_col_index_scroll_adjusted -= amount;
copy
}

Expand All @@ -231,7 +234,7 @@ impl SelectionRange {
/// ```
pub fn shrink_start_by(&self, amount: ChUnit) -> Self {
let mut copy = *self;
copy.start_display_col_index += amount;
copy.start_display_col_index_scroll_adjusted += amount;
copy
}
}
Expand All @@ -244,8 +247,8 @@ mod range_impl_debug_format {
write!(
f,
"[start_display_col_index: {start:?}, end_display_col_index: {end:?}]",
start = self.start_display_col_index,
end = self.end_display_col_index
start = self.start_display_col_index_scroll_adjusted,
end = self.end_display_col_index_scroll_adjusted
)
}
}
Expand Down
1 change: 1 addition & 0 deletions run
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ def install-cargo-tools [] {
cargo install cargo-unmaintained
cargo install cargo-expand
cargo install cargo-readme
cargo install cargo-nextest
}

def all [] {
Expand Down
Loading

0 comments on commit db8ee45

Please sign in to comment.