Skip to content

Commit 399dbe1

Browse files
committed
fix more issues in vertical cursor moves, use utf8_str_visible_length() instead of runes().len
1 parent b835150 commit 399dbe1

File tree

4 files changed

+41
-37
lines changed

4 files changed

+41
-37
lines changed

render.v

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,6 @@ fn render_text(mut shape Shape, clip DrawClip, mut window Window) {
356356
}
357357
y += lh
358358
char_count += len
359-
unsafe { lnr.free() }
360359
}
361360

362361
render_cursor(shape, clip, mut window)

view_input.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,13 +403,13 @@ pub fn (cfg &InputCfg) copy(w &Window) ?string {
403403
input_state := w.view_state.input_state[cfg.id_focus]
404404
if input_state.select_beg != input_state.select_end {
405405
beg, end := u32_sort(input_state.select_beg, input_state.select_end)
406-
len := cfg.text.runes().len
406+
len := utf8_str_visible_length(cfg.text)
407407
if beg >= len || end > len {
408408
log.error('beg or end out of range (copy)')
409409
return none
410410
}
411411
rune_text := cfg.text.runes()
412-
if beg < 0 || end >= rune_text.len {
412+
if beg < 0 || end >= len {
413413
return none
414414
}
415415
cpy := rune_text[beg..end]

view_text.v

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ fn scroll_cursor_into_view(cursor_pos int, layout &Layout, _ &Event, mut w Windo
214214
mut total_len := 0
215215
for i, line in layout.shape.text_lines {
216216
line_idx = i
217-
total_len += line.runes().len
217+
total_len += utf8_str_visible_length(line)
218218
if total_len > cursor_pos {
219219
break
220220
}
@@ -326,11 +326,11 @@ fn (tv &TextView) on_key_down(layout &Layout, mut e Event, mut w Window) {
326326
// Standard navigation: char by char, prev/next line, home/end of text
327327
match e.key_code {
328328
.left { new_cursor_pos = int_max(0, new_cursor_pos - 1) }
329-
.right { new_cursor_pos = int_min(tv.text.runes().len, new_cursor_pos + 1) }
329+
.right { new_cursor_pos = int_min(count_chars(text_lines), new_cursor_pos + 1) }
330330
.up { new_cursor_pos = cursor_up(text_lines, new_cursor_pos) }
331331
.down { new_cursor_pos = cursor_down(text_lines, new_cursor_pos) }
332332
.home { new_cursor_pos = 0 }
333-
.end { new_cursor_pos = tv.text.runes().len }
333+
.end { new_cursor_pos = utf8_str_visible_length(tv.text) }
334334
else { return }
335335
}
336336
} else if e.modifiers == Modifier.super {
@@ -492,7 +492,7 @@ pub fn (tv &TextView) select_all(shape &Shape, mut w Window) {
492492
return
493493
}
494494
input_state := w.view_state.input_state[tv.id_focus]
495-
len := tv.text.runes().len
495+
len := utf8_str_visible_length(tv.text)
496496
w.view_state.input_state[tv.id_focus] = InputState{
497497
...input_state
498498
cursor_pos: len

xtra_text.v

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -476,17 +476,14 @@ fn end_of_word_pos(strs []string, pos int) int {
476476
// start_of_line_pos finds start of line position in wrapped text starting from pos
477477
fn start_of_line_pos(strs []string, pos int) int {
478478
mut len := 0
479-
480479
for str in strs {
481-
runes := str.runes()
482-
if pos > len + runes.len {
483-
len += runes.len
480+
str_len := utf8_str_visible_length(str)
481+
if pos > len + str_len {
482+
len += str_len
484483
continue
485484
}
486-
487485
return len
488486
}
489-
490487
return len
491488
}
492489

@@ -497,15 +494,15 @@ fn end_of_line_pos(strs []string, pos int) int {
497494

498495
for str in strs {
499496
cnt += 1
500-
runes := str.runes()
501-
if pos >= len + runes.len - 1 {
502-
len += runes.len
497+
str_len := utf8_str_visible_length(str)
498+
if pos >= len + str_len - 1 {
499+
len += str_len
503500
continue
504501
}
505502

506503
is_last_line := cnt == strs.len
507504
eol := if is_last_line { 0 } else { 1 }
508-
return int_max(0, len + runes.len - eol)
505+
return int_max(0, len + str_len - eol)
509506
}
510507

511508
return len
@@ -553,26 +550,26 @@ fn start_of_paragraph(strs []string, pos int) int {
553550
fn cursor_up(strs []string, pos int) int {
554551
mut idx := 0
555552
mut offset := 0
556-
runes := strs.map(it.runes())
553+
lengths := strs.map(utf8_str_visible_length(it))
557554

558555
// find which line to from
559-
for i, r in runes {
560-
if offset + r.len > pos {
556+
for i, len in lengths {
557+
idx = i
558+
if offset + len > pos {
561559
break
562560
}
563-
idx = i
564-
offset += r.len
561+
offset += len
565562
}
566563

567564
// move to previous line
568565
if idx > 0 {
569566
col := pos - offset
570567
p_idx := idx - 1
571-
p_len := runes[p_idx].len
568+
p_len := lengths[p_idx]
572569
mut p_start := 0
573-
for i, r in runes {
574-
if i <= p_idx {
575-
p_start += r.len
570+
for i, len in lengths {
571+
if i < p_idx {
572+
p_start += len
576573
}
577574
}
578575
return if col >= p_len { p_start + p_len - 1 } else { p_start + col }
@@ -583,29 +580,29 @@ fn cursor_up(strs []string, pos int) int {
583580
fn cursor_down(strs []string, pos int) int {
584581
mut idx := 0
585582
mut offset := 0
586-
runes := strs.map(it.runes())
583+
lengths := strs.map(utf8_str_visible_length(it))
587584

588585
// find which line to from
589-
for i, r in runes {
590-
if offset + r.len > pos {
586+
for i, len in lengths {
587+
idx = i
588+
if offset + len > pos {
591589
break
592590
}
593-
idx = i
594-
offset += r.len
591+
offset += len
595592
}
596593

597594
// move to next line
598-
if idx < runes.len - 2 {
595+
if idx < strs.len - 1 {
599596
col := pos - offset
600597
n_idx := idx + 1
601-
n_len := runes[n_idx].len
598+
n_len := lengths[n_idx]
602599
mut n_start := 0
603-
for i, r in runes {
604-
if i <= n_idx {
605-
n_start += r.len
600+
for i, len in lengths {
601+
if i < n_idx {
602+
n_start += len
606603
}
607604
}
608-
return if col >= n_len { n_start + n_len } else { n_start + col }
605+
return if col >= n_len { n_start + n_len - 1 } else { n_start + col }
609606
}
610607
return pos
611608
}
@@ -624,3 +621,11 @@ pub fn to_clipboard(s ?string) bool {
624621
}
625622
return false
626623
}
624+
625+
fn count_chars(strs []string) int {
626+
mut count := 0
627+
for str in strs {
628+
count += utf8_str_visible_length(str)
629+
}
630+
return count
631+
}

0 commit comments

Comments
 (0)