[Add] Smart indentation on newline.
This commit is contained in:
		
							parent
							
								
									785e6ddc38
								
							
						
					
					
						commit
						d3b5139e89
					
				@ -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) {
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user