Add better indentation and pasting
This commit is contained in:
@@ -23,6 +23,8 @@ uint32_t get_indent(Editor *editor, Coord cursor) {
|
||||
if (line_len == 0)
|
||||
continue;
|
||||
uint32_t indent = leading_indent(line, line_len);
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
return indent;
|
||||
}
|
||||
free(it->buffer);
|
||||
@@ -38,3 +40,47 @@ bool closing_after_cursor(const char *line, uint32_t len, uint32_t col) {
|
||||
return false;
|
||||
return line[i] == '}' || line[i] == ']' || line[i] == ')';
|
||||
}
|
||||
|
||||
void indent_line(Editor *editor, uint32_t row) {
|
||||
if (!editor)
|
||||
return;
|
||||
LineIterator *it = begin_l_iter(editor->root, row);
|
||||
uint32_t line_len;
|
||||
char *line = next_line(it, &line_len);
|
||||
if (!line) {
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
return;
|
||||
}
|
||||
char *spaces = (char *)malloc(INDENT_WIDTH);
|
||||
memset(spaces, ' ', INDENT_WIDTH);
|
||||
Coord cursor = editor->cursor;
|
||||
edit_insert(editor, {row, 0}, spaces, INDENT_WIDTH);
|
||||
editor->cursor = cursor;
|
||||
free(spaces);
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
}
|
||||
|
||||
void dedent_line(Editor *editor, uint32_t row) {
|
||||
if (!editor)
|
||||
return;
|
||||
LineIterator *it = begin_l_iter(editor->root, row);
|
||||
uint32_t line_len;
|
||||
char *line = next_line(it, &line_len);
|
||||
if (!line) {
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
return;
|
||||
}
|
||||
uint32_t indent = leading_indent(line, line_len);
|
||||
if (indent == 0) {
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
return;
|
||||
}
|
||||
uint32_t remove = indent >= INDENT_WIDTH ? INDENT_WIDTH : indent;
|
||||
edit_erase(editor, {row, 0}, remove);
|
||||
free(it->buffer);
|
||||
free(it);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user