Skip to content

Commit 37419e6

Browse files
committed
UI: fix text editor display issues
1 parent 4f52c96 commit 37419e6

File tree

2 files changed

+17
-23
lines changed

2 files changed

+17
-23
lines changed

src/ui/textedit.cpp

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ inline bool match(const char *str, const char *pattern , int len) {
9393
}
9494

9595
inline bool is_comment(const char *str, int offs) {
96-
return str[offs] == '\'' || str[offs] == '#' || match(str + offs, "RrEeMm ", 3);
96+
return (str[offs] == '\'' || (str[offs] == '#' && !isdigit(str[offs + 1]))
97+
|| match(str + offs, "RrEeMm ", 3));
9798
}
9899

99100
//
@@ -205,12 +206,6 @@ char *EditBuffer::textRange(int start, int end) {
205206
return result;
206207
}
207208

208-
void EditBuffer::replaceChars(const char *replace, int start, int end) {
209-
for (int i = start, j = 0; i < end && i < _len && replace[j] != '\0'; i++, j++) {
210-
_buffer[i] = replace[j];
211-
}
212-
}
213-
214209
void EditBuffer::removeTrailingSpaces(STB_TexteditState *state) {
215210
int lineEnd = _len - 1;
216211
int lastChar = lineEnd;
@@ -293,6 +288,7 @@ void TextEditInput::draw(int x, int y, int w, int h, int chw) {
293288
int cursorMatchX = x;
294289
int cursorMatchY = y;
295290
int row = 0;
291+
int line = 0;
296292
int selectStart = MIN(_state.select_start, _state.select_end);
297293
int selectEnd = MAX(_state.select_start, _state.select_end);
298294

@@ -309,10 +305,10 @@ void TextEditInput::draw(int x, int y, int w, int h, int chw) {
309305
if (i == 0 ||
310306
_buf._buffer[i - 1] == '\r' ||
311307
_buf._buffer[i - 1] == '\n') {
312-
row++;
308+
line++;
313309
}
314310

315-
if (row >= _scroll) {
311+
if (row++ >= _scroll) {
316312
if (_matchingBrace != -1 && _matchingBrace >= i &&
317313
_matchingBrace < i + r.num_chars) {
318314
cursorMatchX = x + ((_matchingBrace - i) * chw);
@@ -380,9 +376,9 @@ void TextEditInput::draw(int x, int y, int w, int h, int chw) {
380376
maSetColor(_theme->_selection_background);
381377
maFillRect(x + _marginWidth, y + baseY, _charWidth / 2, _charHeight);
382378
}
383-
drawLineNumber(x, y + baseY, row, true);
379+
drawLineNumber(x, y + baseY, line, true);
384380
} else {
385-
drawLineNumber(x, y + baseY, row, false);
381+
drawLineNumber(x, y + baseY, line, false);
386382
if (numChars) {
387383
if (_marginWidth > 0) {
388384
drawText(x + _marginWidth, y + baseY, _buf._buffer + i, numChars, syntax);
@@ -397,7 +393,7 @@ void TextEditInput::draw(int x, int y, int w, int h, int chw) {
397393
i += r.num_chars;
398394
}
399395

400-
drawLineNumber(x, y + baseY, row + 1, false);
396+
drawLineNumber(x, y + baseY, line + 1, false);
401397

402398
// draw cursor
403399
maSetColor(_theme->_cursor_background);
@@ -432,13 +428,11 @@ void TextEditInput::drawText(int x, int y, const char *str,
432428

433429
// find the end of the current segment
434430
while (i < length) {
435-
if (is_comment(str, i)) {
431+
if (state == kComment || is_comment(str, i)) {
436432
next = length - i;
437433
nextState = kComment;
438434
break;
439-
} else if (state != kReset) {
440-
break;
441-
} else if (str[i] == '\"') {
435+
} else if (state == kText || str[i] == '\"') {
442436
next = 1;
443437
while (i + next < length && str[i + next] != '\"') {
444438
next++;
@@ -448,7 +442,8 @@ void TextEditInput::drawText(int x, int y, const char *str,
448442
}
449443
nextState = kText;
450444
break;
451-
} else if (isdigit(str[i]) && (i == 0 || !isalpha(str[i - 1]))) {
445+
} else if (state == kReset && isdigit(str[i]) &&
446+
(i == 0 || !isalpha(str[i - 1]))) {
452447
next = 1;
453448
while (i + next < length && isdigit(str[i + next])) {
454449
next++;
@@ -459,7 +454,7 @@ void TextEditInput::drawText(int x, int y, const char *str,
459454
}
460455
nextState = kDigit;
461456
break;
462-
} else {
457+
} else if (state == kReset) {
463458
int size = 0;
464459
uint32_t hash = getHash(str, i, size);
465460
if (hash > 0) {
@@ -471,7 +466,7 @@ void TextEditInput::drawText(int x, int y, const char *str,
471466
nextState = kStatement;
472467
next = size;
473468
break;
474-
} else {
469+
} else if (size > 0) {
475470
i += size - 1;
476471
count += size - 1;
477472
}
@@ -768,9 +763,9 @@ void TextEditInput::changeCase() {
768763
}
769764
}
770765
if (selection[0]) {
771-
_buf.replaceChars(selection, start, end);
772766
_state.select_start = start;
773767
_state.select_end = end;
768+
stb_textedit_paste(&_buf, &_state, selection, strlen(selection));
774769
}
775770
free(selection);
776771
}
@@ -979,8 +974,8 @@ int TextEditInput::getCursorRow() const {
979974

980975
uint32_t TextEditInput::getHash(const char *str, int offs, int &count) {
981976
uint32_t result = 0;
982-
if ((offs == 0 || str[offs - 1] == ' ' || str[offs - 1] == '\n')
983-
&& str[offs] != ' ' && str[offs] != '\n' && str[offs] != '\0') {
977+
if ((offs == 0 || IS_WHITE(str[offs - 1]))
978+
&& !IS_WHITE(str[offs]) && str[offs] != '\0') {
984979
for (count = 0; count < keyword_max_len; count++) {
985980
char ch = str[offs + count];
986981
if (!isalpha(ch) && ch != '_') {

src/ui/textedit.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ struct EditBuffer {
6262
int deleteChars(int pos, int num);
6363
int insertChars(int pos, const char *text, int num);
6464
void removeTrailingSpaces(STB_TexteditState *state);
65-
void replaceChars(const char *replace, int start, int end);
6665
char *textRange(int start, int end);
6766
};
6867

0 commit comments

Comments
 (0)