Fix offseting carets after edit without killing selection, add dedent

This commit is contained in:
Krzosa Karol
2024-08-02 21:30:12 +02:00
parent 03b6b80568
commit 4f12887a91
3 changed files with 24 additions and 5 deletions

View File

@@ -132,11 +132,16 @@ void AfterEdit(Buffer *buffer, Array<Edit> *edits, Array<Caret> *carets, bool ki
for (Int i = 0; i < carets->len; i += 1) { for (Int i = 0; i < carets->len; i += 1) {
Caret &old_cursor = carets->data[i]; Caret &old_cursor = carets->data[i];
Caret &new_cursor = new_carets.data[i]; Caret &new_cursor = new_carets.data[i];
if (old_cursor.range.min == edit.range.min) { if (old_cursor.range.min == edit.range.min) {
new_cursor.range.min += insert_size; new_cursor.range.min += insert_size;
new_cursor.range.max += insert_size;
} else if (old_cursor.range.min > edit.range.min) { } else if (old_cursor.range.min > edit.range.min) {
new_cursor.range.min += offset; new_cursor.range.min += offset;
}
if (old_cursor.range.max == edit.range.max) {
new_cursor.range.max += insert_size;
} else if (old_cursor.range.max > edit.range.max) {
new_cursor.range.max += offset; new_cursor.range.max += offset;
} }
} }

View File

@@ -31,7 +31,7 @@ void Command_DuplicateLine(View *view, int direction) {
For(view->carets) it = MakeCaret(MovePos(*buffer, it.range.min, direction, false)); For(view->carets) it = MakeCaret(MovePos(*buffer, it.range.min, direction, false));
} }
void Command_IndentSelectedLines(View *view) { void Command_IndentSelectedLines(View *view, bool shift = false) {
Scratch scratch; Scratch scratch;
Buffer *buffer = GetBuffer(view->active_buffer); Buffer *buffer = GetBuffer(view->active_buffer);
@@ -65,7 +65,18 @@ void Command_IndentSelectedLines(View *view) {
For(line_ranges_to_indent) { For(line_ranges_to_indent) {
for (Int i = it.min; i <= it.max; i += 1) { for (Int i = it.min; i <= it.max; i += 1) {
Range pos_range_of_line = GetLineRange(*buffer, i); Range pos_range_of_line = GetLineRange(*buffer, i);
if (!shift) {
AddEdit(&edits, {pos_range_of_line.min, pos_range_of_line.min}, L" "); AddEdit(&edits, {pos_range_of_line.min, pos_range_of_line.min}, L" ");
} else {
String16 string = GetString(*buffer, pos_range_of_line);
Int whitespace_len = 0;
for (Int i = 0; i < 4 && i < string.len && string.data[i] == ' '; i += 1) {
whitespace_len += 1;
}
AddEdit(&edits, {pos_range_of_line.min, pos_range_of_line.min + whitespace_len}, L"");
}
} }
} }
@@ -401,8 +412,10 @@ void WindowCommand(Event event, Window *window, View *view) {
} }
bool search = false; bool search = false;
if (Press(SDLK_TAB)) { if (Shift(SDLK_TAB)) {
// Command_Replace(view, L" "); Command_IndentSelectedLines(view, SHIFT_PRESSED);
search = true;
} else if (Press(SDLK_TAB)) {
Command_IndentSelectedLines(view); Command_IndentSelectedLines(view);
search = true; search = true;
} }

View File

@@ -6,6 +6,7 @@
- underline the word which can be loaded / executed - underline the word which can be loaded / executed
- search as a command to execute which is going to be in the title bar - search as a command to execute which is going to be in the title bar
- I think the way sublime text and we display line highlights is confusing with multiple cursors (line highlight can be confused with selection)
- commands: - commands:
- indent selected lines - indent selected lines