diff --git a/common/src/ui/text_buffer.cpp b/common/src/ui/text_buffer.cpp index 181826d..d6a74ec 100644 --- a/common/src/ui/text_buffer.cpp +++ b/common/src/ui/text_buffer.cpp @@ -29,12 +29,19 @@ void TextBuffer::backspace(void) { void TextBuffer::newline(void) { std::string current_line = _lines[_cursor_row]; - std::string new_line = current_line.substr(_cursor_col); + + /* Find leading whitespace of the current line. */ + size_t first_char = current_line.find_first_not_of(" \t"); + std::string indent = (first_char != std::string::npos) + ? current_line.substr(0, first_char) : ""; + + /* Split the line at the cursor. */ + std::string text_after_cursor = current_line.substr(_cursor_col); _lines[_cursor_row] = current_line.substr(0, _cursor_col); _cursor_row++; - _lines.insert(_lines.begin() + _cursor_row, new_line); - _cursor_col = 0; + _lines.insert(_lines.begin() + _cursor_row, indent + text_after_cursor); + _cursor_col = indent.length(); } void TextBuffer::move_cursor(int dx, int dy) {