From d3b5139e890b7f3caee653163044b8bfd7fe7d4d Mon Sep 17 00:00:00 2001 From: Ritchie Cunningham Date: Mon, 3 Nov 2025 20:24:02 +0000 Subject: [PATCH] [Add] Smart indentation on newline. --- common/src/ui/text_buffer.cpp | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) 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) {