Skip to content

Commit db8ee45

Browse files
committed
[tui] Clean up confusing caret types - raw and scroll adjusted
Caret position can be of two kinds: - Raw - Scroll adjusted Make this explicity using the type system and provide conversions between the two.
1 parent 09ccbe1 commit db8ee45

File tree

13 files changed

+426
-123
lines changed

13 files changed

+426
-123
lines changed

.vscode/bookmarks.json

Lines changed: 0 additions & 24 deletions
This file was deleted.

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
"mworld",
9999
"nanos",
100100
"neovim",
101+
"nextest",
101102
"Nodesource",
102103
"notcurses",
103104
"nushell",

core/src/tui_core/dimens/position.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
1817
use std::{fmt::{self, Debug},
19-
ops::{Add, AddAssign, Mul}};
18+
ops::{Add, AddAssign, Mul, Sub}};
2019

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

@@ -144,8 +143,6 @@ impl Position {
144143
}
145144

146145
pub mod position_math_ops {
147-
use std::ops::Sub;
148-
149146
use super::*;
150147

151148
impl AddAssign<ChUnit> for Position {

core/src/tui_core/graphemes/access.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,10 @@ impl UnicodeString {
183183
/// Uses [SelectionRange] to calculate width and simply calls
184184
/// [clip_to_width](Self::clip_to_width).
185185
pub fn clip_to_range(&self, range: SelectionRange) -> &str {
186+
// BUG: [ ] introduce scroll adjusted type
186187
let SelectionRange {
187-
start_display_col_index,
188-
end_display_col_index,
188+
start_display_col_index_scroll_adjusted: start_display_col_index,
189+
end_display_col_index_scroll_adjusted: end_display_col_index,
189190
} = range;
190191
let max_display_col_count = end_display_col_index - start_display_col_index;
191192
self.clip_to_width(start_display_col_index, max_display_col_count)

core/src/tui_core/graphemes/range.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,12 @@ use crate::{ChUnit, Position};
3838
/// - `▓▓` = `😃`
3939
/// - [clip_to_range](crate::UnicodeString::clip_to_range): "e😃"
4040
#[derive(Default, Clone, PartialEq, Copy, size_of::SizeOf)]
41+
// BUG: [ ] introduce scroll adjusted type
4142
pub struct SelectionRange {
42-
pub start_display_col_index: ChUnit,
43-
pub end_display_col_index: ChUnit,
43+
/// This is not "raw", this is "scroll adjusted".
44+
pub start_display_col_index_scroll_adjusted: ChUnit,
45+
/// This is not "raw", this is "scroll adjusted".
46+
pub end_display_col_index_scroll_adjusted: ChUnit,
4447
}
4548

4649
#[derive(Clone, PartialEq, Copy, Debug)]
@@ -54,7 +57,7 @@ impl SelectionRange {
5457
&self,
5558
scroll_offset: Position,
5659
) -> ScrollOffsetColLocationInRange {
57-
if self.start_display_col_index >= scroll_offset.col_index {
60+
if self.start_display_col_index_scroll_adjusted >= scroll_offset.col_index {
5861
ScrollOffsetColLocationInRange::Underflow
5962
} else {
6063
ScrollOffsetColLocationInRange::Overflow
@@ -163,9 +166,9 @@ impl SelectionRange {
163166
/// ```
164167
/// - [UnicodeString::clip_to_range](crate::UnicodeString::clip_to_range): "ell"
165168
pub fn locate_column(&self, caret_display_col_index: ChUnit) -> CaretLocationInRange {
166-
if caret_display_col_index < self.start_display_col_index {
169+
if caret_display_col_index < self.start_display_col_index_scroll_adjusted {
167170
CaretLocationInRange::Underflow
168-
} else if caret_display_col_index >= self.end_display_col_index {
171+
} else if caret_display_col_index >= self.end_display_col_index_scroll_adjusted {
169172
CaretLocationInRange::Overflow
170173
} else {
171174
CaretLocationInRange::Contained
@@ -174,8 +177,8 @@ impl SelectionRange {
174177

175178
pub fn new(start_display_col_index: ChUnit, end_display_col_index: ChUnit) -> Self {
176179
Self {
177-
start_display_col_index,
178-
end_display_col_index,
180+
start_display_col_index_scroll_adjusted: start_display_col_index,
181+
end_display_col_index_scroll_adjusted: end_display_col_index,
179182
}
180183
}
181184

@@ -189,7 +192,7 @@ impl SelectionRange {
189192
/// ```
190193
pub fn grow_end_by(&self, amount: ChUnit) -> Self {
191194
let mut copy = *self;
192-
copy.end_display_col_index += amount;
195+
copy.end_display_col_index_scroll_adjusted += amount;
193196
copy
194197
}
195198

@@ -203,7 +206,7 @@ impl SelectionRange {
203206
/// ```
204207
pub fn shrink_end_by(&self, amount: ChUnit) -> Self {
205208
let mut copy = *self;
206-
copy.end_display_col_index -= amount;
209+
copy.end_display_col_index_scroll_adjusted -= amount;
207210
copy
208211
}
209212

@@ -217,7 +220,7 @@ impl SelectionRange {
217220
/// ```
218221
pub fn grow_start_by(&self, amount: ChUnit) -> Self {
219222
let mut copy = *self;
220-
copy.start_display_col_index -= amount;
223+
copy.start_display_col_index_scroll_adjusted -= amount;
221224
copy
222225
}
223226

@@ -231,7 +234,7 @@ impl SelectionRange {
231234
/// ```
232235
pub fn shrink_start_by(&self, amount: ChUnit) -> Self {
233236
let mut copy = *self;
234-
copy.start_display_col_index += amount;
237+
copy.start_display_col_index_scroll_adjusted += amount;
235238
copy
236239
}
237240
}
@@ -244,8 +247,8 @@ mod range_impl_debug_format {
244247
write!(
245248
f,
246249
"[start_display_col_index: {start:?}, end_display_col_index: {end:?}]",
247-
start = self.start_display_col_index,
248-
end = self.end_display_col_index
250+
start = self.start_display_col_index_scroll_adjusted,
251+
end = self.end_display_col_index_scroll_adjusted
249252
)
250253
}
251254
}

run

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ def install-cargo-tools [] {
148148
cargo install cargo-unmaintained
149149
cargo install cargo-expand
150150
cargo install cargo-readme
151+
cargo install cargo-nextest
151152
}
152153

153154
def all [] {

0 commit comments

Comments
 (0)