Skip to content

Commit 781c74c

Browse files
committed
UI: fix editor display issues with DOS line-endings
1 parent 5ac2a2e commit 781c74c

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/ui/textedit.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -358,13 +358,22 @@ EditBuffer::EditBuffer(TextEditInput *in, const char *text) :
358358
_buffer = (char *)malloc(_size);
359359
memcpy(_buffer, text, _len);
360360
_buffer[_len] = '\0';
361+
convertTabs();
361362
}
362363
}
363364

364365
EditBuffer::~EditBuffer() {
365366
clear();
366367
}
367368

369+
void EditBuffer::convertTabs() {
370+
for (int i = 0; i < _len; i++) {
371+
if (_buffer[i] == '\t') {
372+
_buffer[i] = ' ';
373+
}
374+
}
375+
}
376+
368377
void EditBuffer::clear() {
369378
free(_buffer);
370379
_buffer = nullptr;
@@ -409,6 +418,9 @@ char EditBuffer::getChar(int pos) const {
409418
} else {
410419
result = '\0';
411420
}
421+
if (result == '\r') {
422+
result = '\n';
423+
}
412424
return result;
413425
}
414426

@@ -428,6 +440,7 @@ int EditBuffer::insertChars(int pos, const char *text, int num) {
428440
_len += num;
429441
_buffer[_len] = '\0';
430442
_in->setDirty(true);
443+
convertTabs();
431444
if (num > 1) {
432445
_lines += countNewlines(text, num);
433446
} else if (text[0] == '\n') {
@@ -729,8 +742,7 @@ void TextEditInput::dragPage(int y, bool &redraw) {
729742
}
730743
}
731744

732-
void TextEditInput::drawText(int x, int y, const char *str,
733-
int length, SyntaxState &state) {
745+
void TextEditInput::drawText(int x, int y, const char *str, int length, SyntaxState &state) {
734746
int i = 0;
735747
int offs = 0;
736748
SyntaxState nextState = state;
@@ -1603,10 +1615,10 @@ int TextEditInput::getIndent(char *spaces, int len, int pos) {
16031615

16041616
int TextEditInput::getLineChars(StbTexteditRow *row, int pos) const {
16051617
int numChars = row->num_chars;
1606-
if (numChars > 0 && _buf._buffer[pos + row->num_chars - 1] == '\r') {
1618+
if (numChars > 0 && _buf._buffer[pos + numChars - 1] == '\n') {
16071619
numChars--;
16081620
}
1609-
if (numChars > 0 && _buf._buffer[pos + row->num_chars - 1] == '\n') {
1621+
if (numChars > 0 && _buf._buffer[pos + numChars - 1] == '\r') {
16101622
numChars--;
16111623
}
16121624
return numChars;

src/ui/textedit.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ struct EditBuffer {
5050
void append(const char *text, int len) { insertChars(_len, text, len); }
5151
void append(const char *text) { insertChars(_len, text, strlen(text)); }
5252
void clear();
53+
void convertTabs();
5354
int countNewlines(const char *text, int num);
5455
int deleteChars(int pos, int num);
5556
char getChar(int pos) const;

0 commit comments

Comments
 (0)