[Add] Smart indentation on newline.

This commit is contained in:
Ritchie Cunningham 2025-11-03 20:24:02 +00:00
parent 785e6ddc38
commit d3b5139e89

View File

@ -29,12 +29,19 @@ void TextBuffer::backspace(void) {
void TextBuffer::newline(void) { void TextBuffer::newline(void) {
std::string current_line = _lines[_cursor_row]; 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); _lines[_cursor_row] = current_line.substr(0, _cursor_col);
_cursor_row++; _cursor_row++;
_lines.insert(_lines.begin() + _cursor_row, new_line); _lines.insert(_lines.begin() + _cursor_row, indent + text_after_cursor);
_cursor_col = 0; _cursor_col = indent.length();
} }
void TextBuffer::move_cursor(int dx, int dy) { void TextBuffer::move_cursor(int dx, int dy) {