diff options
author | Leo Tenenbaum <pommicket@gmail.com> | 2021-03-14 15:48:45 -0400 |
---|---|---|
committer | Leo Tenenbaum <pommicket@gmail.com> | 2021-03-14 15:48:45 -0400 |
commit | 72592df32eddeb11ebdbb61a1c2437d82eb2f73b (patch) | |
tree | a877b9ce9d43b46059791c8bb85a40668065fcfd | |
parent | 0d2d32d1a1090ce4c86510be8b10dc65ea1c0d62 (diff) |
fix problem where cursor moves on in/dedent selection
-rw-r--r-- | buffer.c | 22 | ||||
-rw-r--r-- | main.c | 2 |
2 files changed, 22 insertions, 2 deletions
@@ -137,6 +137,24 @@ static void buffer_validate_cursor(TextBuffer *buffer) { buffer_pos_validate(buffer, &buffer->selection_pos); } +// update *pos, given that nchars characters were deleted at del_pos. +static void buffer_pos_handle_deleted_chars(BufferPos *pos, BufferPos del_pos, u32 nchars) { + if (pos->line != del_pos.line) return; + + if (pos->index >= del_pos.index + nchars) { + pos->index -= nchars; + } else if (pos->index >= del_pos.index) { + pos->index = del_pos.index; + } +} +static void buffer_pos_handle_inserted_chars(BufferPos *pos, BufferPos ins_pos, u32 nchars) { + if (pos->line != ins_pos.line) return; + + if (pos->index >= ins_pos.index) { + pos->index += nchars; + } +} + static bool buffer_pos_valid(TextBuffer *buffer, BufferPos p) { return p.line < buffer->nlines && p.index <= buffer->lines[p.line].len; } @@ -1375,6 +1393,8 @@ BufferPos buffer_insert_text_at_pos(TextBuffer *buffer, BufferPos pos, String32 u32 new_len = old_len + text_line_len; if (new_len > old_len) { // handles both overflow and empty text lines if (buffer_line_set_len(buffer, line, new_len)) { + buffer_pos_handle_inserted_chars(&buffer->cursor_pos, (BufferPos){line_idx, index}, text_line_len); + buffer_pos_handle_inserted_chars(&buffer->selection_pos, (BufferPos){line_idx, index}, text_line_len); // make space for text memmove(line->str + index + (new_len - old_len), line->str + index, @@ -1650,6 +1670,8 @@ void buffer_delete_chars_at_pos(TextBuffer *buffer, BufferPos pos, i64 nchars_) } } else { // just delete characters from this line + buffer_pos_handle_deleted_chars(&buffer->cursor_pos, pos, nchars); + buffer_pos_handle_deleted_chars(&buffer->selection_pos, pos, nchars); memmove(line->str + index, line->str + index + nchars, (size_t)(line->len - (nchars + index)) * sizeof(char32_t)); line->len -= nchars; } @@ -1,5 +1,3 @@ -// @TODO: -// fix cursor pos in buffer_delete_chars_at_pos (try dedenting something -- the cursor moves) #include "base.h" no_warn_start #if _WIN32 |