Skip to content

Commit 144d9d6

Browse files
committed
UI: fix line no for wrapped lines
1 parent 6c6084a commit 144d9d6

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/ui/textedit.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,13 @@ void TextEditInput::draw(int x, int y, int w, int h, int chw) {
300300
break;
301301
}
302302

303-
if (row++ >= _scroll) {
303+
if (i == 0 ||
304+
_buf._buffer[i - 1] == '\r' ||
305+
_buf._buffer[i - 1] == '\n') {
306+
row++;
307+
}
308+
309+
if (row >= _scroll) {
304310
if (_matchingBrace != -1 && _matchingBrace >= i &&
305311
_matchingBrace < i + r.num_chars) {
306312
cursorMatchX = x + ((_matchingBrace - i) * chw);
@@ -568,6 +574,8 @@ void TextEditInput::gotoLine(const char *buffer) {
568574
}
569575

570576
void TextEditInput::reload(const char *text) {
577+
_scroll = 0;
578+
_cursorRow = 0;
571579
_buf.clear();
572580
_buf.insertChars(0, text, strlen(text));
573581
stb_textedit_initialize_state(&_state, false);
@@ -672,20 +680,23 @@ void TextEditInput::paste(const char *text) {
672680
void TextEditInput::layout(StbTexteditRow *row, int start) const {
673681
int i = start;
674682
int len = _buf._len;
683+
int x1 = 0;
675684
int x2 = _width - _charWidth - _marginWidth;
676-
row->x1 = 0;
677-
row->num_chars = 0;
685+
int numChars = 0;
678686

679687
// advance to newline or rectangle edge
680688
while (i < len
681689
&& (int)row->x1 < x2
682690
&& _buf._buffer[i] != '\r'
683691
&& _buf._buffer[i] != '\n') {
684-
row->x1 += _charWidth;
685-
row->num_chars++;
692+
x1 += _charWidth;
693+
numChars++;
686694
i++;
687695
}
688696

697+
row->num_chars = numChars;
698+
row->x1 = x1;
699+
689700
if (_buf._buffer[i] == '\r') {
690701
// advance over DOS newline
691702
row->num_chars++;

0 commit comments

Comments
 (0)